@nhtio/validation 1.20250814.0 → 1.20250911.0
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/index.cjs +2 -2
- package/index.cjs.map +1 -1
- package/index.d.ts +2 -1
- package/index.mjs +2 -2
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/private/index.d.ts +91 -2
- package/private/schemas.d.ts +63 -15
package/package.json
CHANGED
package/private/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { default as Joi } from 'joi';
|
|
1
2
|
import type { DateTime } from 'luxon';
|
|
2
3
|
import type { PhoneSchema } from './schemas/phone';
|
|
3
4
|
import type { BigIntSchema } from './schemas/bigint';
|
|
@@ -5,7 +6,7 @@ import type { DatetimeSchema } from './schemas/datetime';
|
|
|
5
6
|
import type { CountryOrUnknown } from '@nhtio/phone-object';
|
|
6
7
|
import type { Root, Reference, SchemaMap, SchemaLike } from 'joi';
|
|
7
8
|
import type { I18nCallback, SetI18nCallback } from './patches/i18n';
|
|
8
|
-
import type {
|
|
9
|
+
import type { AnySchema, StringSchema, BinarySchema, NumberSchema, BooleanSchema, ObjectSchema, ArraySchema, DateSchema, AlternativesSchema, FunctionSchema, LinkSchema, SymbolSchema, Schema } from './schemas';
|
|
9
10
|
/**
|
|
10
11
|
* Extended Joi root interface that includes custom schema types for
|
|
11
12
|
* additional validation scenarios.
|
|
@@ -25,7 +26,7 @@ import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberS
|
|
|
25
26
|
*
|
|
26
27
|
* @public
|
|
27
28
|
*/
|
|
28
|
-
export interface ValidationRoot extends Omit<Root, '
|
|
29
|
+
export interface ValidationRoot extends Omit<Root, 'allow' | 'alt' | 'alternatives' | 'any' | 'array' | 'binary' | 'bool' | 'boolean' | 'date' | 'disallow' | 'equal' | 'exist' | 'forbidden' | 'func' | 'function' | 'invalid' | 'link' | 'not' | 'number' | 'object' | 'optional' | 'preferences' | 'prefs' | 'required' | 'string' | 'symbol' | 'types' | 'valid' | 'when'> {
|
|
29
30
|
/**
|
|
30
31
|
* Generates a schema object that matches any data type.
|
|
31
32
|
*/
|
|
@@ -50,6 +51,14 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
50
51
|
* Generates a schema object that matches a date type (as well as a JavaScript date string or number of milliseconds).
|
|
51
52
|
*/
|
|
52
53
|
date<TSchema = Date>(): DateSchema<TSchema>;
|
|
54
|
+
/**
|
|
55
|
+
* Generates a schema object that matches a function type.
|
|
56
|
+
*/
|
|
57
|
+
func<TSchema = Function>(): FunctionSchema<TSchema>;
|
|
58
|
+
/**
|
|
59
|
+
* Generates a schema object that matches a function type.
|
|
60
|
+
*/
|
|
61
|
+
function<TSchema = Function>(): FunctionSchema<TSchema>;
|
|
53
62
|
/**
|
|
54
63
|
* Generates a schema object that matches a number data type (as well as strings that can be converted to numbers).
|
|
55
64
|
*/
|
|
@@ -62,6 +71,10 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
62
71
|
* 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('').
|
|
63
72
|
*/
|
|
64
73
|
string<TSchema = string>(): StringSchema<TSchema>;
|
|
74
|
+
/**
|
|
75
|
+
* Generates a schema object that matches any symbol.
|
|
76
|
+
*/
|
|
77
|
+
symbol<TSchema = Symbol>(): SymbolSchema<TSchema>;
|
|
65
78
|
/**
|
|
66
79
|
* Generates a type that will match one of the provided alternative schemas
|
|
67
80
|
*/
|
|
@@ -72,6 +85,16 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
72
85
|
*/
|
|
73
86
|
alt<TSchema = any>(types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
74
87
|
alt<TSchema = any>(...types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
88
|
+
/**
|
|
89
|
+
* Links to another schema node and reuses it for validation, typically for creative recursive schemas.
|
|
90
|
+
*
|
|
91
|
+
* @param ref - the reference to the linked schema node.
|
|
92
|
+
* Cannot reference itself or its children as well as other links.
|
|
93
|
+
* Links can be expressed in relative terms like value references (`Joi.link('...')`),
|
|
94
|
+
* in absolute terms from the schema run-time root (`Joi.link('/a')`),
|
|
95
|
+
* or using schema ids implicitly using object keys or explicitly using `any.id()` (`Joi.link('#a.b.c')`).
|
|
96
|
+
*/
|
|
97
|
+
link<TSchema = any>(ref?: string): LinkSchema<TSchema>;
|
|
75
98
|
/**
|
|
76
99
|
* Generates a schema object that matches a BigInt data type.
|
|
77
100
|
*/
|
|
@@ -86,6 +109,72 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
86
109
|
* @param country - Optional country code or reference for phone validation
|
|
87
110
|
*/
|
|
88
111
|
phone<TSchema = string>(country?: CountryOrUnknown | Reference | null): PhoneSchema<TSchema>;
|
|
112
|
+
/**
|
|
113
|
+
* Returns an object where each key is a plain joi schema type.
|
|
114
|
+
* Useful for creating type shortcuts using deconstruction.
|
|
115
|
+
* Note that the types are already formed and do not need to be called as functions (e.g. `string`, not `string()`).
|
|
116
|
+
*/
|
|
117
|
+
types(): {
|
|
118
|
+
alternatives: AlternativesSchema;
|
|
119
|
+
any: AnySchema;
|
|
120
|
+
array: ArraySchema;
|
|
121
|
+
binary: BinarySchema;
|
|
122
|
+
boolean: BooleanSchema;
|
|
123
|
+
date: DateSchema;
|
|
124
|
+
function: FunctionSchema;
|
|
125
|
+
link: LinkSchema;
|
|
126
|
+
number: NumberSchema;
|
|
127
|
+
object: ObjectSchema;
|
|
128
|
+
string: StringSchema;
|
|
129
|
+
symbol: SymbolSchema;
|
|
130
|
+
bigint: BigIntSchema;
|
|
131
|
+
datetime: DatetimeSchema;
|
|
132
|
+
phone: PhoneSchema;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Whitelists a value
|
|
136
|
+
*/
|
|
137
|
+
allow(...values: any[]): Schema;
|
|
138
|
+
/**
|
|
139
|
+
* Adds the provided values into the allowed whitelist and marks them as the only valid values allowed.
|
|
140
|
+
*/
|
|
141
|
+
valid(...values: any[]): Schema;
|
|
142
|
+
equal(...values: any[]): Schema;
|
|
143
|
+
/**
|
|
144
|
+
* Blacklists a value
|
|
145
|
+
*/
|
|
146
|
+
invalid(...values: any[]): Schema;
|
|
147
|
+
disallow(...values: any[]): Schema;
|
|
148
|
+
not(...values: any[]): Schema;
|
|
149
|
+
/**
|
|
150
|
+
* Marks a key as required which will not allow undefined as value. All keys are optional by default.
|
|
151
|
+
*/
|
|
152
|
+
required(): Schema;
|
|
153
|
+
/**
|
|
154
|
+
* Alias of `required`.
|
|
155
|
+
*/
|
|
156
|
+
exist(): Schema;
|
|
157
|
+
/**
|
|
158
|
+
* 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.
|
|
159
|
+
*/
|
|
160
|
+
optional(): Schema;
|
|
161
|
+
/**
|
|
162
|
+
* Marks a key as forbidden which will not allow any value except undefined. Used to explicitly forbid keys.
|
|
163
|
+
*/
|
|
164
|
+
forbidden(): Schema;
|
|
165
|
+
/**
|
|
166
|
+
* Overrides the global validate() options for the current key and any sub-key.
|
|
167
|
+
*/
|
|
168
|
+
preferences(options: Joi.ValidationOptions): Schema;
|
|
169
|
+
/**
|
|
170
|
+
* Overrides the global validate() options for the current key and any sub-key.
|
|
171
|
+
*/
|
|
172
|
+
prefs(options: Joi.ValidationOptions): Schema;
|
|
173
|
+
/**
|
|
174
|
+
* Converts the type into an alternatives type where the conditions are merged into the type definition where:
|
|
175
|
+
*/
|
|
176
|
+
when(ref: string | Reference, options: Joi.WhenOptions | Joi.WhenOptions[]): AlternativesSchema;
|
|
177
|
+
when(ref: Schema, options: Joi.WhenSchemaOptions): AlternativesSchema;
|
|
89
178
|
/**
|
|
90
179
|
* Sets a global internationalization callback that applies to all validator instances.
|
|
91
180
|
*
|
package/private/schemas.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import type { BigIntSchema } from './schemas/bigint';
|
|
|
4
4
|
import type { DatetimeSchema } from './schemas/datetime';
|
|
5
5
|
import type { default as Joi, AnySchema as JoiAnySchema, Reference, BasicType, CustomHelpers } from 'joi';
|
|
6
6
|
export type DefaultableValue = Reference | BasicType | DateTime | bigint | ((parent: any, helpers: CustomHelpers) => Reference | BasicType | DateTime | bigint);
|
|
7
|
-
export type ExtendedSchemaLikeWithoutArray = string | number | boolean | null | Schema | Joi.SchemaMap | PhoneSchema | BigIntSchema | DatetimeSchema;
|
|
8
7
|
/**
|
|
9
8
|
* Base schema type for all validation schemas.
|
|
10
9
|
*
|
|
@@ -13,7 +12,36 @@ export type ExtendedSchemaLikeWithoutArray = string | number | boolean | null |
|
|
|
13
12
|
*
|
|
14
13
|
* @public
|
|
15
14
|
*/
|
|
16
|
-
export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, 'allow' | 'alter' | 'artifact' | 'bind' | 'cache' | 'cast' | 'concat' | 'custom' | 'default' | 'description' | 'disallow' | 'empty' | 'equal' | 'error' | 'example' | 'exist' | 'external' | 'failover' | 'forbidden' | 'fork' | 'id' | 'invalid' | 'keep' | 'label' | 'message' | 'messages' | 'meta' | 'not' | 'note' | 'only' | 'optional' | 'options' | 'preferences' | 'prefs' | 'presence' | 'raw' | 'required' | 'rule' | 'shared' | 'strict' | 'strip' | 'tag' | 'unit' | 'valid' | 'warn' | 'warning' | 'when'> {
|
|
15
|
+
export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, '$_addRule' | '$_compile' | '$_modify' | '$_mutateRebuild' | '$_reach' | '$_super' | '$' | 'allow' | 'alter' | 'artifact' | 'bind' | 'cache' | 'cast' | 'concat' | 'custom' | 'default' | 'description' | 'disallow' | 'empty' | 'equal' | 'error' | 'example' | 'exist' | 'external' | 'extract' | 'failover' | 'forbidden' | 'fork' | 'id' | 'invalid' | 'keep' | 'label' | 'message' | 'messages' | 'meta' | 'not' | 'note' | 'only' | 'optional' | 'options' | 'preferences' | 'prefs' | 'presence' | 'raw' | 'required' | 'rule' | 'ruleset' | 'shared' | 'strict' | 'strip' | 'tag' | 'tailor' | 'unit' | 'valid' | 'warn' | 'warning' | 'when'> {
|
|
16
|
+
/**
|
|
17
|
+
* Parent schema object.
|
|
18
|
+
*/
|
|
19
|
+
$_super: Schema;
|
|
20
|
+
/**
|
|
21
|
+
* Adds a rule to current validation schema.
|
|
22
|
+
*/
|
|
23
|
+
$_addRule(rule: string | Joi.AddRuleOptions): Schema;
|
|
24
|
+
/**
|
|
25
|
+
* Internally compiles schema.
|
|
26
|
+
*/
|
|
27
|
+
$_compile(schema: SchemaLike, options?: Joi.CompileOptions): Schema;
|
|
28
|
+
$_modify(options?: Joi.ModifyOptions): Schema;
|
|
29
|
+
/**
|
|
30
|
+
* Resets current schema.
|
|
31
|
+
*/
|
|
32
|
+
$_mutateRebuild(): this;
|
|
33
|
+
/**
|
|
34
|
+
* Get schema at given path.
|
|
35
|
+
*/
|
|
36
|
+
$_reach(path: string[]): Schema;
|
|
37
|
+
/**
|
|
38
|
+
* Starts a ruleset in order to apply multiple rule options. The set ends when `rule()`, `keep()`, `message()`, or `warn()` is called.
|
|
39
|
+
*/
|
|
40
|
+
$: this;
|
|
41
|
+
/**
|
|
42
|
+
* Starts a ruleset in order to apply multiple rule options. The set ends when `rule()`, `keep()`, `message()`, or `warn()` is called.
|
|
43
|
+
*/
|
|
44
|
+
ruleset: this;
|
|
17
45
|
allow(...values: any[]): this;
|
|
18
46
|
alter(targets: Record<string, (schema: this) => Schema>): this;
|
|
19
47
|
artifact(id: any): this;
|
|
@@ -23,7 +51,7 @@ export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, 'a
|
|
|
23
51
|
custom(fn: Joi.CustomValidator, description?: string): this;
|
|
24
52
|
description(desc: string): this;
|
|
25
53
|
disallow(...values: any[]): this;
|
|
26
|
-
empty(schema?:
|
|
54
|
+
empty(schema?: SchemaLike): this;
|
|
27
55
|
equal(...values: any[]): this;
|
|
28
56
|
error(err: Error | Joi.ValidationErrorFunction): this;
|
|
29
57
|
example(value: any, options?: {
|
|
@@ -31,6 +59,12 @@ export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, 'a
|
|
|
31
59
|
}): this;
|
|
32
60
|
exist(): this;
|
|
33
61
|
external(method: Joi.ExternalValidationFunction, description?: string): this;
|
|
62
|
+
/**
|
|
63
|
+
* Returns a sub-schema based on a path of object keys or schema ids.
|
|
64
|
+
*
|
|
65
|
+
* @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).
|
|
66
|
+
*/
|
|
67
|
+
extract(path: string | string[]): Schema;
|
|
34
68
|
failover(value: any): this;
|
|
35
69
|
forbidden(): this;
|
|
36
70
|
fork(key: string | string[] | string[][], adjuster: Joi.SchemaFunction): this;
|
|
@@ -56,6 +90,10 @@ export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, 'a
|
|
|
56
90
|
strict(isStrict?: boolean): this;
|
|
57
91
|
strip(enabled?: boolean): this;
|
|
58
92
|
tag(...tags: string[]): this;
|
|
93
|
+
/**
|
|
94
|
+
* Applies any assigned target alterations to a copy of the schema that were applied via `any.alter()`.
|
|
95
|
+
*/
|
|
96
|
+
tailor(targets: string | string[]): Schema;
|
|
59
97
|
unit(name: string): this;
|
|
60
98
|
valid(...values: any[]): this;
|
|
61
99
|
warn(): this;
|
|
@@ -375,12 +413,12 @@ export interface ObjectSchema<TSchema = any> extends AnySchema<TSchema> {
|
|
|
375
413
|
/**
|
|
376
414
|
* Appends the allowed object keys. If schema is null, undefined, or {}, no changes will be applied.
|
|
377
415
|
*/
|
|
378
|
-
append(schema?:
|
|
379
|
-
append<TSchemaExtended = any, T = TSchemaExtended>(schema?:
|
|
416
|
+
append(schema?: SchemaMap<TSchema>): this;
|
|
417
|
+
append<TSchemaExtended = any, T = TSchemaExtended>(schema?: SchemaMap<T>): ObjectSchema<T>;
|
|
380
418
|
/**
|
|
381
419
|
* Verifies an assertion where.
|
|
382
420
|
*/
|
|
383
|
-
assert(ref: string | Reference, schema:
|
|
421
|
+
assert(ref: string | Reference, schema: SchemaLike, message?: string): this;
|
|
384
422
|
/**
|
|
385
423
|
* Requires the object to be an instance of a given constructor.
|
|
386
424
|
*
|
|
@@ -391,7 +429,7 @@ export interface ObjectSchema<TSchema = any> extends AnySchema<TSchema> {
|
|
|
391
429
|
/**
|
|
392
430
|
* Sets or extends the allowed object keys.
|
|
393
431
|
*/
|
|
394
|
-
keys(schema?:
|
|
432
|
+
keys(schema?: SchemaMap<TSchema>): this;
|
|
395
433
|
/**
|
|
396
434
|
* Specifies the exact number of keys in the object.
|
|
397
435
|
*/
|
|
@@ -428,7 +466,7 @@ export interface ObjectSchema<TSchema = any> extends AnySchema<TSchema> {
|
|
|
428
466
|
* @param pattern - a pattern that can be either a regular expression or a joi schema that will be tested against the unknown key names
|
|
429
467
|
* @param schema - the schema object matching keys must validate against
|
|
430
468
|
*/
|
|
431
|
-
pattern(pattern: RegExp |
|
|
469
|
+
pattern(pattern: RegExp | SchemaLike, schema: SchemaLike, options?: Joi.ObjectPatternOptions): this;
|
|
432
470
|
/**
|
|
433
471
|
* Requires the object to be a Joi reference.
|
|
434
472
|
*/
|
|
@@ -444,7 +482,7 @@ export interface ObjectSchema<TSchema = any> extends AnySchema<TSchema> {
|
|
|
444
482
|
/**
|
|
445
483
|
* Requires the object to be a Joi schema instance.
|
|
446
484
|
*/
|
|
447
|
-
schema(type?:
|
|
485
|
+
schema(type?: SchemaLike): this;
|
|
448
486
|
/**
|
|
449
487
|
* Overrides the handling of unknown keys for the scope of the current object only (does not apply to children).
|
|
450
488
|
*/
|
|
@@ -489,7 +527,7 @@ export interface ArraySchema<TSchema = any[]> extends AnySchema<TSchema> {
|
|
|
489
527
|
* `schema` - the validation rules required to satisfy the assertion. If the `schema` includes references, they are resolved against
|
|
490
528
|
* the array item being tested, not the value of the `ref` target.
|
|
491
529
|
*/
|
|
492
|
-
has(schema:
|
|
530
|
+
has(schema: SchemaLike): this;
|
|
493
531
|
/**
|
|
494
532
|
* Specifies the exact number of items in the array.
|
|
495
533
|
*/
|
|
@@ -545,7 +583,7 @@ export interface ArraySchema<TSchema = any[]> extends AnySchema<TSchema> {
|
|
|
545
583
|
*
|
|
546
584
|
* @param type - a joi schema object to validate each array item against.
|
|
547
585
|
*/
|
|
548
|
-
items(...types:
|
|
586
|
+
items(...types: SchemaLikeWithoutArray[]): this;
|
|
549
587
|
/**
|
|
550
588
|
* Lists the types in sequence order for the array values where:
|
|
551
589
|
* @param type - a joi schema object to validate against each array item in sequence order. type can be multiple values passed as individual arguments.
|
|
@@ -553,7 +591,7 @@ export interface ArraySchema<TSchema = any[]> extends AnySchema<TSchema> {
|
|
|
553
591
|
* Errors will contain the number of items that didn't match.
|
|
554
592
|
* Any unmatched item having a label will be mentioned explicitly.
|
|
555
593
|
*/
|
|
556
|
-
ordered(...types:
|
|
594
|
+
ordered(...types: SchemaLikeWithoutArray[]): this;
|
|
557
595
|
}
|
|
558
596
|
/**
|
|
559
597
|
* Schema type for date validation.
|
|
@@ -629,7 +667,7 @@ export interface AlternativesSchema<TSchema = any> extends AnySchema<TSchema> {
|
|
|
629
667
|
/**
|
|
630
668
|
* Adds an alternative schema type for attempting to match against the validated value.
|
|
631
669
|
*/
|
|
632
|
-
try(...types:
|
|
670
|
+
try(...types: SchemaLikeWithoutArray[]): this;
|
|
633
671
|
default(value?: DefaultableValue): this;
|
|
634
672
|
}
|
|
635
673
|
/**
|
|
@@ -637,7 +675,7 @@ export interface AlternativesSchema<TSchema = any> extends AnySchema<TSchema> {
|
|
|
637
675
|
*
|
|
638
676
|
* @public
|
|
639
677
|
*/
|
|
640
|
-
export interface FunctionSchema<TSchema = Function> extends
|
|
678
|
+
export interface FunctionSchema<TSchema = Function> extends ObjectSchema<TSchema> {
|
|
641
679
|
/**
|
|
642
680
|
* Specifies the arity of the function where:
|
|
643
681
|
* @param n - the arity expected.
|
|
@@ -688,4 +726,14 @@ export interface SymbolSchema<TSchema = symbol> extends AnySchema<TSchema> {
|
|
|
688
726
|
/**
|
|
689
727
|
* A union type that includes all schema types.
|
|
690
728
|
*/
|
|
691
|
-
export type Schema = AnySchema |
|
|
729
|
+
export type Schema<P = any> = AnySchema<P> | ArraySchema<P> | AlternativesSchema<P> | BinarySchema<P> | BooleanSchema<P> | DateSchema<P> | FunctionSchema<P> | NumberSchema<P> | ObjectSchema<P> | StringSchema<P> | LinkSchema<P> | SymbolSchema<P> | BigIntSchema<P> | DatetimeSchema<P> | PhoneSchema<P>;
|
|
730
|
+
export type ObjectPropertiesSchema<T = any> = true extends Joi.IsNonPrimitiveSubsetUnion<Exclude<T, undefined | null>> ? AlternativesSchema : T extends Joi.NullableType<string> ? StringSchema : T extends Joi.NullableType<number> ? NumberSchema : T extends Joi.NullableType<bigint> ? BigIntSchema : T extends Joi.NullableType<boolean> ? BooleanSchema : T extends Joi.NullableType<Date> ? DateSchema : T extends Joi.NullableType<Buffer> ? BinarySchema : T extends Joi.NullableType<Array<any>> ? ArraySchema : T extends DateTime ? DatetimeSchema : T extends Joi.NullableType<object> ? StrictSchemaMap<T> | ObjectSchema<T> : never;
|
|
731
|
+
export type PartialSchemaMap<TSchema = any> = {
|
|
732
|
+
[key in keyof TSchema]?: SchemaLike | SchemaLike[];
|
|
733
|
+
};
|
|
734
|
+
export type StrictSchemaMap<TSchema = any> = {
|
|
735
|
+
[key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]>;
|
|
736
|
+
};
|
|
737
|
+
export type SchemaMap<TSchema = any, IsStrict = false> = IsStrict extends true ? StrictSchemaMap<TSchema> : PartialSchemaMap<TSchema>;
|
|
738
|
+
export type SchemaLikeWithoutArray = string | number | boolean | null | Schema | SchemaMap;
|
|
739
|
+
export type SchemaLike = SchemaLikeWithoutArray | object;
|