1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function reach<T>(schema: Schema<T>, path: string, value?: any, context?: any): Schema<T>;
|
18 | export function addMethod<T extends Schema<any>>(
|
19 | schemaCtor: AnySchemaConstructor,
|
20 | name: string,
|
21 | method: (this: T, ...args: any[]) => T,
|
22 | ): void;
|
23 | export function ref(path: string, options?: { contextPrefix: string }): Ref;
|
24 | export function lazy<T>(fn: (value: T) => Schema<T>): Lazy;
|
25 | export function setLocale(customLocale: LocaleObject): void;
|
26 | export function isSchema(obj: any): obj is Schema<any>;
|
27 |
|
28 | export const mixed: MixedSchemaConstructor;
|
29 | export const string: StringSchemaConstructor;
|
30 | export const number: NumberSchemaConstructor;
|
31 | export const boolean: BooleanSchemaConstructor;
|
32 | export const bool: BooleanSchemaConstructor;
|
33 | export const date: DateSchemaConstructor;
|
34 | export const array: ArraySchemaConstructor;
|
35 | export const object: ObjectSchemaConstructor;
|
36 |
|
37 | export type AnySchemaConstructor =
|
38 | | MixedSchemaConstructor
|
39 | | StringSchemaConstructor
|
40 | | NumberSchemaConstructor
|
41 | | BooleanSchemaConstructor
|
42 | | DateSchemaConstructor
|
43 | | ArraySchemaConstructor
|
44 | | ObjectSchemaConstructor;
|
45 |
|
46 | export type TestOptionsMessage<Extra extends Record<string, any> = {}, R = any> =
|
47 | | string
|
48 | | ((params: Extra & Partial<TestMessageParams>) => R);
|
49 |
|
50 | export interface Schema<T> {
|
51 | clone(): this;
|
52 | label(label: string): this;
|
53 | meta(metadata: any): this;
|
54 | meta(): any;
|
55 | describe(): SchemaDescription;
|
56 | concat(schema: this): this;
|
57 | validate(value: any, options?: ValidateOptions): Promise<T>;
|
58 | validateSync(value: any, options?: ValidateOptions): T;
|
59 | validateAt(path: string, value: T, options?: ValidateOptions): Promise<T>;
|
60 | validateSyncAt(path: string, value: T, options?: ValidateOptions): T;
|
61 | isValid(value: any, options?: any): Promise<boolean>;
|
62 | isValidSync(value: any, options?: any): value is T;
|
63 | cast(value?: any, options?: any): T;
|
64 | isType(value: any): value is T;
|
65 | strict(isStrict: boolean): this;
|
66 | strip(strip: boolean): this;
|
67 | withMutation(fn: (current: this) => void): void;
|
68 | default(value: any): this;
|
69 | default(): T;
|
70 | typeError(message?: TestOptionsMessage): this;
|
71 | oneOf(arrayOfValues: Array<T | Ref | null>, message?: MixedLocale['oneOf']): this;
|
72 | notOneOf(arrayOfValues: any[], message?: MixedLocale['notOneOf']): this;
|
73 | when(keys: string | any[], builder: WhenOptions<this>): this;
|
74 | test(
|
75 | name: string,
|
76 | message: TestOptionsMessage,
|
77 | test: (this: TestContext, value?: any) => boolean | ValidationError | Promise<boolean | ValidationError>,
|
78 | callbackStyleAsync?: boolean,
|
79 | ): this;
|
80 | // tslint:disable-next-line:no-unnecessary-generics
|
81 | test<P>(options: TestOptions<P>): this;
|
82 | transform(fn: TransformFunction<this>): this;
|
83 | }
|
84 |
|
85 | export interface MixedSchemaConstructor {
|
86 | // tslint:disable-next-line:no-unnecessary-generics
|
87 | <T = any>(): MixedSchema<T>;
|
88 | // tslint:disable-next-line:no-unnecessary-generics
|
89 | new <T = any>(options?: { type?: string; [key: string]: any }): MixedSchema<T>;
|
90 | }
|
91 |
|
92 | export interface MixedSchema<T = any> extends Schema<T> {
|
93 | nullable(isNullable?: true): MixedSchema<T | null>;
|
94 | nullable(isNullable: false): MixedSchema<Exclude<T, null>>;
|
95 | nullable(isNullable?: boolean): MixedSchema<T>;
|
96 | required(message?: TestOptionsMessage): MixedSchema<Exclude<T, undefined>>;
|
97 | notRequired(): MixedSchema<T | undefined>;
|
98 | concat(schema: this): this;
|
99 | concat<U>(schema: MixedSchema<U>): MixedSchema<T | U>;
|
100 | }
|
101 |
|
102 | export interface StringSchemaConstructor {
|
103 | (): StringSchema;
|
104 | new (): StringSchema;
|
105 | }
|
106 |
|
107 | export interface StringSchema<T extends string | null | undefined = string> extends Schema<T> {
|
108 | length(limit: number | Ref, message?: StringLocale['length']): StringSchema<T>;
|
109 | min(limit: number | Ref, message?: StringLocale['min']): StringSchema<T>;
|
110 | max(limit: number | Ref, message?: StringLocale['max']): StringSchema<T>;
|
111 | matches(
|
112 | regex: RegExp,
|
113 | messageOrOptions?:
|
114 | | StringLocale['matches']
|
115 | | { message?: StringLocale['matches']; excludeEmptyString?: boolean },
|
116 | ): StringSchema<T>;
|
117 | email(message?: StringLocale['email']): StringSchema<T>;
|
118 | url(message?: StringLocale['url']): StringSchema<T>;
|
119 | ensure(): StringSchema<T>;
|
120 | trim(message?: StringLocale['trim']): StringSchema<T>;
|
121 | lowercase(message?: StringLocale['lowercase']): StringSchema<T>;
|
122 | uppercase(message?: StringLocale['uppercase']): StringSchema<T>;
|
123 | nullable(isNullable?: true): StringSchema<T | null>;
|
124 | nullable(isNullable: false): StringSchema<Exclude<T, null>>;
|
125 | nullable(isNullable?: boolean): StringSchema<T>;
|
126 | required(message?: TestOptionsMessage): StringSchema<Exclude<T, undefined>>;
|
127 | notRequired(): StringSchema<T | undefined>;
|
128 | }
|
129 |
|
130 | export interface NumberSchemaConstructor {
|
131 | (): NumberSchema;
|
132 | new (): NumberSchema;
|
133 | }
|
134 |
|
135 | export interface NumberSchema<T extends number | null | undefined = number> extends Schema<T> {
|
136 | min(limit: number | Ref, message?: NumberLocale['min']): NumberSchema<T>;
|
137 | max(limit: number | Ref, message?: NumberLocale['max']): NumberSchema<T>;
|
138 | lessThan(limit: number | Ref, message?: NumberLocale['lessThan']): NumberSchema<T>;
|
139 | moreThan(limit: number | Ref, message?: NumberLocale['moreThan']): NumberSchema<T>;
|
140 | positive(message?: NumberLocale['positive']): NumberSchema<T>;
|
141 | negative(message?: NumberLocale['negative']): NumberSchema<T>;
|
142 | integer(message?: NumberLocale['integer']): NumberSchema<T>;
|
143 | truncate(): NumberSchema<T>;
|
144 | round(type: 'floor' | 'ceil' | 'trunc' | 'round'): NumberSchema<T>;
|
145 | nullable(isNullable?: true): NumberSchema<T | null>;
|
146 | nullable(isNullable: false): NumberSchema<Exclude<T, null>>;
|
147 | nullable(isNullable?: boolean): NumberSchema<T>;
|
148 | required(message?: TestOptionsMessage): NumberSchema<Exclude<T, undefined>>;
|
149 | notRequired(): NumberSchema<T | undefined>;
|
150 | }
|
151 |
|
152 | export interface BooleanSchemaConstructor {
|
153 | (): BooleanSchema;
|
154 | new (): BooleanSchema;
|
155 | }
|
156 |
|
157 | export interface BooleanSchema<T extends boolean | null | undefined = boolean> extends Schema<T> {
|
158 | nullable(isNullable?: true): BooleanSchema<T | null>;
|
159 | nullable(isNullable: false): BooleanSchema<Exclude<T, null>>;
|
160 | nullable(isNullable?: boolean): BooleanSchema<T>;
|
161 | required(message?: TestOptionsMessage): BooleanSchema<Exclude<T, undefined>>;
|
162 | notRequired(): BooleanSchema<T | undefined>;
|
163 | }
|
164 |
|
165 | export interface DateSchemaConstructor {
|
166 | (): DateSchema;
|
167 | new (): DateSchema;
|
168 | }
|
169 |
|
170 | export interface DateSchema<T extends Date | null | undefined = Date> extends Schema<T> {
|
171 | min(limit: Date | string | Ref, message?: DateLocale['min']): DateSchema<T>;
|
172 | max(limit: Date | string | Ref, message?: DateLocale['max']): DateSchema<T>;
|
173 | nullable(isNullable?: true): DateSchema<T | null>;
|
174 | nullable(isNullable: false): DateSchema<Exclude<T, null>>;
|
175 | nullable(isNullable?: boolean): DateSchema<T>;
|
176 | required(message?: TestOptionsMessage): DateSchema<Exclude<T, undefined>>;
|
177 | notRequired(): DateSchema<T | undefined>;
|
178 | }
|
179 |
|
180 | export interface ArraySchemaConstructor {
|
181 | <T>(schema?: Schema<T>): ArraySchema<T>;
|
182 | new (): ArraySchema<{}>;
|
183 | }
|
184 |
|
185 | interface BasicArraySchema<T extends any[] | null | undefined> extends Schema<T> {
|
186 | min(limit: number | Ref, message?: ArrayLocale['min']): this;
|
187 | max(limit: number | Ref, message?: ArrayLocale['max']): this;
|
188 | ensure(): this;
|
189 | compact(
|
190 | rejector?: (value: InferredArrayType<T>, index: number, array: Array<InferredArrayType<T>>) => boolean,
|
191 | ): this;
|
192 | }
|
193 |
|
194 | export interface NotRequiredNullableArraySchema<T> extends BasicArraySchema<T[] | null | undefined> {
|
195 | of<U>(type: Schema<U>): NotRequiredNullableArraySchema<U>;
|
196 | nullable(isNullable?: true): NotRequiredNullableArraySchema<T>;
|
197 | nullable(isNullable: false): NotRequiredArraySchema<T>;
|
198 | nullable(isNullable?: boolean): ArraySchema<T>;
|
199 | required(message?: TestOptionsMessage): NullableArraySchema<T>;
|
200 | notRequired(): NotRequiredNullableArraySchema<T>;
|
201 | }
|
202 |
|
203 | export interface NullableArraySchema<T> extends BasicArraySchema<T[] | null> {
|
204 | of<U>(type: Schema<U>): NullableArraySchema<U>;
|
205 | nullable(isNullable?: true): NullableArraySchema<T>;
|
206 | nullable(isNullable: false): ArraySchema<T>;
|
207 | nullable(isNullable?: boolean): ArraySchema<T>;
|
208 | required(message?: TestOptionsMessage): NullableArraySchema<T>;
|
209 | notRequired(): NotRequiredNullableArraySchema<T>;
|
210 | }
|
211 |
|
212 | export interface NotRequiredArraySchema<T> extends BasicArraySchema<T[] | undefined> {
|
213 | of<U>(type: Schema<U>): NotRequiredArraySchema<U>;
|
214 | nullable(isNullable?: true): NotRequiredNullableArraySchema<T>;
|
215 | nullable(isNullable: false): NotRequiredArraySchema<T>;
|
216 | nullable(isNullable: boolean): ArraySchema<T>;
|
217 | required(message?: TestOptionsMessage): ArraySchema<T>;
|
218 | notRequired(): NotRequiredArraySchema<T>;
|
219 | }
|
220 |
|
221 | export interface ArraySchema<T> extends BasicArraySchema<T[]> {
|
222 | of<U>(type: Schema<U>): ArraySchema<U>;
|
223 | nullable(isNullable?: true): NullableArraySchema<T>;
|
224 | nullable(isNullable: false | boolean): ArraySchema<T>;
|
225 | required(message?: TestOptionsMessage): ArraySchema<T>;
|
226 | notRequired(): NotRequiredArraySchema<T>;
|
227 | }
|
228 |
|
229 | export type ObjectSchemaDefinition<T extends object | null | undefined> = {
|
230 | [field in keyof T]: Schema<T[field]> | Ref;
|
231 | };
|
232 |
|
233 | /**
|
234 | * Merges two interfaces. For properties in common, property types from `U` trump those of `T`.
|
235 | * This is conducive to the functionality of
|
236 | * [yup's `object.shape()` method](https:
|
237 | */
|
238 | export type Shape<T extends object | null | undefined, U extends object> = {
|
239 | [P in keyof T]: P extends keyof U ? U[P] : T[P];
|
240 | } &
|
241 | U;
|
242 |
|
243 | export interface ObjectSchemaConstructor {
|
244 | <T extends object>(fields?: ObjectSchemaDefinition<T>): ObjectSchema<T>;
|
245 | new (): ObjectSchema<{}>;
|
246 | }
|
247 |
|
248 | export interface ObjectSchema<T extends object | null | undefined = object> extends Schema<T> {
|
249 | shape<U extends object>(
|
250 | fields: ObjectSchemaDefinition<U>,
|
251 | noSortEdges?: Array<[string, string]>,
|
252 | ): ObjectSchema<Shape<T, U>>;
|
253 | from(fromKey: string, toKey: string, alias?: boolean): ObjectSchema<T>;
|
254 | noUnknown(onlyKnownKeys?: boolean, message?: ObjectLocale['noUnknown']): ObjectSchema<T>;
|
255 | transformKeys(callback: (key: any) => any): void;
|
256 | camelCase(): ObjectSchema<T>;
|
257 | constantCase(): ObjectSchema<T>;
|
258 | nullable(isNullable?: true): ObjectSchema<T | null>;
|
259 | nullable(isNullable: false): ObjectSchema<Exclude<T, null>>;
|
260 | nullable(isNullable?: boolean): ObjectSchema<T>;
|
261 | required(message?: TestOptionsMessage): ObjectSchema<Exclude<T, undefined>>;
|
262 | notRequired(): ObjectSchema<T | undefined>;
|
263 | concat(schema: this): this;
|
264 | concat<U extends object>(schema: ObjectSchema<U>): ObjectSchema<T & U>;
|
265 | }
|
266 |
|
267 | export type TransformFunction<T> = (this: T, value: any, originalValue: any) => any;
|
268 |
|
269 | export interface WhenOptionsBuilderFunction<T> {
|
270 | (value: any, schema: T): T;
|
271 | (v1: any, v2: any, schema: T): T;
|
272 | (v1: any, v2: any, v3: any, schema: T): T;
|
273 | (v1: any, v2: any, v3: any, v4: any, schema: T): T;
|
274 | }
|
275 |
|
276 | export type WhenOptionsBuilderObjectIs = ((...values: any[]) => boolean) | boolean | number | null | object | string;
|
277 |
|
278 | export type WhenOptionsBuilderObject =
|
279 | | {
|
280 | is: WhenOptionsBuilderObjectIs;
|
281 | then: any;
|
282 | otherwise: any;
|
283 | }
|
284 | | object;
|
285 |
|
286 | export type WhenOptions<T> = WhenOptionsBuilderFunction<T> | WhenOptionsBuilderObject;
|
287 |
|
288 | export interface TestContext {
|
289 | path: string;
|
290 | options: ValidateOptions;
|
291 | parent: any;
|
292 | schema: Schema<any>;
|
293 | resolve: (value: any) => any;
|
294 | createError: (params?: { path?: string; message?: string, params?: object }) => ValidationError;
|
295 | }
|
296 |
|
297 | export interface ValidateOptions {
|
298 | |
299 |
|
300 |
|
301 | strict?: boolean;
|
302 | |
303 |
|
304 |
|
305 | abortEarly?: boolean;
|
306 | |
307 |
|
308 |
|
309 | stripUnknown?: boolean;
|
310 | |
311 |
|
312 |
|
313 | recursive?: boolean;
|
314 | |
315 |
|
316 |
|
317 | context?: object;
|
318 | }
|
319 |
|
320 | export interface TestMessageParams {
|
321 | path: string;
|
322 | value: any;
|
323 | originalValue: any;
|
324 | label: string;
|
325 | }
|
326 |
|
327 | export interface TestOptions<P extends Record<string, any> = {}, R = any> {
|
328 | |
329 |
|
330 |
|
331 | name?: string;
|
332 |
|
333 | |
334 |
|
335 |
|
336 | test: (this: TestContext, value: any) => boolean | ValidationError | Promise<boolean | ValidationError>;
|
337 |
|
338 | |
339 |
|
340 |
|
341 | message?: TestOptionsMessage<P, R>;
|
342 |
|
343 | |
344 |
|
345 |
|
346 | params?: P;
|
347 |
|
348 | |
349 |
|
350 |
|
351 | exclusive?: boolean;
|
352 | }
|
353 |
|
354 | export interface SchemaDescription {
|
355 | type: string;
|
356 | label: string;
|
357 | meta: object;
|
358 | tests: Array<{ name: string; params: object }>;
|
359 | fields: object;
|
360 | }
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | export class ValidationError extends Error {
|
368 | name: string;
|
369 | message: string;
|
370 | value: any;
|
371 | |
372 |
|
373 |
|
374 | path: string;
|
375 | type: any;
|
376 | |
377 |
|
378 |
|
379 | errors: string[];
|
380 |
|
381 | |
382 |
|
383 |
|
384 | inner: ValidationError[];
|
385 | params?: object;
|
386 |
|
387 | static isError(err: any): err is ValidationError;
|
388 | static formatError(message: string | ((params?: any) => string), params?: any): string | ((params?: any) => string);
|
389 |
|
390 | constructor(errors: string | string[], value: any, path: string, type?: any);
|
391 | }
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 | export class Ref {
|
416 | private constructor();
|
417 | private readonly __isYupRef: true;
|
418 | }
|
419 |
|
420 |
|
421 | export interface Lazy extends Schema<any> {}
|
422 |
|
423 | export interface FormatErrorParams {
|
424 | path: string;
|
425 | type: string;
|
426 | value?: any;
|
427 | originalValue?: any;
|
428 | }
|
429 |
|
430 | export type LocaleValue = string | ((params: FormatErrorParams) => string);
|
431 |
|
432 | interface MixedLocale {
|
433 | default?: TestOptionsMessage;
|
434 | required?: TestOptionsMessage;
|
435 | oneOf?: TestOptionsMessage<{ values: any }>;
|
436 | notOneOf?: TestOptionsMessage<{ values: any }>;
|
437 | notType?: LocaleValue;
|
438 | }
|
439 |
|
440 | interface StringLocale {
|
441 | length?: TestOptionsMessage<{ length: number }>;
|
442 | min?: TestOptionsMessage<{ min: number }>;
|
443 | max?: TestOptionsMessage<{ max: number }>;
|
444 | matches?: TestOptionsMessage<{ regex: RegExp }>;
|
445 | email?: TestOptionsMessage<{ regex: RegExp }>;
|
446 | url?: TestOptionsMessage<{ regex: RegExp }>;
|
447 | trim?: TestOptionsMessage;
|
448 | lowercase?: TestOptionsMessage;
|
449 | uppercase?: TestOptionsMessage;
|
450 | }
|
451 |
|
452 | interface NumberLocale {
|
453 | min?: TestOptionsMessage<{ min: number }>;
|
454 | max?: TestOptionsMessage<{ max: number }>;
|
455 | lessThan?: TestOptionsMessage<{ less: number }>;
|
456 | moreThan?: TestOptionsMessage<{ more: number }>;
|
457 | positive?: TestOptionsMessage<{ more: number }>;
|
458 | negative?: TestOptionsMessage<{ less: number }>;
|
459 | integer?: TestOptionsMessage;
|
460 | }
|
461 |
|
462 | interface DateLocale {
|
463 | min?: TestOptionsMessage<{ min: Date | string }>;
|
464 | max?: TestOptionsMessage<{ max: Date | string }>;
|
465 | }
|
466 |
|
467 | interface ObjectLocale {
|
468 | noUnknown?: TestOptionsMessage;
|
469 | }
|
470 |
|
471 | interface ArrayLocale {
|
472 | min?: TestOptionsMessage<{ min: number }>;
|
473 | max?: TestOptionsMessage<{ max: number }>;
|
474 | }
|
475 |
|
476 | export interface LocaleObject {
|
477 | mixed?: MixedLocale;
|
478 | string?: StringLocale;
|
479 | number?: NumberLocale;
|
480 | date?: DateLocale;
|
481 | boolean?: {};
|
482 | object?: ObjectLocale;
|
483 | array?: ArrayLocale;
|
484 | }
|
485 |
|
486 | export type InferType<T> = T extends Schema<infer P> ? InnerInferType<P> : never;
|
487 |
|
488 |
|
489 | export {};
|
490 |
|
491 | type KeyOfUndefined<T> = {
|
492 | [P in keyof T]-?: undefined extends T[P] ? P : never;
|
493 | }[keyof T];
|
494 |
|
495 | type Id<T> = { [K in keyof T]: T[K] };
|
496 | type RequiredProps<T> = Pick<T, Exclude<keyof T, KeyOfUndefined<T>>>;
|
497 | type NotRequiredProps<T> = Partial<Pick<T, KeyOfUndefined<T>>>;
|
498 | type InnerInferType<T> = Id<NotRequiredProps<T> & RequiredProps<T>>;
|
499 | type InferredArrayType<T> = T extends Array<infer U> ? U : T;
|
500 |
|
\ | No newline at end of file |