joi 18.0.0 → 18.0.2

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