Skip to content

Commit 883f1c0

Browse files
add object typeguards schema
1 parent 6ff4a71 commit 883f1c0

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

lib/index.d.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,33 @@ declare namespace Joi {
686686
type SchemaLikeWithoutArray = string | number | boolean | null | Schema | SchemaMap;
687687
type SchemaLike = SchemaLikeWithoutArray | object;
688688

689-
type SchemaMap<TSchema = any> = {
689+
type NullableType<T> = undefined | null | T
690+
691+
type ObjectPropertiesSchema<T = any> =
692+
T extends NullableType<string>
693+
? Joi.StringSchema
694+
: T extends NullableType<number>
695+
? Joi.NumberSchema
696+
: T extends NullableType<bigint>
697+
? Joi.NumberSchema
698+
: T extends NullableType<boolean>
699+
? Joi.BooleanSchema
700+
: T extends NullableType<Array<any>>
701+
? Joi.ArraySchema
702+
: T extends NullableType<object>
703+
? ObjectSchema<StrictSchemaMap<T>>
704+
: never
705+
706+
type PartialSchemaMap<TSchema = any> = {
690707
[key in keyof TSchema]?: SchemaLike | SchemaLike[];
708+
}
709+
710+
type StrictSchemaMap<TSchema = any> = {
711+
[key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]>
691712
};
692713

714+
type SchemaMap<TSchema = any, isStrict = false> = isStrict extends true ? StrictSchemaMap<TSchema> : PartialSchemaMap<TSchema>
715+
693716
type Schema<P = any> =
694717
| AnySchema
695718
| ArraySchema
@@ -1972,7 +1995,7 @@ declare namespace Joi {
19721995
* Generates a schema object that matches an object data type (as well as JSON strings that have been parsed into objects).
19731996
*/
19741997
// tslint:disable-next-line:no-unnecessary-generics
1975-
object<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
1998+
object<TSchema = any, isStrict = false, T = TSchema>(schema?: SchemaMap<T, isStrict>): ObjectSchema<TSchema>;
19761999

19772000
/**
19782001
* Generates a schema object that matches a string data type. Note that empty strings are not allowed by default and must be enabled with allow('').

0 commit comments

Comments
 (0)