UNPKG

41.3 kBTypeScriptView Raw
1import { Get } from 'type-fest';
2
3declare type Maybe<T> = T | null | undefined;
4declare type Preserve<T, U> = T extends U ? U : never;
5declare type Optionals<T> = Extract<T, null | undefined>;
6declare type Defined<T> = T extends undefined ? never : T;
7declare type NotNull<T> = T extends null ? never : T;
8declare type Thunk<T> = T | (() => T);
9declare type _<T> = T extends {} ? {
10 [k in keyof T]: T[k];
11} : T;
12declare type Flags = 's' | 'd' | '';
13declare type SetFlag<Old extends Flags, F extends Flags> = Exclude<Old, ''> | F;
14declare type UnsetFlag<Old extends Flags, F extends Flags> = Exclude<Old, F> extends never ? '' : Exclude<Old, F>;
15declare type ToggleDefault<F extends Flags, D> = Preserve<D, undefined> extends never ? SetFlag<F, 'd'> : UnsetFlag<F, 'd'>;
16declare type ResolveFlags<T, F extends Flags, D = T> = Preserve<F, 's'> extends never ? Extract<F, 'd'> extends never ? T : D extends undefined ? T : Defined<T> : never;
17declare type Concat<T, U> = NonNullable<T> & NonNullable<U> extends never ? never : (NonNullable<T> & NonNullable<U>) | Optionals<U>;
18
19declare type Params = Record<string, unknown>;
20declare class ValidationError extends Error {
21 value: any;
22 path?: string;
23 type?: string;
24 errors: string[];
25 params?: Params;
26 inner: ValidationError[];
27 static formatError(message: string | ((params: Params) => string) | unknown, params: Params): any;
28 static isError(err: any): err is ValidationError;
29 constructor(errorOrErrors: string | ValidationError | readonly ValidationError[], value?: any, field?: string, type?: string);
30}
31
32declare type PanicCallback = (err: Error) => void;
33declare type NextCallback = (err: ValidationError[] | ValidationError | null) => void;
34declare type CreateErrorOptions = {
35 path?: string;
36 message?: Message<any>;
37 params?: ExtraParams;
38 type?: string;
39};
40declare type TestContext<TContext = {}> = {
41 path: string;
42 options: ValidateOptions<TContext>;
43 originalValue: any;
44 parent: any;
45 from?: Array<{
46 schema: ISchema<any, TContext>;
47 value: any;
48 }>;
49 schema: any;
50 resolve: <T>(value: T | Reference<T>) => T;
51 createError: (params?: CreateErrorOptions) => ValidationError;
52};
53declare type TestFunction<T = unknown, TContext = {}> = (this: TestContext<TContext>, value: T, context: TestContext<TContext>) => void | boolean | ValidationError | Promise<boolean | ValidationError>;
54declare type TestOptions<TSchema extends AnySchema = AnySchema> = {
55 value: any;
56 path?: string;
57 options: InternalOptions;
58 originalValue: any;
59 schema: TSchema;
60};
61declare type TestConfig<TValue = unknown, TContext = {}> = {
62 name?: string;
63 message?: Message<any>;
64 test: TestFunction<TValue, TContext>;
65 params?: ExtraParams;
66 exclusive?: boolean;
67 skipAbsent?: boolean;
68};
69declare type Test = ((opts: TestOptions, panic: PanicCallback, next: NextCallback) => void) & {
70 OPTIONS?: TestConfig;
71};
72
73declare class ReferenceSet extends Set<unknown | Reference> {
74 describe(): unknown[];
75 resolveAll(resolve: (v: unknown | Reference) => unknown): unknown[];
76 clone(): ReferenceSet;
77 merge(newItems: ReferenceSet, removeItems: ReferenceSet): ReferenceSet;
78}
79
80declare type SchemaSpec<TDefault> = {
81 coerce: boolean;
82 nullable: boolean;
83 optional: boolean;
84 default?: TDefault | (() => TDefault);
85 abortEarly?: boolean;
86 strip?: boolean;
87 strict?: boolean;
88 recursive?: boolean;
89 label?: string | undefined;
90 meta?: any;
91};
92declare type SchemaOptions<TType, TDefault> = {
93 type: string;
94 spec?: Partial<SchemaSpec<TDefault>>;
95 check: (value: any) => value is NonNullable<TType>;
96};
97declare type AnySchema<TType = any, C = any, D = any, F extends Flags = Flags> = Schema<TType, C, D, F>;
98interface CastOptions$1<C = {}> {
99 parent?: any;
100 context?: C;
101 assert?: boolean;
102 stripUnknown?: boolean;
103 path?: string;
104 resolved?: boolean;
105}
106interface CastOptionalityOptions<C = {}> extends Omit<CastOptions$1<C>, 'assert'> {
107 /**
108 * Whether or not to throw TypeErrors if casting fails to produce a valid type.
109 * defaults to `true`. The `'ignore-optionality'` options is provided as a migration
110 * path from pre-v1 where `schema.nullable().required()` was allowed. When provided
111 * cast will only throw for values that are the wrong type *not* including `null` and `undefined`
112 */
113 assert: 'ignore-optionality';
114}
115declare type RunTest = (opts: TestOptions, panic: PanicCallback, next: NextCallback) => void;
116declare type TestRunOptions = {
117 tests: RunTest[];
118 path?: string | undefined;
119 options: InternalOptions;
120 originalValue: any;
121 value: any;
122};
123interface SchemaRefDescription {
124 type: 'ref';
125 key: string;
126}
127interface SchemaInnerTypeDescription extends SchemaDescription {
128 innerType?: SchemaFieldDescription;
129}
130interface SchemaObjectDescription extends SchemaDescription {
131 fields: Record<string, SchemaFieldDescription>;
132}
133interface SchemaLazyDescription {
134 type: string;
135 label?: string;
136 meta: object | undefined;
137}
138declare type SchemaFieldDescription = SchemaDescription | SchemaRefDescription | SchemaObjectDescription | SchemaInnerTypeDescription | SchemaLazyDescription;
139interface SchemaDescription {
140 type: string;
141 label?: string;
142 meta: object | undefined;
143 oneOf: unknown[];
144 notOneOf: unknown[];
145 nullable: boolean;
146 optional: boolean;
147 tests: Array<{
148 name?: string;
149 params: ExtraParams | undefined;
150 }>;
151}
152declare abstract class Schema<TType = any, TContext = any, TDefault = any, TFlags extends Flags = ''> implements ISchema<TType, TContext, TFlags, TDefault> {
153 readonly type: string;
154 readonly __outputType: ResolveFlags<TType, TFlags, TDefault>;
155 readonly __context: TContext;
156 readonly __flags: TFlags;
157 readonly __isYupSchema__: boolean;
158 readonly __default: TDefault;
159 readonly deps: readonly string[];
160 tests: Test[];
161 transforms: TransformFunction<AnySchema>[];
162 private conditions;
163 private _mutate?;
164 private internalTests;
165 protected _whitelist: ReferenceSet;
166 protected _blacklist: ReferenceSet;
167 protected exclusiveTests: Record<string, boolean>;
168 protected _typeCheck: (value: any) => value is NonNullable<TType>;
169 spec: SchemaSpec<any>;
170 constructor(options: SchemaOptions<TType, any>);
171 get _type(): string;
172 clone(spec?: Partial<SchemaSpec<any>>): this;
173 label(label: string): this;
174 meta(): Record<string, unknown> | undefined;
175 meta(obj: Record<string, unknown>): this;
176 withMutation<T>(fn: (schema: this) => T): T;
177 concat(schema: this): this;
178 concat(schema: AnySchema): AnySchema;
179 isType(v: unknown): v is TType;
180 resolve(options: ResolveOptions<TContext>): this;
181 protected resolveOptions<T extends InternalOptions<any>>(options: T): T;
182 /**
183 * Run the configured transform pipeline over an input value.
184 */
185 cast(value: any, options?: CastOptions$1<TContext>): this['__outputType'];
186 cast(value: any, options: CastOptionalityOptions<TContext>): this['__outputType'] | null | undefined;
187 protected _cast(rawValue: any, _options: CastOptions$1<TContext>): any;
188 protected _validate(_value: any, options: InternalOptions<TContext> | undefined, panic: (err: Error, value: unknown) => void, next: (err: ValidationError[], value: unknown) => void): void;
189 /**
190 * Executes a set of validations, either schema, produced Tests or a nested
191 * schema validate result.
192 */
193 protected runTests(runOptions: TestRunOptions, panic: (err: Error, value: unknown) => void, next: (errors: ValidationError[], value: unknown) => void): void;
194 asNestedTest({ key, index, parent, parentPath, originalParent, options, }: NestedTestConfig): RunTest;
195 validate(value: any, options?: ValidateOptions<TContext>): Promise<this['__outputType']>;
196 validateSync(value: any, options?: ValidateOptions<TContext>): this['__outputType'];
197 isValid(value: any, options?: ValidateOptions<TContext>): Promise<boolean>;
198 isValidSync(value: any, options?: ValidateOptions<TContext>): value is this['__outputType'];
199 protected _getDefault(): any;
200 getDefault(options?: ResolveOptions<TContext>): TDefault;
201 default(def: Thunk<any>): any;
202 strict(isStrict?: boolean): this;
203 protected nullability(nullable: boolean, message?: Message<any>): this;
204 protected optionality(optional: boolean, message?: Message<any>): this;
205 optional(): any;
206 defined(message?: Message<any>): any;
207 nullable(): any;
208 nonNullable(message?: Message<any>): any;
209 required(message?: Message<any>): any;
210 notRequired(): any;
211 transform(fn: TransformFunction<this>): this;
212 /**
213 * Adds a test function to the schema's queue of tests.
214 * tests can be exclusive or non-exclusive.
215 *
216 * - exclusive tests, will replace any existing tests of the same name.
217 * - non-exclusive: can be stacked
218 *
219 * If a non-exclusive test is added to a schema with an exclusive test of the same name
220 * the exclusive test is removed and further tests of the same name will be stacked.
221 *
222 * If an exclusive test is added to a schema with non-exclusive tests of the same name
223 * the previous tests are removed and further tests of the same name will replace each other.
224 */
225 test(options: TestConfig<this['__outputType'], TContext>): this;
226 test(test: TestFunction<this['__outputType'], TContext>): this;
227 test(name: string, test: TestFunction<this['__outputType'], TContext>): this;
228 test(name: string, message: Message, test: TestFunction<this['__outputType'], TContext>): this;
229 when<U extends ISchema<any> = this>(builder: ConditionBuilder<this, U>): U;
230 when<U extends ISchema<any> = this>(keys: string | string[], builder: ConditionBuilder<this, U>): U;
231 when<UThen extends ISchema<any> = this, UOtherwise extends ISchema<any> = this>(options: ConditionConfig<this, UThen, UOtherwise>): UThen | UOtherwise;
232 when<UThen extends ISchema<any> = this, UOtherwise extends ISchema<any> = this>(keys: string | string[], options: ConditionConfig<this, UThen, UOtherwise>): UThen | UOtherwise;
233 typeError(message: Message): this;
234 oneOf<U extends TType>(enums: ReadonlyArray<U | Reference>, message?: Message<{
235 values: any;
236 }>): this;
237 oneOf(enums: ReadonlyArray<TType | Reference>, message: Message<{
238 values: any;
239 }>): any;
240 notOneOf<U extends TType>(enums: ReadonlyArray<Maybe<U> | Reference>, message?: Message<{
241 values: any;
242 }>): this;
243 strip(strip?: boolean): any;
244 /**
245 * Return a serialized description of the schema including validations, flags, types etc.
246 *
247 * @param options Provide any needed context for resolving runtime schema alterations (lazy, when conditions, etc).
248 */
249 describe(options?: ResolveOptions<TContext>): SchemaDescription;
250}
251interface Schema<TType = any, TContext = any, TDefault = any, TFlags extends Flags = ''> {
252 validateAt(path: string, value: any, options?: ValidateOptions<TContext>): Promise<any>;
253 validateSyncAt(path: string, value: any, options?: ValidateOptions<TContext>): any;
254 equals: Schema['oneOf'];
255 is: Schema['oneOf'];
256 not: Schema['notOneOf'];
257 nope: Schema['notOneOf'];
258}
259
260declare type ReferenceOptions<TValue = unknown> = {
261 map?: (value: unknown) => TValue;
262};
263declare function create$9<TValue = unknown>(key: string, options?: ReferenceOptions<TValue>): Reference<TValue>;
264declare class Reference<TValue = unknown> {
265 readonly key: string;
266 readonly isContext: boolean;
267 readonly isValue: boolean;
268 readonly isSibling: boolean;
269 readonly path: any;
270 readonly getter: (data: unknown) => unknown;
271 readonly map?: (value: unknown) => TValue;
272 readonly __isYupRef: boolean;
273 constructor(key: string, options?: ReferenceOptions<TValue>);
274 getValue(value: any, parent?: {}, context?: {}): TValue;
275 /**
276 *
277 * @param {*} value
278 * @param {Object} options
279 * @param {Object=} options.context
280 * @param {Object=} options.parent
281 */
282 cast(value: any, options?: {
283 parent?: {};
284 context?: {};
285 }): TValue;
286 resolve(): this;
287 describe(): SchemaRefDescription;
288 toString(): string;
289 static isRef(value: any): value is Reference;
290}
291
292declare type ConditionBuilder<T extends ISchema<any, any>, U extends ISchema<any, any> = T> = (values: any[], schema: T, options: ResolveOptions) => U;
293declare type ConditionConfig<T extends ISchema<any>, TThen extends ISchema<any, any> = T, TOtherwise extends ISchema<any, any> = T> = {
294 is: any | ((...values: any[]) => boolean);
295 then?: (schema: T) => TThen;
296 otherwise?: (schema: T) => TOtherwise;
297};
298declare type ResolveOptions<TContext = any> = {
299 value?: any;
300 parent?: any;
301 context?: TContext;
302};
303
304declare type ObjectShape = {
305 [k: string]: ISchema<any> | Reference;
306};
307declare type AnyObject = {
308 [k: string]: any;
309};
310declare type TypeFromShape<S extends ObjectShape, _C> = {
311 [K in keyof S]: S[K] extends ISchema<any, any> ? S[K]['__outputType'] : S[K] extends Reference<infer T> ? T : unknown;
312};
313declare type DefaultFromShape<Shape extends ObjectShape> = {
314 [K in keyof Shape]: Shape[K] extends ISchema<any> ? Shape[K]['__default'] : undefined;
315};
316declare type ConcatObjectTypes<T extends Maybe<AnyObject>, U extends Maybe<AnyObject>> = ({
317 [P in keyof T]: P extends keyof NonNullable<U> ? NonNullable<U>[P] : T[P];
318} & U) | Optionals<U>;
319declare type PartialDeep<T> = T extends string | number | bigint | boolean | null | undefined | symbol | Date ? T | undefined : T extends Array<infer ArrayType> ? Array<PartialDeep<ArrayType>> : T extends ReadonlyArray<infer ArrayType> ? ReadonlyArray<ArrayType> : {
320 [K in keyof T]?: PartialDeep<T[K]>;
321};
322declare type OptionalKeys<T extends {}> = {
323 [k in keyof T]: undefined extends T[k] ? k : never;
324}[keyof T];
325declare type RequiredKeys<T extends object> = Exclude<keyof T, OptionalKeys<T>>;
326declare type MakePartial<T extends object> = {
327 [k in OptionalKeys<T> as T[k] extends never ? never : k]?: T[k];
328} & {
329 [k in RequiredKeys<T> as T[k] extends never ? never : k]: T[k];
330};
331
332interface ISchema<T, C = any, F extends Flags = any, D = any> {
333 __flags: F;
334 __context: C;
335 __outputType: T;
336 __default: D;
337 cast(value: any, options?: CastOptions$1<C>): T;
338 cast(value: any, options: CastOptionalityOptions<C>): T | null | undefined;
339 validate(value: any, options?: ValidateOptions<C>): Promise<T>;
340 asNestedTest(config: NestedTestConfig): Test;
341 describe(options?: ResolveOptions<C>): SchemaFieldDescription;
342 resolve(options: ResolveOptions<C>): ISchema<T, C, F>;
343}
344declare type InferType<T extends ISchema<any, any>> = T['__outputType'];
345declare type TransformFunction<T extends AnySchema> = (this: T, value: any, originalValue: any, schema: T) => any;
346interface Ancester<TContext> {
347 schema: ISchema<any, TContext>;
348 value: any;
349}
350interface ValidateOptions<TContext = {}> {
351 /**
352 * Only validate the input, skipping type casting and transformation. Default - false
353 */
354 strict?: boolean;
355 /**
356 * Return from validation methods on the first error rather than after all validations run. Default - true
357 */
358 abortEarly?: boolean;
359 /**
360 * Remove unspecified keys from objects. Default - false
361 */
362 stripUnknown?: boolean;
363 /**
364 * When false validations will not descend into nested schema (relevant for objects or arrays). Default - true
365 */
366 recursive?: boolean;
367 /**
368 * Any context needed for validating schema conditions (see: when())
369 */
370 context?: TContext;
371}
372interface InternalOptions<TContext = any> extends ValidateOptions<TContext> {
373 __validating?: boolean;
374 originalValue?: any;
375 index?: number;
376 key?: string;
377 parent?: any;
378 path?: string;
379 sync?: boolean;
380 from?: Ancester<TContext>[];
381}
382interface MessageParams {
383 path: string;
384 value: any;
385 originalValue: any;
386 label: string;
387 type: string;
388 spec: SchemaSpec<any> & Record<string, unknown>;
389}
390declare type Message<Extra extends Record<string, unknown> = any> = string | ((params: Extra & MessageParams) => unknown) | Record<PropertyKey, unknown>;
391declare type ExtraParams = Record<string, unknown>;
392interface NestedTestConfig {
393 options: InternalOptions<any>;
394 parent: any;
395 originalParent: any;
396 parentPath: string | undefined;
397 key?: string;
398 index?: number;
399}
400
401declare type AnyPresentValue = {};
402declare type TypeGuard<TType> = (value: any) => value is NonNullable<TType>;
403interface MixedOptions<TType> {
404 type?: string;
405 check?: TypeGuard<TType>;
406}
407declare function create$8<TType extends AnyPresentValue>(spec?: MixedOptions<TType> | TypeGuard<TType>): MixedSchema<TType | undefined, AnyObject, undefined, "">;
408declare namespace create$8 {
409 var prototype: MixedSchema<any, any, any, any>;
410}
411declare class MixedSchema<TType extends Maybe<AnyPresentValue> = AnyPresentValue | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
412 constructor(spec?: MixedOptions<TType> | TypeGuard<TType>);
413}
414interface MixedSchema<TType extends Maybe<AnyPresentValue> = AnyPresentValue | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
415 default<D extends Maybe<TType>>(def: Thunk<D>): MixedSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;
416 concat<IT, IC, ID, IF extends Flags>(schema: MixedSchema<IT, IC, ID, IF>): MixedSchema<Concat<TType, IT>, TContext & IC, ID, TFlags | IF>;
417 concat<IT, IC, ID, IF extends Flags>(schema: Schema<IT, IC, ID, IF>): MixedSchema<Concat<TType, IT>, TContext & IC, ID, TFlags | IF>;
418 concat(schema: this): this;
419 defined(msg?: Message): MixedSchema<Defined<TType>, TContext, TDefault, TFlags>;
420 optional(): MixedSchema<TType | undefined, TContext, TDefault, TFlags>;
421 required(msg?: Message): MixedSchema<NonNullable<TType>, TContext, TDefault, TFlags>;
422 notRequired(): MixedSchema<Maybe<TType>, TContext, TDefault, TFlags>;
423 nullable(msg?: Message): MixedSchema<TType | null, TContext, TDefault, TFlags>;
424 nonNullable(): MixedSchema<Exclude<TType, null>, TContext, TDefault, TFlags>;
425 strip(enabled: false): MixedSchema<TType, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
426 strip(enabled?: true): MixedSchema<TType, TContext, TDefault, SetFlag<TFlags, 's'>>;
427}
428
429declare function create$7(): BooleanSchema;
430declare function create$7<T extends boolean, TContext extends Maybe<AnyObject> = AnyObject>(): BooleanSchema<T | undefined, TContext>;
431declare namespace create$7 {
432 var prototype: BooleanSchema<any, any, any, any>;
433}
434declare class BooleanSchema<TType extends Maybe<boolean> = boolean | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
435 constructor();
436 isTrue(message?: Message<any> | undefined): BooleanSchema<true | Optionals<TType>, TContext, TFlags>;
437 isFalse(message?: Message<any> | undefined): BooleanSchema<false | Optionals<TType>, TContext, TFlags>;
438 default<D extends Maybe<TType>>(def: Thunk<D>): BooleanSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;
439 defined(msg?: Message): BooleanSchema<Defined<TType>, TContext, TDefault, TFlags>;
440 optional(): BooleanSchema<TType | undefined, TContext, TDefault, TFlags>;
441 required(msg?: Message): BooleanSchema<NonNullable<TType>, TContext, TDefault, TFlags>;
442 notRequired(): BooleanSchema<Maybe<TType>, TContext, TDefault, TFlags>;
443 nullable(): BooleanSchema<TType | null, TContext, TDefault, TFlags>;
444 nonNullable(msg?: Message): BooleanSchema<NotNull<TType>, TContext, TDefault, TFlags>;
445 strip(enabled: false): BooleanSchema<TType, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
446 strip(enabled?: true): BooleanSchema<TType, TContext, TDefault, SetFlag<TFlags, 's'>>;
447}
448
449interface MixedLocale {
450 default?: Message;
451 required?: Message;
452 oneOf?: Message<{
453 values: any;
454 }>;
455 notOneOf?: Message<{
456 values: any;
457 }>;
458 notNull?: Message;
459 notType?: Message;
460 defined?: Message;
461}
462interface StringLocale {
463 length?: Message<{
464 length: number;
465 }>;
466 min?: Message<{
467 min: number;
468 }>;
469 max?: Message<{
470 max: number;
471 }>;
472 matches?: Message<{
473 regex: RegExp;
474 }>;
475 email?: Message<{
476 regex: RegExp;
477 }>;
478 url?: Message<{
479 regex: RegExp;
480 }>;
481 uuid?: Message<{
482 regex: RegExp;
483 }>;
484 trim?: Message;
485 lowercase?: Message;
486 uppercase?: Message;
487}
488interface NumberLocale {
489 min?: Message<{
490 min: number;
491 }>;
492 max?: Message<{
493 max: number;
494 }>;
495 lessThan?: Message<{
496 less: number;
497 }>;
498 moreThan?: Message<{
499 more: number;
500 }>;
501 positive?: Message<{
502 more: number;
503 }>;
504 negative?: Message<{
505 less: number;
506 }>;
507 integer?: Message;
508}
509interface DateLocale {
510 min?: Message<{
511 min: Date | string;
512 }>;
513 max?: Message<{
514 max: Date | string;
515 }>;
516}
517interface ObjectLocale {
518 noUnknown?: Message;
519}
520interface ArrayLocale {
521 length?: Message<{
522 length: number;
523 }>;
524 min?: Message<{
525 min: number;
526 }>;
527 max?: Message<{
528 max: number;
529 }>;
530}
531interface BooleanLocale {
532 isValue?: Message;
533}
534interface LocaleObject {
535 mixed?: MixedLocale;
536 string?: StringLocale;
537 number?: NumberLocale;
538 date?: DateLocale;
539 boolean?: BooleanLocale;
540 object?: ObjectLocale;
541 array?: ArrayLocale;
542}
543
544declare type MatchOptions = {
545 excludeEmptyString?: boolean;
546 message: Message<{
547 regex: RegExp;
548 }>;
549 name?: string;
550};
551declare function create$6(): StringSchema;
552declare function create$6<T extends string, TContext extends Maybe<AnyObject> = AnyObject>(): StringSchema<T | undefined, TContext>;
553declare namespace create$6 {
554 var prototype: StringSchema<any, any, any, any>;
555}
556
557declare class StringSchema<TType extends Maybe<string> = string | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
558 constructor();
559 length(length: number | Reference<number>, message?: Message<{
560 length: number;
561 }>): this;
562 min(min: number | Reference<number>, message?: Message<{
563 min: number;
564 }>): this;
565 max(max: number | Reference<number>, message?: Message<{
566 max: number;
567 }>): this;
568 matches(regex: RegExp, options?: MatchOptions | MatchOptions['message']): this;
569 email(message?: Message<{
570 regex: RegExp;
571 }>): this;
572 url(message?: Message<{
573 regex: RegExp;
574 }>): this;
575 uuid(message?: Message<{
576 regex: RegExp;
577 }>): this;
578 ensure(): StringSchema<NonNullable<TType>>;
579 trim(message?: Message<any>): this;
580 lowercase(message?: Message<any>): this;
581 uppercase(message?: Message<any>): this;
582}
583interface StringSchema<TType extends Maybe<string> = string | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
584 default<D extends Maybe<TType>>(def: Thunk<D>): StringSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;
585 oneOf<U extends TType>(arrayOfValues: ReadonlyArray<U | Reference<U>>, message?: MixedLocale['oneOf']): StringSchema<U | Optionals<TType>, TContext, TDefault, TFlags>;
586 oneOf(enums: ReadonlyArray<TType | Reference>, message?: Message<{
587 values: any;
588 }>): this;
589 concat<UType extends Maybe<string>, UContext, UDefault, UFlags extends Flags>(schema: StringSchema<UType, UContext, UDefault, UFlags>): StringSchema<Concat<TType, UType>, TContext & UContext, UDefault, TFlags | UFlags>;
590 concat(schema: this): this;
591 defined(msg?: Message): StringSchema<Defined<TType>, TContext, TDefault, TFlags>;
592 optional(): StringSchema<TType | undefined, TContext, TDefault, TFlags>;
593 required(msg?: Message): StringSchema<NonNullable<TType>, TContext, TDefault, TFlags>;
594 notRequired(): StringSchema<Maybe<TType>, TContext, TDefault, TFlags>;
595 nullable(msg?: Message<any>): StringSchema<TType | null, TContext, TDefault, TFlags>;
596 nonNullable(): StringSchema<NotNull<TType>, TContext, TDefault, TFlags>;
597 strip(enabled: false): StringSchema<TType, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
598 strip(enabled?: true): StringSchema<TType, TContext, TDefault, SetFlag<TFlags, 's'>>;
599}
600
601declare function create$5(): NumberSchema;
602declare function create$5<T extends number, TContext extends Maybe<AnyObject> = AnyObject>(): NumberSchema<T | undefined, TContext>;
603declare namespace create$5 {
604 var prototype: NumberSchema<any, any, any, any>;
605}
606declare class NumberSchema<TType extends Maybe<number> = number | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
607 constructor();
608 min(min: number | Reference<number>, message?: Message<{
609 min: number;
610 }>): this;
611 max(max: number | Reference<number>, message?: Message<{
612 max: number;
613 }>): this;
614 lessThan(less: number | Reference<number>, message?: Message<{
615 less: number;
616 }>): this;
617 moreThan(more: number | Reference<number>, message?: Message<{
618 more: number;
619 }>): this;
620 positive(msg?: Message<{
621 more: number;
622 }>): this;
623 negative(msg?: Message<{
624 less: number;
625 }>): this;
626 integer(message?: Message<any>): this;
627 truncate(): this;
628 round(method?: 'ceil' | 'floor' | 'round' | 'trunc'): this;
629}
630interface NumberSchema<TType extends Maybe<number> = number | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
631 default<D extends Maybe<TType>>(def: Thunk<D>): NumberSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;
632 concat<UType extends Maybe<number>, UContext, UFlags extends Flags, UDefault>(schema: NumberSchema<UType, UContext, UDefault, UFlags>): NumberSchema<Concat<TType, UType>, TContext & UContext, UDefault, TFlags | UFlags>;
633 concat(schema: this): this;
634 defined(msg?: Message): NumberSchema<Defined<TType>, TContext, TDefault, TFlags>;
635 optional(): NumberSchema<TType | undefined, TContext, TDefault, TFlags>;
636 required(msg?: Message): NumberSchema<NonNullable<TType>, TContext, TDefault, TFlags>;
637 notRequired(): NumberSchema<Maybe<TType>, TContext, TDefault, TFlags>;
638 nullable(msg?: Message): NumberSchema<TType | null, TContext, TDefault, TFlags>;
639 nonNullable(): NumberSchema<NotNull<TType>, TContext, TDefault, TFlags>;
640 strip(enabled: false): NumberSchema<TType, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
641 strip(enabled?: true): NumberSchema<TType, TContext, TDefault, SetFlag<TFlags, 's'>>;
642}
643
644declare function create$4(): DateSchema;
645declare function create$4<T extends Date, TContext extends Maybe<AnyObject> = AnyObject>(): DateSchema<T | undefined, TContext>;
646declare namespace create$4 {
647 var prototype: DateSchema<any, any, any, any>;
648 var INVALID_DATE: Date;
649}
650declare class DateSchema<TType extends Maybe<Date> = Date | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
651 static INVALID_DATE: Date;
652 constructor();
653 private prepareParam;
654 min(min: unknown | Reference<Date>, message?: Message<{
655 min: string | Date;
656 }>): this;
657 max(max: unknown | Reference, message?: Message<{
658 max: string | Date;
659 }>): this;
660}
661interface DateSchema<TType extends Maybe<Date>, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
662 default<D extends Maybe<TType>>(def: Thunk<D>): DateSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;
663 concat<TOther extends DateSchema<any, any>>(schema: TOther): TOther;
664 defined(msg?: Message): DateSchema<Defined<TType>, TContext, TDefault, TFlags>;
665 optional(): DateSchema<TType | undefined, TContext, TDefault, TFlags>;
666 required(msg?: Message): DateSchema<NonNullable<TType>, TContext, TDefault, TFlags>;
667 notRequired(): DateSchema<Maybe<TType>, TContext, TDefault, TFlags>;
668 nullable(msg?: Message): DateSchema<TType | null, TContext, TDefault, TFlags>;
669 nonNullable(): DateSchema<NotNull<TType>, TContext, TDefault, TFlags>;
670 strip(enabled: false): DateSchema<TType, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
671 strip(enabled?: true): DateSchema<TType, TContext, TDefault, SetFlag<TFlags, 's'>>;
672}
673
674declare type MakeKeysOptional<T> = T extends AnyObject ? _<MakePartial<T>> : T;
675declare type Shape<T extends Maybe<AnyObject>, C = any> = {
676 [field in keyof T]-?: ISchema<T[field], C> | Reference;
677};
678declare type ObjectSchemaSpec = SchemaSpec<any> & {
679 noUnknown?: boolean;
680};
681declare function create$3<C extends Maybe<AnyObject> = AnyObject, S extends ObjectShape = {}>(spec?: S): ObjectSchema<_<TypeFromShape<S, C>>, C, _<DefaultFromShape<S>>, "">;
682declare namespace create$3 {
683 var prototype: ObjectSchema<any, any, any, any>;
684}
685interface ObjectSchema<TIn extends Maybe<AnyObject>, TContext = AnyObject, TDefault = any, TFlags extends Flags = ''> extends Schema<MakeKeysOptional<TIn>, TContext, TDefault, TFlags> {
686 default<D extends Maybe<AnyObject>>(def: Thunk<D>): ObjectSchema<TIn, TContext, D, ToggleDefault<TFlags, 'd'>>;
687 defined(msg?: Message): ObjectSchema<Defined<TIn>, TContext, TDefault, TFlags>;
688 optional(): ObjectSchema<TIn | undefined, TContext, TDefault, TFlags>;
689 required(msg?: Message): ObjectSchema<NonNullable<TIn>, TContext, TDefault, TFlags>;
690 notRequired(): ObjectSchema<Maybe<TIn>, TContext, TDefault, TFlags>;
691 nullable(msg?: Message): ObjectSchema<TIn | null, TContext, TDefault, TFlags>;
692 nonNullable(): ObjectSchema<NotNull<TIn>, TContext, TDefault, TFlags>;
693 strip(enabled: false): ObjectSchema<TIn, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
694 strip(enabled?: true): ObjectSchema<TIn, TContext, TDefault, SetFlag<TFlags, 's'>>;
695}
696declare class ObjectSchema<TIn extends Maybe<AnyObject>, TContext = AnyObject, TDefault = any, TFlags extends Flags = ''> extends Schema<MakeKeysOptional<TIn>, TContext, TDefault, TFlags> {
697 fields: Shape<NonNullable<TIn>, TContext>;
698 spec: ObjectSchemaSpec;
699 private _sortErrors;
700 private _nodes;
701 private _excludedEdges;
702 constructor(spec?: Shape<TIn, TContext>);
703 protected _cast(_value: any, options?: InternalOptions<TContext>): any;
704 protected _validate(_value: any, options: InternalOptions<TContext> | undefined, panic: (err: Error, value: unknown) => void, next: (err: ValidationError[], value: unknown) => void): void;
705 clone(spec?: ObjectSchemaSpec): this;
706 concat<IIn extends Maybe<AnyObject>, IC, ID, IF extends Flags>(schema: ObjectSchema<IIn, IC, ID, IF>): ObjectSchema<ConcatObjectTypes<TIn, IIn>, TContext & IC, Extract<IF, 'd'> extends never ? TDefault extends AnyObject ? ID extends AnyObject ? _<ConcatObjectTypes<TDefault, ID>> : ID : ID : ID, TFlags | IF>;
707 concat(schema: this): this;
708 protected _getDefault(): any;
709 private setFields;
710 shape<U extends ObjectShape>(additions: U, excludes?: readonly [string, string][]): ObjectSchema<_<{ [P in keyof TIn]: P extends keyof U ? TypeFromShape<U, TContext>[P] : TIn[P]; } & TypeFromShape<U, TContext>> | _<Extract<TIn, null | undefined>>, TContext, Extract<TFlags, "d"> extends never ? _<TDefault & DefaultFromShape<U>> : TDefault, TFlags>;
711 partial(): ObjectSchema<Partial<TIn>, TContext, TDefault, TFlags>;
712 deepPartial(): ObjectSchema<PartialDeep<TIn>, TContext, TDefault, TFlags>;
713 pick<TKey extends keyof TIn>(keys: readonly TKey[]): ObjectSchema<{ [K in TKey]: TIn[K]; }, TContext, TDefault, TFlags>;
714 omit<TKey extends keyof TIn>(keys: readonly TKey[]): ObjectSchema<Omit<TIn, TKey>, TContext, TDefault, TFlags>;
715 from(from: string, to: keyof TIn, alias?: boolean): this;
716 /** Parse an input JSON string to an object */
717 json(): this;
718 noUnknown(message?: Message): this;
719 noUnknown(noAllow: boolean, message?: Message): this;
720 unknown(allow?: boolean, message?: Message<any>): this;
721 transformKeys(fn: (key: string) => string): this;
722 camelCase(): this;
723 snakeCase(): this;
724 constantCase(): this;
725 describe(options?: ResolveOptions<TContext>): SchemaObjectDescription;
726}
727
728declare type InnerType<T> = T extends Array<infer I> ? I : never;
729declare type RejectorFn = (value: any, index: number, array: readonly any[]) => boolean;
730declare function create$2<C extends Maybe<AnyObject> = AnyObject, T = any>(type?: ISchema<T, C>): ArraySchema<T[] | undefined, C, undefined, "">;
731declare namespace create$2 {
732 var prototype: ArraySchema<any, any, any, any>;
733}
734declare class ArraySchema<TIn extends any[] | null | undefined, TContext, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TIn, TContext, TDefault, TFlags> {
735 readonly innerType?: ISchema<InnerType<TIn>, TContext>;
736 constructor(type?: ISchema<InnerType<TIn>, TContext>);
737 protected _cast(_value: any, _opts: InternalOptions<TContext>): any;
738 protected _validate(_value: any, options: InternalOptions<TContext> | undefined, panic: (err: Error, value: unknown) => void, next: (err: ValidationError[], value: unknown) => void): void;
739 clone(spec?: SchemaSpec<any>): this;
740 /** Parse an input JSON string to an object */
741 json(): this;
742 concat<IIn extends Maybe<any[]>, IC, ID, IF extends Flags>(schema: ArraySchema<IIn, IC, ID, IF>): ArraySchema<Concat<TIn, IIn>, TContext & IC, Extract<IF, 'd'> extends never ? TDefault : ID, TFlags | IF>;
743 concat(schema: this): this;
744 of<U>(schema: ISchema<U, TContext>): ArraySchema<U[] | Optionals<TIn>, TContext, TFlags>;
745 length(length: number | Reference<number>, message?: Message<{
746 length: number;
747 }>): this;
748 min(min: number | Reference<number>, message?: Message<{
749 min: number;
750 }>): this;
751 max(max: number | Reference<number>, message?: Message<{
752 max: number;
753 }>): this;
754 ensure(): ArraySchema<TIn, TContext, TIn, ToggleDefault<TFlags, TIn>>;
755 compact(rejector?: RejectorFn): this;
756 describe(options?: ResolveOptions<TContext>): SchemaInnerTypeDescription;
757}
758interface ArraySchema<TIn extends any[] | null | undefined, TContext, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TIn, TContext, TDefault, TFlags> {
759 default<D extends Maybe<TIn>>(def: Thunk<D>): ArraySchema<TIn, TContext, D, ToggleDefault<TFlags, D>>;
760 defined(msg?: Message): ArraySchema<Defined<TIn>, TContext, TDefault, TFlags>;
761 optional(): ArraySchema<TIn | undefined, TContext, TDefault, TFlags>;
762 required(msg?: Message): ArraySchema<NonNullable<TIn>, TContext, TDefault, TFlags>;
763 notRequired(): ArraySchema<Maybe<TIn>, TContext, TDefault, TFlags>;
764 nullable(msg?: Message): ArraySchema<TIn | null, TContext, TDefault, TFlags>;
765 nonNullable(): ArraySchema<NotNull<TIn>, TContext, TDefault, TFlags>;
766 strip(enabled: false): ArraySchema<TIn, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
767 strip(enabled?: true): ArraySchema<TIn, TContext, TDefault, SetFlag<TFlags, 's'>>;
768}
769
770declare type AnyTuple = [unknown, ...unknown[]];
771declare function create$1<T extends AnyTuple>(schemas: {
772 [K in keyof T]: ISchema<T[K]>;
773}): TupleSchema<T | undefined, AnyObject, undefined, "">;
774declare namespace create$1 {
775 var prototype: TupleSchema<any, any, any, any>;
776}
777interface TupleSchemaSpec<T> extends SchemaSpec<any> {
778 types: T extends any[] ? {
779 [K in keyof T]: ISchema<T[K]>;
780 } : never;
781}
782interface TupleSchema<TType extends Maybe<AnyTuple> = AnyTuple | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
783 default<D extends Maybe<TType>>(def: Thunk<D>): TupleSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;
784 concat<TOther extends TupleSchema<any, any>>(schema: TOther): TOther;
785 defined(msg?: Message): TupleSchema<Defined<TType>, TContext, TDefault, TFlags>;
786 optional(): TupleSchema<TType | undefined, TContext, TDefault, TFlags>;
787 required(msg?: Message): TupleSchema<NonNullable<TType>, TContext, TDefault, TFlags>;
788 notRequired(): TupleSchema<Maybe<TType>, TContext, TDefault, TFlags>;
789 nullable(msg?: Message): TupleSchema<TType | null, TContext, TDefault, TFlags>;
790 nonNullable(): TupleSchema<NotNull<TType>, TContext, TDefault, TFlags>;
791 strip(enabled: false): TupleSchema<TType, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
792 strip(enabled?: true): TupleSchema<TType, TContext, TDefault, SetFlag<TFlags, 's'>>;
793}
794declare class TupleSchema<TType extends Maybe<AnyTuple> = AnyTuple | undefined, TContext = AnyObject, TDefault = undefined, TFlags extends Flags = ''> extends Schema<TType, TContext, TDefault, TFlags> {
795 spec: TupleSchemaSpec<TType>;
796 constructor(schemas: [ISchema<any>, ...ISchema<any>[]]);
797 protected _cast(inputValue: any, options: InternalOptions<TContext>): any;
798 protected _validate(_value: any, options: InternalOptions<TContext> | undefined, panic: (err: Error, value: unknown) => void, next: (err: ValidationError[], value: unknown) => void): void;
799}
800
801declare function create<TSchema extends ISchema<any, TContext>, TContext extends Maybe<AnyObject> = AnyObject>(builder: (value: any, options: ResolveOptions<TContext>) => TSchema): Lazy<InferType<TSchema>, TContext, any>;
802interface LazySpec {
803 meta: Record<string, unknown> | undefined;
804 optional: boolean;
805}
806declare class Lazy<T, TContext = AnyObject, TFlags extends Flags = any> implements ISchema<T, TContext, TFlags, undefined> {
807 private builder;
808 type: "lazy";
809 __isYupSchema__: boolean;
810 readonly __outputType: T;
811 readonly __context: TContext;
812 readonly __flags: TFlags;
813 readonly __default: undefined;
814 spec: LazySpec;
815 constructor(builder: any);
816 clone(spec?: Partial<LazySpec>): Lazy<T, TContext, TFlags>;
817 private _resolve;
818 private optionality;
819 optional(): Lazy<T | undefined, TContext, TFlags>;
820 resolve(options: ResolveOptions<TContext>): Schema<T, TContext, undefined, TFlags>;
821 cast(value: any, options?: CastOptions$1<TContext>): T;
822 cast(value: any, options?: CastOptionalityOptions<TContext>): T | null | undefined;
823 asNestedTest(config: NestedTestConfig): RunTest;
824 validate(value: any, options?: ValidateOptions<TContext>): Promise<T>;
825 validateSync(value: any, options?: ValidateOptions<TContext>): T;
826 validateAt(path: string, value: any, options?: ValidateOptions<TContext>): Promise<any>;
827 validateSyncAt(path: string, value: any, options?: ValidateOptions<TContext>): any;
828 isValid(value: any, options?: ValidateOptions<TContext>): Promise<boolean>;
829 isValidSync(value: any, options?: ValidateOptions<TContext>): boolean;
830 describe(options?: ResolveOptions<TContext>): SchemaLazyDescription | SchemaFieldDescription;
831 meta(): Record<string, unknown> | undefined;
832 meta(obj: Record<string, unknown>): Lazy<T, TContext, TFlags>;
833}
834
835declare function getIn<C = any>(schema: any, path: string, value?: any, context?: C): {
836 schema: ISchema<any> | Reference<any>;
837 parent: any;
838 parentPath: string;
839};
840declare function reach<P extends string, S extends ISchema<any>>(obj: S, path: P, value?: any, context?: any): Reference<Get<InferType<S>, P>> | ISchema<Get<InferType<S>, P>, S['__context']>;
841
842declare const isSchema: (obj: any) => obj is ISchema<any, any, any, any>;
843
844declare function setLocale(custom: LocaleObject): void;
845
846declare function addMethod<T extends AnySchema>(schemaType: (...arg: any[]) => T, name: string, fn: (this: T, ...args: any[]) => T): void;
847declare function addMethod<T extends new (...args: any) => AnySchema>(schemaType: T, name: string, fn: (this: InstanceType<T>, ...args: any[]) => InstanceType<T>): void;
848declare type AnyObjectSchema = ObjectSchema<any, any, any, any>;
849declare type CastOptions = Omit<CastOptions$1, 'path' | 'resolved'>;
850
851export { AnyObject, AnyObjectSchema, AnySchema, ArraySchema, InferType as Asserts, BooleanSchema, CastOptions, CreateErrorOptions, DateSchema, DefaultFromShape, Defined, Flags, ISchema, InferType, LocaleObject, MakePartial, Maybe, Message, MixedOptions, MixedSchema, TypeGuard as MixedTypeGuard, NotNull, NumberSchema, ObjectSchema, ObjectShape, Optionals, Schema, SchemaDescription, SchemaFieldDescription, SchemaInnerTypeDescription, SchemaLazyDescription, SchemaObjectDescription, SchemaRefDescription, SetFlag, StringSchema, TestConfig, TestContext, TestFunction, TestOptions, Thunk, ToggleDefault, TupleSchema, TypeFromShape, UnsetFlag, ValidateOptions, ValidationError, addMethod, create$2 as array, create$7 as bool, create$7 as boolean, create$4 as date, getIn, isSchema, create as lazy, create$8 as mixed, create$5 as number, create$3 as object, reach, create$9 as ref, setLocale, create$6 as string, create$1 as tuple };