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

[types] added "dontChangeOptional" boolean to avoid changing optional props in SchemaMap #2907

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,22 @@ declare namespace Joi {
type PartialSchemaMap<TSchema = any> = {
[key in keyof TSchema]?: SchemaLike | SchemaLike[];
}

type PartialUnchangedSchemaMap<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 StrictUnchangedSchemaMap<TSchema = any> = {
[key in keyof TSchema]: ObjectPropertiesSchema<TSchema[key]>
};

type SchemaMap<TSchema = any, isStrict = false, dontChangeOptional = false> = isStrict extends true
? (dontChangeOptional extends true ? StrictUnchangedSchemaMap<TSchema> : StrictSchemaMap<TSchema>)
: (dontChangeOptional extends true ? PartialUnchangedSchemaMap<TSchema> : PartialSchemaMap<TSchema>);

type Schema<P = any> =
| AnySchema<P>
Expand Down Expand Up @@ -2100,7 +2110,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, isStrict = false, T = TSchema>(schema?: SchemaMap<T, isStrict>): ObjectSchema<TSchema>;
object<TSchema = any, isStrict = false, dontChangeOptional = false, T = TSchema>(schema?: SchemaMap<T, isStrict, dontChangeOptional>): 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