mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
🤖 Merge PR #47367 [yup] Added schema context type param by @MaximSagan
This commit is contained in:
parent
804f1f68a0
commit
bacc18855f
436
types/yup/index.d.ts
vendored
436
types/yup/index.d.ts
vendored
@ -17,7 +17,7 @@
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 3.6
|
||||
|
||||
export function reach<T>(schema: Schema<T>, path: string, value?: any, context?: any): Schema<T>;
|
||||
export function reach<T, C = object>(schema: Schema<T, C>, path: string, value?: any, context?: any): Schema<T, C>;
|
||||
export function addMethod<T extends Schema<any>>(
|
||||
schemaCtor: AnySchemaConstructor,
|
||||
name: string,
|
||||
@ -50,7 +50,7 @@ export type TestOptionsMessage<Extra extends Record<string, any> = {}, R = any>
|
||||
| string
|
||||
| ((params: Extra & Partial<TestMessageParams>) => R);
|
||||
|
||||
export interface Schema<T> {
|
||||
export interface Schema<T, C = object> {
|
||||
type: string;
|
||||
clone(): this;
|
||||
label(label: string): this;
|
||||
@ -58,10 +58,10 @@ export interface Schema<T> {
|
||||
meta(): any;
|
||||
describe(): SchemaDescription;
|
||||
concat(schema: this): this;
|
||||
validate(value: any, options?: ValidateOptions): Promise<T>;
|
||||
validateSync(value: any, options?: ValidateOptions): T;
|
||||
validateAt(path: string, value: T, options?: ValidateOptions): Promise<T>;
|
||||
validateSyncAt(path: string, value: T, options?: ValidateOptions): T;
|
||||
validate(value: any, options?: ValidateOptions<C>): Promise<T>;
|
||||
validateSync(value: any, options?: ValidateOptions<C>): T;
|
||||
validateAt(path: string, value: T, options?: ValidateOptions<C>): Promise<T>;
|
||||
validateSyncAt(path: string, value: T, options?: ValidateOptions<C>): T;
|
||||
isValid(value: any, options?: any): Promise<boolean>;
|
||||
isValidSync(value: any, options?: any): value is T;
|
||||
cast(value?: any, options?: any): T;
|
||||
@ -83,180 +83,212 @@ export interface Schema<T> {
|
||||
|
||||
export interface MixedSchemaConstructor {
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
<T = {} | null | undefined>(): MixedSchema<T>;
|
||||
<T = {} | null | undefined, C = object>(): MixedSchema<T, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <T = {} | null | undefined>(options?: { type?: string; [key: string]: any }): MixedSchema<T>;
|
||||
new <T = {} | null | undefined, C = object>(options?: { type?: string; [key: string]: any }): MixedSchema<T, C>;
|
||||
}
|
||||
|
||||
export interface MixedSchema<T extends any = {} | null | undefined> extends Schema<T> {
|
||||
nullable(isNullable?: true): MixedSchema<T | null>;
|
||||
nullable(isNullable: false): MixedSchema<Exclude<T, null>>;
|
||||
nullable(isNullable?: boolean): MixedSchema<T>;
|
||||
required(message?: TestOptionsMessage): MixedSchema<Exclude<T, undefined | null>>;
|
||||
defined(): MixedSchema<Exclude<T, undefined>>;
|
||||
notRequired(): MixedSchema<T | undefined>;
|
||||
optional(): MixedSchema<T | undefined>;
|
||||
export interface MixedSchema<T extends any = {} | null | undefined, C = object> extends Schema<T, C> {
|
||||
nullable(isNullable?: true): MixedSchema<T | null, C>;
|
||||
nullable(isNullable: false): MixedSchema<Exclude<T, null>, C>;
|
||||
nullable(isNullable?: boolean): MixedSchema<T, C>;
|
||||
required(message?: TestOptionsMessage): MixedSchema<Exclude<T, undefined | null>, C>;
|
||||
defined(): MixedSchema<Exclude<T, undefined>, C>;
|
||||
notRequired(): MixedSchema<T | undefined, C>;
|
||||
optional(): MixedSchema<T | undefined, C>;
|
||||
concat(schema: this): this;
|
||||
concat<U>(schema: Schema<U>): MixedSchema<T | U>;
|
||||
concat<U>(schema: Schema<U, C>): MixedSchema<T | U, C>;
|
||||
oneOf<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): MixedSchema<MaintainOptionality<T, U>>;
|
||||
): MixedSchema<MaintainOptionality<T, U>, C>;
|
||||
equals<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): MixedSchema<MaintainOptionality<T, U>>;
|
||||
test<U extends T = T>(name: string, message: TestOptionsMessage, test: AssertingTestFunction<U>): MixedSchema<U>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction): this;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>>): MixedSchema<U>;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
): MixedSchema<MaintainOptionality<T, U>, C>;
|
||||
test<U extends T = T>(
|
||||
name: string,
|
||||
message: TestOptionsMessage,
|
||||
test: AssertingTestFunction<U, C>,
|
||||
): MixedSchema<U, C>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<unknown, C>): this;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): MixedSchema<U, C>;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
}
|
||||
|
||||
export interface StringSchemaConstructor {
|
||||
(): StringSchema;
|
||||
new (): StringSchema;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
<T extends string | null | undefined = string | undefined, C = object>(): StringSchema<T, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <T extends string | null | undefined = string | undefined, C = object>(): StringSchema<T, C>;
|
||||
}
|
||||
|
||||
export interface StringSchema<T extends string | null | undefined = string | undefined> extends Schema<T> {
|
||||
length(limit: number | Ref, message?: StringLocale['length']): StringSchema<T>;
|
||||
min(limit: number | Ref, message?: StringLocale['min']): StringSchema<T>;
|
||||
max(limit: number | Ref, message?: StringLocale['max']): StringSchema<T>;
|
||||
export interface StringSchema<T extends string | null | undefined = string | undefined, C = object>
|
||||
extends Schema<T, C> {
|
||||
length(limit: number | Ref, message?: StringLocale['length']): StringSchema<T, C>;
|
||||
min(limit: number | Ref, message?: StringLocale['min']): StringSchema<T, C>;
|
||||
max(limit: number | Ref, message?: StringLocale['max']): StringSchema<T, C>;
|
||||
matches(
|
||||
regex: RegExp,
|
||||
messageOrOptions?:
|
||||
| StringLocale['matches']
|
||||
| { message?: StringLocale['matches']; excludeEmptyString?: boolean },
|
||||
): StringSchema<T>;
|
||||
email(message?: StringLocale['email']): StringSchema<T>;
|
||||
url(message?: StringLocale['url']): StringSchema<T>;
|
||||
uuid(message?: StringLocale['uuid']): StringSchema<T>;
|
||||
ensure(): StringSchema<T>;
|
||||
trim(message?: StringLocale['trim']): StringSchema<T>;
|
||||
lowercase(message?: StringLocale['lowercase']): StringSchema<T>;
|
||||
uppercase(message?: StringLocale['uppercase']): StringSchema<T>;
|
||||
nullable(isNullable?: true): StringSchema<T | null>;
|
||||
nullable(isNullable: false): StringSchema<Exclude<T, null>>;
|
||||
nullable(isNullable?: boolean): StringSchema<T>;
|
||||
required(message?: TestOptionsMessage): StringSchema<Exclude<T, undefined | null>>;
|
||||
defined(): StringSchema<Exclude<T, undefined>>;
|
||||
notRequired(): StringSchema<T | undefined>;
|
||||
): StringSchema<T, C>;
|
||||
email(message?: StringLocale['email']): StringSchema<T, C>;
|
||||
url(message?: StringLocale['url']): StringSchema<T, C>;
|
||||
uuid(message?: StringLocale['uuid']): StringSchema<T, C>;
|
||||
ensure(): StringSchema<T, C>;
|
||||
trim(message?: StringLocale['trim']): StringSchema<T, C>;
|
||||
lowercase(message?: StringLocale['lowercase']): StringSchema<T, C>;
|
||||
uppercase(message?: StringLocale['uppercase']): StringSchema<T, C>;
|
||||
nullable(isNullable?: true): StringSchema<T | null, C>;
|
||||
nullable(isNullable: false): StringSchema<Exclude<T, null>, C>;
|
||||
nullable(isNullable?: boolean): StringSchema<T, C>;
|
||||
required(message?: TestOptionsMessage): StringSchema<Exclude<T, undefined | null>, C>;
|
||||
defined(): StringSchema<Exclude<T, undefined>, C>;
|
||||
notRequired(): StringSchema<T | undefined, C>;
|
||||
oneOf<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): StringSchema<MaintainOptionality<T, U>>;
|
||||
): StringSchema<MaintainOptionality<T, U>, C>;
|
||||
equals<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): StringSchema<MaintainOptionality<T, U>>;
|
||||
): StringSchema<MaintainOptionality<T, U>, 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<T | undefined | null>): this;
|
||||
test<U extends T = T>(name: string, message: TestOptionsMessage, test: AssertingTestFunction<U>): StringSchema<U>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>>): StringSchema<U>;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
optional(): StringSchema<T | undefined>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
|
||||
test<U extends T = T>(
|
||||
name: string,
|
||||
message: TestOptionsMessage,
|
||||
test: AssertingTestFunction<U, C>,
|
||||
): StringSchema<U, C>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): StringSchema<U, C>;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
optional(): StringSchema<T | undefined, C>;
|
||||
}
|
||||
|
||||
export interface NumberSchemaConstructor {
|
||||
(): NumberSchema;
|
||||
new (): NumberSchema;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
<T extends number | null | undefined = number | undefined, C = object>(): NumberSchema<T, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <T extends number | null | undefined = number | undefined, C = object>(): NumberSchema<T, C>;
|
||||
}
|
||||
|
||||
export interface NumberSchema<T extends number | null | undefined = number | undefined> extends Schema<T> {
|
||||
min(limit: number | Ref, message?: NumberLocale['min']): NumberSchema<T>;
|
||||
max(limit: number | Ref, message?: NumberLocale['max']): NumberSchema<T>;
|
||||
lessThan(limit: number | Ref, message?: NumberLocale['lessThan']): NumberSchema<T>;
|
||||
moreThan(limit: number | Ref, message?: NumberLocale['moreThan']): NumberSchema<T>;
|
||||
positive(message?: NumberLocale['positive']): NumberSchema<T>;
|
||||
negative(message?: NumberLocale['negative']): NumberSchema<T>;
|
||||
integer(message?: NumberLocale['integer']): NumberSchema<T>;
|
||||
truncate(): NumberSchema<T>;
|
||||
round(type: 'floor' | 'ceil' | 'trunc' | 'round'): NumberSchema<T>;
|
||||
nullable(isNullable?: true): NumberSchema<T | null>;
|
||||
nullable(isNullable: false): NumberSchema<Exclude<T, null>>;
|
||||
nullable(isNullable?: boolean): NumberSchema<T>;
|
||||
required(message?: TestOptionsMessage): NumberSchema<Exclude<T, undefined | null>>;
|
||||
defined(): NumberSchema<Exclude<T, undefined>>;
|
||||
notRequired(): NumberSchema<T | undefined>;
|
||||
export interface NumberSchema<T extends number | null | undefined = number | undefined, C = object>
|
||||
extends Schema<T, C> {
|
||||
min(limit: number | Ref, message?: NumberLocale['min']): NumberSchema<T, C>;
|
||||
max(limit: number | Ref, message?: NumberLocale['max']): NumberSchema<T, C>;
|
||||
lessThan(limit: number | Ref, message?: NumberLocale['lessThan']): NumberSchema<T, C>;
|
||||
moreThan(limit: number | Ref, message?: NumberLocale['moreThan']): NumberSchema<T, C>;
|
||||
positive(message?: NumberLocale['positive']): NumberSchema<T, C>;
|
||||
negative(message?: NumberLocale['negative']): NumberSchema<T, C>;
|
||||
integer(message?: NumberLocale['integer']): NumberSchema<T, C>;
|
||||
truncate(): NumberSchema<T, C>;
|
||||
round(type: 'floor' | 'ceil' | 'trunc' | 'round'): NumberSchema<T, C>;
|
||||
nullable(isNullable?: true): NumberSchema<T | null, C>;
|
||||
nullable(isNullable: false): NumberSchema<Exclude<T, null>, C>;
|
||||
nullable(isNullable?: boolean): NumberSchema<T, C>;
|
||||
required(message?: TestOptionsMessage): NumberSchema<Exclude<T, undefined | null>, C>;
|
||||
defined(): NumberSchema<Exclude<T, undefined>, C>;
|
||||
notRequired(): NumberSchema<T | undefined, C>;
|
||||
oneOf<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): NumberSchema<MaintainOptionality<T, U>>;
|
||||
): NumberSchema<MaintainOptionality<T, U>, C>;
|
||||
equals<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): NumberSchema<MaintainOptionality<T, U>>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null>): this;
|
||||
test<U extends T = T>(name: string, message: TestOptionsMessage, test: AssertingTestFunction<U>): NumberSchema<U>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>>): NumberSchema<U>;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
optional(): NumberSchema<T | undefined>;
|
||||
): NumberSchema<MaintainOptionality<T, U>, C>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
|
||||
test<U extends T = T>(
|
||||
name: string,
|
||||
message: TestOptionsMessage,
|
||||
test: AssertingTestFunction<U, C>,
|
||||
): NumberSchema<U, C>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): NumberSchema<U, C>;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
optional(): NumberSchema<T | undefined, C>;
|
||||
}
|
||||
|
||||
export interface BooleanSchemaConstructor {
|
||||
(): BooleanSchema;
|
||||
new (): BooleanSchema;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
<T extends boolean | null | undefined = boolean | undefined, C = object>(): BooleanSchema<T, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <T extends boolean | null | undefined = boolean | undefined, C = object>(): BooleanSchema<T, C>;
|
||||
}
|
||||
|
||||
export interface BooleanSchema<T extends boolean | null | undefined = boolean | undefined> extends Schema<T> {
|
||||
nullable(isNullable?: true): BooleanSchema<T | null>;
|
||||
nullable(isNullable: false): BooleanSchema<Exclude<T, null>>;
|
||||
nullable(isNullable?: boolean): BooleanSchema<T>;
|
||||
required(message?: TestOptionsMessage): BooleanSchema<Exclude<T, undefined | null>>;
|
||||
defined(): BooleanSchema<Exclude<T, undefined>>;
|
||||
notRequired(): BooleanSchema<T | undefined>;
|
||||
export interface BooleanSchema<T extends boolean | null | undefined = boolean | undefined, C = object>
|
||||
extends Schema<T, C> {
|
||||
nullable(isNullable?: true): BooleanSchema<T | null, C>;
|
||||
nullable(isNullable: false): BooleanSchema<Exclude<T, null>, C>;
|
||||
nullable(isNullable?: boolean): BooleanSchema<T, C>;
|
||||
required(message?: TestOptionsMessage): BooleanSchema<Exclude<T, undefined | null>, C>;
|
||||
defined(): BooleanSchema<Exclude<T, undefined>, C>;
|
||||
notRequired(): BooleanSchema<T | undefined, C>;
|
||||
oneOf<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): BooleanSchema<MaintainOptionality<T, U>>;
|
||||
): BooleanSchema<MaintainOptionality<T, U>, C>;
|
||||
equals<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): BooleanSchema<MaintainOptionality<T, U>>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null>): this;
|
||||
test<U extends T = T>(name: string, message: TestOptionsMessage, test: AssertingTestFunction<U>): BooleanSchema<U>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>>): BooleanSchema<U>;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
optional(): BooleanSchema<T | undefined>;
|
||||
): BooleanSchema<MaintainOptionality<T, U>, C>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
|
||||
test<U extends T = T>(
|
||||
name: string,
|
||||
message: TestOptionsMessage,
|
||||
test: AssertingTestFunction<U, C>,
|
||||
): BooleanSchema<U, C>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): BooleanSchema<U, C>;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
optional(): BooleanSchema<T | undefined, C>;
|
||||
}
|
||||
|
||||
export interface DateSchemaConstructor {
|
||||
(): DateSchema;
|
||||
new (): DateSchema;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
<T extends Date | null | undefined = Date | undefined, C = object>(): DateSchema<T, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <T extends Date | null | undefined = Date | undefined, C = object>(): DateSchema<T, C>;
|
||||
}
|
||||
|
||||
export interface DateSchema<T extends Date | null | undefined = Date | undefined> extends Schema<T> {
|
||||
min(limit: Date | string | Ref, message?: DateLocale['min']): DateSchema<T>;
|
||||
max(limit: Date | string | Ref, message?: DateLocale['max']): DateSchema<T>;
|
||||
nullable(isNullable?: true): DateSchema<T | null>;
|
||||
nullable(isNullable: false): DateSchema<Exclude<T, null>>;
|
||||
nullable(isNullable?: boolean): DateSchema<T>;
|
||||
required(message?: TestOptionsMessage): DateSchema<Exclude<T, undefined | null>>;
|
||||
defined(): DateSchema<Exclude<T, undefined>>;
|
||||
notRequired(): DateSchema<T | undefined>;
|
||||
export interface DateSchema<T extends Date | null | undefined = Date | undefined, C = object> extends Schema<T, C> {
|
||||
min(limit: Date | string | Ref, message?: DateLocale['min']): DateSchema<T, C>;
|
||||
max(limit: Date | string | Ref, message?: DateLocale['max']): DateSchema<T, C>;
|
||||
nullable(isNullable?: true): DateSchema<T | null, C>;
|
||||
nullable(isNullable: false): DateSchema<Exclude<T, null>, C>;
|
||||
nullable(isNullable?: boolean): DateSchema<T, C>;
|
||||
required(message?: TestOptionsMessage): DateSchema<Exclude<T, undefined | null>, C>;
|
||||
defined(): DateSchema<Exclude<T, undefined>, C>;
|
||||
notRequired(): DateSchema<T | undefined, C>;
|
||||
oneOf<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): DateSchema<MaintainOptionality<T, U>>;
|
||||
): DateSchema<MaintainOptionality<T, U>, C>;
|
||||
equals<U extends T>(
|
||||
arrayOfValues: ReadonlyArray<U | Ref>,
|
||||
message?: MixedLocale['oneOf'],
|
||||
): DateSchema<MaintainOptionality<T, U>>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null>): this;
|
||||
test<U extends T = T>(name: string, message: TestOptionsMessage, test: AssertingTestFunction<U>): DateSchema<U>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>>): DateSchema<U>;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
optional(): DateSchema<T | undefined>;
|
||||
): DateSchema<MaintainOptionality<T, U>, C>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
|
||||
test<U extends T = T>(
|
||||
name: string,
|
||||
message: TestOptionsMessage,
|
||||
test: AssertingTestFunction<U, C>,
|
||||
): DateSchema<U, C>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): DateSchema<U, C>;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
optional(): DateSchema<T | undefined, C>;
|
||||
}
|
||||
|
||||
export interface ArraySchemaConstructor {
|
||||
<T>(schema?: Schema<T>): NotRequiredArraySchema<T>;
|
||||
new (): NotRequiredArraySchema<{}>;
|
||||
<T, C = object>(schema?: Schema<T, C>): NotRequiredArraySchema<T, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <C = object>(): NotRequiredArraySchema<{}, C>;
|
||||
}
|
||||
|
||||
export interface BasicArraySchema<E, T extends E[] | null | undefined> extends Schema<T> {
|
||||
export interface BasicArraySchema<E, T extends E[] | null | undefined, C = object> extends Schema<T, C> {
|
||||
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<E, T extends E[] | null | undefined> extends S
|
||||
// applies to arrays anyway.
|
||||
oneOf(arrayOfValues: ReadonlyArray<T | Ref | null>, message?: MixedLocale['oneOf']): this;
|
||||
equals(arrayOfValues: ReadonlyArray<T | Ref | null>, message?: MixedLocale['oneOf']): this;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null>): this;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
innerType: Schema<E>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
innerType: Schema<E, C>;
|
||||
}
|
||||
|
||||
export interface NotRequiredNullableArraySchema<T> extends BasicArraySchema<T, T[] | null | undefined> {
|
||||
of<U>(type: Schema<U>): NotRequiredNullableArraySchema<U>;
|
||||
nullable(isNullable?: true): NotRequiredNullableArraySchema<T>;
|
||||
nullable(isNullable: false): NotRequiredArraySchema<T>;
|
||||
nullable(isNullable?: boolean): ArraySchema<T>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T>;
|
||||
defined(): NullableArraySchema<T>;
|
||||
notRequired(): NotRequiredNullableArraySchema<T>;
|
||||
optional(): NotRequiredNullableArraySchema<T>;
|
||||
export interface NotRequiredNullableArraySchema<T, C = object> extends BasicArraySchema<T, T[] | null | undefined, C> {
|
||||
of<U>(type: Schema<U, C>): NotRequiredNullableArraySchema<U, C>;
|
||||
nullable(isNullable?: true): NotRequiredNullableArraySchema<T, C>;
|
||||
nullable(isNullable: false): NotRequiredArraySchema<T, C>;
|
||||
nullable(isNullable?: boolean): ArraySchema<T, C>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T, C>;
|
||||
defined(): NullableArraySchema<T, C>;
|
||||
notRequired(): NotRequiredNullableArraySchema<T, C>;
|
||||
optional(): NotRequiredNullableArraySchema<T, C>;
|
||||
}
|
||||
|
||||
export interface NullableArraySchema<T> extends BasicArraySchema<T, T[] | null> {
|
||||
of<U>(type: Schema<U>): NullableArraySchema<U>;
|
||||
nullable(isNullable?: true): NullableArraySchema<T>;
|
||||
nullable(isNullable: false): ArraySchema<T>;
|
||||
nullable(isNullable?: boolean): ArraySchema<T>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T>;
|
||||
defined(): NullableArraySchema<T>;
|
||||
notRequired(): NotRequiredNullableArraySchema<T>;
|
||||
optional(): NotRequiredNullableArraySchema<T>;
|
||||
export interface NullableArraySchema<T, C = object> extends BasicArraySchema<T, T[] | null, C> {
|
||||
of<U>(type: Schema<U, C>): NullableArraySchema<U, C>;
|
||||
nullable(isNullable?: true): NullableArraySchema<T, C>;
|
||||
nullable(isNullable: false): ArraySchema<T, C>;
|
||||
nullable(isNullable?: boolean): ArraySchema<T, C>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T, C>;
|
||||
defined(): NullableArraySchema<T, C>;
|
||||
notRequired(): NotRequiredNullableArraySchema<T, C>;
|
||||
optional(): NotRequiredNullableArraySchema<T, C>;
|
||||
}
|
||||
|
||||
export interface NotRequiredArraySchema<T> extends BasicArraySchema<T, T[] | undefined> {
|
||||
of<U>(type: Schema<U>): NotRequiredArraySchema<U>;
|
||||
nullable(isNullable?: true): NotRequiredNullableArraySchema<T>;
|
||||
nullable(isNullable: false): NotRequiredArraySchema<T>;
|
||||
nullable(isNullable: boolean): ArraySchema<T>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T>;
|
||||
defined(): ArraySchema<T>;
|
||||
notRequired(): NotRequiredArraySchema<T>;
|
||||
optional(): NotRequiredArraySchema<T>;
|
||||
export interface NotRequiredArraySchema<T, C = object> extends BasicArraySchema<T, T[] | undefined, C> {
|
||||
of<U>(type: Schema<U, C>): NotRequiredArraySchema<U, C>;
|
||||
nullable(isNullable?: true): NotRequiredNullableArraySchema<T, C>;
|
||||
nullable(isNullable: false): NotRequiredArraySchema<T, C>;
|
||||
nullable(isNullable: boolean): ArraySchema<T, C>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T, C>;
|
||||
defined(): ArraySchema<T, C>;
|
||||
notRequired(): NotRequiredArraySchema<T, C>;
|
||||
optional(): NotRequiredArraySchema<T, C>;
|
||||
}
|
||||
|
||||
export interface ArraySchema<T> extends BasicArraySchema<T, T[]> {
|
||||
of<U>(type: Schema<U>): ArraySchema<U>;
|
||||
nullable(isNullable?: true): NullableArraySchema<T>;
|
||||
nullable(isNullable: false | boolean): ArraySchema<T>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T>;
|
||||
defined(): ArraySchema<T>;
|
||||
notRequired(): NotRequiredArraySchema<T>;
|
||||
optional(): NotRequiredArraySchema<T>;
|
||||
export interface ArraySchema<T, C = object> extends BasicArraySchema<T, T[], C> {
|
||||
of<U>(type: Schema<U, C>): ArraySchema<U, C>;
|
||||
nullable(isNullable?: true): NullableArraySchema<T, C>;
|
||||
nullable(isNullable: false | boolean): ArraySchema<T, C>;
|
||||
required(message?: TestOptionsMessage): ArraySchema<T, C>;
|
||||
defined(): ArraySchema<T, C>;
|
||||
notRequired(): NotRequiredArraySchema<T, C>;
|
||||
optional(): NotRequiredArraySchema<T, C>;
|
||||
}
|
||||
|
||||
export type ObjectSchemaDefinition<T extends object | null | undefined> = {
|
||||
export type ObjectSchemaDefinition<T extends object | null | undefined, C = object> = {
|
||||
// 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<T[field]> | MixedSchema<T[field]> | Ref;
|
||||
[field in keyof T]: Schema<T[field], C> | MixedSchema<T[field], C> | Ref;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -336,47 +368,53 @@ export type Shape<T extends object | null | undefined, U extends object> =
|
||||
| PreserveOptionals<T>;
|
||||
|
||||
export interface ObjectSchemaConstructor {
|
||||
<T extends object>(fields?: ObjectSchemaDefinition<T>): ObjectSchema<T | undefined>;
|
||||
new (): ObjectSchema<{}>;
|
||||
<T extends object, C = object>(fields?: ObjectSchemaDefinition<T, C>): ObjectSchema<T | undefined, C>;
|
||||
// tslint:disable-next-line:no-unnecessary-generics
|
||||
new <C = object>(): ObjectSchema<{}, C>;
|
||||
}
|
||||
|
||||
export interface ObjectSchema<T extends object | null | undefined = object | undefined> extends Schema<T> {
|
||||
export interface ObjectSchema<T extends object | null | undefined = object | undefined, C = object>
|
||||
extends Schema<T, C> {
|
||||
fields: {
|
||||
[k in keyof T]: Schema<T[k]>;
|
||||
[k in keyof T]: Schema<T[k], C>;
|
||||
};
|
||||
shape<U extends object>(
|
||||
fields: ObjectSchemaDefinition<U>,
|
||||
fields: ObjectSchemaDefinition<U, C>,
|
||||
noSortEdges?: Array<[string, string]>,
|
||||
): ObjectSchema<Shape<T, U>>;
|
||||
from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema<T>;
|
||||
noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T>;
|
||||
unknown(allow?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T>;
|
||||
): ObjectSchema<Shape<T, U>, C>;
|
||||
from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema<T, C>;
|
||||
noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T, C>;
|
||||
unknown(allow?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T, C>;
|
||||
transformKeys(callback: (key: any) => any): void;
|
||||
camelCase(): ObjectSchema<T>;
|
||||
snakeCase(): ObjectSchema<T>;
|
||||
constantCase(): ObjectSchema<T>;
|
||||
nullable(isNullable?: true): ObjectSchema<T | null>;
|
||||
nullable(isNullable: false): ObjectSchema<Exclude<T, null>>;
|
||||
nullable(isNullable?: boolean): ObjectSchema<T>;
|
||||
required(message?: TestOptionsMessage): ObjectSchema<Exclude<T, undefined | null>>;
|
||||
defined(): ObjectSchema<Exclude<T, undefined>>;
|
||||
notRequired(): ObjectSchema<T | undefined>;
|
||||
optional(): ObjectSchema<T | undefined>;
|
||||
camelCase(): ObjectSchema<T, C>;
|
||||
snakeCase(): ObjectSchema<T, C>;
|
||||
constantCase(): ObjectSchema<T, C>;
|
||||
nullable(isNullable?: true): ObjectSchema<T | null, C>;
|
||||
nullable(isNullable: false): ObjectSchema<Exclude<T, null>, C>;
|
||||
nullable(isNullable?: boolean): ObjectSchema<T, C>;
|
||||
required(message?: TestOptionsMessage): ObjectSchema<Exclude<T, undefined | null>, C>;
|
||||
defined(): ObjectSchema<Exclude<T, undefined>, C>;
|
||||
notRequired(): ObjectSchema<T | undefined, C>;
|
||||
optional(): ObjectSchema<T | undefined, C>;
|
||||
concat(schema: this): this;
|
||||
concat<U extends object>(schema: ObjectSchema<U>): ObjectSchema<T & U>;
|
||||
oneOf<U extends T>(arrayOfValues: ReadonlyArray<U | Ref>, message?: MixedLocale['oneOf']): ObjectSchema<U>;
|
||||
equals<U extends T>(arrayOfValues: ReadonlyArray<U | Ref>, message?: MixedLocale['oneOf']): ObjectSchema<U>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null>): this;
|
||||
test<U extends T = T>(name: string, message: TestOptionsMessage, test: AssertingTestFunction<U>): ObjectSchema<U>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>>): ObjectSchema<U>;
|
||||
test(options: TestOptions<Record<string, any>>): this;
|
||||
concat<U extends object>(schema: ObjectSchema<U, C>): ObjectSchema<T & U, C>;
|
||||
oneOf<U extends T>(arrayOfValues: ReadonlyArray<U | Ref>, message?: MixedLocale['oneOf']): ObjectSchema<U, C>;
|
||||
equals<U extends T>(arrayOfValues: ReadonlyArray<U | Ref>, message?: MixedLocale['oneOf']): ObjectSchema<U, C>;
|
||||
test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
|
||||
test<U extends T = T>(
|
||||
name: string,
|
||||
message: TestOptionsMessage,
|
||||
test: AssertingTestFunction<U, C>,
|
||||
): ObjectSchema<U, C>;
|
||||
test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): ObjectSchema<U, C>;
|
||||
test(options: TestOptions<Record<string, any>, C>): this;
|
||||
}
|
||||
|
||||
export type TestFunction<T = unknown> = (
|
||||
this: TestContext,
|
||||
export type TestFunction<T = unknown, C = object> = (
|
||||
this: TestContext<C>,
|
||||
value: T,
|
||||
) => boolean | ValidationError | Promise<boolean | ValidationError>;
|
||||
export type AssertingTestFunction<T> = (this: TestContext, value: any) => value is T;
|
||||
export type AssertingTestFunction<T, C> = (this: TestContext<C>, value: any) => value is T;
|
||||
|
||||
export type TransformFunction<T> = (this: T, value: any, originalValue: any) => any;
|
||||
|
||||
@ -399,16 +437,16 @@ export type WhenOptionsBuilderObject =
|
||||
|
||||
export type WhenOptions<T> = WhenOptionsBuilderFunction<T> | WhenOptionsBuilderObject;
|
||||
|
||||
export interface TestContext {
|
||||
export interface TestContext<C = object> {
|
||||
path: string;
|
||||
options: ValidateOptions;
|
||||
options: ValidateOptions<C>;
|
||||
parent: any;
|
||||
schema: Schema<any>;
|
||||
schema: Schema<any, C>;
|
||||
resolve: (value: any) => any;
|
||||
createError: (params?: { path?: string; message?: string; params?: object }) => ValidationError;
|
||||
}
|
||||
|
||||
export interface ValidateOptions {
|
||||
export interface ValidateOptions<C = object> {
|
||||
/**
|
||||
* 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<P extends Record<string, any>> {
|
||||
interface BaseTestOptions<P extends Record<string, any>, C = object> {
|
||||
/**
|
||||
* Unique name identifying the test. Required for exclusive tests.
|
||||
*/
|
||||
@ -447,7 +485,7 @@ interface BaseTestOptions<P extends Record<string, any>> {
|
||||
/**
|
||||
* Test function, determines schema validity
|
||||
*/
|
||||
test: TestFunction;
|
||||
test: TestFunction<unknown, C>;
|
||||
|
||||
/**
|
||||
* The validation error message
|
||||
@ -465,28 +503,30 @@ interface BaseTestOptions<P extends Record<string, any>> {
|
||||
exclusive?: boolean;
|
||||
}
|
||||
|
||||
interface NonExclusiveTestOptions<P extends Record<string, any>> extends BaseTestOptions<P> {
|
||||
interface NonExclusiveTestOptions<P extends Record<string, any>, C> extends BaseTestOptions<P, C> {
|
||||
exclusive?: false;
|
||||
}
|
||||
|
||||
interface ExclusiveTestOptions<P extends Record<string, any>> extends BaseTestOptions<P> {
|
||||
interface ExclusiveTestOptions<P extends Record<string, any>, C> extends BaseTestOptions<P, C> {
|
||||
exclusive: true;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface NonExclusiveAssertingTestOptions<U, P extends Record<string, any>> extends NonExclusiveTestOptions<P> {
|
||||
test: AssertingTestFunction<U>;
|
||||
interface NonExclusiveAssertingTestOptions<U, P extends Record<string, any>, C> extends NonExclusiveTestOptions<P, C> {
|
||||
test: AssertingTestFunction<U, C>;
|
||||
}
|
||||
|
||||
interface ExclusiveAssertingTestOptions<U, P extends Record<string, any>> extends ExclusiveTestOptions<P> {
|
||||
test: AssertingTestFunction<U>;
|
||||
interface ExclusiveAssertingTestOptions<U, P extends Record<string, any>, C> extends ExclusiveTestOptions<P, C> {
|
||||
test: AssertingTestFunction<U, C>;
|
||||
}
|
||||
|
||||
export type TestOptions<P extends Record<string, any> = {}> = NonExclusiveTestOptions<P> | ExclusiveTestOptions<P>;
|
||||
export type TestOptions<P extends Record<string, any> = {}, C = object> =
|
||||
| NonExclusiveTestOptions<P, C>
|
||||
| ExclusiveTestOptions<P, C>;
|
||||
|
||||
export type AssertingTestOptions<U, P extends Record<string, any> = {}> =
|
||||
| NonExclusiveAssertingTestOptions<U, P>
|
||||
| ExclusiveAssertingTestOptions<U, P>;
|
||||
export type AssertingTestOptions<U, P extends Record<string, any> = {}, C = object> =
|
||||
| NonExclusiveAssertingTestOptions<U, P, C>
|
||||
| ExclusiveAssertingTestOptions<U, P, C>;
|
||||
|
||||
export interface SchemaFieldRefDescription {
|
||||
type: 'ref';
|
||||
|
||||
@ -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<Set<any>>
|
||||
// $ExpectType MixedSchema<Set<any>, object>
|
||||
mixed.test(
|
||||
'is-function',
|
||||
// tslint:disable-next-line:no-invalid-template-strings
|
||||
"${path} is not a function",
|
||||
(value): value is Set<any> => value instanceof Set,
|
||||
);
|
||||
// $ExpectType MixedSchema<Set<any>>
|
||||
// $ExpectType MixedSchema<Set<any>, object>
|
||||
mixed.test({
|
||||
name: 'is-function',
|
||||
exclusive: true,
|
||||
@ -218,11 +218,11 @@ mixed.test('with-promise', 'It contains invalid value', value => new Promise<boo
|
||||
const testContext = function(this: TestContext) {
|
||||
// $ExpectType string
|
||||
this.path;
|
||||
// $ExpectType ValidateOptions
|
||||
// $ExpectType ValidateOptions<object>
|
||||
this.options;
|
||||
// $ExpectType any
|
||||
this.parent;
|
||||
// $ExpectType Schema<any>
|
||||
// $ExpectType Schema<any, object>
|
||||
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<string>().defined().concat(yup.date().defined()); // $ExpectType MixedSchema<string | Date>
|
||||
yup.object({ name: yup.string().defined() }).defined().concat(yup.object({ when: yup.date().defined() }).defined()); // $ExpectType ObjectSchema<{ name: string; } & { when: Date; }, object>
|
||||
yup.mixed<string>().defined().concat(yup.date().defined()); // $ExpectType MixedSchema<string | Date, object>
|
||||
|
||||
// Async ValidationError
|
||||
const asyncValidationErrorTest = (includeParams: boolean) =>
|
||||
@ -370,16 +370,16 @@ function strSchemaTests(strSchema: yup.StringSchema) {
|
||||
strSchema.defined();
|
||||
}
|
||||
|
||||
const strSchema = yup.string(); // $ExpectType StringSchema<string | undefined>
|
||||
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<string | undefined, object>
|
||||
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<number | undefined>
|
||||
const numSchema = yup.number(); // $ExpectType NumberSchema<number | undefined, object>
|
||||
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<boolean | undefined>
|
||||
const boolSchema = yup.boolean(); // $ExpectType BooleanSchema<boolean | undefined, object>
|
||||
boolSchema.type;
|
||||
boolSchema.isValid(true); // => true
|
||||
boolSchema.oneOf([true] as const); // $ExpectType BooleanSchema<true | undefined>
|
||||
boolSchema.equals([true] as const); // $ExpectType BooleanSchema<true | undefined>
|
||||
boolSchema.required().oneOf([true] as const); // $ExpectType BooleanSchema<true>
|
||||
boolSchema.oneOf([true] as const); // $ExpectType BooleanSchema<true | undefined, object>
|
||||
boolSchema.equals([true] as const); // $ExpectType BooleanSchema<true | undefined, object>
|
||||
boolSchema.required().oneOf([true] as const); // $ExpectType BooleanSchema<true, object>
|
||||
boolSchema.defined();
|
||||
|
||||
// Date Schema
|
||||
const dateSchema = yup.date(); // $ExpectType DateSchema<Date | undefined>
|
||||
const dateSchema = yup.date(); // $ExpectType DateSchema<Date | undefined, object>
|
||||
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<Date | undefined>
|
||||
dateSchema.equals([new Date()] as const); // $ExpectType DateSchema<Date | undefined>
|
||||
dateSchema.required().oneOf([new Date()] as const); // $ExpectType DateSchema<Date>
|
||||
dateSchema.oneOf([new Date()] as const); // $ExpectType DateSchema<Date | undefined, object>
|
||||
dateSchema.equals([new Date()] as const); // $ExpectType DateSchema<Date | undefined, object>
|
||||
dateSchema.required().oneOf([new Date()] as const); // $ExpectType DateSchema<Date, object>
|
||||
|
||||
// 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<number>
|
||||
arrSchema.equals([]); // $ExpectType NotRequiredArraySchema<number>
|
||||
arrSchema.oneOf([]); // $ExpectType NotRequiredArraySchema<number, object>
|
||||
arrSchema.equals([]); // $ExpectType NotRequiredArraySchema<number, object>
|
||||
arrSchema.defined();
|
||||
|
||||
const arrOfObjSchema = yup.array().defined().of(
|
||||
@ -486,8 +486,8 @@ arrOfObjSchema.compact((value, index, array) => {
|
||||
const arr = yup.array().defined();
|
||||
const top = (<T>(x?: T): T => x!)();
|
||||
const validArr: yup.ArraySchema<typeof top> = arr;
|
||||
yup.array(yup.string().defined()); // $ExpectType NotRequiredArraySchema<string>
|
||||
yup.array().of(yup.string().defined()); // $ExpectType NotRequiredArraySchema<string>
|
||||
yup.array(yup.string().defined()); // $ExpectType NotRequiredArraySchema<string, object>
|
||||
yup.array().of(yup.string().defined()); // $ExpectType NotRequiredArraySchema<string, object>
|
||||
|
||||
// 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<LiteralExampleObject>
|
||||
objSchema.oneOf([{name: "John Doe", age: 35, email: "john@example.com", website: "example.com"}] as LiteralExampleObject[]); // $ExpectType ObjectSchema<LiteralExampleObject, object>
|
||||
objSchema.defined();
|
||||
|
||||
const description: SchemaDescription = {
|
||||
@ -694,16 +694,16 @@ interface SubInterface {
|
||||
testField: string;
|
||||
}
|
||||
|
||||
// $ExpectType ObjectSchema<MyInterface>
|
||||
// $ExpectType ObjectSchema<MyInterface, object>
|
||||
const typedSchema = yup.object<MyInterface>({
|
||||
stringField: yup.string().required(), // $ExpectType StringSchema<string>
|
||||
numberField: yup.number().required(), // $ExpectType NumberSchema<number>
|
||||
stringField: yup.string().required(), // $ExpectType StringSchema<string, object>
|
||||
numberField: yup.number().required(), // $ExpectType NumberSchema<number, object>
|
||||
subFields: yup
|
||||
.object({
|
||||
testField: yup.string().required(),
|
||||
})
|
||||
.required(),
|
||||
arrayField: yup.array(yup.string().defined()).required(), // $ExpectType ArraySchema<string>
|
||||
arrayField: yup.array(yup.string().defined()).required(), // $ExpectType ArraySchema<string, object>
|
||||
}).defined();
|
||||
|
||||
const testObject: MyInterface = {
|
||||
@ -749,7 +749,7 @@ const definitionBC: yup.ObjectSchemaDefinition<BC> = {
|
||||
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<MyInterface>({
|
||||
@ -797,7 +797,7 @@ enum Gender {
|
||||
}
|
||||
|
||||
const personSchema = yup.object({
|
||||
firstName: yup.string().defined(), // $ExpectType StringSchema<string>
|
||||
firstName: yup.string().defined(), // $ExpectType StringSchema<string, object>
|
||||
gender: yup.mixed<Gender>().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<string | null>
|
||||
canBeNull: yup.string().defined().nullable(true), // $ExpectType StringSchema<string | null, object>
|
||||
isAlive: yup
|
||||
.boolean()
|
||||
.nullable()
|
||||
@ -961,8 +961,8 @@ function wrapper<T>(b: boolean, msx: MixedSchema<T>): MixedSchema<T> {
|
||||
return msx.nullable(b);
|
||||
}
|
||||
|
||||
const resultingSchema1 = wrapper<string | number | undefined>(false, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema<string | number | undefined>
|
||||
const resultingSchema2 = wrapper<string | number | undefined>(true, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema<string | number | null | undefined>
|
||||
const resultingSchema1 = wrapper<string | number | undefined>(false, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema<string | number | undefined, object>
|
||||
const resultingSchema2 = wrapper<string | number | undefined>(true, yup.mixed().oneOf(['1', 2])); // $ExpectType MixedSchema<string | number | null | undefined, object>
|
||||
|
||||
const arrayOfStringsSchema = yup.array().of(yup.string().defined()).defined();
|
||||
type ArrayOfStrings = yup.InferType<typeof arrayOfStringsSchema>;
|
||||
@ -1011,8 +1011,8 @@ declare module './index' {
|
||||
chineseMobilePhoneNumber?: TestOptionsMessage;
|
||||
}
|
||||
|
||||
interface StringSchema<T extends string | null | undefined = string | undefined> extends Schema<T> {
|
||||
chineseMobilePhoneNumber(message?: StringLocale['chineseMobilePhoneNumber']): StringSchema<T>;
|
||||
interface StringSchema<T extends string | null | undefined = string | undefined, C = object> extends Schema<T, C> {
|
||||
chineseMobilePhoneNumber(message?: StringLocale['chineseMobilePhoneNumber']): StringSchema<T, C>;
|
||||
}
|
||||
}
|
||||
yup.setLocale({
|
||||
@ -1090,3 +1090,31 @@ yup.object({
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
interface MyContext {
|
||||
a: string;
|
||||
}
|
||||
// $ExpectType MixedSchema<any, MyContext>
|
||||
const schemaWithMyContext = yup.mixed<any, MyContext>().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<any, { abc: number }>();
|
||||
|
||||
const arraySchemaWithMyContext = yup.array<any, MyContext>();
|
||||
arraySchemaWithMyContext.of(schemaWithMyContext);
|
||||
// $ExpectError
|
||||
arraySchemaWithMyContext.of(schemaWithDifferentContext);
|
||||
|
||||
const objectSchemaWithMyContext = yup.object<any, MyContext>();
|
||||
objectSchemaWithMyContext.shape({
|
||||
prop1: schemaWithMyContext,
|
||||
// $ExpectError
|
||||
prop2: schemaWithDifferentContext,
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user