UNPKG

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