@nhtio/validation 0.1.0-master-cd50e735 → 0.1.0-master-fefe361a
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.mjs +23679 -23263
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/private/index.d.ts +59 -4
- 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
|
@@ -4,6 +4,7 @@ import type { BigIntSchema } from './schemas/bigint';
|
|
|
4
4
|
import type { DatetimeSchema } from './schemas/datetime';
|
|
5
5
|
import type { CountryOrUnknown } from '@nhtio/phone-object';
|
|
6
6
|
import type { Root, Reference, SchemaMap, SchemaLike } from 'joi';
|
|
7
|
+
import type { I18nCallback, SetI18nCallback } from './patches/i18n';
|
|
7
8
|
import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberSchema, BooleanSchema, ObjectSchema, ArraySchema, DateSchema } from './schemas';
|
|
8
9
|
/**
|
|
9
10
|
* Extended Joi root interface that includes custom schema types for
|
|
@@ -24,7 +25,7 @@ import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberS
|
|
|
24
25
|
*
|
|
25
26
|
* @public
|
|
26
27
|
*/
|
|
27
|
-
export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'alternatives' | 'alt'> {
|
|
28
|
+
export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' | 'number' | 'bool' | 'boolean' | 'object' | 'array' | 'date' | 'alternatives' | 'alt'> {
|
|
28
29
|
/**
|
|
29
30
|
* Generates a schema object that matches any data type.
|
|
30
31
|
*/
|
|
@@ -85,6 +86,62 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
85
86
|
* @param country - Optional country code or reference for phone validation
|
|
86
87
|
*/
|
|
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;
|
|
88
145
|
}
|
|
89
146
|
/**
|
|
90
147
|
* Extended Joi instance with custom schema types.
|
|
@@ -107,8 +164,6 @@ export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' |
|
|
|
107
164
|
* @public
|
|
108
165
|
*/
|
|
109
166
|
export declare const validator: ValidationRoot;
|
|
110
|
-
export type { BigIntSchema };
|
|
111
|
-
export type { DatetimeSchema };
|
|
112
|
-
export type { PhoneSchema };
|
|
167
|
+
export type { BigIntSchema, DatetimeSchema, PhoneSchema, SetI18nCallback, I18nCallback };
|
|
113
168
|
export type * from './schemas';
|
|
114
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.
|