1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export function reach<T, C = object>(schema: Schema<T, C>, path: string, value?: any, context?: any): Schema<T, C>;
|
22 | export function addMethod<T extends Schema<any>>(
|
23 | schemaCtor: AnySchemaConstructor,
|
24 | name: string,
|
25 | method: (this: T, ...args: any[]) => T,
|
26 | ): void;
|
27 | export function ref(path: string, options?: { contextPrefix: string }): Ref;
|
28 | export function lazy<T>(fn: (value: T) => Schema<T>): Lazy;
|
29 | export function setLocale(customLocale: LocaleObject): void;
|
30 | export function isSchema(obj: any): obj is Schema<any>;
|
31 |
|
32 | export const mixed: MixedSchemaConstructor;
|
33 | export const string: StringSchemaConstructor;
|
34 | export const number: NumberSchemaConstructor;
|
35 | export const boolean: BooleanSchemaConstructor;
|
36 | export const bool: BooleanSchemaConstructor;
|
37 | export const date: DateSchemaConstructor;
|
38 | export const array: ArraySchemaConstructor;
|
39 | export const object: ObjectSchemaConstructor;
|
40 |
|
41 | export type AnySchemaConstructor =
|
42 | | MixedSchemaConstructor
|
43 | | StringSchemaConstructor
|
44 | | NumberSchemaConstructor
|
45 | | BooleanSchemaConstructor
|
46 | | DateSchemaConstructor
|
47 | | ArraySchemaConstructor
|
48 | | ObjectSchemaConstructor;
|
49 |
|
50 | export type TestOptionsMessage<Extra extends Record<string, any> = {}, R = any> =
|
51 | | string
|
52 | | ((params: Extra & Partial<TestMessageParams>) => R);
|
53 |
|
54 | export 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 |
|
85 | export 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 |
|
92 | export 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 |
|
120 | export 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 |
|
127 | export 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 |
|
174 | export 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 |
|
181 | export 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 |
|
217 | export 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 |
|
224 | export 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 |
|
251 | export 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 |
|
258 | export 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 |
|
286 | export 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 |
|
292 | export 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 |
|
311 | export 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 |
|
322 | export 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 |
|
333 | export 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 |
|
344 | export 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 |
|
354 | export 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:
|
366 | */
|
367 | export 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 |
|
371 | export interface ObjectSchemaConstructor {
|
372 | <T extends object, C = object>(fields?: ObjectSchemaDefinition<T, C>): ObjectSchema<T | undefined, C>;
|
373 |
|
374 | new <C = object>(): ObjectSchema<{}, C>;
|
375 | }
|
376 |
|
377 | export 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 |
|
414 | export type TestFunction<T = unknown, C = object> = (
|
415 | this: TestContext<C>,
|
416 | value: T,
|
417 | context: TestContext<C>
|
418 | ) => boolean | ValidationError | Promise<boolean | ValidationError>;
|
419 | export type AssertingTestFunction<T, C> = (this: TestContext<C>, value: any, context: TestContext<C>) => value is T;
|
420 |
|
421 | export type TransformFunction<T> = (this: T, value: any, originalValue: any) => any;
|
422 |
|
423 | export 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 |
|
430 | export type WhenOptionsBuilderObjectIs = ((...values: any[]) => boolean) | boolean | number | null | object | string;
|
431 |
|
432 | export type WhenOptionsBuilderObject =
|
433 | | {
|
434 | is: WhenOptionsBuilderObjectIs;
|
435 | then: any;
|
436 | otherwise: any;
|
437 | }
|
438 | | object;
|
439 |
|
440 | export type WhenOptions<T> = WhenOptionsBuilderFunction<T> | WhenOptionsBuilderObject;
|
441 |
|
442 | export 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 |
|
452 | export interface ValidateOptions<C = object> {
|
453 | |
454 |
|
455 |
|
456 | strict?: boolean;
|
457 | |
458 |
|
459 |
|
460 | abortEarly?: boolean;
|
461 | |
462 |
|
463 |
|
464 | stripUnknown?: boolean;
|
465 | |
466 |
|
467 |
|
468 | recursive?: boolean;
|
469 | |
470 |
|
471 |
|
472 | context?: C;
|
473 | }
|
474 |
|
475 | export interface TestMessageParams {
|
476 | path: string;
|
477 | value: any;
|
478 | originalValue: any;
|
479 | label: string;
|
480 | }
|
481 |
|
482 | interface BaseTestOptions<P extends Record<string, any>, C = object> {
|
483 | |
484 |
|
485 |
|
486 | name?: string;
|
487 |
|
488 | |
489 |
|
490 |
|
491 | test: TestFunction<unknown, C>;
|
492 |
|
493 | |
494 |
|
495 |
|
496 | message?: TestOptionsMessage<P>;
|
497 |
|
498 | |
499 |
|
500 |
|
501 | params?: P;
|
502 |
|
503 | |
504 |
|
505 |
|
506 | exclusive?: boolean;
|
507 | }
|
508 |
|
509 | interface NonExclusiveTestOptions<P extends Record<string, any>, C> extends BaseTestOptions<P, C> {
|
510 | exclusive?: false;
|
511 | }
|
512 |
|
513 | interface ExclusiveTestOptions<P extends Record<string, any>, C> extends BaseTestOptions<P, C> {
|
514 | exclusive: true;
|
515 | name: string;
|
516 | }
|
517 |
|
518 | interface NonExclusiveAssertingTestOptions<U, P extends Record<string, any>, C> extends NonExclusiveTestOptions<P, C> {
|
519 | test: AssertingTestFunction<U, C>;
|
520 | }
|
521 |
|
522 | interface ExclusiveAssertingTestOptions<U, P extends Record<string, any>, C> extends ExclusiveTestOptions<P, C> {
|
523 | test: AssertingTestFunction<U, C>;
|
524 | }
|
525 |
|
526 | export type TestOptions<P extends Record<string, any> = {}, C = object> =
|
527 | | NonExclusiveTestOptions<P, C>
|
528 | | ExclusiveTestOptions<P, C>;
|
529 |
|
530 | export type AssertingTestOptions<U, P extends Record<string, any> = {}, C = object> =
|
531 | | NonExclusiveAssertingTestOptions<U, P, C>
|
532 | | ExclusiveAssertingTestOptions<U, P, C>;
|
533 |
|
534 | export interface SchemaFieldRefDescription {
|
535 | type: 'ref';
|
536 | key: string;
|
537 | }
|
538 |
|
539 | export interface SchemaFieldInnerTypeDescription
|
540 | extends Pick<SchemaDescription, Exclude<keyof SchemaDescription, 'fields'>> {
|
541 | innerType?: SchemaFieldDescription;
|
542 | }
|
543 |
|
544 | export type SchemaFieldDescription = SchemaDescription | SchemaFieldRefDescription | SchemaFieldInnerTypeDescription;
|
545 |
|
546 | export 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 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 | export class ValidationError extends Error {
|
560 | name: string;
|
561 | message: string;
|
562 | value: any;
|
563 | |
564 |
|
565 |
|
566 | path: string;
|
567 | type: any;
|
568 | |
569 |
|
570 |
|
571 | errors: string[];
|
572 |
|
573 | |
574 |
|
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 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | export class Ref {
|
608 | private constructor();
|
609 | private readonly __isYupRef: true;
|
610 | }
|
611 |
|
612 | export interface Lazy extends Schema<any> {}
|
613 |
|
614 | export interface FormatErrorParams {
|
615 | path: string;
|
616 | type: string;
|
617 | value?: any;
|
618 | originalValue?: any;
|
619 | }
|
620 |
|
621 | export type LocaleValue = string | ((params: FormatErrorParams) => string);
|
622 |
|
623 | export 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 |
|
632 | export 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 |
|
645 | export 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 |
|
656 | export interface DateLocale {
|
657 | min?: TestOptionsMessage<{ min: Date | string }>;
|
658 | max?: TestOptionsMessage<{ max: Date | string }>;
|
659 | }
|
660 |
|
661 | export interface ObjectLocale {
|
662 | noUnknown?: TestOptionsMessage<{ unknown: string }>;
|
663 | }
|
664 |
|
665 | export interface ArrayLocale {
|
666 | min?: TestOptionsMessage<{ min: number }>;
|
667 | max?: TestOptionsMessage<{ max: number }>;
|
668 | }
|
669 |
|
670 | export interface LocaleObject {
|
671 | mixed?: MixedLocale;
|
672 | string?: StringLocale;
|
673 | number?: NumberLocale;
|
674 | date?: DateLocale;
|
675 | boolean?: {};
|
676 | object?: ObjectLocale;
|
677 | array?: ArrayLocale;
|
678 | }
|
679 |
|
680 | export type InferType<T> = T extends Schema<infer P> ? InnerInferType<P> : never;
|
681 |
|
682 |
|
683 | export {};
|
684 |
|
685 | type KeyOfUndefined<T> = {
|
686 | [P in keyof T]-?: undefined extends T[P] ? P : never;
|
687 | }[keyof T];
|
688 |
|
689 | type PreserveNull<T> = T extends null ? null : never;
|
690 | type PreserveUndefined<T> = T extends undefined ? undefined : never;
|
691 | type PreserveOptionals<T> = PreserveNull<T> | PreserveUndefined<T>;
|
692 | type Id<T> = {
|
693 | [K in keyof T]: T[K] extends object ? InnerInferType<T[K]> : T[K];
|
694 | };
|
695 | type RequiredProps<T> = Pick<T, Exclude<keyof T, KeyOfUndefined<T>>>;
|
696 | type NotRequiredProps<T> = Partial<Pick<T, KeyOfUndefined<T>>>;
|
697 | type InnerInferType<T> =
|
698 | | (T extends Array<infer T> ? InnerInferTypeArray<T> : Id<NotRequiredProps<T> & RequiredProps<T>>)
|
699 | | PreserveOptionals<T>;
|
700 | interface InnerInferTypeArray<T> extends Array<InnerInferType<T>> {}
|
701 | type InferredArrayType<T> = T extends Array<infer U> ? U : T;
|
702 |
|
703 | type MaintainOptionality<T, U> = T extends undefined ? U | undefined : U;
|
704 |
|
\ | No newline at end of file |