// Type definitions for yup 0.29 // Project: https://github.com/jquense/yup // Definitions by: Dominik Hardtke , // Vladyslav Tserman , // Moreton Bay Regional Council , // Sindre Seppola // Yash Kulshrestha // Vincent Pizzo // Robert Bullen // Yusuke Sato // Desmond Koh // Maurice de Beijer // Kalley Powell // Elías García // Ian Sanders // Jay Fong // Lukas Elmer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.6 export function reach(schema: Schema, path: string, value?: any, context?: any): Schema; export function addMethod>( schemaCtor: AnySchemaConstructor, name: string, method: (this: T, ...args: any[]) => T, ): void; export function ref(path: string, options?: { contextPrefix: string }): Ref; export function lazy(fn: (value: T) => Schema): Lazy; export function setLocale(customLocale: LocaleObject): void; export function isSchema(obj: any): obj is Schema; export const mixed: MixedSchemaConstructor; export const string: StringSchemaConstructor; export const number: NumberSchemaConstructor; export const boolean: BooleanSchemaConstructor; export const bool: BooleanSchemaConstructor; export const date: DateSchemaConstructor; export const array: ArraySchemaConstructor; export const object: ObjectSchemaConstructor; export type AnySchemaConstructor = | MixedSchemaConstructor | StringSchemaConstructor | NumberSchemaConstructor | BooleanSchemaConstructor | DateSchemaConstructor | ArraySchemaConstructor | ObjectSchemaConstructor; export type TestOptionsMessage = {}, R = any> = | string | ((params: Extra & Partial) => R); export interface Schema { type: string; clone(): this; label(label: string): this; meta(metadata: any): this; 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; isValid(value: any, options?: any): Promise; isValidSync(value: any, options?: any): value is T; cast(value?: any, options?: any): T; isType(value: any): value is T; strict(isStrict: boolean): this; strip(strip: boolean): this; withMutation(fn: (current: this) => void): void; default(value: T | (() => T)): Schema; default(): T; typeError(message?: TestOptionsMessage): this; notOneOf(arrayOfValues: any[], message?: MixedLocale['notOneOf']): this; when(keys: string | any[], builder: WhenOptions): this; transform(fn: TransformFunction): this; } // Note: Using `{} | null | undefined` allows excluding `null` & `undefined` // whereas using `any` as the default type would mean that `nullable(false)`, // `defined`, and `required` would all have no effect. export interface MixedSchemaConstructor { // tslint:disable-next-line:no-unnecessary-generics (): MixedSchema; // tslint:disable-next-line:no-unnecessary-generics new (options?: { type?: string | undefined; [key: string]: any }): 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; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): MixedSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): 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; default(value: U | (() => U)): U extends undefined ? MixedSchema : MixedSchema, C>; default(): T; } export interface StringSchemaConstructor { // 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; matches( regex: RegExp, messageOrOptions?: | StringLocale['matches'] | { message?: StringLocale['matches'] | undefined; excludeEmptyString?: boolean | undefined }, ): 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, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): 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, C>): StringSchema; test(options: TestOptions, C>): this; optional(): StringSchema; default(value: U | (() => U)): U extends undefined ? StringSchema : StringSchema, C>; default(): T; } export interface NumberSchemaConstructor { // 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, C>; nullable(isNullable?: boolean): NumberSchema; required(message?: TestOptionsMessage): NumberSchema, C>; defined(): NumberSchema, C>; notRequired(): NumberSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): NumberSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): 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; default(value: U | (() => U)): U extends undefined ? NumberSchema : NumberSchema, C>; default(): T; } export interface BooleanSchemaConstructor { // 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, C>; nullable(isNullable?: boolean): BooleanSchema; required(message?: TestOptionsMessage): BooleanSchema, C>; defined(): BooleanSchema, C>; notRequired(): BooleanSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): BooleanSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): 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; default(value: U | (() => U)): U extends undefined ? BooleanSchema : BooleanSchema, C>; default(): T; } export interface DateSchemaConstructor { // 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, C>; nullable(isNullable?: boolean): DateSchema; required(message?: TestOptionsMessage): DateSchema, C>; defined(): DateSchema, C>; notRequired(): DateSchema; oneOf( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): DateSchema, C>; equals( arrayOfValues: ReadonlyArray, message?: MixedLocale['oneOf'], ): 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; default(value: U | (() => U)): U extends undefined ? DateSchema : DateSchema, C>; default(): T; } export interface ArraySchemaConstructor { (schema?: Schema): NotRequiredArraySchema; // tslint:disable-next-line:no-unnecessary-generics new (): NotRequiredArraySchema<{}, C>; } export interface BasicArraySchema extends Schema { min(limit: number | Ref, message?: ArrayLocale['min']): this; max(limit: number | Ref, message?: ArrayLocale['max']): this; ensure(): this; compact( rejector?: (value: InferredArrayType, index: number, array: Array>) => boolean, ): this; // This doesn't narrow the type of the schema like the more primitive oneOf calls // it would be very complex to do that accurately with the subtypes, and it's not // really worth it because the oneOf check is a shallow (==) comparison so it rarely // 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, 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 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 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 = { // 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; }; /** * Merges two interfaces. For properties in common, property types from `U` trump those of `T`. * This is conducive to the functionality of * [yup's `object.shape()` method](https://www.npmjs.com/package/yup#objectshapefields-object-nosortedges-arraystring-string-schema). */ export type Shape = | ({ [P in keyof T]: P extends keyof U ? U[P] : T[P] } & U) | PreserveOptionals; export interface ObjectSchemaConstructor { (fields?: ObjectSchemaDefinition): ObjectSchema; // tslint:disable-next-line:no-unnecessary-generics new (): ObjectSchema<{}, C>; } export interface ObjectSchema extends Schema { fields: { [k in keyof T]: Schema; }; shape( fields: ObjectSchemaDefinition, noSortEdges?: Array<[string, string]>, ): 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, 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, C>): ObjectSchema; test(options: TestOptions, C>): this; default(value: U | (() => U)): U extends undefined ? ObjectSchema : ObjectSchema, C>; default(): T; } export type TestFunction = ( this: TestContext, value: T, context: TestContext ) => boolean | ValidationError | Promise; export type AssertingTestFunction = (this: TestContext, value: any, context: TestContext) => value is T; export type TransformFunction = (this: T, value: any, originalValue: any) => any; export interface WhenOptionsBuilderFunction { (value: any, schema: T): T; (v1: any, v2: any, schema: T): T; (v1: any, v2: any, v3: any, schema: T): T; (v1: any, v2: any, v3: any, v4: any, schema: T): T; } export type WhenOptionsBuilderObjectIs = ((...values: any[]) => boolean) | boolean | number | null | object | string; export type WhenOptionsBuilderObject = | { is: WhenOptionsBuilderObjectIs; then: any; otherwise: any; } | object; export type WhenOptions = WhenOptionsBuilderFunction | WhenOptionsBuilderObject; export interface TestContext { path: string; options: ValidateOptions; parent: any; schema: Schema; originalValue: any; resolve: (value: any) => any; createError: (params?: { path?: string | undefined; message?: string | undefined; params?: object | undefined }) => ValidationError; } export interface ValidateOptions { /** * Only validate the input, and skip and coercion or transformation. Default - false */ strict?: boolean | undefined; /** * Return from validation methods on the first error rather than after all validations run. Default - true */ abortEarly?: boolean | undefined; /** * Remove unspecified keys from objects. Default - false */ stripUnknown?: boolean | undefined; /** * When false validations will not descend into nested schema (relevant for objects or arrays). Default - true */ recursive?: boolean | undefined; /** * Any context needed for validating schema conditions (see: when()) */ context?: C | undefined; } export interface TestMessageParams { path: string; value: any; originalValue: any; label: string; } interface BaseTestOptions

