Skip to content
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

fix: do not confuse booleans for alernatives in strict mode #2850

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,11 @@ declare namespace Joi {
type NullableType<T> = undefined | null | T

type IsUnion<T, U extends T = T> =
T extends unknown ? [U] extends [T] ? false : true : false;
T extends unknown ?
T extends boolean ?
never :
[U] extends [T] ? never : true
: never;

type ObjectPropertiesSchema<T = any> =
true extends IsUnion<Exclude<T, undefined | null>>
Expand Down
6 changes: 5 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,11 +1263,13 @@ const userSchema2 = Joi.object<User>().keys({
interface Comment {
text: string;
user: User;
isNew: boolean;
}

const commentSchemaObject = Joi.object<Comment, true>({
text: Joi.string().required(),
user: userSchemaObject
user: userSchemaObject,
isNew: Joi.boolean().required(),
});

interface Comment2 {
Expand All @@ -1287,11 +1289,13 @@ const commentSchemaObject2 = Joi.object<Comment2, true>({
interface CommentWithAlternatives {
text: string;
user: string | User;
reported: boolean | number;
}

const commentWithAlternativesSchemaObject = Joi.object<CommentWithAlternatives, true>({
text: Joi.string().required(),
user: Joi.alternatives(Joi.string(), userSchemaObject),
reported: Joi.alternatives(Joi.boolean(), Joi.number()),
});

expect.error(userSchema2.keys({ height: Joi.number() }));
Expand Down