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