, C = object> { /** * Unique name identifying the test. Required for exclusive tests. */ name?: string | undefined; /** * Test function, determines schema validity */ test: TestFunction; /** * The validation error message */ message?: TestOptionsMessage

| undefined; /** * Values passed to message for interpolation */ params?: P | undefined; /** * Mark the test as exclusive, meaning only one of the same can be active at once */ exclusive?: boolean | undefined; } interface NonExclusiveTestOptions

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

, C> extends BaseTestOptions { exclusive: true; name: string; } interface NonExclusiveAssertingTestOptions, C> extends NonExclusiveTestOptions { test: AssertingTestFunction; } interface ExclusiveAssertingTestOptions, C> extends ExclusiveTestOptions { test: AssertingTestFunction; } export type TestOptions

= {}, C = object> = | NonExclusiveTestOptions | ExclusiveTestOptions; export type AssertingTestOptions = {}, C = object> = | NonExclusiveAssertingTestOptions | ExclusiveAssertingTestOptions; export interface SchemaFieldRefDescription { type: 'ref'; key: string; } export interface SchemaFieldInnerTypeDescription extends Pick> { innerType?: SchemaFieldDescription | undefined; } export type SchemaFieldDescription = SchemaDescription | SchemaFieldRefDescription | SchemaFieldInnerTypeDescription; export interface SchemaDescription { type: string; label: string; meta: object; tests: Array<{ name: string; params: { [k: string]: any } }>; fields: Record; } // ValidationError works a lot more like a class vs. a constructor // function that returns an interface. It's also got a couple of // static methods and it inherits from the generic Error class in // the [yup codebase][1]. // [1]: (https://github.com/jquense/yup/blob/master/src/ValidationError.js) export class ValidationError extends Error { name: string; message: string; value: any; /** * A string, indicating where there error was thrown. path is empty at the root level. */ path: string; type: any; /** * array of error messages */ errors: string[]; /** * In the case of aggregate errors, inner is an array of ValidationErrors throw earlier in the validation chain. */ inner: ValidationError[]; params?: object | undefined; static isError(err: any): err is ValidationError; static formatError(message: string | ((params?: any) => string), params?: any): string | ((params?: any) => string); constructor(errors: string | string[], value: any, path: string, type?: any); } // It is tempting to declare `Ref` very simply, but there are problems with these approaches: // // * `type Ref = Record;` - This is essentially how it was originally declared, but // just about any object satisfies this contract, which makes the type declaration too loose to // be useful. // // * `type Ref = object;` - This is a variation on the previous bullet in that it is too loose. // // * `class Ref {}` - This is yet another variation that is too loose. // // * `type Ref = void;` - This works and the emitted JavaScript is just fine, but it results in some // confusing IntelliSense, e.g it looks like the `ref()` returns `void`, which is not the case. // // The solution is twofold. 1.) Declare it as a class with a private constructor to prevent it from // being instantiated by anything but the `ref()` factory function, and; 2.) declare a private // readonly property (that yup actually places on the prototype) to force it to be structurally // incompatible with any other object type. /** * `Ref` is an opaque type that is internal to yup. Creating a `Ref` instance is accomplished via the `ref()` factory * function. */ export class Ref { private constructor(); private readonly __isYupRef: true; } export interface Lazy extends Schema {} export interface FormatErrorParams { path: string; type: string; value?: any; originalValue?: any; } export type LocaleValue = string | ((params: FormatErrorParams) => string); export interface MixedLocale { default?: TestOptionsMessage | undefined; required?: TestOptionsMessage | undefined; oneOf?: TestOptionsMessage<{ values: any }> | undefined; notOneOf?: TestOptionsMessage<{ values: any }> | undefined; notType?: LocaleValue | undefined; defined?: TestOptionsMessage | undefined; } export interface StringLocale { length?: TestOptionsMessage<{ length: number }> | undefined; min?: TestOptionsMessage<{ min: number }> | undefined; max?: TestOptionsMessage<{ max: number }> | undefined; matches?: TestOptionsMessage<{ regex: RegExp }> | undefined; email?: TestOptionsMessage<{ regex: RegExp }> | undefined; url?: TestOptionsMessage<{ regex: RegExp }> | undefined; uuid?: TestOptionsMessage<{ regex: RegExp }> | undefined; trim?: TestOptionsMessage | undefined; lowercase?: TestOptionsMessage | undefined; uppercase?: TestOptionsMessage | undefined; } export interface NumberLocale { min?: TestOptionsMessage<{ min: number }> | undefined; max?: TestOptionsMessage<{ max: number }> | undefined; lessThan?: TestOptionsMessage<{ less: number }> | undefined; moreThan?: TestOptionsMessage<{ more: number }> | undefined; notEqual?: TestOptionsMessage<{ notEqual: number }> | undefined; positive?: TestOptionsMessage<{ more: number }> | undefined; negative?: TestOptionsMessage<{ less: number }> | undefined; integer?: TestOptionsMessage | undefined; } export interface DateLocale { min?: TestOptionsMessage<{ min: Date | string }> | undefined; max?: TestOptionsMessage<{ max: Date | string }> | undefined; } export interface ObjectLocale { noUnknown?: TestOptionsMessage<{ unknown: string }> | undefined; } export interface ArrayLocale { min?: TestOptionsMessage<{ min: number }> | undefined; max?: TestOptionsMessage<{ max: number }> | undefined; } export interface LocaleObject { mixed?: MixedLocale | undefined; string?: StringLocale | undefined; number?: NumberLocale | undefined; date?: DateLocale | undefined; boolean?: {} | undefined; object?: ObjectLocale | undefined; array?: ArrayLocale | undefined; } export type InferType = T extends Schema ? InnerInferType

: never; // Shut off automatic exporting after this statement export {}; type KeyOfUndefined = { [P in keyof T]-?: undefined extends T[P] ? P : never; }[keyof T]; type PreserveNull = T extends null ? null : never; type PreserveUndefined = T extends undefined ? undefined : never; type PreserveOptionals = PreserveNull | PreserveUndefined; type Id = { [K in keyof T]: T[K] extends object ? InnerInferType : T[K]; }; type RequiredProps = Pick>>; type NotRequiredProps = Partial>>; type InnerInferType = | (T extends Array ? InnerInferTypeArray : Id & RequiredProps>) | PreserveOptionals; interface InnerInferTypeArray extends Array> {} type InferredArrayType = T extends Array ? U : T; /** If `T` is optional, returns optional `U`. */ type MaintainOptionality = T extends undefined ? U | undefined : U;