@nhtio/validation 1.20250813.0 → 1.20250814.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 +13 -13
- package/index.cjs.map +1 -1
- package/index.mjs +13 -13
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/private/index.d.ts +115 -14
- 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 +527 -11
- package/private/utils.d.ts +3 -3
package/package.json
CHANGED
package/private/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { DateTime } from 'luxon';
|
|
1
2
|
import type { PhoneSchema } from './schemas/phone';
|
|
2
3
|
import type { BigIntSchema } from './schemas/bigint';
|
|
3
4
|
import type { DatetimeSchema } from './schemas/datetime';
|
|
4
5
|
import type { CountryOrUnknown } from '@nhtio/phone-object';
|
|
5
6
|
import type { Root, Reference, SchemaMap, SchemaLike } from 'joi';
|
|
7
|
+
import type { I18nCallback, SetI18nCallback } from './patches/i18n';
|
|
6
8
|
import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberSchema, BooleanSchema, ObjectSchema, ArraySchema, DateSchema } from './schemas';
|
|
7
9
|
/**
|
|
8
10
|
* Extended Joi root interface that includes custom schema types for
|
|
@@ -23,22 +25,123 @@ import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberS
|
|
|
23
25
|
*
|
|
24
26
|
* @public
|
|
25
27
|
*/
|
|
26
|
-
export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'alternatives' | 'alt'> {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' | 'number' | 'bool' | 'boolean' | 'object' | 'array' | 'date' | 'alternatives' | 'alt'> {
|
|
29
|
+
/**
|
|
30
|
+
* Generates a schema object that matches any data type.
|
|
31
|
+
*/
|
|
32
|
+
any<TSchema = any>(): AnySchema<TSchema>;
|
|
33
|
+
/**
|
|
34
|
+
* Generates a schema object that matches an array data type.
|
|
35
|
+
*/
|
|
36
|
+
array<TSchema = any[]>(): ArraySchema<TSchema>;
|
|
37
|
+
/**
|
|
38
|
+
* Generates a schema object that matches a boolean data type (as well as the strings 'true', 'false', 'yes', and 'no'). Can also be called via boolean().
|
|
39
|
+
*/
|
|
40
|
+
bool<TSchema = boolean>(): BooleanSchema<TSchema>;
|
|
41
|
+
/**
|
|
42
|
+
* Generates a schema object that matches a boolean data type (as well as the strings 'true', 'false', 'yes', and 'no'). Can also be called via bool().
|
|
43
|
+
*/
|
|
44
|
+
boolean<TSchema = boolean>(): BooleanSchema<TSchema>;
|
|
45
|
+
/**
|
|
46
|
+
* Generates a schema object that matches a Buffer data type (as well as the strings which will be converted to Buffers).
|
|
47
|
+
*/
|
|
48
|
+
binary<TSchema = Buffer>(): BinarySchema<TSchema>;
|
|
49
|
+
/**
|
|
50
|
+
* Generates a schema object that matches a date type (as well as a JavaScript date string or number of milliseconds).
|
|
51
|
+
*/
|
|
52
|
+
date<TSchema = Date>(): DateSchema<TSchema>;
|
|
53
|
+
/**
|
|
54
|
+
* Generates a schema object that matches a number data type (as well as strings that can be converted to numbers).
|
|
55
|
+
*/
|
|
56
|
+
number<TSchema = number>(): NumberSchema<TSchema>;
|
|
57
|
+
/**
|
|
58
|
+
* Generates a schema object that matches an object data type (as well as JSON strings that have been parsed into objects).
|
|
59
|
+
*/
|
|
32
60
|
object<TSchema = any, IsStrict = false, T = TSchema>(schema?: SchemaMap<T, IsStrict>): ObjectSchema<TSchema>;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
61
|
+
/**
|
|
62
|
+
* 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
|
+
*/
|
|
64
|
+
string<TSchema = string>(): StringSchema<TSchema>;
|
|
65
|
+
/**
|
|
66
|
+
* Generates a type that will match one of the provided alternative schemas
|
|
67
|
+
*/
|
|
38
68
|
alternatives<TSchema = any>(types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
39
69
|
alternatives<TSchema = any>(...types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
70
|
+
/**
|
|
71
|
+
* Alias for `alternatives`
|
|
72
|
+
*/
|
|
40
73
|
alt<TSchema = any>(types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
41
74
|
alt<TSchema = any>(...types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
75
|
+
/**
|
|
76
|
+
* Generates a schema object that matches a BigInt data type.
|
|
77
|
+
*/
|
|
78
|
+
bigint<TSchema = bigint>(): BigIntSchema<TSchema>;
|
|
79
|
+
/**
|
|
80
|
+
* Generates a schema object that matches a DateTime data type.
|
|
81
|
+
*/
|
|
82
|
+
datetime<TSchema = DateTime>(): DatetimeSchema<TSchema>;
|
|
83
|
+
/**
|
|
84
|
+
* Generates a schema object that matches a phone number data type.
|
|
85
|
+
*
|
|
86
|
+
* @param country - Optional country code or reference for phone validation
|
|
87
|
+
*/
|
|
88
|
+
phone<TSchema = string>(country?: CountryOrUnknown | Reference | null): PhoneSchema<TSchema>;
|
|
89
|
+
/**
|
|
90
|
+
* Sets a global internationalization callback that applies to all validator instances.
|
|
91
|
+
*
|
|
92
|
+
* This method sets a fallback translation function that will be used by the `$i18n` method
|
|
93
|
+
* when no instance-specific callback has been set via `$setI18n`. Once `$setI18n` is called
|
|
94
|
+
* on an instance, that instance will use its own callback instead of the global one.
|
|
95
|
+
*
|
|
96
|
+
* The global callback provides a convenient way to set default translations across your
|
|
97
|
+
* entire application while still allowing individual validator instances to override
|
|
98
|
+
* with their own translations when needed.
|
|
99
|
+
*
|
|
100
|
+
* @param callback - The I18nCallback function that will handle global message translation
|
|
101
|
+
* @returns The validator instance for chaining
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* // Set up global Spanish translations
|
|
106
|
+
* validator.$setGlobalI18n((term: string) => {
|
|
107
|
+
* return spanishTranslations[term] || term
|
|
108
|
+
* })
|
|
109
|
+
*
|
|
110
|
+
* // All validators will now use Spanish by default
|
|
111
|
+
* const schema1 = validator.string().min(5)
|
|
112
|
+
* const schema2 = validator.number().positive()
|
|
113
|
+
*
|
|
114
|
+
* // But you can still override for specific instances
|
|
115
|
+
* const germanSchema = validator.string().$setI18n(germanCallback)
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
$setI18n: SetI18nCallback<ValidationRoot>;
|
|
119
|
+
/**
|
|
120
|
+
* Clears the global internationalization callback.
|
|
121
|
+
*
|
|
122
|
+
* This method removes any previously set global i18n callback, causing the `$i18n` method
|
|
123
|
+
* to fall back to default English messages for validator instances that haven't had their
|
|
124
|
+
* own callback set via `$setI18n`.
|
|
125
|
+
*
|
|
126
|
+
* This is useful for testing scenarios, dynamic language switching, or memory cleanup
|
|
127
|
+
* when you no longer need global translations.
|
|
128
|
+
*
|
|
129
|
+
* @returns The validator instance for chaining
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* // Set up global translations
|
|
134
|
+
* validator.$setGlobalI18n(spanishCallback)
|
|
135
|
+
*
|
|
136
|
+
* // Later, clear them to return to default English
|
|
137
|
+
* validator.$clearGlobalI18n()
|
|
138
|
+
*
|
|
139
|
+
* // Now all validators use default English messages again
|
|
140
|
+
* const schema = validator.string().min(5) // Uses English messages
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
$clearI18n: () => ValidationRoot;
|
|
144
|
+
$i18n: I18nCallback;
|
|
42
145
|
}
|
|
43
146
|
/**
|
|
44
147
|
* Extended Joi instance with custom schema types.
|
|
@@ -61,8 +164,6 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
61
164
|
* @public
|
|
62
165
|
*/
|
|
63
166
|
export declare const validator: ValidationRoot;
|
|
64
|
-
export type { BigIntSchema };
|
|
65
|
-
export type { DatetimeSchema };
|
|
66
|
-
export type { PhoneSchema };
|
|
167
|
+
export type { BigIntSchema, DatetimeSchema, PhoneSchema, SetI18nCallback, I18nCallback };
|
|
67
168
|
export type * from './schemas';
|
|
68
169
|
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.
|