1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | declare namespace Joi {
|
33 | type Types =
|
34 | | 'any'
|
35 | | 'alternatives'
|
36 | | 'array'
|
37 | | 'boolean'
|
38 | | 'binary'
|
39 | | 'date'
|
40 | | 'function'
|
41 | | 'link'
|
42 | | 'number'
|
43 | | 'object'
|
44 | | 'string'
|
45 | | 'symbol';
|
46 |
|
47 | type BasicType = boolean|number|string|any[]|object|null;
|
48 |
|
49 | type LanguageMessages = Record<string, string>;
|
50 |
|
51 | type PresenceMode = 'optional' | 'required' | 'forbidden';
|
52 |
|
53 | interface ErrorFormattingOptions {
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 | escapeHtml?: boolean | undefined;
|
60 | |
61 |
|
62 |
|
63 | label?: 'path' | 'key' | false | undefined;
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | language?: keyof LanguageMessages | undefined;
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | render?: boolean | undefined;
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | stack?: boolean | undefined;
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | wrap?: {
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 | label?: string|false | undefined,
|
98 |
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 | array?: string|false | undefined
|
105 | } | undefined;
|
106 | }
|
107 |
|
108 | interface BaseValidationOptions {
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | abortEarly?: boolean | undefined;
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 | allowUnknown?: boolean | undefined;
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | cache?: boolean | undefined;
|
127 | |
128 |
|
129 |
|
130 | context?: Context | undefined;
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 | convert?: boolean | undefined;
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | dateFormat?: 'date' | 'iso' | 'string' | 'time' | 'utc' | undefined;
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 | debug?: boolean | undefined;
|
149 | |
150 |
|
151 |
|
152 | errors?: ErrorFormattingOptions | undefined;
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 | externals?: boolean | undefined;
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 | noDefaults?: boolean | undefined;
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 | nonEnumerables?: boolean | undefined;
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 | presence?: PresenceMode | undefined;
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 | skipFunctions?: boolean | undefined;
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | stripUnknown?: boolean | { arrays?: boolean | undefined; objects?: boolean | undefined } | undefined;
|
192 | }
|
193 |
|
194 | interface ValidationOptions extends BaseValidationOptions {
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | messages?: LanguageMessages | undefined;
|
203 | }
|
204 |
|
205 | interface AsyncValidationOptions extends ValidationOptions {
|
206 | |
207 |
|
208 |
|
209 |
|
210 |
|
211 | warnings?: boolean | undefined;
|
212 | }
|
213 |
|
214 | interface LanguageMessageTemplate {
|
215 | source: string;
|
216 | rendered: string;
|
217 | }
|
218 |
|
219 | interface ErrorValidationOptions extends BaseValidationOptions {
|
220 | messages?: Record<string, LanguageMessageTemplate> | undefined;
|
221 | }
|
222 |
|
223 | interface RenameOptions {
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 | alias?: boolean | undefined;
|
230 | |
231 |
|
232 |
|
233 |
|
234 |
|
235 | multiple?: boolean | undefined;
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 | override?: boolean | undefined;
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 | ignoreUndefined?: boolean | undefined;
|
248 | }
|
249 |
|
250 | interface TopLevelDomainOptions {
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 | allow?: Set<string> | string[] | boolean | undefined;
|
257 | |
258 |
|
259 |
|
260 | deny?: Set<string> | string[] | undefined;
|
261 | }
|
262 |
|
263 | interface HierarchySeparatorOptions {
|
264 | |
265 |
|
266 |
|
267 |
|
268 |
|
269 | separator?: string | false | undefined;
|
270 | }
|
271 |
|
272 | interface EmailOptions {
|
273 | |
274 |
|
275 |
|
276 |
|
277 |
|
278 | allowUnicode?: boolean | undefined;
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 | ignoreLength?: boolean | undefined;
|
285 | |
286 |
|
287 |
|
288 |
|
289 |
|
290 | multiple?: boolean | undefined;
|
291 | |
292 |
|
293 |
|
294 |
|
295 |
|
296 | separator?: string | string[] | undefined;
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 | tlds?: TopLevelDomainOptions | false | undefined;
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 | minDomainSegments?: number | undefined;
|
309 | }
|
310 |
|
311 | interface DomainOptions {
|
312 | |
313 |
|
314 |
|
315 |
|
316 |
|
317 | allowUnicode?: boolean | undefined;
|
318 |
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 | tlds?: TopLevelDomainOptions | false | undefined;
|
325 | |
326 |
|
327 |
|
328 |
|
329 |
|
330 | minDomainSegments?: number | undefined;
|
331 | }
|
332 |
|
333 | interface HexOptions {
|
334 | |
335 |
|
336 |
|
337 |
|
338 | byteAligned?: boolean | undefined;
|
339 | }
|
340 |
|
341 | interface IpOptions {
|
342 | |
343 |
|
344 |
|
345 | version?: string | string[] | undefined;
|
346 | |
347 |
|
348 |
|
349 | cidr?: PresenceMode | undefined;
|
350 | }
|
351 |
|
352 | type GuidVersions = 'uuidv1' | 'uuidv2' | 'uuidv3' | 'uuidv4' | 'uuidv5';
|
353 |
|
354 | interface GuidOptions {
|
355 | version: GuidVersions[] | GuidVersions;
|
356 | }
|
357 |
|
358 | interface UriOptions {
|
359 | |
360 |
|
361 |
|
362 |
|
363 | scheme?: string | RegExp | Array<string | RegExp> | undefined;
|
364 | |
365 |
|
366 |
|
367 |
|
368 |
|
369 | allowRelative?: boolean | undefined;
|
370 | |
371 |
|
372 |
|
373 |
|
374 |
|
375 | relativeOnly?: boolean | undefined;
|
376 | |
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 | allowQuerySquareBrackets?: boolean | undefined;
|
383 | |
384 |
|
385 |
|
386 | domain?: DomainOptions | undefined;
|
387 | }
|
388 |
|
389 | interface DataUriOptions {
|
390 | |
391 |
|
392 |
|
393 |
|
394 |
|
395 | paddingRequired?: boolean | undefined;
|
396 | }
|
397 |
|
398 | interface Base64Options extends Pick<DataUriOptions, 'paddingRequired'> {
|
399 | |
400 |
|
401 |
|
402 |
|
403 |
|
404 | urlSafe?: boolean | undefined;
|
405 | }
|
406 |
|
407 | interface SwitchCases {
|
408 | |
409 |
|
410 |
|
411 | is: SchemaLike;
|
412 | |
413 |
|
414 |
|
415 | then: SchemaLike;
|
416 | }
|
417 |
|
418 | interface SwitchDefault {
|
419 | |
420 |
|
421 |
|
422 |
|
423 | otherwise: SchemaLike;
|
424 | }
|
425 |
|
426 | interface WhenOptions {
|
427 | |
428 |
|
429 |
|
430 | is?: SchemaLike | undefined;
|
431 |
|
432 | |
433 |
|
434 |
|
435 |
|
436 | not?: SchemaLike | undefined;
|
437 |
|
438 | |
439 |
|
440 |
|
441 | then?: SchemaLike | undefined;
|
442 |
|
443 | |
444 |
|
445 |
|
446 | otherwise?: SchemaLike | undefined;
|
447 |
|
448 | |
449 |
|
450 |
|
451 | switch?: Array<SwitchCases | SwitchDefault> | undefined;
|
452 |
|
453 | |
454 |
|
455 |
|
456 | break?: boolean | undefined;
|
457 | }
|
458 |
|
459 | interface WhenSchemaOptions {
|
460 | |
461 |
|
462 |
|
463 | then?: SchemaLike | undefined;
|
464 | |
465 |
|
466 |
|
467 | otherwise?: SchemaLike | undefined;
|
468 | }
|
469 |
|
470 | interface Cache {
|
471 | |
472 |
|
473 |
|
474 |
|
475 |
|
476 | set(key: any, value: any): void;
|
477 |
|
478 | |
479 |
|
480 |
|
481 |
|
482 |
|
483 | get(key: any): any;
|
484 | }
|
485 | interface CacheProvisionOptions {
|
486 | |
487 |
|
488 |
|
489 |
|
490 |
|
491 | max: number;
|
492 | }
|
493 |
|
494 | interface CacheConfiguration {
|
495 | |
496 |
|
497 |
|
498 | provision(options?: CacheProvisionOptions): void;
|
499 | }
|
500 |
|
501 | interface CompileOptions {
|
502 | |
503 |
|
504 |
|
505 |
|
506 | legacy: boolean;
|
507 | }
|
508 |
|
509 | interface IsSchemaOptions {
|
510 | |
511 |
|
512 |
|
513 |
|
514 |
|
515 | legacy: boolean;
|
516 | }
|
517 |
|
518 | interface ReferenceOptions extends HierarchySeparatorOptions {
|
519 | |
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 | adjust?: ((value: any) => any) | undefined;
|
527 |
|
528 | /**
|
529 | * an array of array pairs using the format `[[key, value], [key, value]]` used to maps the resolved reference value to another value.
|
530 | * If the resolved value is not in the map, it is returned as-is.
|
531 | * Cannot be used with `adjust`.
|
532 | */
|
533 | map?: Array<[any, any]> | undefined;
|
534 |
|
535 | /**
|
536 | * overrides default prefix characters.
|
537 | */
|
538 | prefix?: {
|
539 | /**
|
540 | * references to the globally provided context preference.
|
541 | *
|
542 | * @default '$'
|
543 | */
|
544 | global?: string | undefined;
|
545 |
|
546 | /**
|
547 | * references to error-specific or rule specific context.
|
548 | *
|
549 | * @default '#'
|
550 | */
|
551 | local?: string | undefined;
|
552 |
|
553 | /**
|
554 | * references to the root value being validated.
|
555 | *
|
556 | * @default '/'
|
557 | */
|
558 | root?: string | undefined;
|
559 | } | undefined;
|
560 |
|
561 | /**
|
562 | * If set to a number, sets the reference relative starting point.
|
563 | * Cannot be combined with separator prefix characters.
|
564 | * Defaults to the reference key prefix (or 1 if none present)
|
565 | */
|
566 | ancestor?: number | undefined;
|
567 |
|
568 | /**
|
569 | * creates an in-reference.
|
570 | */
|
571 | in?: boolean | undefined;
|
572 |
|
573 | /**
|
574 | * when true, the reference resolves by reaching into maps and sets.
|
575 | */
|
576 | iterables?: boolean | undefined;
|
577 | }
|
578 |
|
579 | interface StringRegexOptions {
|
580 | /**
|
581 | * optional pattern name.
|
582 | */
|
583 | name?: string | undefined;
|
584 |
|
585 | /**
|
586 | * when true, the provided pattern will be disallowed instead of required.
|
587 | *
|
588 | * @default false
|
589 | */
|
590 | invert?: boolean | undefined;
|
591 | }
|
592 |
|
593 | interface RuleOptions {
|
594 | /**
|
595 | * if true, the rules will not be replaced by the same unique rule later.
|
596 | *
|
597 | * For example, `Joi.number().min(1).rule({ keep: true }).min(2)` will keep both `min()` rules instead of the later rule overriding the first.
|
598 | *
|
599 | * @default false
|
600 | */
|
601 | keep?: boolean | undefined;
|
602 |
|
603 | /**
|
604 | * a single message string or a messages object where each key is an error code and corresponding message string as value.
|
605 | *
|
606 | * The object is the same as the messages used as an option in `any.validate()`.
|
607 | * The strings can be plain messages or a message template.
|
608 | */
|
609 | message?: string | LanguageMessages | undefined;
|
610 |
|
611 | /**
|
612 | * if true, turns any error generated by the ruleset to warnings.
|
613 | */
|
614 | warn?: boolean | undefined;
|
615 | }
|
616 |
|
617 | interface ErrorReport extends Error {
|
618 | code: string;
|
619 | flags: Record<string, ExtensionFlag>;
|
620 | path: string[];
|
621 | prefs: ErrorValidationOptions;
|
622 | messages: LanguageMessages;
|
623 | state: State;
|
624 | value: any;
|
625 | }
|
626 |
|
627 | interface ValidationError extends Error {
|
628 | name: 'ValidationError';
|
629 |
|
630 | isJoi: boolean;
|
631 |
|
632 | /**
|
633 | * array of errors.
|
634 | */
|
635 | details: ValidationErrorItem[];
|
636 |
|
637 | /**
|
638 | * function that returns a string with an annotated version of the object pointing at the places where errors occurred.
|
639 | *
|
640 | * NOTE: This method does not exist in browser builds of Joi
|
641 | *
|
642 | * @param stripColors - if truthy, will strip the colors out of the output.
|
643 | */
|
644 | annotate(stripColors?: boolean): string;
|
645 |
|
646 | _object: any;
|
647 | }
|
648 |
|
649 | interface ValidationErrorItem {
|
650 | message: string;
|
651 | path: Array<string | number>;
|
652 | type: string;
|
653 | context?: Context | undefined;
|
654 | }
|
655 |
|
656 | type ValidationErrorFunction = (errors: ErrorReport[]) => string | ValidationErrorItem | Error;
|
657 |
|
658 | interface ValidationResult {
|
659 | error?: ValidationError | undefined;
|
660 | errors?: ValidationError | undefined;
|
661 | warning?: ValidationError | undefined;
|
662 | value: any;
|
663 | }
|
664 |
|
665 | interface CreateErrorOptions {
|
666 | flags?: boolean | undefined;
|
667 | messages?: LanguageMessages | undefined;
|
668 | }
|
669 |
|
670 | interface ModifyOptions {
|
671 | each?: boolean | undefined;
|
672 | once?: boolean | undefined;
|
673 | ref?: boolean | undefined;
|
674 | schema?: boolean | undefined;
|
675 | }
|
676 |
|
677 | interface MutateRegisterOptions {
|
678 | family?: any;
|
679 | key?: any;
|
680 | }
|
681 |
|
682 | interface SetFlagOptions {
|
683 | clone: boolean;
|
684 | }
|
685 |
|
686 | interface CustomHelpers<V = any> {
|
687 | schema: ExtensionBoundSchema;
|
688 | state: State;
|
689 | prefs: ValidationOptions;
|
690 | original: V;
|
691 | warn: (code: string, local?: Context) => void;
|
692 | error: (code: string, local?: Context) => ErrorReport;
|
693 | message: (messages: LanguageMessages, local?: Context) => ErrorReport;
|
694 | }
|
695 |
|
696 | type CustomValidator<V = any> = (value: V, helpers: CustomHelpers) => V;
|
697 |
|
698 | type ExternalValidationFunction = (value: any) => any;
|
699 |
|
700 | type SchemaLike = string | number | boolean | object | null | Schema | SchemaMap;
|
701 |
|
702 | type SchemaMap<TSchema = any> = {
|
703 | [key in keyof TSchema]?: SchemaLike | SchemaLike[];
|
704 | };
|
705 |
|
706 | type Schema =
|
707 | | AnySchema
|
708 | | ArraySchema
|
709 | | AlternativesSchema
|
710 | | BinarySchema
|
711 | | BooleanSchema
|
712 | | DateSchema
|
713 | | FunctionSchema
|
714 | | NumberSchema
|
715 | | ObjectSchema
|
716 | | StringSchema
|
717 | | LinkSchema
|
718 | | SymbolSchema;
|
719 |
|
720 | type SchemaFunction = (schema: Schema) => Schema;
|
721 |
|
722 | interface AddRuleOptions {
|
723 | name: string;
|
724 | args?: {
|
725 | [key: string]: any;
|
726 | } | undefined;
|
727 | }
|
728 |
|
729 | interface GetRuleOptions {
|
730 | args?: Record<string, any> | undefined;
|
731 | method?: string | undefined;
|
732 | name: string;
|
733 | operator?: string | undefined;
|
734 | }
|
735 |
|
736 | interface SchemaInternals {
|
737 | |
738 |
|
739 |
|
740 | $_super: Schema;
|
741 |
|
742 | |
743 |
|
744 |
|
745 | $_terms: Record<string, any>;
|
746 |
|
747 | |
748 |
|
749 |
|
750 | $_addRule(rule: string | AddRuleOptions): Schema;
|
751 |
|
752 | |
753 |
|
754 |
|
755 | $_compile(schema: SchemaLike, options?: CompileOptions): Schema;
|
756 |
|
757 | |
758 |
|
759 |
|
760 | $_createError(
|
761 | code: string,
|
762 | value: any,
|
763 | context: Context,
|
764 | state: State,
|
765 | prefs: ValidationOptions,
|
766 | options?: CreateErrorOptions,
|
767 | ): Err;
|
768 |
|
769 | |
770 |
|
771 |
|
772 | $_getFlag(name: string): any;
|
773 |
|
774 | |
775 |
|
776 |
|
777 | $_getRule(name: string): GetRuleOptions | undefined;
|
778 |
|
779 | $_mapLabels(path: string | string[]): string;
|
780 |
|
781 | |
782 |
|
783 |
|
784 | $_match(value: any, state: State, prefs: ValidationOptions): boolean;
|
785 |
|
786 | $_modify(options?: ModifyOptions): Schema;
|
787 |
|
788 | |
789 |
|
790 |
|
791 | $_mutateRebuild(): this;
|
792 |
|
793 | $_mutateRegister(schema: Schema, options?: MutateRegisterOptions): void;
|
794 |
|
795 | |
796 |
|
797 |
|
798 | $_property(name: string): any;
|
799 |
|
800 | |
801 |
|
802 |
|
803 | $_reach(path: string[]): Schema;
|
804 |
|
805 | |
806 |
|
807 |
|
808 | $_rootReferences(): any;
|
809 |
|
810 | |
811 |
|
812 |
|
813 | $_setFlag(flag: string, value: any, options?: SetFlagOptions): void;
|
814 |
|
815 | |
816 |
|
817 |
|
818 | $_validate(value: any, state: State, prefs: ValidationOptions): ValidationResult;
|
819 | }
|
820 |
|
821 | interface AnySchema extends SchemaInternals {
|
822 | |
823 |
|
824 |
|
825 | _flags: Record<string, any>;
|
826 |
|
827 | |
828 |
|
829 |
|
830 | $: this;
|
831 |
|
832 | |
833 |
|
834 |
|
835 | ruleset: this;
|
836 |
|
837 | type?: Types | string | undefined;
|
838 |
|
839 | |
840 |
|
841 |
|
842 | allow(...values: any[]): this;
|
843 |
|
844 | |
845 |
|
846 |
|
847 |
|
848 | alter(targets: Record<string, SchemaFunction>): this;
|
849 |
|
850 | |
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 | bind(): this;
|
857 |
|
858 | |
859 |
|
860 |
|
861 |
|
862 | cache(cache?: Cache): this;
|
863 |
|
864 | |
865 |
|
866 |
|
867 | cast(to: 'map' | 'number' | 'set' | 'string'): this;
|
868 |
|
869 | |
870 |
|
871 |
|
872 | concat(schema: this): this;
|
873 |
|
874 | |
875 |
|
876 |
|
877 | custom(fn: CustomValidator, description?: string): this;
|
878 |
|
879 | |
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 | default(value?: BasicType|Reference|((parent: any, helpers: CustomHelpers) => BasicType|Reference)): this;
|
896 |
|
897 | /**
|
898 | * Returns a plain object representing the schema's rules and properties
|
899 | */
|
900 | describe(): Description;
|
901 |
|
902 | /**
|
903 | * Annotates the key
|
904 | */
|
905 | description(desc: string): this;
|
906 |
|
907 | /**
|
908 | * Disallows values.
|
909 | */
|
910 | disallow(...values: any[]): this;
|
911 |
|
912 | /**
|
913 | * Considers anything that matches the schema to be empty (undefined).
|
914 | * @param schema - any object or joi schema to match. An undefined schema unsets that rule.
|
915 | */
|
916 | empty(schema?: SchemaLike): this;
|
917 |
|
918 | /**
|
919 | * Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.
|
920 | */
|
921 | equal(...values: any[]): this;
|
922 |
|
923 | /**
|
924 | * Overrides the default joi error with a custom error if the rule fails where:
|
925 | * @param err - can be:
|
926 | * an instance of `Error` - the override error.
|
927 | * a `function(errors)`, taking an array of errors as argument, where it must either:
|
928 | * return a `string` - substitutes the error message with this text
|
929 | * return a single ` object` or an `Array` of it, where:
|
930 | * `type` - optional parameter providing the type of the error (eg. `number.min`).
|
931 | * `message` - optional parameter if `template` is provided, containing the text of the error.
|
932 | * `template` - optional parameter if `message` is provided, containing a template string, using the same format as usual joi language errors.
|
933 | * `context` - optional parameter, to provide context to your error if you are using the `template`.
|
934 | * return an `Error` - same as when you directly provide an `Error`, but you can customize the error message based on the errors.
|
935 | *
|
936 | * Note that if you provide an `Error`, it will be returned as-is, unmodified and undecorated with any of the
|
937 | * normal joi error properties. If validation fails and another error is found before the error
|
938 | * override, that error will be returned and the override will be ignored (unless the `abortEarly`
|
939 | * option has been set to `false`).
|
940 | */
|
941 | error(err: Error | ValidationErrorFunction): this;
|
942 |
|
943 | /**
|
944 | * Annotates the key with an example value, must be valid.
|
945 | */
|
946 | example(value: any, options?: { override: boolean }): this;
|
947 |
|
948 | /**
|
949 | * Marks a key as required which will not allow undefined as value. All keys are optional by default.
|
950 | */
|
951 | exist(): this;
|
952 |
|
953 | /**
|
954 | * Adds an external validation rule.
|
955 | *
|
956 | * Note that external validation rules are only called after the all other validation rules for the entire schema (from the value root) are checked.
|
957 | * This means that any changes made to the value by the external rules are not available to any other validation rules during the non-external validation phase.
|
958 | * If schema validation failed, no external validation rules are called.
|
959 | */
|
960 | external(method: ExternalValidationFunction, description?: string): this;
|
961 |
|
962 | /**
|
963 | * Returns a sub-schema based on a path of object keys or schema ids.
|
964 | *
|
965 | * @param path - a dot `.` separated path string or a pre-split array of path keys. The keys must match the sub-schema id or object key (if no id was explicitly set).
|
966 | */
|
967 | extract(path: string | string[]): Schema;
|
968 |
|
969 | /**
|
970 | * Sets a failover value if the original value fails passing validation.
|
971 | *
|
972 | * @param value - the failover value. value supports references. value may be assigned a function which returns the default value.
|
973 | *
|
974 | * If value is specified as a function that accepts a single parameter, that parameter will be a context object that can be used to derive the resulting value.
|
975 | * Note that if value is an object, any changes to the object after `failover()` is called will change the reference and any future assignment.
|
976 | * Use a function when setting a dynamic value (e.g. the current time).
|
977 | * Using a function with a single argument performs some internal cloning which has a performance impact.
|
978 | * If you do not need access to the context, define the function without any arguments.
|
979 | */
|
980 | failover(value: any): this;
|
981 |
|
982 | /**
|
983 | * Marks a key as forbidden which will not allow any value except undefined. Used to explicitly forbid keys.
|
984 | */
|
985 | forbidden(): this;
|
986 |
|
987 | /**
|
988 | * Returns a new schema where each of the path keys listed have been modified.
|
989 | *
|
990 | * @param key - an array of key strings, a single key string, or an array of arrays of pre-split key strings.
|
991 | * @param adjuster - a function which must return a modified schema.
|
992 | */
|
993 | fork(key: string | string[] | string[][], adjuster: SchemaFunction): this;
|
994 |
|
995 | /**
|
996 | * Sets a schema id for reaching into the schema via `any.extract()`.
|
997 | * If no id is set, the schema id defaults to the object key it is associated with.
|
998 | * If the schema is used in an array or alternatives type and no id is set, the schema in unreachable.
|
999 | */
|
1000 | id(name?: string): this;
|
1001 |
|
1002 | /**
|
1003 | * Disallows values.
|
1004 | */
|
1005 | invalid(...values: any[]): this;
|
1006 |
|
1007 | /**
|
1008 | * Same as `rule({ keep: true })`.
|
1009 | *
|
1010 | * Note that `keep()` will terminate the current ruleset and cannot be followed by another rule option.
|
1011 | * Use `rule()` to apply multiple rule options.
|
1012 | */
|
1013 | keep(): this;
|
1014 |
|
1015 | /**
|
1016 | * Overrides the key name in error messages.
|
1017 | */
|
1018 | label(name: string): this;
|
1019 |
|
1020 | /**
|
1021 | * Same as `rule({ message })`.
|
1022 | *
|
1023 | * Note that `message()` will terminate the current ruleset and cannot be followed by another rule option.
|
1024 | * Use `rule()` to apply multiple rule options.
|
1025 | */
|
1026 | message(message: string): this;
|
1027 |
|
1028 | /**
|
1029 | * Same as `any.prefs({ messages })`.
|
1030 | * Note that while `any.message()` applies only to the last rule or ruleset, `any.messages()` applies to the entire schema.
|
1031 | */
|
1032 | messages(messages: LanguageMessages): this;
|
1033 |
|
1034 | /**
|
1035 | * Attaches metadata to the key.
|
1036 | */
|
1037 | meta(meta: object): this;
|
1038 |
|
1039 | /**
|
1040 | * Disallows values.
|
1041 | */
|
1042 | not(...values: any[]): this;
|
1043 |
|
1044 | /**
|
1045 | * Annotates the key
|
1046 | */
|
1047 | note(...notes: string[]): this;
|
1048 |
|
1049 | /**
|
1050 | * Requires the validated value to match of the provided `any.allow()` values.
|
1051 | * It has not effect when called together with `any.valid()` since it already sets the requirements.
|
1052 | * When used with `any.allow()` it converts it to an `any.valid()`.
|
1053 | */
|
1054 | only(): this;
|
1055 |
|
1056 | /**
|
1057 | * Marks a key as optional which will allow undefined as values. Used to annotate the schema for readability as all keys are optional by default.
|
1058 | */
|
1059 | optional(): this;
|
1060 |
|
1061 | /**
|
1062 | * Overrides the global validate() options for the current key and any sub-key.
|
1063 | */
|
1064 | options(options: ValidationOptions): this;
|
1065 |
|
1066 | /**
|
1067 | * Overrides the global validate() options for the current key and any sub-key.
|
1068 | */
|
1069 | prefs(options: ValidationOptions): this;
|
1070 |
|
1071 | /**
|
1072 | * Overrides the global validate() options for the current key and any sub-key.
|
1073 | */
|
1074 | preferences(options: ValidationOptions): this;
|
1075 |
|
1076 | /**
|
1077 | * Sets the presence mode for the schema.
|
1078 | */
|
1079 | presence(mode: PresenceMode): this;
|
1080 |
|
1081 | /**
|
1082 | * Outputs the original untouched value instead of the casted value.
|
1083 | */
|
1084 | raw(enabled?: boolean): this;
|
1085 |
|
1086 | /**
|
1087 | * Marks a key as required which will not allow undefined as value. All keys are optional by default.
|
1088 | */
|
1089 | required(): this;
|
1090 |
|
1091 | /**
|
1092 | * Applies a set of rule options to the current ruleset or last rule added.
|
1093 | *
|
1094 | * When applying rule options, the last rule (e.g. `min()`) is used unless there is an active ruleset defined (e.g. `$.min().max()`)
|
1095 | * in which case the options are applied to all the provided rules.
|
1096 | * Once `rule()` is called, the previous rules can no longer be modified and any active ruleset is terminated.
|
1097 | *
|
1098 | * Rule modifications can only be applied to supported rules.
|
1099 | * Most of the `any` methods do not support rule modifications because they are implemented using schema flags (e.g. `required()`) or special
|
1100 | * internal implementation (e.g. `valid()`).
|
1101 | * In those cases, use the `any.messages()` method to override the error codes for the errors you want to customize.
|
1102 | */
|
1103 | rule(options: RuleOptions): this;
|
1104 |
|
1105 | /**
|
1106 | * Registers a schema to be used by decendents of the current schema in named link references.
|
1107 | */
|
1108 | shared(ref: Schema): this;
|
1109 |
|
1110 | /**
|
1111 | * Sets the options.convert options to false which prevent type casting for the current key and any child keys.
|
1112 | */
|
1113 | strict(isStrict?: boolean): this;
|
1114 |
|
1115 | /**
|
1116 | * Marks a key to be removed from a resulting object or array after validation. Used to sanitize output.
|
1117 | * @param [enabled=true] - if true, the value is stripped, otherwise the validated value is retained. Defaults to true.
|
1118 | */
|
1119 | strip(enabled?: boolean): this;
|
1120 |
|
1121 | /**
|
1122 | * Annotates the key
|
1123 | */
|
1124 | tag(...tags: string[]): this;
|
1125 |
|
1126 | /**
|
1127 | * Applies any assigned target alterations to a copy of the schema that were applied via `any.alter()`.
|
1128 | */
|
1129 | tailor(targets: string | string[]): Schema;
|
1130 |
|
1131 | /**
|
1132 | * Annotates the key with a unit name.
|
1133 | */
|
1134 | unit(name: string): this;
|
1135 |
|
1136 | /**
|
1137 | * Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.
|
1138 | */
|
1139 | valid(...values: any[]): this;
|
1140 |
|
1141 | /**
|
1142 | * Validates a value using the schema and options.
|
1143 | */
|
1144 | validate(value: any, options?: ValidationOptions): ValidationResult;
|
1145 |
|
1146 | /**
|
1147 | * Validates a value using the schema and options.
|
1148 | */
|
1149 | validateAsync(value: any, options?: AsyncValidationOptions): Promise<any>;
|
1150 |
|
1151 | /**
|
1152 | * Same as `rule({ warn: true })`.
|
1153 | * Note that `warn()` will terminate the current ruleset and cannot be followed by another rule option.
|
1154 | * Use `rule()` to apply multiple rule options.
|
1155 | */
|
1156 | warn(): this;
|
1157 |
|
1158 | /**
|
1159 | * Generates a warning.
|
1160 | * When calling `any.validateAsync()`, set the `warning` option to true to enable warnings.
|
1161 | * Warnings are reported separately from errors alongside the result value via the warning key (i.e. `{ value, warning }`).
|
1162 | * Warning are always included when calling `any.validate()`.
|
1163 | */
|
1164 | warning(code: string, context: Context): this;
|
1165 |
|
1166 | /**
|
1167 | * Converts the type into an alternatives type where the conditions are merged into the type definition where:
|
1168 | */
|
1169 | when(ref: string | Reference, options: WhenOptions): this;
|
1170 |
|
1171 | /**
|
1172 | * Converts the type into an alternatives type where the conditions are merged into the type definition where:
|
1173 | */
|
1174 | when(ref: Schema, options: WhenSchemaOptions): this;
|
1175 | }
|
1176 |
|
1177 | interface Description {
|
1178 | type?: Types | string | undefined;
|
1179 | label?: string | undefined;
|
1180 | description?: string | undefined;
|
1181 | flags?: object | undefined;
|
1182 | notes?: string[] | undefined;
|
1183 | tags?: string[] | undefined;
|
1184 | meta?: any[] | undefined;
|
1185 | example?: any[] | undefined;
|
1186 | valids?: any[] | undefined;
|
1187 | invalids?: any[] | undefined;
|
1188 | unit?: string | undefined;
|
1189 | options?: ValidationOptions | undefined;
|
1190 | [key: string]: any;
|
1191 | }
|
1192 |
|
1193 | interface Context {
|
1194 | [key: string]: any;
|
1195 | key?: string | undefined;
|
1196 | label?: string | undefined;
|
1197 | value?: any;
|
1198 | }
|
1199 |
|
1200 | interface State {
|
1201 | key?: string | undefined;
|
1202 | path?: string | undefined;
|
1203 | parent?: any;
|
1204 | reference?: any;
|
1205 | ancestors?: any;
|
1206 | localize?(...args: any[]): State;
|
1207 | }
|
1208 |
|
1209 | interface BooleanSchema extends AnySchema {
|
1210 | /**
|
1211 | * Allows for additional values to be considered valid booleans by converting them to false during validation.
|
1212 | * String comparisons are by default case insensitive,
|
1213 | * see `boolean.sensitive()` to change this behavior.
|
1214 | * @param values - strings, numbers or arrays of them
|
1215 | */
|
1216 | falsy(...values: Array<string | number>): this;
|
1217 |
|
1218 | /**
|
1219 | * Allows the values provided to truthy and falsy as well as the "true" and "false" default conversion
|
1220 | * (when not in `strict()` mode) to be matched in a case insensitive manner.
|
1221 | */
|
1222 | sensitive(enabled?: boolean): this;
|
1223 |
|
1224 | /**
|
1225 | * Allows for additional values to be considered valid booleans by converting them to true during validation.
|
1226 | * String comparisons are by default case insensitive, see `boolean.sensitive()` to change this behavior.
|
1227 | * @param values - strings, numbers or arrays of them
|
1228 | */
|
1229 | truthy(...values: Array<string | number>): this;
|
1230 | }
|
1231 |
|
1232 | interface NumberSchema extends AnySchema {
|
1233 | /**
|
1234 | * Specifies that the value must be greater than limit.
|
1235 | * It can also be a reference to another field.
|
1236 | */
|
1237 | greater(limit: number | Reference): this;
|
1238 |
|
1239 | /**
|
1240 | * Requires the number to be an integer (no floating point).
|
1241 | */
|
1242 | integer(): this;
|
1243 |
|
1244 | /**
|
1245 | * Specifies that the value must be less than limit.
|
1246 | * It can also be a reference to another field.
|
1247 | */
|
1248 | less(limit: number | Reference): this;
|
1249 |
|
1250 | /**
|
1251 | * Specifies the maximum value.
|
1252 | * It can also be a reference to another field.
|
1253 | */
|
1254 | max(limit: number | Reference): this;
|
1255 |
|
1256 | /**
|
1257 | * Specifies the minimum value.
|
1258 | * It can also be a reference to another field.
|
1259 | */
|
1260 | min(limit: number | Reference): this;
|
1261 |
|
1262 | /**
|
1263 | * Specifies that the value must be a multiple of base.
|
1264 | */
|
1265 | multiple(base: number | Reference): this;
|
1266 |
|
1267 | /**
|
1268 | * Requires the number to be negative.
|
1269 | */
|
1270 | negative(): this;
|
1271 |
|
1272 | /**
|
1273 | * Requires the number to be a TCP port, so between 0 and 65535.
|
1274 | */
|
1275 | port(): this;
|
1276 |
|
1277 | /**
|
1278 | * Requires the number to be positive.
|
1279 | */
|
1280 | positive(): this;
|
1281 |
|
1282 | /**
|
1283 | * Specifies the maximum number of decimal places where:
|
1284 | * @param limit - the maximum number of decimal places allowed.
|
1285 | */
|
1286 | precision(limit: number): this;
|
1287 |
|
1288 | /**
|
1289 | * Requires the number to be negative or positive.
|
1290 | */
|
1291 | sign(sign: 'positive' | 'negative'): this;
|
1292 |
|
1293 | /**
|
1294 | * Allows the number to be outside of JavaScript's safety range (Number.MIN_SAFE_INTEGER & Number.MAX_SAFE_INTEGER).
|
1295 | */
|
1296 | unsafe(enabled?: any): this;
|
1297 | }
|
1298 |
|
1299 | interface StringSchema extends AnySchema {
|
1300 | /**
|
1301 | * Requires the string value to only contain a-z, A-Z, and 0-9.
|
1302 | */
|
1303 | alphanum(): this;
|
1304 |
|
1305 | /**
|
1306 | * Requires the string value to be a valid base64 string; does not check the decoded value.
|
1307 | */
|
1308 | base64(options?: Base64Options): this;
|
1309 |
|
1310 | /**
|
1311 | * Sets the required string case.
|
1312 | */
|
1313 | case(direction: 'upper' | 'lower'): this;
|
1314 |
|
1315 | /**
|
1316 | * Requires the number to be a credit card number (Using Lunh Algorithm).
|
1317 | */
|
1318 | creditCard(): this;
|
1319 |
|
1320 | /**
|
1321 | * Requires the string value to be a valid data URI string.
|
1322 | */
|
1323 | dataUri(options?: DataUriOptions): this;
|
1324 |
|
1325 | /**
|
1326 | * Requires the string value to be a valid domain.
|
1327 | */
|
1328 | domain(options?: DomainOptions): this;
|
1329 |
|
1330 | /**
|
1331 | * Requires the string value to be a valid email address.
|
1332 | */
|
1333 | email(options?: EmailOptions): this;
|
1334 |
|
1335 | /**
|
1336 | * Requires the string value to be a valid GUID.
|
1337 | */
|
1338 | guid(options?: GuidOptions): this;
|
1339 |
|
1340 | /**
|
1341 | * Requires the string value to be a valid hexadecimal string.
|
1342 | */
|
1343 | hex(options?: HexOptions): this;
|
1344 |
|
1345 | /**
|
1346 | * Requires the string value to be a valid hostname as per RFC1123.
|
1347 | */
|
1348 | hostname(): this;
|
1349 |
|
1350 | /**
|
1351 | * Allows the value to match any whitelist of blacklist item in a case insensitive comparison.
|
1352 | */
|
1353 | insensitive(): this;
|
1354 |
|
1355 | /**
|
1356 | * Requires the string value to be a valid ip address.
|
1357 | */
|
1358 | ip(options?: IpOptions): this;
|
1359 |
|
1360 | /**
|
1361 | * Requires the string value to be in valid ISO 8601 date format.
|
1362 | */
|
1363 | isoDate(): this;
|
1364 |
|
1365 | /**
|
1366 | * Requires the string value to be in valid ISO 8601 duration format.
|
1367 | */
|
1368 | isoDuration(): this;
|
1369 |
|
1370 | /**
|
1371 | * Specifies the exact string length required
|
1372 | * @param limit - the required string length. It can also be a reference to another field.
|
1373 | * @param encoding - if specified, the string length is calculated in bytes using the provided encoding.
|
1374 | */
|
1375 | length(limit: number | Reference, encoding?: string): this;
|
1376 |
|
1377 | /**
|
1378 | * Requires the string value to be all lowercase. If the validation convert option is on (enabled by default), the string will be forced to lowercase.
|
1379 | */
|
1380 | lowercase(): this;
|
1381 |
|
1382 | /**
|
1383 | * Specifies the maximum number of string characters.
|
1384 | * @param limit - the maximum number of string characters allowed. It can also be a reference to another field.
|
1385 | * @param encoding - if specified, the string length is calculated in bytes using the provided encoding.
|
1386 | */
|
1387 | max(limit: number | Reference, encoding?: string): this;
|
1388 |
|
1389 | /**
|
1390 | * Specifies the minimum number string characters.
|
1391 | * @param limit - the minimum number of string characters required. It can also be a reference to another field.
|
1392 | * @param encoding - if specified, the string length is calculated in bytes using the provided encoding.
|
1393 | */
|
1394 | min(limit: number | Reference, encoding?: string): this;
|
1395 |
|
1396 | /**
|
1397 | * Requires the string value to be in a unicode normalized form. If the validation convert option is on (enabled by default), the string will be normalized.
|
1398 | * @param [form='NFC'] - The unicode normalization form to use. Valid values: NFC [default], NFD, NFKC, NFKD
|
1399 | */
|
1400 | normalize(form?: 'NFC' | 'NFD' | 'NFKC' | 'NFKD'): this;
|
1401 |
|
1402 | /**
|
1403 | * Defines a regular expression rule.
|
1404 | * @param pattern - a regular expression object the string value must match against.
|
1405 | * @param options - optional, can be:
|
1406 | * Name for patterns (useful with multiple patterns). Defaults to 'required'.
|
1407 | * An optional configuration object with the following supported properties:
|
1408 | * name - optional pattern name.
|
1409 | * invert - optional boolean flag. Defaults to false behavior. If specified as true, the provided pattern will be disallowed instead of required.
|
1410 | */
|
1411 | pattern(pattern: RegExp, options?: string | StringRegexOptions): this;
|
1412 |
|
1413 | /**
|
1414 | * Defines a regular expression rule.
|
1415 | * @param pattern - a regular expression object the string value must match against.
|
1416 | * @param options - optional, can be:
|
1417 | * Name for patterns (useful with multiple patterns). Defaults to 'required'.
|
1418 | * An optional configuration object with the following supported properties:
|
1419 | * name - optional pattern name.
|
1420 | * invert - optional boolean flag. Defaults to false behavior. If specified as true, the provided pattern will be disallowed instead of required.
|
1421 | */
|
1422 | regex(pattern: RegExp, options?: string | StringRegexOptions): this;
|
1423 |
|
1424 | /**
|
1425 | * Replace characters matching the given pattern with the specified replacement string where:
|
1426 | * @param pattern - a regular expression object to match against, or a string of which all occurrences will be replaced.
|
1427 | * @param replacement - the string that will replace the pattern.
|
1428 | */
|
1429 | replace(pattern: RegExp | string, replacement: string): this;
|
1430 |
|
1431 | /**
|
1432 | * Requires the string value to only contain a-z, A-Z, 0-9, and underscore _.
|
1433 | */
|
1434 | token(): this;
|
1435 |
|
1436 | /**
|
1437 | * Requires the string value to contain no whitespace before or after. If the validation convert option is on (enabled by default), the string will be trimmed.
|
1438 | * @param [enabled=true] - optional parameter defaulting to true which allows you to reset the behavior of trim by providing a falsy value.
|
1439 | */
|
1440 | trim(enabled?: any): this;
|
1441 |
|
1442 | /**
|
1443 | * Specifies whether the string.max() limit should be used as a truncation.
|
1444 | * @param [enabled=true] - optional parameter defaulting to true which allows you to reset the behavior of truncate by providing a falsy value.
|
1445 | */
|
1446 | truncate(enabled?: boolean): this;
|
1447 |
|
1448 | /**
|
1449 | * Requires the string value to be all uppercase. If the validation convert option is on (enabled by default), the string will be forced to uppercase.
|
1450 | */
|
1451 | uppercase(): this;
|
1452 |
|
1453 | /**
|
1454 | * Requires the string value to be a valid RFC 3986 URI.
|
1455 | */
|
1456 | uri(options?: UriOptions): this;
|
1457 |
|
1458 | /**
|
1459 | * Requires the string value to be a valid GUID.
|
1460 | */
|
1461 | uuid(options?: GuidOptions): this;
|
1462 | }
|
1463 |
|
1464 | interface SymbolSchema extends AnySchema {
|
1465 | // TODO: support number and symbol index
|
1466 | map(iterable: Iterable<[string | number | boolean | symbol, symbol]> | { [key: string]: symbol }): this;
|
1467 | }
|
1468 |
|
1469 | interface ArraySortOptions {
|
1470 | /**
|
1471 | * @default 'ascending'
|
1472 | */
|
1473 | order?: 'ascending' | 'descending' | undefined;
|
1474 | by?: string | Reference | undefined;
|
1475 | }
|
1476 |
|
1477 | interface ArrayUniqueOptions extends HierarchySeparatorOptions {
|
1478 | /**
|
1479 | * if true, undefined values for the dot notation string comparator will not cause the array to fail on uniqueness.
|
1480 | *
|
1481 | * @default false
|
1482 | */
|
1483 | ignoreUndefined?: boolean | undefined;
|
1484 | }
|
1485 |
|
1486 | type ComparatorFunction = (a: any, b: any) => boolean;
|
1487 |
|
1488 | interface ArraySchema extends AnySchema {
|
1489 | |
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 | has(schema: SchemaLike): this;
|
1495 |
|
1496 | |
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 | items(...types: SchemaLike[]): this;
|
1507 |
|
1508 | |
1509 |
|
1510 |
|
1511 | length(limit: number | Reference): this;
|
1512 |
|
1513 | |
1514 |
|
1515 |
|
1516 | max(limit: number | Reference): this;
|
1517 |
|
1518 | |
1519 |
|
1520 |
|
1521 | min(limit: number | Reference): this;
|
1522 |
|
1523 | |
1524 |
|
1525 |
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 | ordered(...types: SchemaLike[]): this;
|
1531 |
|
1532 | |
1533 |
|
1534 |
|
1535 |
|
1536 | single(enabled?: any): this;
|
1537 |
|
1538 | |
1539 |
|
1540 |
|
1541 | sort(options?: ArraySortOptions): this;
|
1542 |
|
1543 | |
1544 |
|
1545 |
|
1546 |
|
1547 | sparse(enabled?: any): this;
|
1548 |
|
1549 | |
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 |
|
1556 | unique(comparator?: string | ComparatorFunction, options?: ArrayUniqueOptions): this;
|
1557 | }
|
1558 |
|
1559 | interface ObjectPatternOptions {
|
1560 | fallthrough?: boolean | undefined;
|
1561 | matches: SchemaLike | Reference;
|
1562 | }
|
1563 |
|
1564 | interface ObjectSchema<TSchema = any> extends AnySchema {
|
1565 | |
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 | and(...peers: Array<string | HierarchySeparatorOptions>): this;
|
1571 |
|
1572 | |
1573 |
|
1574 |
|
1575 | append(schema?: SchemaMap<TSchema>): this;
|
1576 |
|
1577 | |
1578 |
|
1579 |
|
1580 | assert(ref: string | Reference, schema: SchemaLike, message?: string): this;
|
1581 |
|
1582 | |
1583 |
|
1584 |
|
1585 |
|
1586 |
|
1587 |
|
1588 |
|
1589 | instance(constructor: Function, name?: string): this;
|
1590 |
|
1591 | /**
|
1592 | * Sets or extends the allowed object keys.
|
1593 | */
|
1594 | keys(schema?: SchemaMap<TSchema>): this;
|
1595 |
|
1596 | /**
|
1597 | * Specifies the exact number of keys in the object.
|
1598 | */
|
1599 | length(limit: number): this;
|
1600 |
|
1601 | /**
|
1602 | * Specifies the maximum number of keys in the object.
|
1603 | */
|
1604 | max(limit: number | Reference): this;
|
1605 |
|
1606 | /**
|
1607 | * Specifies the minimum number of keys in the object.
|
1608 | */
|
1609 | min(limit: number | Reference): this;
|
1610 |
|
1611 | /**
|
1612 | * Defines a relationship between keys where not all peers can be present at the same time.
|
1613 | *
|
1614 | * Optional settings must be the last argument.
|
1615 | */
|
1616 | nand(...peers: Array<string | HierarchySeparatorOptions>): this;
|
1617 |
|
1618 | /**
|
1619 | * Defines a relationship between keys where one of the peers is required (and more than one is allowed).
|
1620 | *
|
1621 | * Optional settings must be the last argument.
|
1622 | */
|
1623 | or(...peers: Array<string | HierarchySeparatorOptions>): this;
|
1624 |
|
1625 | /**
|
1626 | * Defines an exclusive relationship between a set of keys where only one is allowed but none are required.
|
1627 | *
|
1628 | * Optional settings must be the last argument.
|
1629 | */
|
1630 | oxor(...peers: Array<string | HierarchySeparatorOptions>): this;
|
1631 |
|
1632 | /**
|
1633 | * Specify validation rules for unknown keys matching a pattern.
|
1634 | *
|
1635 | * @param pattern - a pattern that can be either a regular expression or a joi schema that will be tested against the unknown key names
|
1636 | * @param schema - the schema object matching keys must validate against
|
1637 | */
|
1638 | pattern(pattern: RegExp | SchemaLike, schema: SchemaLike, options?: ObjectPatternOptions): this;
|
1639 |
|
1640 | /**
|
1641 | * Requires the object to be a Joi reference.
|
1642 | */
|
1643 | ref(): this;
|
1644 |
|
1645 | /**
|
1646 | * Requires the object to be a `RegExp` object.
|
1647 | */
|
1648 | regex(): this;
|
1649 |
|
1650 | /**
|
1651 | * Renames a key to another name (deletes the renamed key).
|
1652 | */
|
1653 | rename(from: string | RegExp, to: string, options?: RenameOptions): this;
|
1654 |
|
1655 | /**
|
1656 | * Requires the object to be a Joi schema instance.
|
1657 | */
|
1658 | schema(type?: SchemaLike): this;
|
1659 |
|
1660 | /**
|
1661 | * Overrides the handling of unknown keys for the scope of the current object only (does not apply to children).
|
1662 | */
|
1663 | unknown(allow?: boolean): this;
|
1664 |
|
1665 | /**
|
1666 | * Requires the presence of other keys whenever the specified key is present.
|
1667 | */
|
1668 | with(key: string, peers: string | string[], options?: HierarchySeparatorOptions): this;
|
1669 |
|
1670 | /**
|
1671 | * Forbids the presence of other keys whenever the specified is present.
|
1672 | */
|
1673 | without(key: string, peers: string | string[], options?: HierarchySeparatorOptions): this;
|
1674 |
|
1675 | /**
|
1676 | * Defines an exclusive relationship between a set of keys. one of them is required but not at the same time.
|
1677 | *
|
1678 | * Optional settings must be the last argument.
|
1679 | */
|
1680 | xor(...peers: Array<string | HierarchySeparatorOptions>): this;
|
1681 | }
|
1682 |
|
1683 | interface BinarySchema extends AnySchema {
|
1684 | |
1685 |
|
1686 |
|
1687 | encoding(encoding: string): this;
|
1688 |
|
1689 | |
1690 |
|
1691 |
|
1692 | min(limit: number | Reference): this;
|
1693 |
|
1694 | |
1695 |
|
1696 |
|
1697 | max(limit: number | Reference): this;
|
1698 |
|
1699 | |
1700 |
|
1701 |
|
1702 | length(limit: number | Reference): this;
|
1703 | }
|
1704 |
|
1705 | interface DateSchema extends AnySchema {
|
1706 | |
1707 |
|
1708 |
|
1709 |
|
1710 |
|
1711 |
|
1712 | greater(date: 'now' | Date | number | string | Reference): this;
|
1713 |
|
1714 | |
1715 |
|
1716 |
|
1717 | iso(): this;
|
1718 |
|
1719 | |
1720 |
|
1721 |
|
1722 |
|
1723 |
|
1724 |
|
1725 | less(date: 'now' | Date | number | string | Reference): this;
|
1726 |
|
1727 | |
1728 |
|
1729 |
|
1730 |
|
1731 |
|
1732 |
|
1733 | min(date: 'now' | Date | number | string | Reference): this;
|
1734 |
|
1735 | |
1736 |
|
1737 |
|
1738 |
|
1739 |
|
1740 |
|
1741 | max(date: 'now' | Date | number | string | Reference): this;
|
1742 |
|
1743 | |
1744 |
|
1745 |
|
1746 |
|
1747 | timestamp(type?: 'javascript' | 'unix'): this;
|
1748 | }
|
1749 |
|
1750 | interface FunctionSchema extends ObjectSchema {
|
1751 | |
1752 |
|
1753 |
|
1754 |
|
1755 | arity(n: number): this;
|
1756 |
|
1757 | |
1758 |
|
1759 |
|
1760 | class(): this;
|
1761 |
|
1762 | |
1763 |
|
1764 |
|
1765 |
|
1766 | minArity(n: number): this;
|
1767 |
|
1768 | |
1769 |
|
1770 |
|
1771 |
|
1772 | maxArity(n: number): this;
|
1773 | }
|
1774 |
|
1775 | interface AlternativesSchema extends AnySchema {
|
1776 | |
1777 |
|
1778 |
|
1779 | conditional(ref: string | Reference, options: WhenOptions): this;
|
1780 | conditional(ref: Schema, options: WhenSchemaOptions): this;
|
1781 |
|
1782 | |
1783 |
|
1784 |
|
1785 |
|
1786 | match(mode: 'any' | 'all' | 'one'): this;
|
1787 |
|
1788 | |
1789 |
|
1790 |
|
1791 | try(...types: SchemaLike[]): this;
|
1792 | }
|
1793 |
|
1794 | interface LinkSchema extends AnySchema {
|
1795 | |
1796 |
|
1797 |
|
1798 |
|
1799 | concat(schema: Schema): this;
|
1800 |
|
1801 | |
1802 |
|
1803 |
|
1804 |
|
1805 | ref(ref: string): this;
|
1806 | }
|
1807 |
|
1808 | interface Reference extends Exclude<ReferenceOptions, 'prefix'> {
|
1809 | depth: number;
|
1810 | type: string;
|
1811 | key: string;
|
1812 | root: string;
|
1813 | path: string[];
|
1814 | display: string;
|
1815 | toString(): string;
|
1816 | }
|
1817 |
|
1818 | type ExtensionBoundSchema = Schema & SchemaInternals;
|
1819 |
|
1820 | interface RuleArgs {
|
1821 | name: string;
|
1822 | ref?: boolean | undefined;
|
1823 | assert?: ((value: any) => boolean) | AnySchema | undefined;
|
1824 | message?: string | undefined;
|
1825 |
|
1826 | /**
|
1827 | * Undocumented properties
|
1828 | */
|
1829 | normalize?(value: any): any;
|
1830 | }
|
1831 |
|
1832 | type RuleMethod = (...args: any[]) => any;
|
1833 |
|
1834 | interface ExtensionRule {
|
1835 | |
1836 |
|
1837 |
|
1838 | alias?: string | undefined;
|
1839 | |
1840 |
|
1841 |
|
1842 | multi?: boolean | undefined;
|
1843 | |
1844 |
|
1845 |
|
1846 | convert?: boolean | undefined;
|
1847 | |
1848 |
|
1849 |
|
1850 | args?: Array<RuleArgs | string> | undefined;
|
1851 | |
1852 |
|
1853 |
|
1854 | method?: RuleMethod | false | undefined;
|
1855 | |
1856 |
|
1857 |
|
1858 | validate?(value: any, helpers: any, args: Record<string, any>, options: any): any;
|
1859 |
|
1860 | |
1861 |
|
1862 |
|
1863 | priority?: boolean | undefined;
|
1864 | manifest?: boolean | undefined;
|
1865 | }
|
1866 |
|
1867 | interface CoerceResult {
|
1868 | errors?: ErrorReport[] | undefined;
|
1869 | value?: any;
|
1870 | }
|
1871 |
|
1872 | type CoerceFunction = (value: any, helpers: CustomHelpers) => CoerceResult;
|
1873 |
|
1874 | interface CoerceObject {
|
1875 | method: CoerceFunction;
|
1876 | from?: string | string[] | undefined;
|
1877 | }
|
1878 |
|
1879 | interface ExtensionFlag {
|
1880 | setter?: string | undefined;
|
1881 | default?: any;
|
1882 | }
|
1883 |
|
1884 | interface ExtensionTermManifest {
|
1885 | mapped: {
|
1886 | from: string;
|
1887 | to: string;
|
1888 | };
|
1889 | }
|
1890 |
|
1891 | interface ExtensionTerm {
|
1892 | init: any[] | null;
|
1893 | register?: any;
|
1894 | manifest?: Record<string, 'schema' | 'single' | ExtensionTermManifest> | undefined;
|
1895 | }
|
1896 |
|
1897 | interface Extension {
|
1898 | type: string;
|
1899 | args?(...args: SchemaLike[]): Schema;
|
1900 | base?: Schema | undefined;
|
1901 | coerce?: CoerceFunction | CoerceObject | undefined;
|
1902 | flags?: Record<string, ExtensionFlag> | undefined;
|
1903 | manifest?: {
|
1904 | build?(obj: ExtensionBoundSchema, desc: Record<string, any>): any;
|
1905 | } | undefined;
|
1906 | messages?: LanguageMessages | string | undefined;
|
1907 | modifiers?: Record<string, (rule: any, enabled?: boolean) => any> | undefined;
|
1908 | overrides?: Record<string, (value: any) => Schema> | undefined;
|
1909 | prepare?(value: any, helpers: CustomHelpers): any;
|
1910 | rebuild?(schema: ExtensionBoundSchema): void;
|
1911 | rules?: Record<string, ExtensionRule & ThisType<SchemaInternals>> | undefined;
|
1912 | terms?: Record<string, ExtensionTerm> | undefined;
|
1913 | validate?(value: any, helpers: CustomHelpers): any;
|
1914 |
|
1915 | |
1916 |
|
1917 |
|
1918 | cast?: Record<string, { from(value: any): any; to(value: any, helpers: CustomHelpers): any }> | undefined;
|
1919 | properties?: Record<string, any> | undefined;
|
1920 | }
|
1921 |
|
1922 | type ExtensionFactory = (joi: Root) => Extension;
|
1923 |
|
1924 | interface Err {
|
1925 | toString(): string;
|
1926 | }
|
1927 |
|
1928 |
|
1929 |
|
1930 | interface Root {
|
1931 | |
1932 |
|
1933 |
|
1934 | version: string;
|
1935 |
|
1936 | ValidationError: new (message: string, details: any, original: any) => ValidationError;
|
1937 |
|
1938 | |
1939 |
|
1940 |
|
1941 | any(): AnySchema;
|
1942 |
|
1943 | |
1944 |
|
1945 |
|
1946 | array(): ArraySchema;
|
1947 |
|
1948 | |
1949 |
|
1950 |
|
1951 | bool(): BooleanSchema;
|
1952 |
|
1953 | |
1954 |
|
1955 |
|
1956 | boolean(): BooleanSchema;
|
1957 |
|
1958 | |
1959 |
|
1960 |
|
1961 | binary(): BinarySchema;
|
1962 |
|
1963 | |
1964 |
|
1965 |
|
1966 | date(): DateSchema;
|
1967 |
|
1968 | |
1969 |
|
1970 |
|
1971 | func(): FunctionSchema;
|
1972 |
|
1973 | |
1974 |
|
1975 |
|
1976 | function(): FunctionSchema;
|
1977 |
|
1978 | |
1979 |
|
1980 |
|
1981 | number(): NumberSchema;
|
1982 |
|
1983 | |
1984 |
|
1985 |
|
1986 |
|
1987 | object<TSchema = any, T = TSchema>(schema?: SchemaMap<T>): ObjectSchema<TSchema>;
|
1988 |
|
1989 | |
1990 |
|
1991 |
|
1992 | string(): StringSchema;
|
1993 |
|
1994 | |
1995 |
|
1996 |
|
1997 | symbol(): SymbolSchema;
|
1998 |
|
1999 | |
2000 |
|
2001 |
|
2002 | alternatives(types: SchemaLike[]): AlternativesSchema;
|
2003 | alternatives(...types: SchemaLike[]): AlternativesSchema;
|
2004 |
|
2005 | |
2006 |
|
2007 |
|
2008 | alt(types: SchemaLike[]): AlternativesSchema;
|
2009 | alt(...types: SchemaLike[]): AlternativesSchema;
|
2010 |
|
2011 | |
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
2017 |
|
2018 |
|
2019 |
|
2020 | link(ref?: string): LinkSchema;
|
2021 |
|
2022 | |
2023 |
|
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 | assert(value: any, schema: SchemaLike, options?: ValidationOptions): void;
|
2030 | assert(value: any, schema: SchemaLike, message: string | Error, options?: ValidationOptions): void;
|
2031 |
|
2032 | |
2033 |
|
2034 |
|
2035 |
|
2036 |
|
2037 |
|
2038 |
|
2039 | attempt(value: any, schema: SchemaLike, options?: ValidationOptions): any;
|
2040 | attempt(value: any, schema: SchemaLike, message: string | Error, options?: ValidationOptions): any;
|
2041 |
|
2042 | cache: CacheConfiguration;
|
2043 |
|
2044 | |
2045 |
|
2046 |
|
2047 | compile(schema: SchemaLike, options?: CompileOptions): Schema;
|
2048 |
|
2049 | |
2050 |
|
2051 |
|
2052 |
|
2053 |
|
2054 |
|
2055 |
|
2056 |
|
2057 | checkPreferences(prefs: ValidationOptions): void;
|
2058 |
|
2059 | |
2060 |
|
2061 |
|
2062 | custom(fn: CustomValidator, description?: string): Schema;
|
2063 |
|
2064 | |
2065 |
|
2066 |
|
2067 |
|
2068 |
|
2069 |
|
2070 | defaults(fn: SchemaFunction): Root;
|
2071 |
|
2072 | |
2073 |
|
2074 |
|
2075 | expression(template: string, options?: ReferenceOptions): any;
|
2076 |
|
2077 | |
2078 |
|
2079 |
|
2080 | extend(...extensions: Array<Extension | ExtensionFactory>): any;
|
2081 |
|
2082 | |
2083 |
|
2084 |
|
2085 | in(ref: string, options?: ReferenceOptions): Reference;
|
2086 |
|
2087 | |
2088 |
|
2089 |
|
2090 | isError(error: any): error is ValidationError;
|
2091 |
|
2092 | |
2093 |
|
2094 |
|
2095 | isExpression(expression: any): boolean;
|
2096 |
|
2097 | |
2098 |
|
2099 |
|
2100 | isRef(ref: any): ref is Reference;
|
2101 |
|
2102 | |
2103 |
|
2104 |
|
2105 | isSchema(schema: any, options?: CompileOptions): boolean;
|
2106 |
|
2107 | |
2108 |
|
2109 |
|
2110 | override: symbol;
|
2111 |
|
2112 | |
2113 |
|
2114 |
|
2115 | ref(key: string, options?: ReferenceOptions): Reference;
|
2116 |
|
2117 | |
2118 |
|
2119 |
|
2120 |
|
2121 |
|
2122 | types(): {
|
2123 | alternatives: AlternativesSchema;
|
2124 | any: AnySchema;
|
2125 | array: ArraySchema;
|
2126 | binary: BinarySchema;
|
2127 | boolean: BooleanSchema;
|
2128 | date: DateSchema;
|
2129 | function: FunctionSchema;
|
2130 | link: LinkSchema;
|
2131 | number: NumberSchema;
|
2132 | object: ObjectSchema;
|
2133 | string: StringSchema;
|
2134 | symbol: SymbolSchema;
|
2135 | };
|
2136 |
|
2137 | |
2138 |
|
2139 |
|
2140 | x(template: string, options?: ReferenceOptions): any;
|
2141 |
|
2142 |
|
2143 |
|
2144 |
|
2145 |
|
2146 | |
2147 |
|
2148 |
|
2149 | allow(...values: any[]): Schema;
|
2150 |
|
2151 | |
2152 |
|
2153 |
|
2154 | valid(...values: any[]): Schema;
|
2155 | equal(...values: any[]): Schema;
|
2156 |
|
2157 | |
2158 |
|
2159 |
|
2160 | invalid(...values: any[]): Schema;
|
2161 | disallow(...values: any[]): Schema;
|
2162 | not(...values: any[]): Schema;
|
2163 |
|
2164 | |
2165 |
|
2166 |
|
2167 | required(): Schema;
|
2168 |
|
2169 | |
2170 |
|
2171 |
|
2172 | exist(): Schema;
|
2173 |
|
2174 | |
2175 |
|
2176 |
|
2177 | optional(): Schema;
|
2178 |
|
2179 | |
2180 |
|
2181 |
|
2182 | forbidden(): Schema;
|
2183 |
|
2184 | |
2185 |
|
2186 |
|
2187 | preferences(options: ValidationOptions): Schema;
|
2188 |
|
2189 | |
2190 |
|
2191 |
|
2192 | prefs(options: ValidationOptions): Schema;
|
2193 |
|
2194 | |
2195 |
|
2196 |
|
2197 | when(ref: string | Reference, options: WhenOptions): AlternativesSchema;
|
2198 | when(ref: Schema, options: WhenSchemaOptions): AlternativesSchema;
|
2199 |
|
2200 | |
2201 |
|
2202 |
|
2203 | build(...args: any[]): any;
|
2204 |
|
2205 | |
2206 |
|
2207 |
|
2208 | options(...args: any[]): any;
|
2209 |
|
2210 | |
2211 |
|
2212 |
|
2213 | trace(...args: any[]): any;
|
2214 | untrace(...args: any[]): any;
|
2215 | }
|
2216 | }
|
2217 |
|
2218 | declare const Joi: Joi.Root;
|
2219 | export = Joi;
|