Skip to content

Commit

Permalink
types: support strict alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Sep 22, 2022
1 parent 7c9ca86 commit 78ed862
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,13 @@ 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;

type ObjectPropertiesSchema<T = any> =
T extends NullableType<string>
true extends IsUnion<Exclude<T, undefined | null>>
? Joi.AlternativesSchema
: T extends NullableType<string>
? Joi.StringSchema
: T extends NullableType<number>
? Joi.NumberSchema
Expand Down
10 changes: 10 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,16 @@ const commentSchemaObject2 = Joi.object<Comment2, true>({
}
})

interface CommentWithAlternatives {
text: string;
user: string | User;
}

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

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

expect.error(Joi.string('x'));

0 comments on commit 78ed862

Please sign in to comment.