@nhtio/validation 1.20250720.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nhtio/validation",
3
- "version": "1.20250720.0",
3
+ "version": "1.20250813.0",
4
4
  "description": "A powerful schema description language and data validator",
5
5
  "keywords": [],
6
6
  "author": "Jak Giveon <jak@nht.io>",
@@ -1,6 +1,9 @@
1
- import type { Root } from 'joi';
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,3 @@
1
+ import type { Root } from 'joi';
2
+ import type { ValidationRoot } from '../index';
3
+ export declare const patch: (root: Root) => ValidationRoot;
@@ -0,0 +1,2 @@
1
+ import type { Root } from 'joi';
2
+ export declare const json: (root: Root) => Root;
@@ -2,8 +2,8 @@ import { DateTime } from 'luxon';
2
2
  import { Dayjs } from 'dayjs';
3
3
  import type { Tokens } from '../types';
4
4
  import type { AnySchema } from '../schemas';
5
- import type { Zone, LocaleOptions, DateObjectUnits } from 'luxon';
6
5
  import type { ExtensionFactory, Reference } from 'joi';
6
+ import type { Zone, LocaleOptions, DateObjectUnits, ZoneOptions, ToISOTimeOptions, ToISODateOptions, ToSQLOptions, ToRelativeOptions } from 'luxon';
7
7
  /**
8
8
  * Types that can be parsed into a DateTime object.
9
9
  *
@@ -232,7 +232,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
232
232
  * // Returns DateTime in Eastern timezone
233
233
  * ```
234
234
  */
235
- setZone(zone: string | Zone): this;
235
+ setZone(zone: string | Zone, opts?: ZoneOptions): this;
236
236
  /**
237
237
  * Formats the datetime using a custom format string during validation.
238
238
  *
@@ -264,6 +264,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
264
264
  /**
265
265
  * Converts the datetime to ISO 8601 string format during validation.
266
266
  *
267
+ * @param opts - Options for ISO string formatting
267
268
  * @returns The schema instance for method chaining
268
269
  *
269
270
  * @example
@@ -273,10 +274,11 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
273
274
  * // Returns: "2023-07-15T15:30:00.000Z"
274
275
  * ```
275
276
  */
276
- toISO(): this;
277
+ toISO(opts?: ToISOTimeOptions): this;
277
278
  /**
278
279
  * Converts the datetime to ISO date format (YYYY-MM-DD) during validation.
279
280
  *
281
+ * @param opts - Options for ISO date formatting
280
282
  * @returns The schema instance for method chaining
281
283
  *
282
284
  * @example
@@ -286,7 +288,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
286
288
  * // Returns: "2023-07-15"
287
289
  * ```
288
290
  */
289
- toISODate(): this;
291
+ toISODate(opts?: ToISODateOptions): this;
290
292
  /**
291
293
  * Converts the datetime to ISO week date format during validation.
292
294
  *
@@ -303,6 +305,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
303
305
  /**
304
306
  * Converts the datetime to ISO time format during validation.
305
307
  *
308
+ * @param opts - Options for ISO time formatting
306
309
  * @returns The schema instance for method chaining
307
310
  *
308
311
  * @example
@@ -312,7 +315,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
312
315
  * // Returns: "15:30:00.000Z"
313
316
  * ```
314
317
  */
315
- toISOTime(): this;
318
+ toISOTime(opts?: ToISOTimeOptions): this;
316
319
  /**
317
320
  * Converts the datetime to RFC 2822 format during validation.
318
321
  *
@@ -355,6 +358,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
355
358
  /**
356
359
  * Converts the datetime to SQL time format (HH:mm:ss.SSS) during validation.
357
360
  *
361
+ * @param opts - Options for SQL time formatting
358
362
  * @returns The schema instance for method chaining
359
363
  *
360
364
  * @example
@@ -364,10 +368,11 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
364
368
  * // Returns: "15:30:45.123"
365
369
  * ```
366
370
  */
367
- toSQLTime(): this;
371
+ toSQLTime(opts?: ToSQLOptions): this;
368
372
  /**
369
373
  * Converts the datetime to SQL datetime format during validation.
370
374
  *
375
+ * @param opts - Options for SQL formatting
371
376
  * @returns The schema instance for method chaining
372
377
  *
373
378
  * @example
@@ -377,7 +382,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
377
382
  * // Returns: "2023-07-15 15:30:45.123 Z"
378
383
  * ```
379
384
  */
380
- toSQL(): this;
385
+ toSQL(opts?: ToSQLOptions): this;
381
386
  /**
382
387
  * Converts the datetime to milliseconds since Unix epoch during validation.
383
388
  *
@@ -446,6 +451,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
446
451
  /**
447
452
  * Converts the datetime to a plain object representation during validation.
448
453
  *
454
+ * @param opts - Options for object conversion
449
455
  * @returns The schema instance for method chaining
450
456
  *
451
457
  * @example
@@ -455,7 +461,9 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
455
461
  * // Returns: { year: 2023, month: 7, day: 15, hour: 15, minute: 30, second: 0, millisecond: 0 }
456
462
  * ```
457
463
  */
458
- toObject(): this;
464
+ toObject(opts?: {
465
+ includeConfig?: boolean;
466
+ }): this;
459
467
  /**
460
468
  * Converts the datetime to a JavaScript Date object during validation.
461
469
  *
@@ -472,6 +480,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
472
480
  /**
473
481
  * Converts the datetime to a relative time string during validation.
474
482
  *
483
+ * @param opts - Options for relative time formatting
475
484
  * @returns The schema instance for method chaining
476
485
  *
477
486
  * @example
@@ -481,7 +490,7 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
481
490
  * // Returns: "2 hours ago" (relative to current time)
482
491
  * ```
483
492
  */
484
- toRelative(): this;
493
+ toRelative(opts?: ToRelativeOptions): this;
485
494
  /**
486
495
  * Sets the locale for the datetime during validation.
487
496
  *
@@ -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;
@@ -1,4 +1,10 @@
1
- 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, Reference, BasicType, CustomHelpers } from 'joi';
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
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
  }