@nhtio/validation 1.20250814.1 → 1.20250911.1

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.20250814.1",
3
+ "version": "1.20250911.1",
4
4
  "description": "A powerful schema description language and data validator",
5
5
  "keywords": [],
6
6
  "author": "Jak Giveon <jak@nht.io>",
@@ -2,8 +2,9 @@ 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 { ExtensionFactory, Reference } from 'joi';
5
+ import type { ExtensionFactory, CustomHelpers, Reference } from 'joi';
6
6
  import type { Zone, LocaleOptions, DateObjectUnits, ZoneOptions, ToISOTimeOptions, ToISODateOptions, ToSQLOptions, ToRelativeOptions } from 'luxon';
7
+ type CallbackOrValue<T> = T | ((dt: DateTime, helpers: CustomHelpers) => T);
7
8
  export declare const messages: {
8
9
  'datetime.base': string;
9
10
  'datetime.exactly': string;
@@ -251,59 +252,90 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
251
252
  /**
252
253
  * Formats the datetime using a custom format string during validation.
253
254
  *
254
- * @param format - The format string (Luxon format tokens)
255
+ * @param format - The format string (Luxon format tokens) or callback function
255
256
  * @returns The schema instance for method chaining
256
257
  *
257
- * @example
258
+ * @example Static format
258
259
  * ```typescript
259
260
  * const schema = joi.datetime().toFormat('yyyy-MM-dd HH:mm:ss')
260
261
  * const result = schema.validate('2023-07-15T15:30:00Z')
261
262
  * // Returns: "2023-07-15 15:30:00"
262
263
  * ```
264
+ *
265
+ * @example Callback function (serializable)
266
+ * ```typescript
267
+ * const schema = joi.datetime().toFormat((dt, helpers) =>
268
+ * dt.year > 2000 ? 'yyyy-MM-dd' : 'MM/dd/yy'
269
+ * )
270
+ * ```
263
271
  */
264
- toFormat(format: Tokens): this;
272
+ toFormat(format: CallbackOrValue<Tokens>): this;
265
273
  /**
266
274
  * Formats the datetime using locale-specific formatting during validation.
267
275
  *
268
- * @param formatOpts - Locale formatting options
276
+ * @param formatOpts - Locale formatting options or callback function
269
277
  * @returns The schema instance for method chaining
270
278
  *
271
- * @example
279
+ * @example Static options
272
280
  * ```typescript
273
- * const schema = joi.datetime().toLocaleString({ month: 'long', day: 'numeric' })
281
+ * const schema = joi.datetime().toLocalizedString({ month: 'long', day: 'numeric' })
274
282
  * const result = schema.validate('2023-07-15T15:30:00Z')
275
283
  * // Returns: "July 15" (or localized equivalent)
276
284
  * ```
285
+ *
286
+ * @example Callback function (serializable)
287
+ * ```typescript
288
+ * const schema = joi.datetime().toLocalizedString((dt, helpers) => ({
289
+ * month: dt.month > 6 ? 'long' : 'short',
290
+ * day: 'numeric'
291
+ * }))
292
+ * ```
277
293
  */
278
- toLocalizedString(formatOpts: LocaleOptions): this;
294
+ toLocalizedString(formatOpts: CallbackOrValue<LocaleOptions>): this;
279
295
  /**
280
296
  * Converts the datetime to ISO 8601 string format during validation.
281
297
  *
282
- * @param opts - Options for ISO string formatting
298
+ * @param opts - Options for ISO string formatting or callback function
283
299
  * @returns The schema instance for method chaining
284
300
  *
285
- * @example
301
+ * @example Static options
286
302
  * ```typescript
287
- * const schema = joi.datetime().toISO()
303
+ * const schema = joi.datetime().toISO({ suppressMilliseconds: true })
288
304
  * const result = schema.validate('July 15, 2023 3:30 PM')
289
- * // Returns: "2023-07-15T15:30:00.000Z"
305
+ * // Returns: "2023-07-15T15:30:00Z"
306
+ * ```
307
+ *
308
+ * @example Callback function (serializable)
309
+ * ```typescript
310
+ * const schema = joi.datetime().toISO((dt, helpers) => ({
311
+ * suppressMilliseconds: true,
312
+ * extendedZone: dt.year > 1970,
313
+ * precision: 'seconds'
314
+ * }))
290
315
  * ```
291
316
  */
292
- toISO(opts?: ToISOTimeOptions): this;
317
+ toISO(opts?: CallbackOrValue<ToISOTimeOptions>): this;
293
318
  /**
294
319
  * Converts the datetime to ISO date format (YYYY-MM-DD) during validation.
295
320
  *
296
- * @param opts - Options for ISO date formatting
321
+ * @param opts - Options for ISO date formatting or callback function
297
322
  * @returns The schema instance for method chaining
298
323
  *
299
- * @example
324
+ * @example Static options
300
325
  * ```typescript
301
326
  * const schema = joi.datetime().toISODate()
302
327
  * const result = schema.validate('July 15, 2023 3:30 PM')
303
328
  * // Returns: "2023-07-15"
304
329
  * ```
330
+ *
331
+ * @example Callback function (serializable)
332
+ * ```typescript
333
+ * const schema = joi.datetime().toISODate((dt, helpers) => ({
334
+ * format: dt.year > 2000 ? 'extended' : 'basic'
335
+ * }))
336
+ * ```
305
337
  */
306
- toISODate(opts?: ToISODateOptions): this;
338
+ toISODate(opts?: CallbackOrValue<ToISODateOptions>): this;
307
339
  /**
308
340
  * Converts the datetime to ISO week date format during validation.
309
341
  *
@@ -320,17 +352,24 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
320
352
  /**
321
353
  * Converts the datetime to ISO time format during validation.
322
354
  *
323
- * @param opts - Options for ISO time formatting
355
+ * @param opts - Options for ISO time formatting or callback function
324
356
  * @returns The schema instance for method chaining
325
357
  *
326
- * @example
358
+ * @example Static options
327
359
  * ```typescript
328
- * const schema = joi.datetime().toISOTime()
360
+ * const schema = joi.datetime().toISOTime({ suppressMilliseconds: true })
329
361
  * const result = schema.validate('2023-07-15T15:30:00Z')
330
- * // Returns: "15:30:00.000Z"
362
+ * // Returns: "15:30:00Z"
363
+ * ```
364
+ *
365
+ * @example Callback function (serializable)
366
+ * ```typescript
367
+ * const schema = joi.datetime().toISOTime((dt, helpers) => ({
368
+ * suppressMilliseconds: dt.millisecond === 0
369
+ * }))
331
370
  * ```
332
371
  */
333
- toISOTime(opts?: ToISOTimeOptions): this;
372
+ toISOTime(opts?: CallbackOrValue<ToISOTimeOptions>): this;
334
373
  /**
335
374
  * Converts the datetime to RFC 2822 format during validation.
336
375
  *
@@ -373,31 +412,45 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
373
412
  /**
374
413
  * Converts the datetime to SQL time format (HH:mm:ss.SSS) during validation.
375
414
  *
376
- * @param opts - Options for SQL time formatting
415
+ * @param opts - Options for SQL time formatting or callback function
377
416
  * @returns The schema instance for method chaining
378
417
  *
379
- * @example
418
+ * @example Static options
380
419
  * ```typescript
381
- * const schema = joi.datetime().toSQLTime()
420
+ * const schema = joi.datetime().toSQLTime({ includeZone: true })
382
421
  * const result = schema.validate('2023-07-15T15:30:45.123Z')
383
- * // Returns: "15:30:45.123"
422
+ * // Returns: "15:30:45.123 Z"
423
+ * ```
424
+ *
425
+ * @example Callback function (serializable)
426
+ * ```typescript
427
+ * const schema = joi.datetime().toSQLTime((dt, helpers) => ({
428
+ * includeZone: dt.zone.name !== 'UTC'
429
+ * }))
384
430
  * ```
385
431
  */
386
- toSQLTime(opts?: ToSQLOptions): this;
432
+ toSQLTime(opts?: CallbackOrValue<ToSQLOptions>): this;
387
433
  /**
388
434
  * Converts the datetime to SQL datetime format during validation.
389
435
  *
390
- * @param opts - Options for SQL formatting
436
+ * @param opts - Options for SQL formatting or callback function
391
437
  * @returns The schema instance for method chaining
392
438
  *
393
- * @example
439
+ * @example Static options
394
440
  * ```typescript
395
- * const schema = joi.datetime().toSQL()
441
+ * const schema = joi.datetime().toSQL({ includeZone: false })
396
442
  * const result = schema.validate('2023-07-15T15:30:45.123Z')
397
- * // Returns: "2023-07-15 15:30:45.123 Z"
443
+ * // Returns: "2023-07-15 15:30:45.123"
444
+ * ```
445
+ *
446
+ * @example Callback function (serializable)
447
+ * ```typescript
448
+ * const schema = joi.datetime().toSQL((dt, helpers) => ({
449
+ * includeZone: dt.zone.name !== 'UTC'
450
+ * }))
398
451
  * ```
399
452
  */
400
- toSQL(opts?: ToSQLOptions): this;
453
+ toSQL(opts?: CallbackOrValue<ToSQLOptions>): this;
401
454
  /**
402
455
  * Converts the datetime to milliseconds since Unix epoch during validation.
403
456
  *
@@ -466,19 +519,26 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
466
519
  /**
467
520
  * Converts the datetime to a plain object representation during validation.
468
521
  *
469
- * @param opts - Options for object conversion
522
+ * @param opts - Options for object conversion or callback function
470
523
  * @returns The schema instance for method chaining
471
524
  *
472
- * @example
525
+ * @example Static options
473
526
  * ```typescript
474
- * const schema = joi.datetime().toObject()
527
+ * const schema = joi.datetime().toObject({ includeConfig: true })
475
528
  * const result = schema.validate('2023-07-15T15:30:00Z')
476
529
  * // Returns: { year: 2023, month: 7, day: 15, hour: 15, minute: 30, second: 0, millisecond: 0 }
477
530
  * ```
531
+ *
532
+ * @example Callback function (serializable)
533
+ * ```typescript
534
+ * const schema = joi.datetime().toObject((dt, helpers) => ({
535
+ * includeConfig: dt.year > 2000
536
+ * }))
537
+ * ```
478
538
  */
479
- toObject(opts?: {
539
+ toObject(opts?: CallbackOrValue<{
480
540
  includeConfig?: boolean;
481
- }): this;
541
+ }>): this;
482
542
  /**
483
543
  * Converts the datetime to a JavaScript Date object during validation.
484
544
  *
@@ -495,17 +555,24 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
495
555
  /**
496
556
  * Converts the datetime to a relative time string during validation.
497
557
  *
498
- * @param opts - Options for relative time formatting
558
+ * @param opts - Options for relative time formatting or callback function
499
559
  * @returns The schema instance for method chaining
500
560
  *
501
- * @example
561
+ * @example Static options
502
562
  * ```typescript
503
- * const schema = joi.datetime().toRelative()
563
+ * const schema = joi.datetime().toRelative({ base: DateTime.now() })
504
564
  * const result = schema.validate('2023-07-15T15:30:00Z')
505
565
  * // Returns: "2 hours ago" (relative to current time)
506
566
  * ```
567
+ *
568
+ * @example Callback function (serializable)
569
+ * ```typescript
570
+ * const schema = joi.datetime().toRelative((dt, helpers) => ({
571
+ * base: dt.year > 2020 ? DateTime.now() : DateTime.fromISO('2020-01-01')
572
+ * }))
573
+ * ```
507
574
  */
508
- toRelative(opts?: ToRelativeOptions): this;
575
+ toRelative(opts?: CallbackOrValue<ToRelativeOptions>): this;
509
576
  /**
510
577
  * Sets the locale for the datetime during validation.
511
578
  *
@@ -584,3 +651,4 @@ export interface DatetimeSchema<TSchema = DateTime> extends AnySchema<TSchema> {
584
651
  * ```
585
652
  */
586
653
  export declare const datetime: ExtensionFactory;
654
+ export {};
@@ -727,7 +727,7 @@ export interface SymbolSchema<TSchema = symbol> extends AnySchema<TSchema> {
727
727
  * A union type that includes all schema types.
728
728
  */
729
729
  export type Schema<P = any> = AnySchema<P> | ArraySchema<P> | AlternativesSchema<P> | BinarySchema<P> | BooleanSchema<P> | DateSchema<P> | FunctionSchema<P> | NumberSchema<P> | ObjectSchema<P> | StringSchema<P> | LinkSchema<P> | SymbolSchema<P> | BigIntSchema<P> | DatetimeSchema<P> | PhoneSchema<P>;
730
- export type ObjectPropertiesSchema<T = any> = true extends Joi.IsNonPrimitiveSubsetUnion<Exclude<T, undefined | null>> ? AlternativesSchema : T extends Joi.NullableType<string> ? StringSchema : T extends Joi.NullableType<number> ? NumberSchema : T extends Joi.NullableType<bigint> ? NumberSchema : T extends Joi.NullableType<boolean> ? BooleanSchema : T extends Joi.NullableType<Date> ? DateSchema : T extends Joi.NullableType<Buffer> ? BinarySchema : T extends Joi.NullableType<Array<any>> ? ArraySchema : T extends Joi.NullableType<object> ? StrictSchemaMap<T> | ObjectSchema<T> : never;
730
+ export type ObjectPropertiesSchema<T = any> = true extends Joi.IsNonPrimitiveSubsetUnion<Exclude<T, undefined | null>> ? AlternativesSchema : T extends Joi.NullableType<string> ? StringSchema : T extends Joi.NullableType<number> ? NumberSchema : T extends Joi.NullableType<bigint> ? BigIntSchema : T extends Joi.NullableType<boolean> ? BooleanSchema : T extends Joi.NullableType<Date> ? DateSchema : T extends Joi.NullableType<Buffer> ? BinarySchema : T extends Joi.NullableType<Array<any>> ? ArraySchema : T extends DateTime ? DatetimeSchema : T extends Joi.NullableType<object> ? StrictSchemaMap<T> | ObjectSchema<T> : never;
731
731
  export type PartialSchemaMap<TSchema = any> = {
732
732
  [key in keyof TSchema]?: SchemaLike | SchemaLike[];
733
733
  };