diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index aabeb3443e..4a480eed0d 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -93,7 +93,7 @@ export interface MixedSchema extends Schema { nullable(isNullable?: true): MixedSchema; nullable(isNullable: false): MixedSchema>; nullable(isNullable?: boolean): MixedSchema; - required(message?: TestOptionsMessage): MixedSchema>; + required(message?: TestOptionsMessage): MixedSchema>; notRequired(): MixedSchema; concat(schema: this): this; concat(schema: MixedSchema): MixedSchema; @@ -123,7 +123,7 @@ export interface StringSchema exte nullable(isNullable?: true): StringSchema; nullable(isNullable: false): StringSchema>; nullable(isNullable?: boolean): StringSchema; - required(message?: TestOptionsMessage): StringSchema>; + required(message?: TestOptionsMessage): StringSchema>; notRequired(): StringSchema; } @@ -145,7 +145,7 @@ export interface NumberSchema exte nullable(isNullable?: true): NumberSchema; nullable(isNullable: false): NumberSchema>; nullable(isNullable?: boolean): NumberSchema; - required(message?: TestOptionsMessage): NumberSchema>; + required(message?: TestOptionsMessage): NumberSchema>; notRequired(): NumberSchema; } @@ -158,7 +158,7 @@ export interface BooleanSchema e nullable(isNullable?: true): BooleanSchema; nullable(isNullable: false): BooleanSchema>; nullable(isNullable?: boolean): BooleanSchema; - required(message?: TestOptionsMessage): BooleanSchema>; + required(message?: TestOptionsMessage): BooleanSchema>; notRequired(): BooleanSchema; } @@ -173,7 +173,7 @@ export interface DateSchema extends Sc nullable(isNullable?: true): DateSchema; nullable(isNullable: false): DateSchema>; nullable(isNullable?: boolean): DateSchema; - required(message?: TestOptionsMessage): DateSchema>; + required(message?: TestOptionsMessage): DateSchema>; notRequired(): DateSchema; } @@ -258,7 +258,7 @@ export interface ObjectSchema exte nullable(isNullable?: true): ObjectSchema; nullable(isNullable: false): ObjectSchema>; nullable(isNullable?: boolean): ObjectSchema; - required(message?: TestOptionsMessage): ObjectSchema>; + required(message?: TestOptionsMessage): ObjectSchema>; notRequired(): ObjectSchema; concat(schema: this): this; concat(schema: ObjectSchema): ObjectSchema; @@ -291,7 +291,7 @@ export interface TestContext { parent: any; schema: Schema; resolve: (value: any) => any; - createError: (params?: { path?: string; message?: string, params?: object }) => ValidationError; + createError: (params?: { path?: string; message?: string; params?: object }) => ValidationError; } export interface ValidateOptions { diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index f1b2898749..3c2808e7a8 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -636,8 +636,7 @@ const definitionBC: yup.ObjectSchemaDefinition = { }; const combinedSchema = yup.object(definitionAB).shape(definitionBC); // $ExpectType ObjectSchema> -// $ExpectError -yup.object({ +const schemaWithoutNumberField = { stringField: yup.string().required(), subFields: yup .object({ @@ -645,10 +644,13 @@ yup.object({ }) .required(), arrayField: yup.array(yup.string()).required(), -}); +}; // $ExpectError -yup.object({ stringField: yup.number().required(), +yup.object(schemaWithoutNumberField); + +const schemaWithWrongType = { + stringField: yup.number().required(), numberField: yup.number().required(), subFields: yup .object({ @@ -656,17 +658,22 @@ yup.object({ stringField: yup.number().required(), }) .required(), arrayField: yup.array(yup.string()).required(), -}); +}; // $ExpectError -yup.object({ +yup.object(schemaWithWrongType); + +const schemaWithoutSubtype = { stringField: yup.string().required(), numberField: yup.number().required(), arrayField: yup.array(yup.string()).required(), -}); +}; // $ExpectError -yup.object({ subFields: yup +yup.object(schemaWithoutSubtype); + +const schemaWithWrongSubtype = { + subFields: yup .object({ testField: yup.number().required(), }) @@ -674,7 +681,10 @@ yup.object({ subFields: yup stringField: yup.string().required(), numberField: yup.number().required(), arrayField: yup.array(yup.string()).required(), -}); +}; + +// $ExpectError +yup.object(schemaWithWrongSubtype); enum Gender { Male = 'Male', @@ -694,7 +704,13 @@ const personSchema = yup.object({ .nullable() .notRequired() .min(new Date(1900, 0, 1)), - canBeNull: yup.string().nullable(true), // $ExpectType StringSchema + // $ExpectType StringSchema + canBeNull: yup.string().nullable(true), + // $ExpectType StringSchema + requiredNullable: yup + .string() + .nullable() + .required(), isAlive: yup .boolean() .nullable() @@ -720,6 +736,7 @@ type Person = yup.InferType; // email?: string | null | undefined; // birthDate?: Date | null | undefined; // canBeNull: string | null; +// requiredNullable: string; // isAlive: boolean | null | undefined; // mustBeAString: string; // children?: string[] | null | undefined; @@ -729,6 +746,7 @@ const minimalPerson: Person = { firstName: '', gender: Gender.Female, canBeNull: null, + requiredNullable: '', mustBeAString: '', }; @@ -738,6 +756,7 @@ const person: Person = { email: null, birthDate: null, canBeNull: null, + requiredNullable: '', isAlive: null, mustBeAString: '', children: null,