Skip to content

Commit

Permalink
Merge pull request #2627 from petrenkoVitaliy/feature/add-object-type…
Browse files Browse the repository at this point in the history
…guards-schema

Add object type guards schema
  • Loading branch information
hueniverse authored Jul 11, 2021
2 parents 6ff4a71 + 883f1c0 commit 13006ec
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,33 @@ declare namespace Joi {
type SchemaLikeWithoutArray = string | number | boolean | null | Schema | SchemaMap;
type SchemaLike = SchemaLikeWithoutArray | object;

type SchemaMap<TSchema = any> = {
type NullableType<T> = undefined | null | T

type ObjectPropertiesSchema<T = any> =
T extends NullableType<string>
? Joi.StringSchema
: T extends NullableType<number>
? Joi.NumberSchema
: T extends NullableType<bigint>
? Joi.NumberSchema
: T extends NullableType<boolean>
? Joi.BooleanSchema
: T extends NullableType<Array<any>>
? Joi.ArraySchema
: T extends NullableType<object>
? ObjectSchema<StrictSchemaMap<T>>
: never

type PartialSchemaMap<TSchema = any> = {
[key in keyof TSchema]?: SchemaLike | SchemaLike[];
}

type StrictSchemaMap<TSchema = any> = {
[key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]>
};

type SchemaMap<TSchema = any, isStrict = false> = isStrict extends true ? StrictSchemaMap<TSchema> : PartialSchemaMap<TSchema>

type Schema<P = any> =
| AnySchema
| ArraySchema
Expand Down Expand Up @@ -1972,7 +1995,7 @@ declare namespace Joi {
* Generates a schema object that matches an object data type (as well as JSON strings that have been parsed into objects).
*/
// tslint:disable-next-line:no-unnecessary-generics
object<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
object<TSchema = any, isStrict = false, T = TSchema>(schema?: SchemaMap<T, isStrict>): ObjectSchema<TSchema>;

/**
* 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('').
Expand Down

0 comments on commit 13006ec

Please sign in to comment.