-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strict object schema with a boolean property expects alternative, not boolean #2848
Comments
Same issue here. |
@matgott in general, please avoid doing that, "+1 comments" are not bringing new information nor helping projects prioritize issues. Use GitHub's 👍🏻 reaction if you want to vote for issues instead. |
@joshkel I've tried bringing it right after the boolean check, it then breaks the alternatives case, still looking into possible solutions but it seems tricky. |
I'm getting into the same issue with union strings: export interface ConfigSchema {
NODE_ENV: 'development' | 'production' | 'test';
}
export const schema = Joi.object<ConfigSchema, true>({
NODE_ENV: Joi.string()
.valid('development', 'production', 'test')
.default('production'),
// ^ TS2739: Type 'StringSchema' is missing the following properties from type 'AlternativesSchema': conditional, match, try
}); But maybe I'm using it wrong 🤷♂️ I didn't expect a breaking change in a patch release, so I can't dig deeper now. |
Your use case seems valid, maybe I'll revert that change until there's a reliable solution 🤔 |
The goal is to handle "complex" unions (unions that aren't merely subsets of a single primitive type) without interfering with simple enumerated-constant-style unions (such as `'development' | 'production' | 'test'`) or booleans. Fixes hapijs#2848
That was going to be my next attempt, thanks a lot, I think that works. |
New version is published. |
Support plan
Context
What are you trying to achieve or the steps to reproduce?
Joi 17.6.1's addition of
AlternativesSchema
for a strict object map has caused TypeScript to start reporting an error for boolean properties.What was the result you got?
What result did you expect?
Successful compilation.
The problem appears to be that
IsUnion
returns true for booleans. I think that this may be because TypeScript treatsboolean
as the union oftrue
andfalse
(see the mentions of'boolean'
within checker.ts), but I'm really not sure. See also Stack Overflow's discussion on the limitations of anIsUnion
implementation.To fix it, maybe reorder ObjectPropertiesSchema to check boolean before union?
TypeScript playground link for experimenting with IsUnion implementations
(I'm not sure about ObjectPropertiesSchema's union check for another reason: It thinks that
interface Test { value: 'a' | 'b' }
should use an AlternativesSchema, but it seems like a more idiomatic Joi implementation would beJoi.string().valid('a', 'b')
. That's a separate issue and not currently affecting me, but I wanted to mention it in case it affects how this issue should be addressed. Thanks!)The text was updated successfully, but these errors were encountered: