@nhtio/validation 1.20250804.0 → 1.20250813.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 +42 -9
- package/index.cjs.map +1 -1
- package/index.d.ts +1 -0
- package/index.mjs +42 -9
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- package/private/index.d.ts +19 -2
- package/private/schemas/phone.d.ts +180 -0
- package/private/schemas.d.ts +27 -9
package/package.json
CHANGED
package/private/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { PhoneSchema } from './schemas/phone';
|
|
2
2
|
import type { BigIntSchema } from './schemas/bigint';
|
|
3
3
|
import type { DatetimeSchema } from './schemas/datetime';
|
|
4
|
+
import type { CountryOrUnknown } from '@nhtio/phone-object';
|
|
5
|
+
import type { Root, Reference, SchemaMap, SchemaLike } from 'joi';
|
|
6
|
+
import type { AlternativesSchema, AnySchema, StringSchema, BinarySchema, NumberSchema, BooleanSchema, ObjectSchema, ArraySchema, DateSchema } from './schemas';
|
|
4
7
|
/**
|
|
5
8
|
* Extended Joi root interface that includes custom schema types for
|
|
6
9
|
* additional validation scenarios.
|
|
@@ -20,9 +23,22 @@ import type { DatetimeSchema } from './schemas/datetime';
|
|
|
20
23
|
*
|
|
21
24
|
* @public
|
|
22
25
|
*/
|
|
23
|
-
export interface ValidationRoot extends Root {
|
|
26
|
+
export interface ValidationRoot extends Omit<Root, 'any' | 'string' | 'binary' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'alternatives' | 'alt'> {
|
|
27
|
+
any: () => AnySchema;
|
|
28
|
+
string: () => StringSchema;
|
|
29
|
+
binary: () => BinarySchema;
|
|
30
|
+
number: () => NumberSchema;
|
|
31
|
+
boolean: () => BooleanSchema;
|
|
32
|
+
object<TSchema = any, IsStrict = false, T = TSchema>(schema?: SchemaMap<T, IsStrict>): ObjectSchema<TSchema>;
|
|
33
|
+
array: () => ArraySchema;
|
|
34
|
+
date: () => DateSchema;
|
|
24
35
|
bigint(): BigIntSchema;
|
|
25
36
|
datetime(): DatetimeSchema;
|
|
37
|
+
phone(country?: CountryOrUnknown | Reference | null): PhoneSchema;
|
|
38
|
+
alternatives<TSchema = any>(types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
39
|
+
alternatives<TSchema = any>(...types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
40
|
+
alt<TSchema = any>(types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
41
|
+
alt<TSchema = any>(...types: SchemaLike[]): AlternativesSchema<TSchema>;
|
|
26
42
|
}
|
|
27
43
|
/**
|
|
28
44
|
* Extended Joi instance with custom schema types.
|
|
@@ -47,5 +63,6 @@ export interface ValidationRoot extends Root {
|
|
|
47
63
|
export declare const validator: ValidationRoot;
|
|
48
64
|
export type { BigIntSchema };
|
|
49
65
|
export type { DatetimeSchema };
|
|
66
|
+
export type { PhoneSchema };
|
|
50
67
|
export type * from './schemas';
|
|
51
68
|
export { encode, decode } from './utils';
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { AnySchema } from '../../index';
|
|
2
|
+
import type { CountryOrUnknown, PhoneTypes } from '@nhtio/phone-object';
|
|
3
|
+
import type { ExtensionFactory, Reference } from 'joi';
|
|
4
|
+
/**
|
|
5
|
+
* Phone number validation schema interface extending AnySchema.
|
|
6
|
+
* Provides methods for validating and formatting phone numbers with various constraints.
|
|
7
|
+
*
|
|
8
|
+
* @template TSchema - The schema type, defaults to string
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import Joi from 'joi'
|
|
13
|
+
* import { phone } from './phone'
|
|
14
|
+
*
|
|
15
|
+
* const extended = Joi.extend(phone)
|
|
16
|
+
* const schema = extended.phone().country('US').mobile()
|
|
17
|
+
*
|
|
18
|
+
* const result = schema.validate('+1234567890')
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export interface PhoneSchema<TSchema = string> extends Omit<AnySchema<TSchema>, 'cast'> {
|
|
22
|
+
/**
|
|
23
|
+
* Sets the country context for phone number validation.
|
|
24
|
+
*
|
|
25
|
+
* @param country - Country code (ISO 3166-1 alpha-2), country name, Joi reference, or null
|
|
26
|
+
* @returns The schema instance for chaining
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* schema.country('US')
|
|
31
|
+
* schema.country('United States')
|
|
32
|
+
* schema.country(Joi.ref('countryField'))
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
country(country: CountryOrUnknown | Reference | null): this;
|
|
36
|
+
/**
|
|
37
|
+
* Sets the output format for the phone number.
|
|
38
|
+
*
|
|
39
|
+
* @param as - The desired output format
|
|
40
|
+
* @returns The schema instance for chaining
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* schema.format('international') // +1 234 567 8900
|
|
45
|
+
* schema.format('national') // (234) 567-8900
|
|
46
|
+
* schema.format('e164') // +12345678900
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
format(as: 'e164' | 'international' | 'national' | 'raw' | 'timezone' | 'type' | 'country'): this;
|
|
50
|
+
/**
|
|
51
|
+
* Validates that the phone number is a fixed line or fixed line/mobile number.
|
|
52
|
+
*
|
|
53
|
+
* @returns The schema instance for chaining
|
|
54
|
+
*/
|
|
55
|
+
fixedLine(): this;
|
|
56
|
+
/**
|
|
57
|
+
* Validates that the phone number is a mobile or fixed line/mobile number.
|
|
58
|
+
*
|
|
59
|
+
* @returns The schema instance for chaining
|
|
60
|
+
*/
|
|
61
|
+
mobile(): this;
|
|
62
|
+
/**
|
|
63
|
+
* Validates that the phone number is strictly a fixed line number only.
|
|
64
|
+
*
|
|
65
|
+
* @returns The schema instance for chaining
|
|
66
|
+
*/
|
|
67
|
+
strictFixedLine(): this;
|
|
68
|
+
/**
|
|
69
|
+
* Validates that the phone number is strictly a mobile number only.
|
|
70
|
+
*
|
|
71
|
+
* @returns The schema instance for chaining
|
|
72
|
+
*/
|
|
73
|
+
strictMobile(): this;
|
|
74
|
+
/**
|
|
75
|
+
* Validates that the phone number is either a fixed line or mobile number.
|
|
76
|
+
*
|
|
77
|
+
* @returns The schema instance for chaining
|
|
78
|
+
*/
|
|
79
|
+
fixedLineOrMobile(): this;
|
|
80
|
+
/**
|
|
81
|
+
* Validates that the phone number is a toll-free number.
|
|
82
|
+
*
|
|
83
|
+
* @returns The schema instance for chaining
|
|
84
|
+
*/
|
|
85
|
+
tollFree(): this;
|
|
86
|
+
/**
|
|
87
|
+
* Validates that the phone number is a premium rate number.
|
|
88
|
+
*
|
|
89
|
+
* @returns The schema instance for chaining
|
|
90
|
+
*/
|
|
91
|
+
premiumRate(): this;
|
|
92
|
+
/**
|
|
93
|
+
* Validates that the phone number is a shared cost number.
|
|
94
|
+
*
|
|
95
|
+
* @returns The schema instance for chaining
|
|
96
|
+
*/
|
|
97
|
+
sharedCost(): this;
|
|
98
|
+
/**
|
|
99
|
+
* Validates that the phone number is a VoIP number.
|
|
100
|
+
*
|
|
101
|
+
* @returns The schema instance for chaining
|
|
102
|
+
*/
|
|
103
|
+
voip(): this;
|
|
104
|
+
/**
|
|
105
|
+
* Validates that the phone number is a personal number.
|
|
106
|
+
*
|
|
107
|
+
* @returns The schema instance for chaining
|
|
108
|
+
*/
|
|
109
|
+
personalNumber(): this;
|
|
110
|
+
/**
|
|
111
|
+
* Validates that the phone number is a pager number.
|
|
112
|
+
*
|
|
113
|
+
* @returns The schema instance for chaining
|
|
114
|
+
*/
|
|
115
|
+
pager(): this;
|
|
116
|
+
/**
|
|
117
|
+
* Validates that the phone number is a UAN (Universal Access Number).
|
|
118
|
+
*
|
|
119
|
+
* @returns The schema instance for chaining
|
|
120
|
+
*/
|
|
121
|
+
uan(): this;
|
|
122
|
+
/**
|
|
123
|
+
* Validates that the phone number is a voicemail number.
|
|
124
|
+
*
|
|
125
|
+
* @returns The schema instance for chaining
|
|
126
|
+
*/
|
|
127
|
+
voicemail(): this;
|
|
128
|
+
/**
|
|
129
|
+
* Validates that the phone number is of unknown type.
|
|
130
|
+
*
|
|
131
|
+
* @returns The schema instance for chaining
|
|
132
|
+
*/
|
|
133
|
+
unknown(): this;
|
|
134
|
+
/**
|
|
135
|
+
* Validates that the phone number matches one of the specified types.
|
|
136
|
+
*
|
|
137
|
+
* @param types - Array of phone types to allow
|
|
138
|
+
* @returns The schema instance for chaining
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* schema.types('MOBILE', 'FIXED_LINE')
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
types(...types: PhoneTypes[]): this;
|
|
146
|
+
cast(to: 'number' | 'string' | 'object'): this;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Joi extension factory for phone number validation.
|
|
150
|
+
* Creates a custom Joi schema type that validates phone numbers using the @nhtio/phone-object library.
|
|
151
|
+
*
|
|
152
|
+
* @param joi - The Joi instance to extend
|
|
153
|
+
* @returns The phone schema extension definition
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* import Joi from 'joi'
|
|
158
|
+
* import { phone } from './phone'
|
|
159
|
+
*
|
|
160
|
+
* const extended = Joi.extend(phone)
|
|
161
|
+
*
|
|
162
|
+
* // Basic phone validation
|
|
163
|
+
* const schema = extended.phone()
|
|
164
|
+
* schema.validate('+1234567890') // validates any phone number
|
|
165
|
+
*
|
|
166
|
+
* // Country-specific validation
|
|
167
|
+
* const usSchema = extended.phone().country('US')
|
|
168
|
+
* usSchema.validate('(555) 123-4567')
|
|
169
|
+
*
|
|
170
|
+
* // Type-specific validation
|
|
171
|
+
* const mobileSchema = extended.phone().mobile()
|
|
172
|
+
* mobileSchema.validate('+1234567890')
|
|
173
|
+
*
|
|
174
|
+
* // Format output
|
|
175
|
+
* const formattedSchema = extended.phone().format('international')
|
|
176
|
+
* const result = formattedSchema.validate('+1234567890')
|
|
177
|
+
* // result.value would be "+1 234 567 8900"
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare const phone: ExtensionFactory;
|
package/private/schemas.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { DateTime } from 'luxon';
|
|
2
|
+
import type { PhoneSchema } from './schemas/phone';
|
|
3
|
+
import type { BigIntSchema } from './schemas/bigint';
|
|
4
|
+
import type { DatetimeSchema } from './schemas/datetime';
|
|
5
|
+
import type { AnySchema as JoiAnySchema, StringSchema as JoiStringSchema, BinarySchema as JoiBinarySchema, NumberSchema as JoiNumberSchema, BooleanSchema as JoiBooleanSchema, ObjectSchema as JoiObjectSchema, ArraySchema as JoiArraySchema, DateSchema as JoiDateSchema, AlternativesSchema as JoiAlternativesSchema, Reference, BasicType, CustomHelpers, SchemaLikeWithoutArray } from 'joi';
|
|
6
|
+
export type DefaultableValue = Reference | BasicType | DateTime | bigint | ((parent: any, helpers: CustomHelpers) => Reference | BasicType | DateTime | bigint);
|
|
7
|
+
export type ExtendedSchemaLikeWithoutArray = SchemaLikeWithoutArray | PhoneSchema | BigIntSchema | DatetimeSchema;
|
|
2
8
|
/**
|
|
3
9
|
* Base schema type for all validation schemas.
|
|
4
10
|
*
|
|
@@ -24,7 +30,8 @@ export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, 'd
|
|
|
24
30
|
* const schemaWithFunction = joi.number().default((parent, helpers) => parent.someOtherField * 2)
|
|
25
31
|
* ```
|
|
26
32
|
*/
|
|
27
|
-
default(value?:
|
|
33
|
+
default(value?: DefaultableValue): this;
|
|
34
|
+
cast(to: 'map' | 'number' | 'set' | 'string' | 'object'): this;
|
|
28
35
|
}
|
|
29
36
|
/**
|
|
30
37
|
* Schema type for string validation.
|
|
@@ -42,7 +49,7 @@ export interface StringSchema<TSchema = string> extends Omit<JoiStringSchema<TSc
|
|
|
42
49
|
* - `parent` - A clone of the object containing the value being validated
|
|
43
50
|
* - `helpers` - Validation helper functions for custom validation logic
|
|
44
51
|
*/
|
|
45
|
-
default(value?:
|
|
52
|
+
default(value?: DefaultableValue): this;
|
|
46
53
|
}
|
|
47
54
|
/**
|
|
48
55
|
* Schema type for binary data validation.
|
|
@@ -60,7 +67,7 @@ export interface BinarySchema<TSchema = Buffer> extends Omit<JoiBinarySchema<TSc
|
|
|
60
67
|
* - `parent` - A clone of the object containing the value being validated
|
|
61
68
|
* - `helpers` - Validation helper functions for custom validation logic
|
|
62
69
|
*/
|
|
63
|
-
default(value?:
|
|
70
|
+
default(value?: DefaultableValue): this;
|
|
64
71
|
}
|
|
65
72
|
/**
|
|
66
73
|
* Schema type for number validation.
|
|
@@ -78,7 +85,7 @@ export interface NumberSchema<TSchema = number> extends Omit<JoiNumberSchema<TSc
|
|
|
78
85
|
* - `parent` - A clone of the object containing the value being validated
|
|
79
86
|
* - `helpers` - Validation helper functions for custom validation logic
|
|
80
87
|
*/
|
|
81
|
-
default(value?:
|
|
88
|
+
default(value?: DefaultableValue): this;
|
|
82
89
|
}
|
|
83
90
|
/**
|
|
84
91
|
* Schema type for boolean validation.
|
|
@@ -96,7 +103,7 @@ export interface BooleanSchema<TSchema = boolean> extends Omit<JoiBooleanSchema<
|
|
|
96
103
|
* - `parent` - A clone of the object containing the value being validated
|
|
97
104
|
* - `helpers` - Validation helper functions for custom validation logic
|
|
98
105
|
*/
|
|
99
|
-
default(value?:
|
|
106
|
+
default(value?: DefaultableValue): this;
|
|
100
107
|
}
|
|
101
108
|
/**
|
|
102
109
|
* Schema type for object validation.
|
|
@@ -117,7 +124,7 @@ export interface ObjectSchema<TSchema = any> extends Omit<JoiObjectSchema<TSchem
|
|
|
117
124
|
* When called without any value on an object schema type, a default value will be
|
|
118
125
|
* automatically generated based on the default values of the object keys.
|
|
119
126
|
*/
|
|
120
|
-
default(value?:
|
|
127
|
+
default(value?: DefaultableValue): this;
|
|
121
128
|
}
|
|
122
129
|
/**
|
|
123
130
|
* Schema type for array validation.
|
|
@@ -135,7 +142,9 @@ export interface ArraySchema<TSchema = any[]> extends Omit<JoiArraySchema<TSchem
|
|
|
135
142
|
* - `parent` - A clone of the object containing the value being validated
|
|
136
143
|
* - `helpers` - Validation helper functions for custom validation logic
|
|
137
144
|
*/
|
|
138
|
-
default(value?:
|
|
145
|
+
default(value?: DefaultableValue): this;
|
|
146
|
+
items(...types: ExtendedSchemaLikeWithoutArray[]): this;
|
|
147
|
+
ordered(...types: ExtendedSchemaLikeWithoutArray[]): this;
|
|
139
148
|
}
|
|
140
149
|
/**
|
|
141
150
|
* Schema type for date validation.
|
|
@@ -153,5 +162,14 @@ export interface DateSchema<TSchema = Date> extends Omit<JoiDateSchema<TSchema>,
|
|
|
153
162
|
* - `parent` - A clone of the object containing the value being validated
|
|
154
163
|
* - `helpers` - Validation helper functions for custom validation logic
|
|
155
164
|
*/
|
|
156
|
-
default(value?:
|
|
165
|
+
default(value?: DefaultableValue): this;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Schema type for alternatives validation.
|
|
169
|
+
*
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
export interface AlternativesSchema<TSchema = any> extends Omit<JoiAlternativesSchema<TSchema>, 'default' | 'try'> {
|
|
173
|
+
try(...types: ExtendedSchemaLikeWithoutArray[]): this;
|
|
174
|
+
default(value?: DefaultableValue): this;
|
|
157
175
|
}
|