UNPKG

30.3 kBTypeScriptView Raw
1// Type definitions for yup 0.29
2// Project: https://github.com/jquense/yup
3// Definitions by: Dominik Hardtke <https://github.com/dhardtke>,
4// Vladyslav Tserman <https://github.com/vtserman>,
5// Moreton Bay Regional Council <https://github.com/MoretonBayRC>,
6// Sindre Seppola <https://github.com/sseppola>
7// Yash Kulshrestha <https://github.com/YashdalfTheGray>
8// Vincent Pizzo <https://github.com/vincentjames501>
9// Robert Bullen <https://github.com/robertbullen>
10// Yusuke Sato <https://github.com/sat0yu>
11// Desmond Koh <https://github.com/deskoh>
12// Maurice de Beijer <https://github.com/mauricedb>
13// Kalley Powell <https://github.com/kalley>
14// Elías García <https://github.com/elias-garcia>
15// Ian Sanders <https://github.com/iansan5653>
16// Jay Fong <https://github.com/fjc0k>
17// Lukas Elmer <https://github.com/lukaselmer>
18// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
19// TypeScript Version: 3.6
20
21export function reach<T, C = object>(schema: Schema<T, C>, path: string, value?: any, context?: any): Schema<T, C>;
22export function addMethod<T extends Schema<any>>(
23 schemaCtor: AnySchemaConstructor,
24 name: string,
25 method: (this: T, ...args: any[]) => T,
26): void;
27export function ref(path: string, options?: { contextPrefix: string }): Ref;
28export function lazy<T>(fn: (value: T) => Schema<T>): Lazy;
29export function setLocale(customLocale: LocaleObject): void;
30export function isSchema(obj: any): obj is Schema<any>;
31
32export const mixed: MixedSchemaConstructor;
33export const string: StringSchemaConstructor;
34export const number: NumberSchemaConstructor;
35export const boolean: BooleanSchemaConstructor;
36export const bool: BooleanSchemaConstructor;
37export const date: DateSchemaConstructor;
38export const array: ArraySchemaConstructor;
39export const object: ObjectSchemaConstructor;
40
41export type AnySchemaConstructor =
42 | MixedSchemaConstructor
43 | StringSchemaConstructor
44 | NumberSchemaConstructor
45 | BooleanSchemaConstructor
46 | DateSchemaConstructor
47 | ArraySchemaConstructor
48 | ObjectSchemaConstructor;
49
50export type TestOptionsMessage<Extra extends Record<string, any> = {}, R = any> =
51 | string
52 | ((params: Extra & Partial<TestMessageParams>) => R);
53
54export interface Schema<T, C = object> {
55 type: string;
56 clone(): this;
57 label(label: string): this;
58 meta(metadata: any): this;
59 meta(): any;
60 describe(): SchemaDescription;
61 concat(schema: this): this;
62 validate(value: any, options?: ValidateOptions<C>): Promise<T>;
63 validateSync(value: any, options?: ValidateOptions<C>): T;
64 validateAt(path: string, value: T, options?: ValidateOptions<C>): Promise<T>;
65 validateSyncAt(path: string, value: T, options?: ValidateOptions<C>): T;
66 isValid(value: any, options?: any): Promise<boolean>;
67 isValidSync(value: any, options?: any): value is T;
68 cast(value?: any, options?: any): T;
69 isType(value: any): value is T;
70 strict(isStrict: boolean): this;
71 strip(strip: boolean): this;
72 withMutation(fn: (current: this) => void): void;
73 default(value: any): this;
74 default(): T;
75 typeError(message?: TestOptionsMessage): this;
76 notOneOf(arrayOfValues: any[], message?: MixedLocale['notOneOf']): this;
77 when(keys: string | any[], builder: WhenOptions<this>): this;
78 transform(fn: TransformFunction<this>): this;
79}
80
81// Note: Using `{} | null | undefined` allows excluding `null` & `undefined`
82// whereas using `any` as the default type would mean that `nullable(false)`,
83// `defined`, and `required` would all have no effect.
84
85export interface MixedSchemaConstructor {
86 // tslint:disable-next-line:no-unnecessary-generics
87 <T = {} | null | undefined, C = object>(): MixedSchema<T, C>;
88 // tslint:disable-next-line:no-unnecessary-generics
89 new <T = {} | null | undefined, C = object>(options?: { type?: string; [key: string]: any }): MixedSchema<T, C>;
90}
91
92export interface MixedSchema<T extends any = {} | null | undefined, C = object> extends Schema<T, C> {
93 nullable(isNullable?: true): MixedSchema<T | null, C>;
94 nullable(isNullable: false): MixedSchema<Exclude<T, null>, C>;
95 nullable(isNullable?: boolean): MixedSchema<T, C>;
96 required(message?: TestOptionsMessage): MixedSchema<Exclude<T, undefined | null>, C>;
97 defined(): MixedSchema<Exclude<T, undefined>, C>;
98 notRequired(): MixedSchema<T | undefined, C>;
99 optional(): MixedSchema<T | undefined, C>;
100 concat(schema: this): this;
101 concat<U>(schema: Schema<U, C>): MixedSchema<T | U, C>;
102 oneOf<U extends T>(
103 arrayOfValues: ReadonlyArray<U | Ref>,
104 message?: MixedLocale['oneOf'],
105 ): MixedSchema<MaintainOptionality<T, U>, C>;
106 equals<U extends T>(
107 arrayOfValues: ReadonlyArray<U | Ref>,
108 message?: MixedLocale['oneOf'],
109 ): MixedSchema<MaintainOptionality<T, U>, C>;
110 test<U extends T = T>(
111 name: string,
112 message: TestOptionsMessage,
113 test: AssertingTestFunction<U, C>,
114 ): MixedSchema<U, C>;
115 test(name: string, message: TestOptionsMessage, test: TestFunction<unknown, C>): this;
116 test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): MixedSchema<U, C>;
117 test(options: TestOptions<Record<string, any>, C>): this;
118}
119
120export interface StringSchemaConstructor {
121 // tslint:disable-next-line:no-unnecessary-generics
122 <T extends string | null | undefined = string | undefined, C = object>(): StringSchema<T, C>;
123 // tslint:disable-next-line:no-unnecessary-generics
124 new <T extends string | null | undefined = string | undefined, C = object>(): StringSchema<T, C>;
125}
126
127export interface StringSchema<T extends string | null | undefined = string | undefined, C = object>
128 extends Schema<T, C> {
129 length(limit: number | Ref, message?: StringLocale['length']): StringSchema<T, C>;
130 min(limit: number | Ref, message?: StringLocale['min']): StringSchema<T, C>;
131 max(limit: number | Ref, message?: StringLocale['max']): StringSchema<T, C>;
132 matches(
133 regex: RegExp,
134 messageOrOptions?:
135 | StringLocale['matches']
136 | { message?: StringLocale['matches']; excludeEmptyString?: boolean },
137 ): StringSchema<T, C>;
138 email(message?: StringLocale['email']): StringSchema<T, C>;
139 url(message?: StringLocale['url']): StringSchema<T, C>;
140 uuid(message?: StringLocale['uuid']): StringSchema<T, C>;
141 ensure(): StringSchema<T, C>;
142 trim(message?: StringLocale['trim']): StringSchema<T, C>;
143 lowercase(message?: StringLocale['lowercase']): StringSchema<T, C>;
144 uppercase(message?: StringLocale['uppercase']): StringSchema<T, C>;
145 nullable(isNullable?: true): StringSchema<T | null, C>;
146 nullable(isNullable: false): StringSchema<Exclude<T, null>, C>;
147 nullable(isNullable?: boolean): StringSchema<T, C>;
148 required(message?: TestOptionsMessage): StringSchema<Exclude<T, undefined | null>, C>;
149 defined(): StringSchema<Exclude<T, undefined>, C>;
150 notRequired(): StringSchema<T | undefined, C>;
151 oneOf<U extends T>(
152 arrayOfValues: ReadonlyArray<U | Ref>,
153 message?: MixedLocale['oneOf'],
154 ): StringSchema<MaintainOptionality<T, U>, C>;
155 equals<U extends T>(
156 arrayOfValues: ReadonlyArray<U | Ref>,
157 message?: MixedLocale['oneOf'],
158 ): StringSchema<MaintainOptionality<T, U>, C>;
159 /*
160 All TestFunction generics are intentionally T with (undefined | null) as previous .required / .defined / .nullable
161 will narrow out those types, and tests run for (undefined | null) even if they're not allowed.
162 */
163 test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
164 test<U extends T = T>(
165 name: string,
166 message: TestOptionsMessage,
167 test: AssertingTestFunction<U, C>,
168 ): StringSchema<U, C>;
169 test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): StringSchema<U, C>;
170 test(options: TestOptions<Record<string, any>, C>): this;
171 optional(): StringSchema<T | undefined, C>;
172}
173
174export interface NumberSchemaConstructor {
175 // tslint:disable-next-line:no-unnecessary-generics
176 <T extends number | null | undefined = number | undefined, C = object>(): NumberSchema<T, C>;
177 // tslint:disable-next-line:no-unnecessary-generics
178 new <T extends number | null | undefined = number | undefined, C = object>(): NumberSchema<T, C>;
179}
180
181export interface NumberSchema<T extends number | null | undefined = number | undefined, C = object>
182 extends Schema<T, C> {
183 min(limit: number | Ref, message?: NumberLocale['min']): NumberSchema<T, C>;
184 max(limit: number | Ref, message?: NumberLocale['max']): NumberSchema<T, C>;
185 lessThan(limit: number | Ref, message?: NumberLocale['lessThan']): NumberSchema<T, C>;
186 moreThan(limit: number | Ref, message?: NumberLocale['moreThan']): NumberSchema<T, C>;
187 positive(message?: NumberLocale['positive']): NumberSchema<T, C>;
188 negative(message?: NumberLocale['negative']): NumberSchema<T, C>;
189 integer(message?: NumberLocale['integer']): NumberSchema<T, C>;
190 truncate(): NumberSchema<T, C>;
191 round(type: 'floor' | 'ceil' | 'trunc' | 'round'): NumberSchema<T, C>;
192 nullable(isNullable?: true): NumberSchema<T | null, C>;
193 nullable(isNullable: false): NumberSchema<Exclude<T, null>, C>;
194 nullable(isNullable?: boolean): NumberSchema<T, C>;
195 required(message?: TestOptionsMessage): NumberSchema<Exclude<T, undefined | null>, C>;
196 defined(): NumberSchema<Exclude<T, undefined>, C>;
197 notRequired(): NumberSchema<T | undefined, C>;
198 oneOf<U extends T>(
199 arrayOfValues: ReadonlyArray<U | Ref>,
200 message?: MixedLocale['oneOf'],
201 ): NumberSchema<MaintainOptionality<T, U>, C>;
202 equals<U extends T>(
203 arrayOfValues: ReadonlyArray<U | Ref>,
204 message?: MixedLocale['oneOf'],
205 ): NumberSchema<MaintainOptionality<T, U>, C>;
206 test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
207 test<U extends T = T>(
208 name: string,
209 message: TestOptionsMessage,
210 test: AssertingTestFunction<U, C>,
211 ): NumberSchema<U, C>;
212 test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): NumberSchema<U, C>;
213 test(options: TestOptions<Record<string, any>, C>): this;
214 optional(): NumberSchema<T | undefined, C>;
215}
216
217export interface BooleanSchemaConstructor {
218 // tslint:disable-next-line:no-unnecessary-generics
219 <T extends boolean | null | undefined = boolean | undefined, C = object>(): BooleanSchema<T, C>;
220 // tslint:disable-next-line:no-unnecessary-generics
221 new <T extends boolean | null | undefined = boolean | undefined, C = object>(): BooleanSchema<T, C>;
222}
223
224export interface BooleanSchema<T extends boolean | null | undefined = boolean | undefined, C = object>
225 extends Schema<T, C> {
226 nullable(isNullable?: true): BooleanSchema<T | null, C>;
227 nullable(isNullable: false): BooleanSchema<Exclude<T, null>, C>;
228 nullable(isNullable?: boolean): BooleanSchema<T, C>;
229 required(message?: TestOptionsMessage): BooleanSchema<Exclude<T, undefined | null>, C>;
230 defined(): BooleanSchema<Exclude<T, undefined>, C>;
231 notRequired(): BooleanSchema<T | undefined, C>;
232 oneOf<U extends T>(
233 arrayOfValues: ReadonlyArray<U | Ref>,
234 message?: MixedLocale['oneOf'],
235 ): BooleanSchema<MaintainOptionality<T, U>, C>;
236 equals<U extends T>(
237 arrayOfValues: ReadonlyArray<U | Ref>,
238 message?: MixedLocale['oneOf'],
239 ): BooleanSchema<MaintainOptionality<T, U>, C>;
240 test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
241 test<U extends T = T>(
242 name: string,
243 message: TestOptionsMessage,
244 test: AssertingTestFunction<U, C>,
245 ): BooleanSchema<U, C>;
246 test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): BooleanSchema<U, C>;
247 test(options: TestOptions<Record<string, any>, C>): this;
248 optional(): BooleanSchema<T | undefined, C>;
249}
250
251export interface DateSchemaConstructor {
252 // tslint:disable-next-line:no-unnecessary-generics
253 <T extends Date | string | null | undefined = Date | undefined, C = object>(): DateSchema<T, C>;
254 // tslint:disable-next-line:no-unnecessary-generics
255 new <T extends Date | string | null | undefined = Date | undefined, C = object>(): DateSchema<T, C>;
256}
257
258export interface DateSchema<T extends Date | string | null | undefined = Date | undefined, C = object> extends Schema<T, C> {
259 min(limit: Date | string | Ref, message?: DateLocale['min']): DateSchema<T, C>;
260 max(limit: Date | string | Ref, message?: DateLocale['max']): DateSchema<T, C>;
261 nullable(isNullable?: true): DateSchema<T | null, C>;
262 nullable(isNullable: false): DateSchema<Exclude<T, null>, C>;
263 nullable(isNullable?: boolean): DateSchema<T, C>;
264 required(message?: TestOptionsMessage): DateSchema<Exclude<T, undefined | null>, C>;
265 defined(): DateSchema<Exclude<T, undefined>, C>;
266 notRequired(): DateSchema<T | undefined, C>;
267 oneOf<U extends T>(
268 arrayOfValues: ReadonlyArray<U | Ref>,
269 message?: MixedLocale['oneOf'],
270 ): DateSchema<MaintainOptionality<T, U>, C>;
271 equals<U extends T>(
272 arrayOfValues: ReadonlyArray<U | Ref>,
273 message?: MixedLocale['oneOf'],
274 ): DateSchema<MaintainOptionality<T, U>, C>;
275 test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
276 test<U extends T = T>(
277 name: string,
278 message: TestOptionsMessage,
279 test: AssertingTestFunction<U, C>,
280 ): DateSchema<U, C>;
281 test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): DateSchema<U, C>;
282 test(options: TestOptions<Record<string, any>, C>): this;
283 optional(): DateSchema<T | undefined, C>;
284}
285
286export interface ArraySchemaConstructor {
287 <T, C = object>(schema?: Schema<T, C>): NotRequiredArraySchema<T, C>;
288 // tslint:disable-next-line:no-unnecessary-generics
289 new <C = object>(): NotRequiredArraySchema<{}, C>;
290}
291
292export interface BasicArraySchema<E, T extends E[] | null | undefined, C = object> extends Schema<T, C> {
293 min(limit: number | Ref, message?: ArrayLocale['min']): this;
294 max(limit: number | Ref, message?: ArrayLocale['max']): this;
295 ensure(): this;
296 compact(
297 rejector?: (value: InferredArrayType<T>, index: number, array: Array<InferredArrayType<T>>) => boolean,
298 ): this;
299
300 // This doesn't narrow the type of the schema like the more primitive oneOf calls
301 // it would be very complex to do that accurately with the subtypes, and it's not
302 // really worth it because the oneOf check is a shallow (==) comparison so it rarely
303 // applies to arrays anyway.
304 oneOf(arrayOfValues: ReadonlyArray<T | Ref | null>, message?: MixedLocale['oneOf']): this;
305 equals(arrayOfValues: ReadonlyArray<T | Ref | null>, message?: MixedLocale['oneOf']): this;
306 test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
307 test(options: TestOptions<Record<string, any>, C>): this;
308 innerType: Schema<E, C>;
309}
310
311export interface NotRequiredNullableArraySchema<T, C = object> extends BasicArraySchema<T, T[] | null | undefined, C> {
312 of<U>(type: Schema<U, C>): NotRequiredNullableArraySchema<U, C>;
313 nullable(isNullable?: true): NotRequiredNullableArraySchema<T, C>;
314 nullable(isNullable: false): NotRequiredArraySchema<T, C>;
315 nullable(isNullable?: boolean): ArraySchema<T, C>;
316 required(message?: TestOptionsMessage): ArraySchema<T, C>;
317 defined(): NullableArraySchema<T, C>;
318 notRequired(): NotRequiredNullableArraySchema<T, C>;
319 optional(): NotRequiredNullableArraySchema<T, C>;
320}
321
322export interface NullableArraySchema<T, C = object> extends BasicArraySchema<T, T[] | null, C> {
323 of<U>(type: Schema<U, C>): NullableArraySchema<U, C>;
324 nullable(isNullable?: true): NullableArraySchema<T, C>;
325 nullable(isNullable: false): ArraySchema<T, C>;
326 nullable(isNullable?: boolean): ArraySchema<T, C>;
327 required(message?: TestOptionsMessage): ArraySchema<T, C>;
328 defined(): NullableArraySchema<T, C>;
329 notRequired(): NotRequiredNullableArraySchema<T, C>;
330 optional(): NotRequiredNullableArraySchema<T, C>;
331}
332
333export interface NotRequiredArraySchema<T, C = object> extends BasicArraySchema<T, T[] | undefined, C> {
334 of<U>(type: Schema<U, C>): NotRequiredArraySchema<U, C>;
335 nullable(isNullable?: true): NotRequiredNullableArraySchema<T, C>;
336 nullable(isNullable: false): NotRequiredArraySchema<T, C>;
337 nullable(isNullable: boolean): ArraySchema<T, C>;
338 required(message?: TestOptionsMessage): ArraySchema<T, C>;
339 defined(): ArraySchema<T, C>;
340 notRequired(): NotRequiredArraySchema<T, C>;
341 optional(): NotRequiredArraySchema<T, C>;
342}
343
344export interface ArraySchema<T, C = object> extends BasicArraySchema<T, T[], C> {
345 of<U>(type: Schema<U, C>): ArraySchema<U, C>;
346 nullable(isNullable?: true): NullableArraySchema<T, C>;
347 nullable(isNullable: false | boolean): ArraySchema<T, C>;
348 required(message?: TestOptionsMessage): ArraySchema<T, C>;
349 defined(): ArraySchema<T, C>;
350 notRequired(): NotRequiredArraySchema<T, C>;
351 optional(): NotRequiredArraySchema<T, C>;
352}
353
354export type ObjectSchemaDefinition<T extends object | null | undefined, C = object> = {
355 // This shouldn't be necessary because MixedSchema extends Schema, but type
356 // inference only works with it this way - otherwise when you use a mixed
357 // field in object schema, it will type as `unknown`. Not sure why that is -
358 // maybe some sort of inference depth limit?
359 [field in keyof T]: Schema<T[field], C> | MixedSchema<T[field], C> | Ref;
360};
361
362/**
363 * Merges two interfaces. For properties in common, property types from `U` trump those of `T`.
364 * This is conducive to the functionality of
365 * [yup's `object.shape()` method](https://www.npmjs.com/package/yup#objectshapefields-object-nosortedges-arraystring-string-schema).
366 */
367export type Shape<T extends object | null | undefined, U extends object> =
368 | ({ [P in keyof T]: P extends keyof U ? U[P] : T[P] } & U)
369 | PreserveOptionals<T>;
370
371export interface ObjectSchemaConstructor {
372 <T extends object, C = object>(fields?: ObjectSchemaDefinition<T, C>): ObjectSchema<T | undefined, C>;
373 // tslint:disable-next-line:no-unnecessary-generics
374 new <C = object>(): ObjectSchema<{}, C>;
375}
376
377export interface ObjectSchema<T extends object | null | undefined = object | undefined, C = object>
378 extends Schema<T, C> {
379 fields: {
380 [k in keyof T]: Schema<T[k], C>;
381 };
382 shape<U extends object>(
383 fields: ObjectSchemaDefinition<U, C>,
384 noSortEdges?: Array<[string, string]>,
385 ): ObjectSchema<Shape<T, U>, C>;
386 from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema<T, C>;
387 noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T, C>;
388 unknown(allow?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T, C>;
389 transformKeys(callback: (key: any) => any): void;
390 camelCase(): ObjectSchema<T, C>;
391 snakeCase(): ObjectSchema<T, C>;
392 constantCase(): ObjectSchema<T, C>;
393 nullable(isNullable?: true): ObjectSchema<T | null, C>;
394 nullable(isNullable: false): ObjectSchema<Exclude<T, null>, C>;
395 nullable(isNullable?: boolean): ObjectSchema<T, C>;
396 required(message?: TestOptionsMessage): ObjectSchema<Exclude<T, undefined | null>, C>;
397 defined(): ObjectSchema<Exclude<T, undefined>, C>;
398 notRequired(): ObjectSchema<T | undefined, C>;
399 optional(): ObjectSchema<T | undefined, C>;
400 concat(schema: this): this;
401 concat<U extends object>(schema: ObjectSchema<U, C>): ObjectSchema<T & U, C>;
402 oneOf<U extends T>(arrayOfValues: ReadonlyArray<U | Ref>, message?: MixedLocale['oneOf']): ObjectSchema<U, C>;
403 equals<U extends T>(arrayOfValues: ReadonlyArray<U | Ref>, message?: MixedLocale['oneOf']): ObjectSchema<U, C>;
404 test(name: string, message: TestOptionsMessage, test: TestFunction<T | undefined | null, C>): this;
405 test<U extends T = T>(
406 name: string,
407 message: TestOptionsMessage,
408 test: AssertingTestFunction<U, C>,
409 ): ObjectSchema<U, C>;
410 test<U extends T = T>(options: AssertingTestOptions<U, Record<string, any>, C>): ObjectSchema<U, C>;
411 test(options: TestOptions<Record<string, any>, C>): this;
412}
413
414export type TestFunction<T = unknown, C = object> = (
415 this: TestContext<C>,
416 value: T,
417 context: TestContext<C>
418) => boolean | ValidationError | Promise<boolean | ValidationError>;
419export type AssertingTestFunction<T, C> = (this: TestContext<C>, value: any, context: TestContext<C>) => value is T;
420
421export type TransformFunction<T> = (this: T, value: any, originalValue: any) => any;
422
423export interface WhenOptionsBuilderFunction<T> {
424 (value: any, schema: T): T;
425 (v1: any, v2: any, schema: T): T;
426 (v1: any, v2: any, v3: any, schema: T): T;
427 (v1: any, v2: any, v3: any, v4: any, schema: T): T;
428}
429
430export type WhenOptionsBuilderObjectIs = ((...values: any[]) => boolean) | boolean | number | null | object | string;
431
432export type WhenOptionsBuilderObject =
433 | {
434 is: WhenOptionsBuilderObjectIs;
435 then: any;
436 otherwise: any;
437 }
438 | object;
439
440export type WhenOptions<T> = WhenOptionsBuilderFunction<T> | WhenOptionsBuilderObject;
441
442export interface TestContext<C = object> {
443 path: string;
444 options: ValidateOptions<C>;
445 parent: any;
446 schema: Schema<any, C>;
447 originalValue: any;
448 resolve: (value: any) => any;
449 createError: (params?: { path?: string; message?: string; params?: object }) => ValidationError;
450}
451
452export interface ValidateOptions<C = object> {
453 /**
454 * Only validate the input, and skip and coercion or transformation. Default - false
455 */
456 strict?: boolean;
457 /**
458 * Return from validation methods on the first error rather than after all validations run. Default - true
459 */
460 abortEarly?: boolean;
461 /**
462 * Remove unspecified keys from objects. Default - false
463 */
464 stripUnknown?: boolean;
465 /**
466 * When false validations will not descend into nested schema (relevant for objects or arrays). Default - true
467 */
468 recursive?: boolean;
469 /**
470 * Any context needed for validating schema conditions (see: when())
471 */
472 context?: C;
473}
474
475export interface TestMessageParams {
476 path: string;
477 value: any;
478 originalValue: any;
479 label: string;
480}
481
482interface BaseTestOptions<P extends Record<string, any>, C = object> {
483 /**
484 * Unique name identifying the test. Required for exclusive tests.
485 */
486 name?: string;
487
488 /**
489 * Test function, determines schema validity
490 */
491 test: TestFunction<unknown, C>;
492
493 /**
494 * The validation error message
495 */
496 message?: TestOptionsMessage<P>;
497
498 /**
499 * Values passed to message for interpolation
500 */
501 params?: P;
502
503 /**
504 * Mark the test as exclusive, meaning only one of the same can be active at once
505 */
506 exclusive?: boolean;
507}
508
509interface NonExclusiveTestOptions<P extends Record<string, any>, C> extends BaseTestOptions<P, C> {
510 exclusive?: false;
511}
512
513interface ExclusiveTestOptions<P extends Record<string, any>, C> extends BaseTestOptions<P, C> {
514 exclusive: true;
515 name: string;
516}
517
518interface NonExclusiveAssertingTestOptions<U, P extends Record<string, any>, C> extends NonExclusiveTestOptions<P, C> {
519 test: AssertingTestFunction<U, C>;
520}
521
522interface ExclusiveAssertingTestOptions<U, P extends Record<string, any>, C> extends ExclusiveTestOptions<P, C> {
523 test: AssertingTestFunction<U, C>;
524}
525
526export type TestOptions<P extends Record<string, any> = {}, C = object> =
527 | NonExclusiveTestOptions<P, C>
528 | ExclusiveTestOptions<P, C>;
529
530export type AssertingTestOptions<U, P extends Record<string, any> = {}, C = object> =
531 | NonExclusiveAssertingTestOptions<U, P, C>
532 | ExclusiveAssertingTestOptions<U, P, C>;
533
534export interface SchemaFieldRefDescription {
535 type: 'ref';
536 key: string;
537}
538
539export interface SchemaFieldInnerTypeDescription
540 extends Pick<SchemaDescription, Exclude<keyof SchemaDescription, 'fields'>> {
541 innerType?: SchemaFieldDescription;
542}
543
544export type SchemaFieldDescription = SchemaDescription | SchemaFieldRefDescription | SchemaFieldInnerTypeDescription;
545
546export interface SchemaDescription {
547 type: string;
548 label: string;
549 meta: object;
550 tests: Array<{ name: string; params: { [k: string]: any } }>;
551 fields: Record<string, SchemaFieldDescription>;
552}
553
554// ValidationError works a lot more like a class vs. a constructor
555// function that returns an interface. It's also got a couple of
556// static methods and it inherits from the generic Error class in
557// the [yup codebase][1].
558// [1]: (https://github.com/jquense/yup/blob/master/src/ValidationError.js)
559export class ValidationError extends Error {
560 name: string;
561 message: string;
562 value: any;
563 /**
564 * A string, indicating where there error was thrown. path is empty at the root level.
565 */
566 path: string;
567 type: any;
568 /**
569 * array of error messages
570 */
571 errors: string[];
572
573 /**
574 * In the case of aggregate errors, inner is an array of ValidationErrors throw earlier in the validation chain.
575 */
576 inner: ValidationError[];
577 params?: object;
578
579 static isError(err: any): err is ValidationError;
580 static formatError(message: string | ((params?: any) => string), params?: any): string | ((params?: any) => string);
581
582 constructor(errors: string | string[], value: any, path: string, type?: any);
583}
584
585// It is tempting to declare `Ref` very simply, but there are problems with these approaches:
586//
587// * `type Ref = Record<string, any>;` - This is essentially how it was originally declared, but
588// just about any object satisfies this contract, which makes the type declaration too loose to
589// be useful.
590//
591// * `type Ref = object;` - This is a variation on the previous bullet in that it is too loose.
592//
593// * `class Ref {}` - This is yet another variation that is too loose.
594//
595// * `type Ref = void;` - This works and the emitted JavaScript is just fine, but it results in some
596// confusing IntelliSense, e.g it looks like the `ref()` returns `void`, which is not the case.
597//
598// The solution is twofold. 1.) Declare it as a class with a private constructor to prevent it from
599// being instantiated by anything but the `ref()` factory function, and; 2.) declare a private
600// readonly property (that yup actually places on the prototype) to force it to be structurally
601// incompatible with any other object type.
602
603/**
604 * `Ref` is an opaque type that is internal to yup. Creating a `Ref` instance is accomplished via the `ref()` factory
605 * function.
606 */
607export class Ref {
608 private constructor();
609 private readonly __isYupRef: true;
610}
611
612export interface Lazy extends Schema<any> {}
613
614export interface FormatErrorParams {
615 path: string;
616 type: string;
617 value?: any;
618 originalValue?: any;
619}
620
621export type LocaleValue = string | ((params: FormatErrorParams) => string);
622
623export interface MixedLocale {
624 default?: TestOptionsMessage;
625 required?: TestOptionsMessage;
626 oneOf?: TestOptionsMessage<{ values: any }>;
627 notOneOf?: TestOptionsMessage<{ values: any }>;
628 notType?: LocaleValue;
629 defined?: TestOptionsMessage;
630}
631
632export interface StringLocale {
633 length?: TestOptionsMessage<{ length: number }>;
634 min?: TestOptionsMessage<{ min: number }>;
635 max?: TestOptionsMessage<{ max: number }>;
636 matches?: TestOptionsMessage<{ regex: RegExp }>;
637 email?: TestOptionsMessage<{ regex: RegExp }>;
638 url?: TestOptionsMessage<{ regex: RegExp }>;
639 uuid?: TestOptionsMessage<{ regex: RegExp }>;
640 trim?: TestOptionsMessage;
641 lowercase?: TestOptionsMessage;
642 uppercase?: TestOptionsMessage;
643}
644
645export interface NumberLocale {
646 min?: TestOptionsMessage<{ min: number }>;
647 max?: TestOptionsMessage<{ max: number }>;
648 lessThan?: TestOptionsMessage<{ less: number }>;
649 moreThan?: TestOptionsMessage<{ more: number }>;
650 notEqual?: TestOptionsMessage<{ notEqual: number }>;
651 positive?: TestOptionsMessage<{ more: number }>;
652 negative?: TestOptionsMessage<{ less: number }>;
653 integer?: TestOptionsMessage;
654}
655
656export interface DateLocale {
657 min?: TestOptionsMessage<{ min: Date | string }>;
658 max?: TestOptionsMessage<{ max: Date | string }>;
659}
660
661export interface ObjectLocale {
662 noUnknown?: TestOptionsMessage<{ unknown: string }>;
663}
664
665export interface ArrayLocale {
666 min?: TestOptionsMessage<{ min: number }>;
667 max?: TestOptionsMessage<{ max: number }>;
668}
669
670export interface LocaleObject {
671 mixed?: MixedLocale;
672 string?: StringLocale;
673 number?: NumberLocale;
674 date?: DateLocale;
675 boolean?: {};
676 object?: ObjectLocale;
677 array?: ArrayLocale;
678}
679
680export type InferType<T> = T extends Schema<infer P> ? InnerInferType<P> : never;
681
682// Shut off automatic exporting after this statement
683export {};
684
685type KeyOfUndefined<T> = {
686 [P in keyof T]-?: undefined extends T[P] ? P : never;
687}[keyof T];
688
689type PreserveNull<T> = T extends null ? null : never;
690type PreserveUndefined<T> = T extends undefined ? undefined : never;
691type PreserveOptionals<T> = PreserveNull<T> | PreserveUndefined<T>;
692type Id<T> = {
693 [K in keyof T]: T[K] extends object ? InnerInferType<T[K]> : T[K];
694};
695type RequiredProps<T> = Pick<T, Exclude<keyof T, KeyOfUndefined<T>>>;
696type NotRequiredProps<T> = Partial<Pick<T, KeyOfUndefined<T>>>;
697type InnerInferType<T> =
698 | (T extends Array<infer T> ? InnerInferTypeArray<T> : Id<NotRequiredProps<T> & RequiredProps<T>>)
699 | PreserveOptionals<T>;
700interface InnerInferTypeArray<T> extends Array<InnerInferType<T>> {}
701type InferredArrayType<T> = T extends Array<infer U> ? U : T;
702/** If `T` is optional, returns optional `U`. */
703type MaintainOptionality<T, U> = T extends undefined ? U | undefined : U;
704
\No newline at end of file