// Type definitions for yup 0.26 // 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 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 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 { 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: any): this; default(): T; typeError(message?: TestOptionsMessage): this; oneOf(arrayOfValues: Array, message?: MixedLocale['oneOf']): this; notOneOf(arrayOfValues: any[], message?: MixedLocale['notOneOf']): this; when(keys: string | any[], builder: WhenOptions): this; test( name: string, message: TestOptionsMessage, test: (this: TestContext, value?: any) => boolean | ValidationError | Promise, callbackStyleAsync?: boolean, ): this; // tslint:disable-next-line:no-unnecessary-generics test

(options: TestOptions

): this; transform(fn: TransformFunction): this; } export interface MixedSchemaConstructor { // tslint:disable-next-line:no-unnecessary-generics (): MixedSchema; // tslint:disable-next-line:no-unnecessary-generics 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>; notRequired(): MixedSchema; concat(schema: this): this; concat(schema: MixedSchema): MixedSchema; } export interface StringSchemaConstructor { (): StringSchema; 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']; excludeEmptyString?: boolean }, ): StringSchema; email(message?: StringLocale['email']): StringSchema; url(message?: StringLocale['url']): 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>; notRequired(): StringSchema; } export interface NumberSchemaConstructor { (): NumberSchema; 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>; notRequired(): NumberSchema; } export interface BooleanSchemaConstructor { (): BooleanSchema; new (): BooleanSchema; } export interface BooleanSchema extends Schema { nullable(isNullable?: true): BooleanSchema; nullable(isNullable: false): BooleanSchema>; nullable(isNullable?: boolean): BooleanSchema; required(message?: TestOptionsMessage): BooleanSchema>; notRequired(): BooleanSchema; } export interface DateSchemaConstructor { (): DateSchema; 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>; notRequired(): DateSchema; } export interface ArraySchemaConstructor { (schema?: Schema): ArraySchema; new (): ArraySchema<{}>; } 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; } export interface NotRequiredNullableArraySchema extends BasicArraySchema { of(type: Schema): NotRequiredNullableArraySchema; nullable(isNullable?: true): NotRequiredNullableArraySchema; nullable(isNullable: false): NotRequiredArraySchema; nullable(isNullable?: boolean): ArraySchema; required(message?: TestOptionsMessage): NullableArraySchema; notRequired(): 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): NullableArraySchema; notRequired(): 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; notRequired(): NotRequiredArraySchema; } export interface ArraySchema extends BasicArraySchema { of(type: Schema): ArraySchema; nullable(isNullable?: true): NullableArraySchema; nullable(isNullable: false | boolean): ArraySchema; required(message?: TestOptionsMessage): ArraySchema; notRequired(): NotRequiredArraySchema; } export type ObjectSchemaDefinition = { [field in keyof T]: Schema | 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; export interface ObjectSchemaConstructor { (fields?: ObjectSchemaDefinition): ObjectSchema; new (): ObjectSchema<{}>; } export interface ObjectSchema extends Schema { shape( fields: ObjectSchemaDefinition, noSortEdges?: Array<[string, string]>, ): ObjectSchema>; from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema; noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema; transformKeys(callback: (key: any) => any): void; camelCase(): ObjectSchema; constantCase(): ObjectSchema; nullable(isNullable?: true): ObjectSchema; nullable(isNullable: false): ObjectSchema>; nullable(isNullable?: boolean): ObjectSchema; required(message?: TestOptionsMessage): ObjectSchema>; notRequired(): ObjectSchema; concat(schema: this): this; concat(schema: ObjectSchema): ObjectSchema; } 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; resolve: (value: any) => any; createError: (params?: { path?: string; message?: string, params?: object }) => ValidationError; } export interface ValidateOptions { /** * Only validate the input, and skip and coercion or transformation. Default - false */ strict?: boolean; /** * Teturn from validation methods on the first error rather than after all validations run. Default - true */ abortEarly?: boolean; /** * Remove unspecified keys from objects. Default - false */ stripUnknown?: boolean; /** * When false validations will not descend into nested schema (relevant for objects or arrays). Default - true */ recursive?: boolean; /** * Any context needed for validating schema conditions (see: when()) */ context?: object; } export interface TestMessageParams { path: string; value: any; originalValue: any; label: string; } export interface TestOptions

= {}, R = any> { /** * Unique name identifying the test */ name?: string; /** * Test function, determines schema validity */ test: (this: TestContext, value: any) => boolean | ValidationError | Promise; /** * The validation error message */ message?: TestOptionsMessage; /** * Values passed to message for interpolation */ params?: P; /** * Mark the test as exclusive, meaning only one of the same can be active at once */ exclusive?: boolean; } export interface SchemaDescription { type: string; label: string; meta: object; tests: Array<{ name: string; params: object }>; fields: object; } // 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; 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; } // tslint:disable-next-line:no-empty-interface export interface Lazy extends Schema {} export interface FormatErrorParams { path: string; type: string; value?: any; originalValue?: any; } export type LocaleValue = string | ((params: FormatErrorParams) => string); interface MixedLocale { default?: TestOptionsMessage; required?: TestOptionsMessage; oneOf?: TestOptionsMessage<{ values: any }>; notOneOf?: TestOptionsMessage<{ values: any }>; notType?: LocaleValue; } interface StringLocale { length?: TestOptionsMessage<{ length: number }>; min?: TestOptionsMessage<{ min: number }>; max?: TestOptionsMessage<{ max: number }>; matches?: TestOptionsMessage<{ regex: RegExp }>; email?: TestOptionsMessage<{ regex: RegExp }>; url?: TestOptionsMessage<{ regex: RegExp }>; trim?: TestOptionsMessage; lowercase?: TestOptionsMessage; uppercase?: TestOptionsMessage; } interface NumberLocale { min?: TestOptionsMessage<{ min: number }>; max?: TestOptionsMessage<{ max: number }>; lessThan?: TestOptionsMessage<{ less: number }>; moreThan?: TestOptionsMessage<{ more: number }>; positive?: TestOptionsMessage<{ more: number }>; negative?: TestOptionsMessage<{ less: number }>; integer?: TestOptionsMessage; } interface DateLocale { min?: TestOptionsMessage<{ min: Date | string }>; max?: TestOptionsMessage<{ max: Date | string }>; } interface ObjectLocale { noUnknown?: TestOptionsMessage; } interface ArrayLocale { min?: TestOptionsMessage<{ min: number }>; max?: TestOptionsMessage<{ max: number }>; } export interface LocaleObject { mixed?: MixedLocale; string?: StringLocale; number?: NumberLocale; date?: DateLocale; boolean?: {}; object?: ObjectLocale; array?: ArrayLocale; } 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 Id = { [K in keyof T]: T[K] }; type RequiredProps = Pick>>; type NotRequiredProps = Partial>>; type InnerInferType = Id & RequiredProps>; type InferredArrayType = T extends Array ? U : T;