@nhtio/validation 0.1.0-master-5ddd6b01

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.
@@ -0,0 +1,562 @@
1
+ import { DateTime } from 'luxon';
2
+ import { Dayjs } from 'dayjs';
3
+ import type { Tokens } from '../types';
4
+ import type { AnySchema } from '../schemas';
5
+ import type { Zone, LocaleOptions, DateObjectUnits } from 'luxon';
6
+ import type { ExtensionFactory, Reference } from 'joi';
7
+ /**
8
+ * Types that can be parsed into a DateTime object.
9
+ *
10
+ * Supports Luxon DateTime objects, JavaScript Date objects, Day.js objects,
11
+ * ISO strings, Unix timestamps, and Luxon DateObjectUnits.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const schema = joi.datetime()
16
+ *
17
+ * // All of these are valid Parseable types:
18
+ * schema.validate('2023-01-01T00:00:00Z') // ISO string
19
+ * schema.validate(1672531200000) // Unix timestamp
20
+ * schema.validate(new Date()) // JavaScript Date
21
+ * schema.validate(DateTime.now()) // Luxon DateTime
22
+ * schema.validate({ year: 2023, month: 1, day: 1 }) // DateObjectUnits
23
+ * ```
24
+ */
25
+ export type Parseable = DateTime | Date | Dayjs | string | number | DateObjectUnits;
26
+ /**
27
+ * Schema for datetime validation with comprehensive formatting and comparison methods.
28
+ *
29
+ * Extends the base AnySchema to provide datetime-specific validation rules including
30
+ * comparisons (before/after), weekend/weekday checks, timezone conversions, and
31
+ * various output formatting options.
32
+ *
33
+ * @template TSchema - The output type after validation and potential transformation
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const schema: DatetimeSchema = joi.datetime()
38
+ * .after('2023-01-01')
39
+ * .toUTC()
40
+ * .toISO()
41
+ *
42
+ * const result = schema.validate('2023-06-15T10:30:00Z')
43
+ * // Returns ISO string in UTC timezone
44
+ * ```
45
+ */
46
+ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
47
+ /**
48
+ * Validates that the datetime is exactly equal to the specified limit.
49
+ * Uses strict equality comparison (===) between DateTime objects.
50
+ *
51
+ * @param limit - The datetime value to compare against
52
+ * @returns The schema instance for method chaining
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * const schema = joi.datetime().exactly('2023-01-01T00:00:00Z')
57
+ * schema.validate('2023-01-01T00:00:00Z') // Valid
58
+ * schema.validate('2023-01-01T00:00:01Z') // Invalid
59
+ * ```
60
+ */
61
+ exactly(limit: Parseable | Reference): this;
62
+ /**
63
+ * Validates that the datetime is equal to the specified limit.
64
+ * Uses loose equality comparison (==) between DateTime objects.
65
+ *
66
+ * @param limit - The datetime value to compare against
67
+ * @returns The schema instance for method chaining
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const schema = joi.datetime().equals('2023-01-01T00:00:00Z')
72
+ * schema.validate('2023-01-01T00:00:00.000Z') // Valid (same moment)
73
+ * ```
74
+ */
75
+ equals(limit: Parseable | Reference): this;
76
+ /**
77
+ * Validates that the datetime is after the specified limit.
78
+ *
79
+ * @param limit - The datetime value that the input must be after
80
+ * @returns The schema instance for method chaining
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * const schema = joi.datetime().after('2023-01-01T00:00:00Z')
85
+ * schema.validate('2023-01-02T00:00:00Z') // Valid
86
+ * schema.validate('2022-12-31T23:59:59Z') // Invalid
87
+ * ```
88
+ */
89
+ after(limit: Parseable | Reference): this;
90
+ /**
91
+ * Validates that the datetime is greater than the specified limit.
92
+ * Alias for `after()`.
93
+ *
94
+ * @param limit - The datetime value that the input must be greater than
95
+ * @returns The schema instance for method chaining
96
+ */
97
+ greater(limit: Parseable | Reference): this;
98
+ /**
99
+ * Validates that the datetime is before the specified limit.
100
+ *
101
+ * @param limit - The datetime value that the input must be before
102
+ * @returns The schema instance for method chaining
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * const schema = joi.datetime().before('2023-12-31T23:59:59Z')
107
+ * schema.validate('2023-06-15T12:00:00Z') // Valid
108
+ * schema.validate('2024-01-01T00:00:00Z') // Invalid
109
+ * ```
110
+ */
111
+ before(limit: Parseable | Reference): this;
112
+ /**
113
+ * Validates that the datetime is less than the specified limit.
114
+ * Alias for `before()`.
115
+ *
116
+ * @param limit - The datetime value that the input must be less than
117
+ * @returns The schema instance for method chaining
118
+ */
119
+ less(limit: Parseable | Reference): this;
120
+ /**
121
+ * Validates that the datetime is after or equal to the specified limit.
122
+ *
123
+ * @param limit - The datetime value that the input must be after or equal to
124
+ * @returns The schema instance for method chaining
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const schema = joi.datetime().afterOrEqual('2023-01-01T00:00:00Z')
129
+ * schema.validate('2023-01-01T00:00:00Z') // Valid (equal)
130
+ * schema.validate('2023-01-02T00:00:00Z') // Valid (after)
131
+ * schema.validate('2022-12-31T23:59:59Z') // Invalid
132
+ * ```
133
+ */
134
+ afterOrEqual(limit: Parseable | Reference): this;
135
+ /**
136
+ * Validates that the datetime is greater than or equal to the specified limit.
137
+ * Alias for `afterOrEqual()`.
138
+ *
139
+ * @param limit - The minimum datetime value
140
+ * @returns The schema instance for method chaining
141
+ */
142
+ min(limit: Parseable | Reference): this;
143
+ /**
144
+ * Validates that the datetime is before or equal to the specified limit.
145
+ *
146
+ * @param limit - The datetime value that the input must be before or equal to
147
+ * @returns The schema instance for method chaining
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const schema = joi.datetime().beforeOrEqual('2023-12-31T23:59:59Z')
152
+ * schema.validate('2023-12-31T23:59:59Z') // Valid (equal)
153
+ * schema.validate('2023-06-15T12:00:00Z') // Valid (before)
154
+ * schema.validate('2024-01-01T00:00:00Z') // Invalid
155
+ * ```
156
+ */
157
+ beforeOrEqual(limit: Parseable | Reference): this;
158
+ /**
159
+ * Validates that the datetime is less than or equal to the specified limit.
160
+ * Alias for `beforeOrEqual()`.
161
+ *
162
+ * @param limit - The maximum datetime value
163
+ * @returns The schema instance for method chaining
164
+ */
165
+ max(limit: Parseable | Reference): this;
166
+ /**
167
+ * Validates that the datetime falls on a weekend day.
168
+ * Based on the configured weekend days in Luxon settings (default: Saturday and Sunday).
169
+ *
170
+ * @returns The schema instance for method chaining
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * const schema = joi.datetime().weekend()
175
+ * schema.validate('2023-07-01T12:00:00Z') // Valid if Saturday
176
+ * schema.validate('2023-07-03T12:00:00Z') // Invalid if Monday
177
+ * ```
178
+ */
179
+ weekend(): this;
180
+ /**
181
+ * Validates that the datetime falls on a weekday (non-weekend day).
182
+ * Based on the configured weekend days in Luxon settings.
183
+ *
184
+ * @returns The schema instance for method chaining
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * const schema = joi.datetime().weekday()
189
+ * schema.validate('2023-07-03T12:00:00Z') // Valid if Monday
190
+ * schema.validate('2023-07-01T12:00:00Z') // Invalid if Saturday
191
+ * ```
192
+ */
193
+ weekday(): this;
194
+ /**
195
+ * Converts the datetime to UTC timezone during validation.
196
+ * The output will be a DateTime object in UTC.
197
+ *
198
+ * @returns The schema instance for method chaining
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * const schema = joi.datetime().toUTC()
203
+ * const result = schema.validate('2023-07-15T10:30:00-05:00')
204
+ * // Returns DateTime in UTC: 2023-07-15T15:30:00Z
205
+ * ```
206
+ */
207
+ toUTC(): this;
208
+ /**
209
+ * Converts the datetime to local timezone during validation.
210
+ * The output will be a DateTime object in the system's local timezone.
211
+ *
212
+ * @returns The schema instance for method chaining
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * const schema = joi.datetime().toLocal()
217
+ * const result = schema.validate('2023-07-15T15:30:00Z')
218
+ * // Returns DateTime in local timezone
219
+ * ```
220
+ */
221
+ toLocal(): this;
222
+ /**
223
+ * Sets the timezone for the datetime during validation.
224
+ *
225
+ * @param zone - The timezone to set (IANA timezone name or Luxon Zone object)
226
+ * @returns The schema instance for method chaining
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * const schema = joi.datetime().setZone('America/New_York')
231
+ * const result = schema.validate('2023-07-15T15:30:00Z')
232
+ * // Returns DateTime in Eastern timezone
233
+ * ```
234
+ */
235
+ setZone(zone: string | Zone): this;
236
+ /**
237
+ * Formats the datetime using a custom format string during validation.
238
+ *
239
+ * @param format - The format string (Luxon format tokens)
240
+ * @returns The schema instance for method chaining
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * const schema = joi.datetime().toFormat('yyyy-MM-dd HH:mm:ss')
245
+ * const result = schema.validate('2023-07-15T15:30:00Z')
246
+ * // Returns: "2023-07-15 15:30:00"
247
+ * ```
248
+ */
249
+ toFormat(format: Tokens): this;
250
+ /**
251
+ * Formats the datetime using locale-specific formatting during validation.
252
+ *
253
+ * @param formatOpts - Locale formatting options
254
+ * @returns The schema instance for method chaining
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * const schema = joi.datetime().toLocaleString({ month: 'long', day: 'numeric' })
259
+ * const result = schema.validate('2023-07-15T15:30:00Z')
260
+ * // Returns: "July 15" (or localized equivalent)
261
+ * ```
262
+ */
263
+ toLocalizedString(formatOpts: LocaleOptions): this;
264
+ /**
265
+ * Converts the datetime to ISO 8601 string format during validation.
266
+ *
267
+ * @returns The schema instance for method chaining
268
+ *
269
+ * @example
270
+ * ```typescript
271
+ * const schema = joi.datetime().toISO()
272
+ * const result = schema.validate('July 15, 2023 3:30 PM')
273
+ * // Returns: "2023-07-15T15:30:00.000Z"
274
+ * ```
275
+ */
276
+ toISO(): this;
277
+ /**
278
+ * Converts the datetime to ISO date format (YYYY-MM-DD) during validation.
279
+ *
280
+ * @returns The schema instance for method chaining
281
+ *
282
+ * @example
283
+ * ```typescript
284
+ * const schema = joi.datetime().toISODate()
285
+ * const result = schema.validate('July 15, 2023 3:30 PM')
286
+ * // Returns: "2023-07-15"
287
+ * ```
288
+ */
289
+ toISODate(): this;
290
+ /**
291
+ * Converts the datetime to ISO week date format during validation.
292
+ *
293
+ * @returns The schema instance for method chaining
294
+ *
295
+ * @example
296
+ * ```typescript
297
+ * const schema = joi.datetime().toISOWeekDate()
298
+ * const result = schema.validate('2023-07-15T15:30:00Z')
299
+ * // Returns: "2023-W28-6"
300
+ * ```
301
+ */
302
+ toISOWeekDate(): this;
303
+ /**
304
+ * Converts the datetime to ISO time format during validation.
305
+ *
306
+ * @returns The schema instance for method chaining
307
+ *
308
+ * @example
309
+ * ```typescript
310
+ * const schema = joi.datetime().toISOTime()
311
+ * const result = schema.validate('2023-07-15T15:30:00Z')
312
+ * // Returns: "15:30:00.000Z"
313
+ * ```
314
+ */
315
+ toISOTime(): this;
316
+ /**
317
+ * Converts the datetime to RFC 2822 format during validation.
318
+ *
319
+ * @returns The schema instance for method chaining
320
+ *
321
+ * @example
322
+ * ```typescript
323
+ * const schema = joi.datetime().toRFC2822()
324
+ * const result = schema.validate('2023-07-15T15:30:00Z')
325
+ * // Returns: "Sat, 15 Jul 2023 15:30:00 +0000"
326
+ * ```
327
+ */
328
+ toRFC2822(): this;
329
+ /**
330
+ * Converts the datetime to HTTP date format during validation.
331
+ *
332
+ * @returns The schema instance for method chaining
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * const schema = joi.datetime().toHTTP()
337
+ * const result = schema.validate('2023-07-15T15:30:00Z')
338
+ * // Returns: "Sat, 15 Jul 2023 15:30:00 GMT"
339
+ * ```
340
+ */
341
+ toHTTP(): this;
342
+ /**
343
+ * Converts the datetime to SQL date format (YYYY-MM-DD) during validation.
344
+ *
345
+ * @returns The schema instance for method chaining
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * const schema = joi.datetime().toSQLDate()
350
+ * const result = schema.validate('2023-07-15T15:30:00Z')
351
+ * // Returns: "2023-07-15"
352
+ * ```
353
+ */
354
+ toSQLDate(): this;
355
+ /**
356
+ * Converts the datetime to SQL time format (HH:mm:ss.SSS) during validation.
357
+ *
358
+ * @returns The schema instance for method chaining
359
+ *
360
+ * @example
361
+ * ```typescript
362
+ * const schema = joi.datetime().toSQLTime()
363
+ * const result = schema.validate('2023-07-15T15:30:45.123Z')
364
+ * // Returns: "15:30:45.123"
365
+ * ```
366
+ */
367
+ toSQLTime(): this;
368
+ /**
369
+ * Converts the datetime to SQL datetime format during validation.
370
+ *
371
+ * @returns The schema instance for method chaining
372
+ *
373
+ * @example
374
+ * ```typescript
375
+ * const schema = joi.datetime().toSQL()
376
+ * const result = schema.validate('2023-07-15T15:30:45.123Z')
377
+ * // Returns: "2023-07-15 15:30:45.123 Z"
378
+ * ```
379
+ */
380
+ toSQL(): this;
381
+ /**
382
+ * Converts the datetime to milliseconds since Unix epoch during validation.
383
+ *
384
+ * @returns The schema instance for method chaining
385
+ *
386
+ * @example
387
+ * ```typescript
388
+ * const schema = joi.datetime().toMillis()
389
+ * const result = schema.validate('2023-07-15T15:30:00Z')
390
+ * // Returns: 1689433800000
391
+ * ```
392
+ */
393
+ toMillis(): this;
394
+ /**
395
+ * Converts the datetime to seconds since Unix epoch during validation.
396
+ *
397
+ * @returns The schema instance for method chaining
398
+ *
399
+ * @example
400
+ * ```typescript
401
+ * const schema = joi.datetime().toSeconds()
402
+ * const result = schema.validate('2023-07-15T15:30:00Z')
403
+ * // Returns: 1689433800.0
404
+ * ```
405
+ */
406
+ toSeconds(): this;
407
+ /**
408
+ * Converts the datetime to Unix timestamp (seconds as integer) during validation.
409
+ *
410
+ * @returns The schema instance for method chaining
411
+ *
412
+ * @example
413
+ * ```typescript
414
+ * const schema = joi.datetime().toUnixInteger()
415
+ * const result = schema.validate('2023-07-15T15:30:00Z')
416
+ * // Returns: 1689433800
417
+ * ```
418
+ */
419
+ toUnixInteger(): this;
420
+ /**
421
+ * Converts the datetime to JSON string format during validation.
422
+ *
423
+ * @returns The schema instance for method chaining
424
+ *
425
+ * @example
426
+ * ```typescript
427
+ * const schema = joi.datetime().toJSON()
428
+ * const result = schema.validate('2023-07-15T15:30:00Z')
429
+ * // Returns: "2023-07-15T15:30:00.000Z"
430
+ * ```
431
+ */
432
+ toJSON(): this;
433
+ /**
434
+ * Converts the datetime to BSON format during validation.
435
+ *
436
+ * @returns The schema instance for method chaining
437
+ *
438
+ * @example
439
+ * ```typescript
440
+ * const schema = joi.datetime().toBSON()
441
+ * const result = schema.validate('2023-07-15T15:30:00Z')
442
+ * // Returns: Date object for BSON serialization
443
+ * ```
444
+ */
445
+ toBSON(): this;
446
+ /**
447
+ * Converts the datetime to a plain object representation during validation.
448
+ *
449
+ * @returns The schema instance for method chaining
450
+ *
451
+ * @example
452
+ * ```typescript
453
+ * const schema = joi.datetime().toObject()
454
+ * const result = schema.validate('2023-07-15T15:30:00Z')
455
+ * // Returns: { year: 2023, month: 7, day: 15, hour: 15, minute: 30, second: 0, millisecond: 0 }
456
+ * ```
457
+ */
458
+ toObject(): this;
459
+ /**
460
+ * Converts the datetime to a JavaScript Date object during validation.
461
+ *
462
+ * @returns The schema instance for method chaining
463
+ *
464
+ * @example
465
+ * ```typescript
466
+ * const schema = joi.datetime().toJSDate()
467
+ * const result = schema.validate('2023-07-15T15:30:00Z')
468
+ * // Returns: Date object
469
+ * ```
470
+ */
471
+ toJSDate(): this;
472
+ /**
473
+ * Converts the datetime to a relative time string during validation.
474
+ *
475
+ * @returns The schema instance for method chaining
476
+ *
477
+ * @example
478
+ * ```typescript
479
+ * const schema = joi.datetime().toRelative()
480
+ * const result = schema.validate('2023-07-15T15:30:00Z')
481
+ * // Returns: "2 hours ago" (relative to current time)
482
+ * ```
483
+ */
484
+ toRelative(): this;
485
+ /**
486
+ * Sets the locale for the datetime during validation.
487
+ *
488
+ * @param locale - The locale code (e.g., 'en-US', 'fr-FR')
489
+ * @returns The schema instance for method chaining
490
+ *
491
+ * @example
492
+ * ```typescript
493
+ * const schema = joi.datetime().setLocale('fr-FR').toLocaleString()
494
+ * const result = schema.validate('2023-07-15T15:30:00Z')
495
+ * // Returns localized string in French
496
+ * ```
497
+ */
498
+ setLocale(locale: string): this;
499
+ }
500
+ /**
501
+ * Joi extension factory for datetime validation using Luxon.
502
+ *
503
+ * Creates a comprehensive datetime validation schema that can parse various input formats
504
+ * (ISO strings, Unix timestamps, JavaScript Dates, Day.js objects, etc.) and convert them
505
+ * to Luxon DateTime objects. Provides extensive validation rules for comparisons, formatting,
506
+ * timezone handling, and output transformations.
507
+ *
508
+ * The schema automatically handles timezone conversion to UTC by default for consistent
509
+ * datetime handling across different environments. All parsing attempts maintain UTC timezone
510
+ * unless explicitly overridden.
511
+ *
512
+ * @param this - The Joi root instance
513
+ * @param joi - The Joi root instance for creating base schemas
514
+ * @returns Joi extension configuration object
515
+ *
516
+ * @example Basic Usage
517
+ * ```typescript
518
+ * import Joi from 'joi'
519
+ * import { datetime } from './datetime'
520
+ *
521
+ * const extendedJoi = Joi.extend(datetime)
522
+ *
523
+ * const schema = extendedJoi.datetime()
524
+ * const result = schema.validate('2023-07-15T15:30:00Z')
525
+ * // Returns { value: DateTime, error: undefined }
526
+ * ```
527
+ *
528
+ * @example With Validation Rules
529
+ * ```typescript
530
+ * const schema = extendedJoi.datetime()
531
+ * .after('2023-01-01')
532
+ * .before('2024-01-01')
533
+ * .weekday()
534
+ *
535
+ * schema.validate('2023-07-17T10:00:00Z') // Valid (Monday in 2023)
536
+ * schema.validate('2023-07-15T10:00:00Z') // Invalid (Saturday)
537
+ * ```
538
+ *
539
+ * @example With Output Formatting
540
+ * ```typescript
541
+ * const schema = extendedJoi.datetime()
542
+ * .toUTC()
543
+ * .toISO()
544
+ *
545
+ * const result = schema.validate('2023-07-15T10:30:00-05:00')
546
+ * // Returns: "2023-07-15T15:30:00.000Z"
547
+ * ```
548
+ *
549
+ * @example Supported Input Formats
550
+ * ```typescript
551
+ * const schema = extendedJoi.datetime()
552
+ *
553
+ * // All of these are valid inputs:
554
+ * schema.validate('2023-07-15T15:30:00Z') // ISO string
555
+ * schema.validate('Sat, 15 Jul 2023 15:30:00 GMT') // RFC2822
556
+ * schema.validate(1689433800000) // Unix timestamp (ms)
557
+ * schema.validate(new Date()) // JavaScript Date
558
+ * schema.validate(DateTime.now()) // Luxon DateTime
559
+ * schema.validate({ year: 2023, month: 7, day: 15 }) // DateObjectUnits
560
+ * ```
561
+ */
562
+ export declare const datetime: ExtensionFactory;