diff --git a/types/yup/index.d.ts b/types/yup/index.d.ts index d7d864cb68..295d127a66 100644 --- a/types/yup/index.d.ts +++ b/types/yup/index.d.ts @@ -17,7 +17,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.6 -export function reach(schema: Schema, path: string, value?: any, context?: any): Schema; +export function reach(schema: Schema, path: string, value?: any, context?: any): Schema; export function addMethod>( schemaCtor: AnySchemaConstructor, name: string, @@ -50,7 +50,7 @@ export type TestOptionsMessage = {}, R = any> | string | ((params: Extra & Partial) => R); -export interface Schema { +export interface Schema { type: string; clone(): this; label(label: string): this; @@ -58,10 +58,10 @@ export interface Schema { meta(): any; describe(): SchemaDescription; concat(schema: this): this; - validate(value: any, options?: ValidateOptions): Promise; - validateSync(value: any, options?: ValidateOptions): T; - validateAt(path: string, value: T, options?: ValidateOptions): Promise; - validateSyncAt(path: string, value: T, options?: ValidateOptions): T; + validate(value: any, options?: ValidateOptions): Promise; + validateSync(value: any, options?: ValidateOptions): T; + validateAt(path: string, value: T, options?: ValidateOptions): Promise; + validateSyncAt(path: string, value: T, options?: ValidateOptions): T; isValid(value: any, options?: any): Promise; isValidSync(value: any, options?: any): value is T; cast(value?: any, options?: any): T; @@ -83,180 +83,212 @@ export interface Schema { export interface MixedSchemaConstructor { // tslint:disable-next-line:no-unnecessary-generics - (): MixedSchema; + (): MixedSchema; // tslint:disable-next-line:no-unnecessary-generics - new (options?: { type?: string; [key: string]: any }): MixedSchema; + new (options?: { type?: string; [key: string]: any }): MixedSchema; } -export interface MixedSchema extends Schema { - nullable(isNullable?: true): MixedSchema; - nullable(isNullable: false): MixedSchema>; - nullable(isNullable?: boolean): MixedSchema; - required(message?: TestOptionsMessage): MixedSchema>; - defined(): MixedSchema>; - notRequired(): MixedSchema; - optional(): MixedSchema; +export interface MixedSchema extends Schema { + nullable(isNullable?: true): MixedSchema; + nullable(isNullable: false): MixedSchema, C>; + nullable(isNullable?: boolean): MixedSchema; + required(message?: TestOptionsMessage): MixedSchema, C>; + defined(): MixedSchema, C>; + notRequired(): MixedSchema; + optional(): MixedSchema; concat(schema: this): this; - concat(schema: Schema): MixedSchema; + concat(schema: Schema): MixedSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): MixedSchema>; + ): MixedSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): MixedSchema>; - test(name: string, message: TestOptionsMessage, test: AssertingTestFunction): MixedSchema; - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(options: AssertingTestOptions>): MixedSchema; - test(options: TestOptions>): this; + ): MixedSchema, C>; + test( + name: string, + message: TestOptionsMessage, + test: AssertingTestFunction, + ): MixedSchema; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test(options: AssertingTestOptions, C>): MixedSchema; + test(options: TestOptions, C>): this; } export interface StringSchemaConstructor { - (): StringSchema; - new (): StringSchema; + // tslint:disable-next-line:no-unnecessary-generics + (): StringSchema; + // tslint:disable-next-line:no-unnecessary-generics + new (): StringSchema; } -export interface StringSchema extends Schema { - length(limit: number | Ref, message?: StringLocale['length']): StringSchema; - min(limit: number | Ref, message?: StringLocale['min']): StringSchema; - max(limit: number | Ref, message?: StringLocale['max']): StringSchema; +export interface StringSchema + extends Schema { + length(limit: number | Ref, message?: StringLocale['length']): StringSchema; + min(limit: number | Ref, message?: StringLocale['min']): StringSchema; + max(limit: number | Ref, message?: StringLocale['max']): StringSchema; matches( regex: RegExp, messageOrOptions?: | StringLocale['matches'] | { message?: StringLocale['matches']; excludeEmptyString?: boolean }, - ): StringSchema; - email(message?: StringLocale['email']): StringSchema; - url(message?: StringLocale['url']): StringSchema; - uuid(message?: StringLocale['uuid']): StringSchema; - ensure(): StringSchema; - trim(message?: StringLocale['trim']): StringSchema; - lowercase(message?: StringLocale['lowercase']): StringSchema; - uppercase(message?: StringLocale['uppercase']): StringSchema; - nullable(isNullable?: true): StringSchema; - nullable(isNullable: false): StringSchema>; - nullable(isNullable?: boolean): StringSchema; - required(message?: TestOptionsMessage): StringSchema>; - defined(): StringSchema>; - notRequired(): StringSchema; + ): StringSchema; + email(message?: StringLocale['email']): StringSchema; + url(message?: StringLocale['url']): StringSchema; + uuid(message?: StringLocale['uuid']): StringSchema; + ensure(): StringSchema; + trim(message?: StringLocale['trim']): StringSchema; + lowercase(message?: StringLocale['lowercase']): StringSchema; + uppercase(message?: StringLocale['uppercase']): StringSchema; + nullable(isNullable?: true): StringSchema; + nullable(isNullable: false): StringSchema, C>; + nullable(isNullable?: boolean): StringSchema; + required(message?: TestOptionsMessage): StringSchema, C>; + defined(): StringSchema, C>; + notRequired(): StringSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): StringSchema>; + ): StringSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): StringSchema>; + ): StringSchema, C>; /* All TestFunction generics are intentionally T with (undefined | null) as previous .required / .defined / .nullable will narrow out those types, and tests run for (undefined | null) even if they're not allowed. */ - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(name: string, message: TestOptionsMessage, test: AssertingTestFunction): StringSchema; - test(options: AssertingTestOptions>): StringSchema; - test(options: TestOptions>): this; - optional(): StringSchema; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test( + name: string, + message: TestOptionsMessage, + test: AssertingTestFunction, + ): StringSchema; + test(options: AssertingTestOptions, C>): StringSchema; + test(options: TestOptions, C>): this; + optional(): StringSchema; } export interface NumberSchemaConstructor { - (): NumberSchema; - new (): NumberSchema; + // tslint:disable-next-line:no-unnecessary-generics + (): NumberSchema; + // tslint:disable-next-line:no-unnecessary-generics + new (): NumberSchema; } -export interface NumberSchema extends Schema { - min(limit: number | Ref, message?: NumberLocale['min']): NumberSchema; - max(limit: number | Ref, message?: NumberLocale['max']): NumberSchema; - lessThan(limit: number | Ref, message?: NumberLocale['lessThan']): NumberSchema; - moreThan(limit: number | Ref, message?: NumberLocale['moreThan']): NumberSchema; - positive(message?: NumberLocale['positive']): NumberSchema; - negative(message?: NumberLocale['negative']): NumberSchema; - integer(message?: NumberLocale['integer']): NumberSchema; - truncate(): NumberSchema; - round(type: 'floor' | 'ceil' | 'trunc' | 'round'): NumberSchema; - nullable(isNullable?: true): NumberSchema; - nullable(isNullable: false): NumberSchema>; - nullable(isNullable?: boolean): NumberSchema; - required(message?: TestOptionsMessage): NumberSchema>; - defined(): NumberSchema>; - notRequired(): NumberSchema; +export interface NumberSchema + extends Schema { + min(limit: number | Ref, message?: NumberLocale['min']): NumberSchema; + max(limit: number | Ref, message?: NumberLocale['max']): NumberSchema; + lessThan(limit: number | Ref, message?: NumberLocale['lessThan']): NumberSchema; + moreThan(limit: number | Ref, message?: NumberLocale['moreThan']): NumberSchema; + positive(message?: NumberLocale['positive']): NumberSchema; + negative(message?: NumberLocale['negative']): NumberSchema; + integer(message?: NumberLocale['integer']): NumberSchema; + truncate(): NumberSchema; + round(type: 'floor' | 'ceil' | 'trunc' | 'round'): NumberSchema; + nullable(isNullable?: true): NumberSchema; + nullable(isNullable: false): NumberSchema, C>; + nullable(isNullable?: boolean): NumberSchema; + required(message?: TestOptionsMessage): NumberSchema, C>; + defined(): NumberSchema, C>; + notRequired(): NumberSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): NumberSchema>; + ): NumberSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): NumberSchema>; - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(name: string, message: TestOptionsMessage, test: AssertingTestFunction): NumberSchema; - test(options: AssertingTestOptions>): NumberSchema; - test(options: TestOptions>): this; - optional(): NumberSchema; + ): NumberSchema, C>; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test( + name: string, + message: TestOptionsMessage, + test: AssertingTestFunction, + ): NumberSchema; + test(options: AssertingTestOptions, C>): NumberSchema; + test(options: TestOptions, C>): this; + optional(): NumberSchema; } export interface BooleanSchemaConstructor { - (): BooleanSchema; - new (): BooleanSchema; + // tslint:disable-next-line:no-unnecessary-generics + (): BooleanSchema; + // tslint:disable-next-line:no-unnecessary-generics + new (): BooleanSchema; } -export interface BooleanSchema extends Schema { - nullable(isNullable?: true): BooleanSchema; - nullable(isNullable: false): BooleanSchema>; - nullable(isNullable?: boolean): BooleanSchema; - required(message?: TestOptionsMessage): BooleanSchema>; - defined(): BooleanSchema>; - notRequired(): BooleanSchema; +export interface BooleanSchema + extends Schema { + nullable(isNullable?: true): BooleanSchema; + nullable(isNullable: false): BooleanSchema, C>; + nullable(isNullable?: boolean): BooleanSchema; + required(message?: TestOptionsMessage): BooleanSchema, C>; + defined(): BooleanSchema, C>; + notRequired(): BooleanSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): BooleanSchema>; + ): BooleanSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): BooleanSchema>; - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(name: string, message: TestOptionsMessage, test: AssertingTestFunction): BooleanSchema; - test(options: AssertingTestOptions>): BooleanSchema; - test(options: TestOptions>): this; - optional(): BooleanSchema; + ): BooleanSchema, C>; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test( + name: string, + message: TestOptionsMessage, + test: AssertingTestFunction, + ): BooleanSchema; + test(options: AssertingTestOptions, C>): BooleanSchema; + test(options: TestOptions, C>): this; + optional(): BooleanSchema; } export interface DateSchemaConstructor { - (): DateSchema; - new (): DateSchema; + // tslint:disable-next-line:no-unnecessary-generics + (): DateSchema; + // tslint:disable-next-line:no-unnecessary-generics + new (): DateSchema; } -export interface DateSchema extends Schema { - min(limit: Date | string | Ref, message?: DateLocale['min']): DateSchema; - max(limit: Date | string | Ref, message?: DateLocale['max']): DateSchema; - nullable(isNullable?: true): DateSchema; - nullable(isNullable: false): DateSchema>; - nullable(isNullable?: boolean): DateSchema; - required(message?: TestOptionsMessage): DateSchema>; - defined(): DateSchema>; - notRequired(): DateSchema; +export interface DateSchema extends Schema { + min(limit: Date | string | Ref, message?: DateLocale['min']): DateSchema; + max(limit: Date | string | Ref, message?: DateLocale['max']): DateSchema; + nullable(isNullable?: true): DateSchema; + nullable(isNullable: false): DateSchema, C>; + nullable(isNullable?: boolean): DateSchema; + required(message?: TestOptionsMessage): DateSchema, C>; + defined(): DateSchema, C>; + notRequired(): DateSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): DateSchema>; + ): DateSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], - ): DateSchema>; - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(name: string, message: TestOptionsMessage, test: AssertingTestFunction): DateSchema; - test(options: AssertingTestOptions>): DateSchema; - test(options: TestOptions>): this; - optional(): DateSchema; + ): DateSchema, C>; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test( + name: string, + message: TestOptionsMessage, + test: AssertingTestFunction, + ): DateSchema; + test(options: AssertingTestOptions, C>): DateSchema; + test(options: TestOptions, C>): this; + optional(): DateSchema; } export interface ArraySchemaConstructor { - (schema?: Schema): NotRequiredArraySchema; - new (): NotRequiredArraySchema<{}>; + (schema?: Schema): NotRequiredArraySchema; + // tslint:disable-next-line:no-unnecessary-generics + new (): NotRequiredArraySchema<{}, C>; } -export interface BasicArraySchema extends Schema { +export interface BasicArraySchema extends Schema { min(limit: number | Ref, message?: ArrayLocale['min']): this; max(limit: number | Ref, message?: ArrayLocale['max']): this; ensure(): this; @@ -270,60 +302,60 @@ export interface BasicArraySchema extends S // applies to arrays anyway. oneOf(arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf']): this; equals(arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf']): this; - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(options: TestOptions>): this; - innerType: Schema; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test(options: TestOptions, C>): this; + innerType: Schema; } -export interface NotRequiredNullableArraySchema extends BasicArraySchema { - of(type: Schema): NotRequiredNullableArraySchema; - nullable(isNullable?: true): NotRequiredNullableArraySchema; - nullable(isNullable: false): NotRequiredArraySchema; - nullable(isNullable?: boolean): ArraySchema; - required(message?: TestOptionsMessage): ArraySchema; - defined(): NullableArraySchema; - notRequired(): NotRequiredNullableArraySchema; - optional(): NotRequiredNullableArraySchema; +export interface NotRequiredNullableArraySchema extends BasicArraySchema { + of(type: Schema): NotRequiredNullableArraySchema; + nullable(isNullable?: true): NotRequiredNullableArraySchema; + nullable(isNullable: false): NotRequiredArraySchema; + nullable(isNullable?: boolean): ArraySchema; + required(message?: TestOptionsMessage): ArraySchema; + defined(): NullableArraySchema; + notRequired(): NotRequiredNullableArraySchema; + optional(): NotRequiredNullableArraySchema; } -export interface NullableArraySchema extends BasicArraySchema { - of(type: Schema): NullableArraySchema; - nullable(isNullable?: true): NullableArraySchema; - nullable(isNullable: false): ArraySchema; - nullable(isNullable?: boolean): ArraySchema; - required(message?: TestOptionsMessage): ArraySchema; - defined(): NullableArraySchema; - notRequired(): NotRequiredNullableArraySchema; - optional(): NotRequiredNullableArraySchema; +export interface NullableArraySchema extends BasicArraySchema { + of(type: Schema): NullableArraySchema; + nullable(isNullable?: true): NullableArraySchema; + nullable(isNullable: false): ArraySchema; + nullable(isNullable?: boolean): ArraySchema; + required(message?: TestOptionsMessage): ArraySchema; + defined(): NullableArraySchema; + notRequired(): NotRequiredNullableArraySchema; + optional(): NotRequiredNullableArraySchema; } -export interface NotRequiredArraySchema extends BasicArraySchema { - of(type: Schema): NotRequiredArraySchema; - nullable(isNullable?: true): NotRequiredNullableArraySchema; - nullable(isNullable: false): NotRequiredArraySchema; - nullable(isNullable: boolean): ArraySchema; - required(message?: TestOptionsMessage): ArraySchema; - defined(): ArraySchema; - notRequired(): NotRequiredArraySchema; - optional(): NotRequiredArraySchema; +export interface NotRequiredArraySchema extends BasicArraySchema { + of(type: Schema): NotRequiredArraySchema; + nullable(isNullable?: true): NotRequiredNullableArraySchema; + nullable(isNullable: false): NotRequiredArraySchema; + nullable(isNullable: boolean): ArraySchema; + required(message?: TestOptionsMessage): ArraySchema; + defined(): ArraySchema; + notRequired(): NotRequiredArraySchema; + optional(): NotRequiredArraySchema; } -export interface ArraySchema extends BasicArraySchema { - of(type: Schema): ArraySchema; - nullable(isNullable?: true): NullableArraySchema; - nullable(isNullable: false | boolean): ArraySchema; - required(message?: TestOptionsMessage): ArraySchema; - defined(): ArraySchema; - notRequired(): NotRequiredArraySchema; - optional(): NotRequiredArraySchema; +export interface ArraySchema extends BasicArraySchema { + of(type: Schema): ArraySchema; + nullable(isNullable?: true): NullableArraySchema; + nullable(isNullable: false | boolean): ArraySchema; + required(message?: TestOptionsMessage): ArraySchema; + defined(): ArraySchema; + notRequired(): NotRequiredArraySchema; + optional(): NotRequiredArraySchema; } -export type ObjectSchemaDefinition = { +export type ObjectSchemaDefinition = { // This shouldn't be necessary because MixedSchema extends Schema, but type // inference only works with it this way - otherwise when you use a mixed // field in object schema, it will type as `unknown`. Not sure why that is - // maybe some sort of inference depth limit? - [field in keyof T]: Schema | MixedSchema | Ref; + [field in keyof T]: Schema | MixedSchema | Ref; }; /** @@ -336,47 +368,53 @@ export type Shape = | PreserveOptionals; export interface ObjectSchemaConstructor { - (fields?: ObjectSchemaDefinition): ObjectSchema; - new (): ObjectSchema<{}>; + (fields?: ObjectSchemaDefinition): ObjectSchema; + // tslint:disable-next-line:no-unnecessary-generics + new (): ObjectSchema<{}, C>; } -export interface ObjectSchema extends Schema { +export interface ObjectSchema + extends Schema { fields: { - [k in keyof T]: Schema; + [k in keyof T]: Schema; }; shape( - fields: ObjectSchemaDefinition, + fields: ObjectSchemaDefinition, noSortEdges?: Array<[string, string]>, - ): ObjectSchema>; - from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema; - noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema; - unknown(allow?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema; + ): ObjectSchema, C>; + from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema; + noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema; + unknown(allow?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema; transformKeys(callback: (key: any) => any): void; - camelCase(): ObjectSchema; - snakeCase(): ObjectSchema; - constantCase(): ObjectSchema; - nullable(isNullable?: true): ObjectSchema; - nullable(isNullable: false): ObjectSchema>; - nullable(isNullable?: boolean): ObjectSchema; - required(message?: TestOptionsMessage): ObjectSchema>; - defined(): ObjectSchema>; - notRequired(): ObjectSchema; - optional(): ObjectSchema; + camelCase(): ObjectSchema; + snakeCase(): ObjectSchema; + constantCase(): ObjectSchema; + nullable(isNullable?: true): ObjectSchema; + nullable(isNullable: false): ObjectSchema, C>; + nullable(isNullable?: boolean): ObjectSchema; + required(message?: TestOptionsMessage): ObjectSchema, C>; + defined(): ObjectSchema, C>; + notRequired(): ObjectSchema; + optional(): ObjectSchema; concat(schema: this): this; - concat(schema: ObjectSchema): ObjectSchema; - oneOf(arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf']): ObjectSchema; - equals(arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf']): ObjectSchema; - test(name: string, message: TestOptionsMessage, test: TestFunction): this; - test(name: string, message: TestOptionsMessage, test: AssertingTestFunction): ObjectSchema; - test(options: AssertingTestOptions>): ObjectSchema; - test(options: TestOptions>): this; + concat(schema: ObjectSchema): ObjectSchema; + oneOf(arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf']): ObjectSchema; + equals(arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf']): ObjectSchema; + test(name: string, message: TestOptionsMessage, test: TestFunction): this; + test( + name: string, + message: TestOptionsMessage, + test: AssertingTestFunction, + ): ObjectSchema; + test(options: AssertingTestOptions, C>): ObjectSchema; + test(options: TestOptions, C>): this; } -export type TestFunction = ( - this: TestContext, +export type TestFunction = ( + this: TestContext, value: T, ) => boolean | ValidationError | Promise; -export type AssertingTestFunction = (this: TestContext, value: any) => value is T; +export type AssertingTestFunction = (this: TestContext, value: any) => value is T; export type TransformFunction = (this: T, value: any, originalValue: any) => any; @@ -399,16 +437,16 @@ export type WhenOptionsBuilderObject = export type WhenOptions = WhenOptionsBuilderFunction | WhenOptionsBuilderObject; -export interface TestContext { +export interface TestContext { path: string; - options: ValidateOptions; + options: ValidateOptions; parent: any; - schema: Schema; + schema: Schema; resolve: (value: any) => any; createError: (params?: { path?: string; message?: string; params?: object }) => ValidationError; } -export interface ValidateOptions { +export interface ValidateOptions { /** * Only validate the input, and skip and coercion or transformation. Default - false */ @@ -428,7 +466,7 @@ export interface ValidateOptions { /** * Any context needed for validating schema conditions (see: when()) */ - context?: object; + context?: C; } export interface TestMessageParams { @@ -438,7 +476,7 @@ export interface TestMessageParams { label: string; } -interface BaseTestOptions

> { +interface BaseTestOptions

, C = object> { /** * Unique name identifying the test. Required for exclusive tests. */ @@ -447,7 +485,7 @@ interface BaseTestOptions

> { /** * Test function, determines schema validity */ - test: TestFunction; + test: TestFunction; /** * The validation error message @@ -465,28 +503,30 @@ interface BaseTestOptions

> { exclusive?: boolean; } -interface NonExclusiveTestOptions

> extends BaseTestOptions

{ +interface NonExclusiveTestOptions

, C> extends BaseTestOptions { exclusive?: false; } -interface ExclusiveTestOptions

> extends BaseTestOptions

{ +interface ExclusiveTestOptions

, C> extends BaseTestOptions { exclusive: true; name: string; } -interface NonExclusiveAssertingTestOptions> extends NonExclusiveTestOptions

{ - test: AssertingTestFunction; +interface NonExclusiveAssertingTestOptions, C> extends NonExclusiveTestOptions { + test: AssertingTestFunction; } -interface ExclusiveAssertingTestOptions> extends ExclusiveTestOptions

{ - test: AssertingTestFunction; +interface ExclusiveAssertingTestOptions, C> extends ExclusiveTestOptions { + test: AssertingTestFunction; } -export type TestOptions

= {}> = NonExclusiveTestOptions

| ExclusiveTestOptions

; +export type TestOptions

= {}, C = object> = + | NonExclusiveTestOptions + | ExclusiveTestOptions; -export type AssertingTestOptions = {}> = - | NonExclusiveAssertingTestOptions - | ExclusiveAssertingTestOptions; +export type AssertingTestOptions = {}, C = object> = + | NonExclusiveAssertingTestOptions + | ExclusiveAssertingTestOptions; export interface SchemaFieldRefDescription { type: 'ref'; diff --git a/types/yup/yup-tests.ts b/types/yup/yup-tests.ts index e6de8888a0..ca6c853392 100644 --- a/types/yup/yup-tests.ts +++ b/types/yup/yup-tests.ts @@ -112,9 +112,9 @@ error.type = []; error.errors = ['error']; // mixed -let mixed: MixedSchema = yup.mixed(); // $ExpectType MixedSchema<{} | null | undefined> +let mixed: MixedSchema = yup.mixed(); // $ExpectType MixedSchema<{} | null | undefined, object> mixed.type; -mixed.defined(); // $ExpectType MixedSchema<{} | null> +mixed.defined(); // $ExpectType MixedSchema<{} | null, object> mixed.clone(); mixed.label('label'); mixed.meta({ meta: 'value' }); @@ -143,12 +143,12 @@ mixed.nullable(true); mixed.nullable(); mixed.required(); mixed.required('Foo'); -mixed.nullable().required(); // $ExpectType MixedSchema<{}> +mixed.nullable().required(); // $ExpectType MixedSchema<{}, object> mixed.required(() => 'Foo'); mixed.defined(); -mixed.notRequired(); // $ExpectType MixedSchema<{} | null | undefined> -mixed.optional(); // $ExpectType MixedSchema<{} | null | undefined> -mixed.required().optional(); // $ExpectType MixedSchema<{} | undefined> +mixed.notRequired(); // $ExpectType MixedSchema<{} | null | undefined, object> +mixed.optional(); // $ExpectType MixedSchema<{} | null | undefined, object> +mixed.required().optional(); // $ExpectType MixedSchema<{} | undefined, object> mixed.typeError('type error'); mixed.typeError(() => 'type error'); mixed.oneOf(['hello', 'world'], 'message'); @@ -156,8 +156,8 @@ mixed.oneOf(['hello', 'world'], () => 'message'); mixed.oneOf(['hello', 'world'], ({ values }) => `one of ${values}`); // $ExpectError mixed.oneOf(['hello', 'world'], ({ random }) => `one of ${random}`); -mixed.oneOf(["hello", 1] as const); // $ExpectType MixedSchema<"hello" | 1 | undefined> -mixed.equals(["hello", 1] as const); // $ExpectType MixedSchema<"hello" | 1 | undefined> +mixed.oneOf(["hello", 1] as const); // $ExpectType MixedSchema<"hello" | 1 | undefined, object> +mixed.equals(["hello", 1] as const); // $ExpectType MixedSchema<"hello" | 1 | undefined, object> mixed.notOneOf(['hello', 'world'], 'message'); mixed.notOneOf(['hello', 'world'], () => 'message'); mixed.when('isBig', { @@ -191,14 +191,14 @@ mixed.test({ message: '${path} must be less than 5 characters', test: value => value == null || value.length <= 5, }); -// $ExpectType MixedSchema> +// $ExpectType MixedSchema, object> mixed.test( 'is-function', // tslint:disable-next-line:no-invalid-template-strings "${path} is not a function", (value): value is Set => value instanceof Set, ); -// $ExpectType MixedSchema> +// $ExpectType MixedSchema, object> mixed.test({ name: 'is-function', exclusive: true, @@ -218,11 +218,11 @@ mixed.test('with-promise', 'It contains invalid value', value => new Promise this.options; // $ExpectType any this.parent; - // $ExpectType Schema + // $ExpectType Schema this.schema; // $ExpectType (value: any) => any this.resolve; @@ -247,12 +247,12 @@ mixed.test({ test: value => !!value, }); -yup.mixed().oneOf(['hello', 1, null] as const).nullable(); // $ExpectType MixedSchema<"hello" | 1 | null | undefined> -yup.mixed().nullable().oneOf(['hello', 1, null] as const); // $ExpectType MixedSchema<"hello" | 1 | null | undefined> +yup.mixed().oneOf(['hello', 1, null] as const).nullable(); // $ExpectType MixedSchema<"hello" | 1 | null | undefined, object> +yup.mixed().nullable().oneOf(['hello', 1, null] as const); // $ExpectType MixedSchema<"hello" | 1 | null | undefined, object> // mixed with concat -yup.object({ name: yup.string().defined() }).defined().concat(yup.object({ when: yup.date().defined() }).defined()); // $ExpectType ObjectSchema<{ name: string; } & { when: Date; }> -yup.mixed().defined().concat(yup.date().defined()); // $ExpectType MixedSchema +yup.object({ name: yup.string().defined() }).defined().concat(yup.object({ when: yup.date().defined() }).defined()); // $ExpectType ObjectSchema<{ name: string; } & { when: Date; }, object> +yup.mixed().defined().concat(yup.date().defined()); // $ExpectType MixedSchema // Async ValidationError const asyncValidationErrorTest = (includeParams: boolean) => @@ -370,16 +370,16 @@ function strSchemaTests(strSchema: yup.StringSchema) { strSchema.defined(); } -const strSchema = yup.string(); // $ExpectType StringSchema -strSchema.oneOf(["hello", "world"] as const); // $ExpectType StringSchema<"hello" | "world" | undefined> -strSchema.required().oneOf(["hello", "world"] as const); // $ExpectType StringSchema<"hello" | "world"> +const strSchema = yup.string(); // $ExpectType StringSchema +strSchema.oneOf(["hello", "world"] as const); // $ExpectType StringSchema<"hello" | "world" | undefined, object> +strSchema.required().oneOf(["hello", "world"] as const); // $ExpectType StringSchema<"hello" | "world", object> strSchemaTests(strSchema); // $ExpectError yup.string<123>(); // Number schema -const numSchema = yup.number(); // $ExpectType NumberSchema +const numSchema = yup.number(); // $ExpectType NumberSchema numSchema.type; numSchema.isValid(10); // => true numSchema.min(5); @@ -415,22 +415,22 @@ numSchema .validate(5, { strict: true }) .then(value => value) .catch(err => err); -numSchema.oneOf([1, 2] as const); // $ExpectType NumberSchema<1 | 2 | undefined> -numSchema.equals([1, 2] as const); // $ExpectType NumberSchema<1 | 2 | undefined> -numSchema.required().oneOf([1, 2] as const); // $ExpectType NumberSchema<1 | 2> +numSchema.oneOf([1, 2] as const); // $ExpectType NumberSchema<1 | 2 | undefined, object> +numSchema.equals([1, 2] as const); // $ExpectType NumberSchema<1 | 2 | undefined, object> +numSchema.required().oneOf([1, 2] as const); // $ExpectType NumberSchema<1 | 2, object> numSchema.defined(); // Boolean Schema -const boolSchema = yup.boolean(); // $ExpectType BooleanSchema +const boolSchema = yup.boolean(); // $ExpectType BooleanSchema boolSchema.type; boolSchema.isValid(true); // => true -boolSchema.oneOf([true] as const); // $ExpectType BooleanSchema -boolSchema.equals([true] as const); // $ExpectType BooleanSchema -boolSchema.required().oneOf([true] as const); // $ExpectType BooleanSchema +boolSchema.oneOf([true] as const); // $ExpectType BooleanSchema +boolSchema.equals([true] as const); // $ExpectType BooleanSchema +boolSchema.required().oneOf([true] as const); // $ExpectType BooleanSchema boolSchema.defined(); // Date Schema -const dateSchema = yup.date(); // $ExpectType DateSchema +const dateSchema = yup.date(); // $ExpectType DateSchema dateSchema.type; dateSchema.isValid(new Date()); // => true dateSchema.min(new Date()); @@ -443,9 +443,9 @@ dateSchema.max('2017-11-12'); dateSchema.max(new Date(), 'message'); dateSchema.max('2017-11-12', 'message'); dateSchema.max('2017-11-12', () => 'message'); -dateSchema.oneOf([new Date()] as const); // $ExpectType DateSchema -dateSchema.equals([new Date()] as const); // $ExpectType DateSchema -dateSchema.required().oneOf([new Date()] as const); // $ExpectType DateSchema +dateSchema.oneOf([new Date()] as const); // $ExpectType DateSchema +dateSchema.equals([new Date()] as const); // $ExpectType DateSchema +dateSchema.required().oneOf([new Date()] as const); // $ExpectType DateSchema // Array Schema const arrSchema = yup.array().of(yup.number().defined().min(2)); @@ -470,8 +470,8 @@ arrSchema.min(5); arrSchema.min(5, 'min'); arrSchema.min(5, () => 'min'); arrSchema.compact((value, index, array) => value === array[index]); -arrSchema.oneOf([]); // $ExpectType NotRequiredArraySchema -arrSchema.equals([]); // $ExpectType NotRequiredArraySchema +arrSchema.oneOf([]); // $ExpectType NotRequiredArraySchema +arrSchema.equals([]); // $ExpectType NotRequiredArraySchema arrSchema.defined(); const arrOfObjSchema = yup.array().defined().of( @@ -486,8 +486,8 @@ arrOfObjSchema.compact((value, index, array) => { const arr = yup.array().defined(); const top = ((x?: T): T => x!)(); const validArr: yup.ArraySchema = arr; -yup.array(yup.string().defined()); // $ExpectType NotRequiredArraySchema -yup.array().of(yup.string().defined()); // $ExpectType NotRequiredArraySchema +yup.array(yup.string().defined()); // $ExpectType NotRequiredArraySchema +yup.array().of(yup.string().defined()); // $ExpectType NotRequiredArraySchema // Object Schema const objSchema = yup.object().shape({ @@ -525,7 +525,7 @@ interface LiteralExampleObject { email: "john@example.com"; website: "example.com"; } -objSchema.oneOf([{name: "John Doe", age: 35, email: "john@example.com", website: "example.com"}] as LiteralExampleObject[]); // $ExpectType ObjectSchema +objSchema.oneOf([{name: "John Doe", age: 35, email: "john@example.com", website: "example.com"}] as LiteralExampleObject[]); // $ExpectType ObjectSchema objSchema.defined(); const description: SchemaDescription = { @@ -694,16 +694,16 @@ interface SubInterface { testField: string; } -// $ExpectType ObjectSchema +// $ExpectType ObjectSchema const typedSchema = yup.object({ - stringField: yup.string().required(), // $ExpectType StringSchema - numberField: yup.number().required(), // $ExpectType NumberSchema + stringField: yup.string().required(), // $ExpectType StringSchema + numberField: yup.number().required(), // $ExpectType NumberSchema subFields: yup .object({ testField: yup.string().required(), }) .required(), - arrayField: yup.array(yup.string().defined()).required(), // $ExpectType ArraySchema + arrayField: yup.array(yup.string().defined()).required(), // $ExpectType ArraySchema }).defined(); const testObject: MyInterface = { @@ -749,7 +749,7 @@ const definitionBC: yup.ObjectSchemaDefinition = { b: yup.string().defined(), c: yup.number().defined(), }; -const combinedSchema = yup.object(definitionAB).defined().shape(definitionBC); // $ExpectType ObjectSchema<{ a: string; b: string; } & BC> +const combinedSchema = yup.object(definitionAB).defined().shape(definitionBC); // $ExpectType ObjectSchema<{ a: string; b: string; } & BC, object> // $ExpectError yup.object({ @@ -797,7 +797,7 @@ enum Gender { } const personSchema = yup.object({ - firstName: yup.string().defined(), // $ExpectType StringSchema + firstName: yup.string().defined(), // $ExpectType StringSchema gender: yup.mixed().defined().oneOf([Gender.Male, Gender.Female]), // handle nested optional property address: yup.object({ @@ -822,7 +822,7 @@ const personSchema = yup.object({ .nullable() .notRequired() .min(new Date(1900, 0, 1)), - canBeNull: yup.string().defined().nullable(true), // $ExpectType StringSchema + canBeNull: yup.string().defined().nullable(true), // $ExpectType StringSchema isAlive: yup .boolean() .nullable() @@ -961,8 +961,8 @@ function wrapper(b: boolean, msx: MixedSchema): MixedSchema { return msx.nullable(b); } -const resultingSchema1 = wrapper(false, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema -const resultingSchema2 = wrapper(true, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema +const resultingSchema1 = wrapper(false, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema +const resultingSchema2 = wrapper(true, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema const arrayOfStringsSchema = yup.array().of(yup.string().defined()).defined(); type ArrayOfStrings = yup.InferType; @@ -1011,8 +1011,8 @@ declare module './index' { chineseMobilePhoneNumber?: TestOptionsMessage; } - interface StringSchema extends Schema { - chineseMobilePhoneNumber(message?: StringLocale['chineseMobilePhoneNumber']): StringSchema; + interface StringSchema extends Schema { + chineseMobilePhoneNumber(message?: StringLocale['chineseMobilePhoneNumber']): StringSchema; } } yup.setLocale({ @@ -1090,3 +1090,31 @@ yup.object({ }); }) }); + +interface MyContext { + a: string; +} +// $ExpectType MixedSchema +const schemaWithMyContext = yup.mixed().test({ + test() { + const context = this.options.context; // $ExpectType MyContext | undefined + return false; + }, +}); +schemaWithMyContext.validateSync('test value', { context: { a: 'context value' }}); +// $ExpectError +schemaWithMyContext.validateSync('test value', { context: { x: 1 }}); + +const schemaWithDifferentContext = yup.mixed(); + +const arraySchemaWithMyContext = yup.array(); +arraySchemaWithMyContext.of(schemaWithMyContext); +// $ExpectError +arraySchemaWithMyContext.of(schemaWithDifferentContext); + +const objectSchemaWithMyContext = yup.object(); +objectSchemaWithMyContext.shape({ + prop1: schemaWithMyContext, + // $ExpectError + prop2: schemaWithDifferentContext, +});