@nhtio/validation 0.1.0-master-cd50e735 → 0.1.0-master-67185cae
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 +23679 -23263
- package/index.cjs.map +1 -1
- package/index.d.ts +2 -1
- package/index.mjs +23679 -23263
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/private/index.d.ts +149 -5
- package/private/patches/i18n.d.ts +51 -0
- package/private/schemas/bigint.d.ts +10 -0
- package/private/schemas/datetime.d.ts +15 -0
- package/private/schemas/phone.d.ts +19 -0
- package/private/schemas.d.ts +578 -14
- package/private/utils.d.ts +3 -3
package/package.json
CHANGED
package/private/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
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';
|
|
4
5
|
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
|
-
import type {
|
|
8
|
+
import type { I18nCallback, SetI18nCallback } from './patches/i18n';
|
|
9
|
+
import type { AnySchema, StringSchema, BinarySchema, NumberSchema, BooleanSchema, ObjectSchema, ArraySchema, DateSchema, AlternativesSchema, FunctionSchema, LinkSchema, SymbolSchema, Schema } from './schemas';
|
|
8
10
|
/**
|
|
9
11
|
* Extended Joi root interface that includes custom schema types for
|
|
10
12
|
* additional validation scenarios.
|
|
@@ -24,7 +26,7 @@ import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberS
|
|
|
24
26
|
*
|
|
25
27
|
* @public
|
|
26
28
|
*/
|
|
27
|
-
export interface ValidationRoot extends Omit<Root, 'any' | '
|
|
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'> {
|
|
28
30
|
/**
|
|
29
31
|
* Generates a schema object that matches any data type.
|
|
30
32
|
*/
|
|
@@ -49,6 +51,14 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
49
51
|
* Generates a schema object that matches a date type (as well as a JavaScript date string or number of milliseconds).
|
|
50
52
|
*/
|
|
51
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>;
|
|
52
62
|
/**
|
|
53
63
|
* Generates a schema object that matches a number data type (as well as strings that can be converted to numbers).
|
|
54
64
|
*/
|
|
@@ -61,6 +71,10 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
61
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('').
|
|
62
72
|
*/
|
|
63
73
|
string<TSchema = string>(): StringSchema<TSchema>;
|
|
74
|
+
/**
|
|
75
|
+
* Generates a schema object that matches any symbol.
|
|
76
|
+
*/
|
|
77
|
+
symbol<TSchema = Symbol>(): SymbolSchema<TSchema>;
|
|
64
78
|
/**
|
|
65
79
|
* Generates a type that will match one of the provided alternative schemas
|
|
66
80
|
*/
|
|
@@ -71,6 +85,16 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
71
85
|
*/
|
|
72
86
|
alt<TSchema = any>(types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
73
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>;
|
|
74
98
|
/**
|
|
75
99
|
* Generates a schema object that matches a BigInt data type.
|
|
76
100
|
*/
|
|
@@ -85,6 +109,128 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
85
109
|
* @param country - Optional country code or reference for phone validation
|
|
86
110
|
*/
|
|
87
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;
|
|
178
|
+
/**
|
|
179
|
+
* Sets a global internationalization callback that applies to all validator instances.
|
|
180
|
+
*
|
|
181
|
+
* This method sets a fallback translation function that will be used by the `$i18n` method
|
|
182
|
+
* when no instance-specific callback has been set via `$setI18n`. Once `$setI18n` is called
|
|
183
|
+
* on an instance, that instance will use its own callback instead of the global one.
|
|
184
|
+
*
|
|
185
|
+
* The global callback provides a convenient way to set default translations across your
|
|
186
|
+
* entire application while still allowing individual validator instances to override
|
|
187
|
+
* with their own translations when needed.
|
|
188
|
+
*
|
|
189
|
+
* @param callback - The I18nCallback function that will handle global message translation
|
|
190
|
+
* @returns The validator instance for chaining
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* // Set up global Spanish translations
|
|
195
|
+
* validator.$setGlobalI18n((term: string) => {
|
|
196
|
+
* return spanishTranslations[term] || term
|
|
197
|
+
* })
|
|
198
|
+
*
|
|
199
|
+
* // All validators will now use Spanish by default
|
|
200
|
+
* const schema1 = validator.string().min(5)
|
|
201
|
+
* const schema2 = validator.number().positive()
|
|
202
|
+
*
|
|
203
|
+
* // But you can still override for specific instances
|
|
204
|
+
* const germanSchema = validator.string().$setI18n(germanCallback)
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
$setI18n: SetI18nCallback<ValidationRoot>;
|
|
208
|
+
/**
|
|
209
|
+
* Clears the global internationalization callback.
|
|
210
|
+
*
|
|
211
|
+
* This method removes any previously set global i18n callback, causing the `$i18n` method
|
|
212
|
+
* to fall back to default English messages for validator instances that haven't had their
|
|
213
|
+
* own callback set via `$setI18n`.
|
|
214
|
+
*
|
|
215
|
+
* This is useful for testing scenarios, dynamic language switching, or memory cleanup
|
|
216
|
+
* when you no longer need global translations.
|
|
217
|
+
*
|
|
218
|
+
* @returns The validator instance for chaining
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* // Set up global translations
|
|
223
|
+
* validator.$setGlobalI18n(spanishCallback)
|
|
224
|
+
*
|
|
225
|
+
* // Later, clear them to return to default English
|
|
226
|
+
* validator.$clearGlobalI18n()
|
|
227
|
+
*
|
|
228
|
+
* // Now all validators use default English messages again
|
|
229
|
+
* const schema = validator.string().min(5) // Uses English messages
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
$clearI18n: () => ValidationRoot;
|
|
233
|
+
$i18n: I18nCallback;
|
|
88
234
|
}
|
|
89
235
|
/**
|
|
90
236
|
* Extended Joi instance with custom schema types.
|
|
@@ -107,8 +253,6 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
107
253
|
* @public
|
|
108
254
|
*/
|
|
109
255
|
export declare const validator: ValidationRoot;
|
|
110
|
-
export type { BigIntSchema };
|
|
111
|
-
export type { DatetimeSchema };
|
|
112
|
-
export type { PhoneSchema };
|
|
256
|
+
export type { BigIntSchema, DatetimeSchema, PhoneSchema, SetI18nCallback, I18nCallback };
|
|
113
257
|
export type * from './schemas';
|
|
114
258
|
export { encode, decode } from './utils';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Root } from 'joi';
|
|
2
|
+
/**
|
|
3
|
+
* Callback function type for internationalization translation.
|
|
4
|
+
*
|
|
5
|
+
* This function is responsible for translating validation message keys into
|
|
6
|
+
* localized strings. It receives a message term (like 'string.min') and should
|
|
7
|
+
* return the translated message in the appropriate language.
|
|
8
|
+
*
|
|
9
|
+
* @param term - The message key to translate (e.g., 'string.base', 'number.min')
|
|
10
|
+
* @returns The translated message string, or the original term if translation fails
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const translateToSpanish: I18nCallback = (term: string) => {
|
|
15
|
+
* const translations = {
|
|
16
|
+
* 'string.base': 'debe ser una cadena de texto',
|
|
17
|
+
* 'number.min': 'debe ser mayor o igual a {{#limit}}'
|
|
18
|
+
* }
|
|
19
|
+
* return translations[term] || term
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export interface I18nCallback {
|
|
24
|
+
(term: string): string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Callback function type for setting up internationalization.
|
|
28
|
+
*
|
|
29
|
+
* This function is used to configure the i18n system by providing a translation
|
|
30
|
+
* callback. Once called, it sets up the root validator instance to use the
|
|
31
|
+
* provided translation function for all validation messages.
|
|
32
|
+
*
|
|
33
|
+
* @param callback - The I18nCallback function that will handle message translation
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Set up Spanish translations
|
|
38
|
+
* validator.$setI18n((term: string) => {
|
|
39
|
+
* return spanishTranslations[term] || term
|
|
40
|
+
* })
|
|
41
|
+
*
|
|
42
|
+
* // Set up with external i18n library
|
|
43
|
+
* validator.$setI18n((term: string) => {
|
|
44
|
+
* return i18next.t(`validation.${term}`, { defaultValue: term })
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export interface SetI18nCallback<T = Root> {
|
|
49
|
+
(this: T, callback: I18nCallback): T;
|
|
50
|
+
}
|
|
51
|
+
export declare const i18n: (toPatch: Root) => Root;
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import type { AnySchema } from '../schemas';
|
|
2
2
|
import type { ExtensionFactory, Reference } from 'joi';
|
|
3
|
+
export declare const messages: {
|
|
4
|
+
'bigint.base': string;
|
|
5
|
+
'bigint.greater': string;
|
|
6
|
+
'bigint.less': string;
|
|
7
|
+
'bigint.max': string;
|
|
8
|
+
'bigint.min': string;
|
|
9
|
+
'bigint.multiple': string;
|
|
10
|
+
'bigint.negative': string;
|
|
11
|
+
'bigint.positive': string;
|
|
12
|
+
};
|
|
3
13
|
/**
|
|
4
14
|
* A Joi extension that adds support for BigInt validation with comprehensive
|
|
5
15
|
* comparison operations and type coercion.
|
|
@@ -4,6 +4,21 @@ import type { Tokens } from '../types';
|
|
|
4
4
|
import type { AnySchema } from '../schemas';
|
|
5
5
|
import type { ExtensionFactory, Reference } from 'joi';
|
|
6
6
|
import type { Zone, LocaleOptions, DateObjectUnits, ZoneOptions, ToISOTimeOptions, ToISODateOptions, ToSQLOptions, ToRelativeOptions } from 'luxon';
|
|
7
|
+
export declare const messages: {
|
|
8
|
+
'datetime.base': string;
|
|
9
|
+
'datetime.exactly': string;
|
|
10
|
+
'datetime.equals': string;
|
|
11
|
+
'datetime.after': string;
|
|
12
|
+
'datetime.greater': string;
|
|
13
|
+
'datetime.before': string;
|
|
14
|
+
'datetime.less': string;
|
|
15
|
+
'datetime.afterOrEqual': string;
|
|
16
|
+
'datetime.min': string;
|
|
17
|
+
'datetime.beforeOrEqual': string;
|
|
18
|
+
'datetime.max': string;
|
|
19
|
+
'datetime.weekend': string;
|
|
20
|
+
'datetime.weekday': string;
|
|
21
|
+
};
|
|
7
22
|
/**
|
|
8
23
|
* Types that can be parsed into a DateTime object.
|
|
9
24
|
*
|
|
@@ -145,6 +145,25 @@ export interface PhoneSchema<TSchema = string> extends Omit<AnySchema<TSchema>,
|
|
|
145
145
|
types(...types: PhoneTypes[]): this;
|
|
146
146
|
cast(to: 'number' | 'string' | 'object'): this;
|
|
147
147
|
}
|
|
148
|
+
export declare const messages: {
|
|
149
|
+
'phone.base': string;
|
|
150
|
+
'phone.invalid': string;
|
|
151
|
+
'phone.fixedLine': string;
|
|
152
|
+
'phone.mobile': string;
|
|
153
|
+
'phone.strictFixedLine': string;
|
|
154
|
+
'phone.strictMobile': string;
|
|
155
|
+
'phone.fixedLineOrMobile': string;
|
|
156
|
+
'phone.tollFree': string;
|
|
157
|
+
'phone.premiumRate': string;
|
|
158
|
+
'phone.sharedCost': string;
|
|
159
|
+
'phone.voip': string;
|
|
160
|
+
'phone.personalNumber': string;
|
|
161
|
+
'phone.pager': string;
|
|
162
|
+
'phone.uan': string;
|
|
163
|
+
'phone.voicemail': string;
|
|
164
|
+
'phone.unknown': string;
|
|
165
|
+
'phone.types': string;
|
|
166
|
+
};
|
|
148
167
|
/**
|
|
149
168
|
* Joi extension factory for phone number validation.
|
|
150
169
|
* Creates a custom Joi schema type that validates phone numbers using the @nhtio/phone-object library.
|