validator-fns 3.0.0-next.2 → 3.0.0-next.3

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/dist/index.d.cts CHANGED
@@ -34,6 +34,15 @@ type ValidatorResult<T = unknown, E = unknown> = ValidResult<T> | InvalidResult<
34
34
  * @category Types
35
35
  */
36
36
  type ValidatorTest<T = unknown, E = unknown> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
37
+ /**
38
+ * Validation test function, such as `min()`: it checks a parsed value. Type
39
+ * validators run their tests on the parsed value once it's present, so a test
40
+ * takes the validator's value type. Its own result value is never read.
41
+ * @typeParam T The value type the test checks
42
+ * @typeParam E The error type
43
+ * @category Types
44
+ */
45
+ type ValidationTest<T = unknown, E = unknown> = (value: T, field?: string) => Promise<ValidatorResult<unknown, E>>;
37
46
  /**
38
47
  * Extracts the generic error type from a ValidatorTest.
39
48
  * @typeParam T a ValidatorTest
@@ -57,7 +66,7 @@ type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; };
57
66
  * @typeParam C The configuration type
58
67
  * @category Types
59
68
  */
60
- type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest<unknown, E>, ...tests: ValidatorTest<unknown, E>[]) => ValidatorTest<T, E>;
69
+ type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidationTest<T, E>, ...tests: ValidationTest<T, E>[]) => ValidatorTest<T, E>;
61
70
  /**
62
71
  * Base configuration type.
63
72
  * @typeParam T The target type.
@@ -78,12 +87,34 @@ interface ValidatorMessageParameters<T> {
78
87
  value: T | null | undefined;
79
88
  field: string | undefined;
80
89
  }
90
+ /**
91
+ * The extra parameters of a message parameters type, without the value and
92
+ * field every message gets.
93
+ * @internal
94
+ */
95
+ type MessageExtras<P extends ValidatorMessageParameters<unknown>> = Omit<P, 'value' | 'field'>;
81
96
  /**
82
97
  * Validation message. May be a plain string or a function that accepts arguments.
83
98
  * While using a string, values in squiggly brackets `{}` are replaced.
99
+ * A function gets the value and field, and any extra parameters `X` the test
100
+ * adds, such as `min` for `min()`.
101
+ * @typeParam T The value type
102
+ * @typeParam X The extra parameters
84
103
  * @category Types
85
104
  */
86
- type ValidatorMessage<T, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>> = string | ((parameters: P) => string);
105
+ type ValidatorMessage<T, X extends object = object> = string | ((parameters: X & ValidatorMessageParameters<T>) => string);
106
+ /**
107
+ * Paths into nested plain objects, as lists of property names, for `check()`.
108
+ * Arrays, dates and functions end a path.
109
+ * @internal
110
+ */
111
+ type KeyPath<T> = T extends readonly unknown[] | Date | ((...args: never[]) => unknown) ? never : T extends object ? { [K in keyof T & string]: readonly [K] | readonly [K, ...KeyPath<NonNullable<T[K]>>]; }[keyof T & string] : never;
112
+ /**
113
+ * The errors with a message at `Path` that `check()` produces, such as
114
+ * `{ shipping?: { zip?: string } }` for `['shipping', 'zip']`.
115
+ * @internal
116
+ */
117
+ type AtPath<Path> = Path extends readonly [infer Head extends string, ...infer Rest] ? { [P in Head]?: AtPath<Rest>; } : string;
87
118
  /**
88
119
  * Simple check for objects.
89
120
  * @param value Value to test
@@ -101,7 +132,7 @@ export declare function hasOwnProperty(object: unknown, property: string | numbe
101
132
  * @param parameters Params to inject into template
102
133
  * @category Helpers
103
134
  */
104
- export declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
135
+ export declare function formatMessage<T, X extends object = object>(template: ValidatorMessage<T, X>, parameters: X & ValidatorMessageParameters<T>): string;
105
136
  /**
106
137
  * Creates a valid result
107
138
  * @param result Result fields
@@ -113,23 +144,14 @@ export declare function valid<T, E = never>(result: {
113
144
  value: T;
114
145
  field: string | undefined;
115
146
  }): Promise<ValidatorResult<T, E>>;
147
+ //#endregion
148
+ //#region src/invalid-detail.d.ts
116
149
  /**
117
- * Creates an invalid result.
118
- * @param result Result fields
119
- * @param result.message Error message, formatted with `formatMessage`
120
- * @param result.value Parsed value
121
- * @param result.field Field name
122
- * @param result.errors Extra error details, such as per-field errors from `object` and `array`
123
- * @param result.extras Extra message params
124
- * @category Helpers
150
+ * A test on an array itself, such as `max()`. It runs on the parsed array,
151
+ * before the item validator, so the items are `unknown`.
152
+ * @internal
125
153
  */
126
- export declare function invalid<T, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
127
- message: ValidatorMessage<T, P>;
128
- value: unknown;
129
- field: string | undefined;
130
- errors?: E | undefined;
131
- extras?: Omit<P, 'field' | 'value'> | undefined;
132
- }): Promise<ValidatorResult<T, E>>;
154
+ type ArrayTest = ValidationTest<unknown[]>;
133
155
  //#endregion
134
156
  //#region src/standard-schema.d.ts
135
157
  /**
@@ -283,11 +305,13 @@ export declare function parseArray(value: unknown): unknown[] | null | undefined
283
305
  */
284
306
  export declare function applyArrayConfig(config: ArrayConfig, value: unknown): unknown[] | null | undefined;
285
307
  /**
286
- * Validates an array. The first passed validator is used to validate individual
287
- * items. The remaining validators target the array itself.
308
+ * Validates an array. The item validator, if any, comes first and runs on each
309
+ * item. Then an optional configuration, and tests that target the array
310
+ * itself.
288
311
  * @category Type Validators
289
312
  */
290
- export declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): TypeValidator<T[], string | ArrayItemValidatorResult<E>[]>;
313
+ export declare function array<T, E>(item: ValidatorTest<T, E>, config?: Partial<ArrayConfig> | ArrayTest, ...tests: ArrayTest[]): TypeValidator<T[], string | ArrayItemValidatorResult<E>[]>;
314
+ export declare function array(config?: Partial<ArrayConfig>): TypeValidator<unknown[], string | ArrayItemValidatorResult[]>;
291
315
  //#endregion
292
316
  //#region src/booleans.d.ts
293
317
  /**
@@ -312,7 +336,7 @@ type BooleanConfig = ConfigBase<boolean>;
312
336
  * Validates a boolean value.
313
337
  * @category Type Validators
314
338
  */
315
- export declare const boolean: (config?: Partial<BooleanConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<boolean, string>;
339
+ export declare const boolean: (config?: Partial<BooleanConfig> | ValidationTest<boolean, string> | undefined, ...tests: ValidationTest<boolean, string>[]) => TypeValidator<boolean, string>;
316
340
  //#endregion
317
341
  //#region src/dates.d.ts
318
342
  /**
@@ -335,7 +359,7 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
335
359
  * @param exclusive Makes comparison exclusive instead of inclusive
336
360
  * @category Validation Tests
337
361
  */
338
- export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
362
+ export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MessageExtras<MinDateValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<Date, never>;
339
363
  interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
340
364
  max: Date | null | undefined;
341
365
  limit: Date | null | undefined;
@@ -348,7 +372,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
348
372
  * @param exclusive Makes comparison exclusive instead of inclusive
349
373
  * @category Validation Tests
350
374
  */
351
- export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
375
+ export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MessageExtras<MaxDateValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<Date, never>;
352
376
  /**
353
377
  * Configuration for date validation.
354
378
  * @category Types
@@ -358,7 +382,7 @@ type DateConfig = ConfigBase<Date>;
358
382
  * Validates a date value.
359
383
  * @category Type Validators
360
384
  */
361
- export declare const date: (config?: Partial<DateConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<Date, string>;
385
+ export declare const date: (config?: Partial<DateConfig> | ValidationTest<Date, string> | undefined, ...tests: ValidationTest<Date, string>[]) => TypeValidator<Date, string>;
362
386
  //#endregion
363
387
  //#region src/numbers.d.ts
364
388
  /**
@@ -379,7 +403,7 @@ export declare function applyNumberConfig(value: unknown, config: NumberConfig):
379
403
  * @param message Error message
380
404
  * @category Validation Tests
381
405
  */
382
- export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number, never>;
406
+ export declare function integer(message: ValidatorMessage<number>): ValidationTest<number, never>;
383
407
  /**
384
408
  * Configuration for number validation.
385
409
  * @category Types
@@ -392,7 +416,7 @@ interface NumberConfig extends ConfigBase<number> {
392
416
  * Validates a number value.
393
417
  * @category Type Validators
394
418
  */
395
- export declare const number: (config?: Partial<NumberConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<number, string>;
419
+ export declare const number: (config?: Partial<NumberConfig> | ValidationTest<number, string> | undefined, ...tests: ValidationTest<number, string>[]) => TypeValidator<number, string>;
396
420
  //#endregion
397
421
  //#region src/parse-date.d.ts
398
422
  /** Badly formatted date for internal use. */
@@ -411,11 +435,13 @@ export declare function parseDate(value: unknown): Date | null | undefined;
411
435
  * the parsed object. The message goes to the error at `path`, a property name
412
436
  * or a list of names into nested objects, or to the object's own message
413
437
  * without a path. The message gets the parsed object as `value` and the
414
- * object's `field`.
438
+ * object's `field`. The path is checked against the object's type, and so are
439
+ * the errors it produces.
415
440
  * @category Validation Tests
416
441
  */
417
- export declare function check<T, E = never>(test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
418
- export declare function check<T, E = never>(path: string | readonly string[], test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
442
+ export declare function check<T>(test: (value: T) => boolean, message: ValidatorMessage<T>): ValidationTest<T, never>;
443
+ export declare function check<T, const K extends keyof T & string>(path: K, test: (value: T) => boolean, message: ValidatorMessage<T>): ValidationTest<T, { [P in K]?: string; }>;
444
+ export declare function check<T, const Path extends KeyPath<T>>(path: Path, test: (value: T) => boolean, message: ValidatorMessage<T>): ValidationTest<T, AtPath<Path>>;
419
445
  //#endregion
420
446
  //#region src/objects.d.ts
421
447
  type ObjectParameter = Record<string, ValidatorTest>;
@@ -454,7 +480,7 @@ interface ObjectConfig {
454
480
  * every property is valid, and the first failure is the result.
455
481
  * @category Type Validators
456
482
  */
457
- export declare function object<P extends ObjectParameter, V extends ObjectValue<P>, E extends ObjectErrors<P>>(properties: P, config?: ObjectConfig | ValidatorTest<ObjectValue<P>, ObjectErrors<P>>, ...tests: ValidatorTest<ObjectValue<P>, ObjectErrors<P>>[]): TypeValidator<V, E>;
483
+ export declare function object<P extends ObjectParameter>(properties: P, config?: ObjectConfig | ValidationTest<ObjectValue<P>, ObjectErrors<P>>, ...tests: ValidationTest<ObjectValue<P>, ObjectErrors<P>>[]): TypeValidator<ObjectValue<P>, ObjectErrors<P>>;
458
484
  //#endregion
459
485
  //#region src/optional.d.ts
460
486
  /**
@@ -487,7 +513,7 @@ interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<
487
513
  * @param message Error message
488
514
  * @category Validation Tests
489
515
  */
490
- export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T, never>;
516
+ export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, MessageExtras<OneOfValidatorMessageParameters<T>>>): ValidationTest<T, never>;
491
517
  //#endregion
492
518
  //#region src/factories.d.ts
493
519
  /**
@@ -498,14 +524,16 @@ export declare function oneOf<T extends string | number | boolean | Date>(values
498
524
  * @param applyConfig Function that applies configuration to value.
499
525
  * @category Helpers
500
526
  */
501
- export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: NoInfer<C>, applyConfig: (value: unknown, config: C) => T | null | undefined): (config?: Partial<C> | ValidatorTest<unknown, string>, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<T, string>;
527
+ export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: NoInfer<C>, applyConfig: (value: unknown, config: C) => T | null | undefined): (config?: Partial<C> | ValidationTest<T, string>, ...tests: ValidationTest<T, string>[]) => TypeValidator<T, string>;
502
528
  /**
503
- * Captures basic validator test pattern
529
+ * Captures basic validator test pattern. The test gets the parsed value, and
530
+ * fails a missing one, which type validators never pass (#660).
504
531
  * @param test Test function
505
532
  * @param message Error message
506
- * @param getExtras Optional extra fields for error message
533
+ * @param getExtras Extra parameters for the error message
507
534
  */
508
- export declare function createValidatorTest<T = unknown, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(test: (value: unknown, field?: string) => boolean, message: ValidatorMessage<T, P>, getExtras?: (value: T | null | undefined) => Omit<P, 'value' | 'field'>): ValidatorTest<T, E>;
535
+ export declare function createValidatorTest<T, E = never>(test: (value: T, field?: string) => boolean, message: ValidatorMessage<T>): ValidationTest<T, E>;
536
+ export declare function createValidatorTest<T, E, X extends object>(test: (value: T, field?: string) => boolean, message: ValidatorMessage<T, X>, getExtras: (value: T) => X): ValidationTest<T, E>;
509
537
  //#endregion
510
538
  //#region src/limits.d.ts
511
539
  type SharedValueType = string | number | unknown[];
@@ -522,7 +550,7 @@ interface MaxValidatorMessageParameters extends ValidatorMessageParameters<Share
522
550
  * @param exclusive Use exclusive comparison instead of inclusive
523
551
  * @category Validation Tests
524
552
  */
525
- export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
553
+ export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MessageExtras<MaxValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<SharedValueType, never>;
526
554
  interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
527
555
  min: number;
528
556
  limit: number;
@@ -536,7 +564,7 @@ interface MinValidatorMessageParameters extends ValidatorMessageParameters<Share
536
564
  * @param exclusive Use exclusive comparison instead of inclusive
537
565
  * @category Validation Tests
538
566
  */
539
- export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
567
+ export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MessageExtras<MinValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<SharedValueType, never>;
540
568
  interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
541
569
  limit: number;
542
570
  amount: number | null | undefined;
@@ -548,7 +576,33 @@ interface ExactValidatorMessageParameters extends ValidatorMessageParameters<Sha
548
576
  * @param message Error message
549
577
  * @category Validation Tests
550
578
  */
551
- export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType, never>;
579
+ export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, MessageExtras<ExactValidatorMessageParameters>>): ValidationTest<SharedValueType, never>;
580
+ //#endregion
581
+ //#region src/invalid.d.ts
582
+ /** @internal */
583
+ type InvalidFields<E> = {
584
+ value: unknown;
585
+ field: string | undefined;
586
+ errors?: E | undefined;
587
+ };
588
+ /**
589
+ * Creates an invalid result. A message function gets the value as `unknown`,
590
+ * and `extras` must hold the extra parameters it reads.
591
+ * @param result Result fields
592
+ * @param result.message Error message, formatted with `formatMessage`
593
+ * @param result.value Parsed value
594
+ * @param result.field Field name
595
+ * @param result.errors Extra error details, such as per-field errors from `object` and `array`
596
+ * @param result.extras Extra message params
597
+ * @category Helpers
598
+ */
599
+ export declare function invalid<T, E = never>(result: InvalidFields<E> & {
600
+ message: ValidatorMessage<unknown>;
601
+ }): Promise<ValidatorResult<T, E>>;
602
+ export declare function invalid<T, E, X extends object>(result: InvalidFields<E> & {
603
+ message: ValidatorMessage<unknown, X>;
604
+ extras: X;
605
+ }): Promise<ValidatorResult<T, E>>;
552
606
  //#endregion
553
607
  //#region src/strings.d.ts
554
608
  /**
@@ -581,20 +635,20 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
581
635
  * @param message Error message
582
636
  * @category Validation Tests
583
637
  */
584
- export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string, never>;
638
+ export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MessageExtras<MatchesValidatorMessageParameters>>): ValidationTest<string, never>;
585
639
  /**
586
640
  * Ensures a string value matches an email pattern
587
641
  * @param message Error message
588
642
  * @category Validation Tests
589
643
  */
590
- export declare function email(message: ValidatorMessage<string>): ValidatorTest<string, never>;
644
+ export declare function email(message: ValidatorMessage<string>): ValidationTest<string, never>;
591
645
  /**
592
646
  * Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
593
647
  * nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
594
648
  * @param message Error message
595
649
  * @category Validation Tests
596
650
  */
597
- export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string, never>;
651
+ export declare function uuid(message: ValidatorMessage<string>): ValidationTest<string, never>;
598
652
  /**
599
653
  * Ensures a string value is a valid URL
600
654
  * @param message Error message
@@ -602,11 +656,11 @@ export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<s
602
656
  * replaces the default.
603
657
  * @category Validation Tests
604
658
  */
605
- export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string, never>;
659
+ export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidationTest<string, never>;
606
660
  /**
607
661
  * Validates a string value.
608
662
  * @category Type Validators
609
663
  */
610
- export declare const string: (config?: Partial<StringConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<string, string>;
664
+ export declare const string: (config?: Partial<StringConfig> | ValidationTest<string, string> | undefined, ...tests: ValidationTest<string, string>[]) => TypeValidator<string, string>;
611
665
  //#endregion
612
- export type { ArrayConfig, ArrayItemValidatorResult, BooleanConfig, ConfigBase, DateConfig, DeepPartial, ExactValidatorMessageParameters, ExtractError, ExtractValue, InvalidResult, MatchesValidatorMessageParameters, MaxDateValidatorMessageParameters, MaxValidatorMessageParameters, MinDateValidatorMessageParameters, MinValidatorMessageParameters, NumberConfig, ObjectConfig, ObjectParameter as ObjectParam, OneOfValidatorMessageParameters, SharedDateValueType, SharedValueType, StringConfig, TypeValidator, ValidResult, ValidatorFactory, ValidatorMessage, ValidatorMessageParameters, ValidatorResult, ValidatorTest };
666
+ export type { ArrayConfig, ArrayItemValidatorResult, BooleanConfig, ConfigBase, DateConfig, DeepPartial, ExactValidatorMessageParameters, ExtractError, ExtractValue, InvalidResult, MatchesValidatorMessageParameters, MaxDateValidatorMessageParameters, MaxValidatorMessageParameters, MinDateValidatorMessageParameters, MinValidatorMessageParameters, NumberConfig, ObjectConfig, ObjectParameter as ObjectParam, OneOfValidatorMessageParameters, SharedDateValueType, SharedValueType, StringConfig, TypeValidator, ValidResult, ValidationTest, ValidatorFactory, ValidatorMessage, ValidatorMessageParameters, ValidatorResult, ValidatorTest };
package/dist/index.d.ts CHANGED
@@ -34,6 +34,15 @@ type ValidatorResult<T = unknown, E = unknown> = ValidResult<T> | InvalidResult<
34
34
  * @category Types
35
35
  */
36
36
  type ValidatorTest<T = unknown, E = unknown> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
37
+ /**
38
+ * Validation test function, such as `min()`: it checks a parsed value. Type
39
+ * validators run their tests on the parsed value once it's present, so a test
40
+ * takes the validator's value type. Its own result value is never read.
41
+ * @typeParam T The value type the test checks
42
+ * @typeParam E The error type
43
+ * @category Types
44
+ */
45
+ type ValidationTest<T = unknown, E = unknown> = (value: T, field?: string) => Promise<ValidatorResult<unknown, E>>;
37
46
  /**
38
47
  * Extracts the generic error type from a ValidatorTest.
39
48
  * @typeParam T a ValidatorTest
@@ -57,7 +66,7 @@ type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; };
57
66
  * @typeParam C The configuration type
58
67
  * @category Types
59
68
  */
60
- type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest<unknown, E>, ...tests: ValidatorTest<unknown, E>[]) => ValidatorTest<T, E>;
69
+ type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidationTest<T, E>, ...tests: ValidationTest<T, E>[]) => ValidatorTest<T, E>;
61
70
  /**
62
71
  * Base configuration type.
63
72
  * @typeParam T The target type.
@@ -78,12 +87,34 @@ interface ValidatorMessageParameters<T> {
78
87
  value: T | null | undefined;
79
88
  field: string | undefined;
80
89
  }
90
+ /**
91
+ * The extra parameters of a message parameters type, without the value and
92
+ * field every message gets.
93
+ * @internal
94
+ */
95
+ type MessageExtras<P extends ValidatorMessageParameters<unknown>> = Omit<P, 'value' | 'field'>;
81
96
  /**
82
97
  * Validation message. May be a plain string or a function that accepts arguments.
83
98
  * While using a string, values in squiggly brackets `{}` are replaced.
99
+ * A function gets the value and field, and any extra parameters `X` the test
100
+ * adds, such as `min` for `min()`.
101
+ * @typeParam T The value type
102
+ * @typeParam X The extra parameters
84
103
  * @category Types
85
104
  */
86
- type ValidatorMessage<T, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>> = string | ((parameters: P) => string);
105
+ type ValidatorMessage<T, X extends object = object> = string | ((parameters: X & ValidatorMessageParameters<T>) => string);
106
+ /**
107
+ * Paths into nested plain objects, as lists of property names, for `check()`.
108
+ * Arrays, dates and functions end a path.
109
+ * @internal
110
+ */
111
+ type KeyPath<T> = T extends readonly unknown[] | Date | ((...args: never[]) => unknown) ? never : T extends object ? { [K in keyof T & string]: readonly [K] | readonly [K, ...KeyPath<NonNullable<T[K]>>]; }[keyof T & string] : never;
112
+ /**
113
+ * The errors with a message at `Path` that `check()` produces, such as
114
+ * `{ shipping?: { zip?: string } }` for `['shipping', 'zip']`.
115
+ * @internal
116
+ */
117
+ type AtPath<Path> = Path extends readonly [infer Head extends string, ...infer Rest] ? { [P in Head]?: AtPath<Rest>; } : string;
87
118
  /**
88
119
  * Simple check for objects.
89
120
  * @param value Value to test
@@ -101,7 +132,7 @@ export declare function hasOwnProperty(object: unknown, property: string | numbe
101
132
  * @param parameters Params to inject into template
102
133
  * @category Helpers
103
134
  */
104
- export declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
135
+ export declare function formatMessage<T, X extends object = object>(template: ValidatorMessage<T, X>, parameters: X & ValidatorMessageParameters<T>): string;
105
136
  /**
106
137
  * Creates a valid result
107
138
  * @param result Result fields
@@ -113,23 +144,14 @@ export declare function valid<T, E = never>(result: {
113
144
  value: T;
114
145
  field: string | undefined;
115
146
  }): Promise<ValidatorResult<T, E>>;
147
+ //#endregion
148
+ //#region src/invalid-detail.d.ts
116
149
  /**
117
- * Creates an invalid result.
118
- * @param result Result fields
119
- * @param result.message Error message, formatted with `formatMessage`
120
- * @param result.value Parsed value
121
- * @param result.field Field name
122
- * @param result.errors Extra error details, such as per-field errors from `object` and `array`
123
- * @param result.extras Extra message params
124
- * @category Helpers
150
+ * A test on an array itself, such as `max()`. It runs on the parsed array,
151
+ * before the item validator, so the items are `unknown`.
152
+ * @internal
125
153
  */
126
- export declare function invalid<T, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
127
- message: ValidatorMessage<T, P>;
128
- value: unknown;
129
- field: string | undefined;
130
- errors?: E | undefined;
131
- extras?: Omit<P, 'field' | 'value'> | undefined;
132
- }): Promise<ValidatorResult<T, E>>;
154
+ type ArrayTest = ValidationTest<unknown[]>;
133
155
  //#endregion
134
156
  //#region src/standard-schema.d.ts
135
157
  /**
@@ -283,11 +305,13 @@ export declare function parseArray(value: unknown): unknown[] | null | undefined
283
305
  */
284
306
  export declare function applyArrayConfig(config: ArrayConfig, value: unknown): unknown[] | null | undefined;
285
307
  /**
286
- * Validates an array. The first passed validator is used to validate individual
287
- * items. The remaining validators target the array itself.
308
+ * Validates an array. The item validator, if any, comes first and runs on each
309
+ * item. Then an optional configuration, and tests that target the array
310
+ * itself.
288
311
  * @category Type Validators
289
312
  */
290
- export declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): TypeValidator<T[], string | ArrayItemValidatorResult<E>[]>;
313
+ export declare function array<T, E>(item: ValidatorTest<T, E>, config?: Partial<ArrayConfig> | ArrayTest, ...tests: ArrayTest[]): TypeValidator<T[], string | ArrayItemValidatorResult<E>[]>;
314
+ export declare function array(config?: Partial<ArrayConfig>): TypeValidator<unknown[], string | ArrayItemValidatorResult[]>;
291
315
  //#endregion
292
316
  //#region src/booleans.d.ts
293
317
  /**
@@ -312,7 +336,7 @@ type BooleanConfig = ConfigBase<boolean>;
312
336
  * Validates a boolean value.
313
337
  * @category Type Validators
314
338
  */
315
- export declare const boolean: (config?: Partial<BooleanConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<boolean, string>;
339
+ export declare const boolean: (config?: Partial<BooleanConfig> | ValidationTest<boolean, string> | undefined, ...tests: ValidationTest<boolean, string>[]) => TypeValidator<boolean, string>;
316
340
  //#endregion
317
341
  //#region src/dates.d.ts
318
342
  /**
@@ -335,7 +359,7 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
335
359
  * @param exclusive Makes comparison exclusive instead of inclusive
336
360
  * @category Validation Tests
337
361
  */
338
- export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
362
+ export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MessageExtras<MinDateValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<Date, never>;
339
363
  interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
340
364
  max: Date | null | undefined;
341
365
  limit: Date | null | undefined;
@@ -348,7 +372,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
348
372
  * @param exclusive Makes comparison exclusive instead of inclusive
349
373
  * @category Validation Tests
350
374
  */
351
- export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
375
+ export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MessageExtras<MaxDateValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<Date, never>;
352
376
  /**
353
377
  * Configuration for date validation.
354
378
  * @category Types
@@ -358,7 +382,7 @@ type DateConfig = ConfigBase<Date>;
358
382
  * Validates a date value.
359
383
  * @category Type Validators
360
384
  */
361
- export declare const date: (config?: Partial<DateConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<Date, string>;
385
+ export declare const date: (config?: Partial<DateConfig> | ValidationTest<Date, string> | undefined, ...tests: ValidationTest<Date, string>[]) => TypeValidator<Date, string>;
362
386
  //#endregion
363
387
  //#region src/numbers.d.ts
364
388
  /**
@@ -379,7 +403,7 @@ export declare function applyNumberConfig(value: unknown, config: NumberConfig):
379
403
  * @param message Error message
380
404
  * @category Validation Tests
381
405
  */
382
- export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number, never>;
406
+ export declare function integer(message: ValidatorMessage<number>): ValidationTest<number, never>;
383
407
  /**
384
408
  * Configuration for number validation.
385
409
  * @category Types
@@ -392,7 +416,7 @@ interface NumberConfig extends ConfigBase<number> {
392
416
  * Validates a number value.
393
417
  * @category Type Validators
394
418
  */
395
- export declare const number: (config?: Partial<NumberConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<number, string>;
419
+ export declare const number: (config?: Partial<NumberConfig> | ValidationTest<number, string> | undefined, ...tests: ValidationTest<number, string>[]) => TypeValidator<number, string>;
396
420
  //#endregion
397
421
  //#region src/parse-date.d.ts
398
422
  /** Badly formatted date for internal use. */
@@ -411,11 +435,13 @@ export declare function parseDate(value: unknown): Date | null | undefined;
411
435
  * the parsed object. The message goes to the error at `path`, a property name
412
436
  * or a list of names into nested objects, or to the object's own message
413
437
  * without a path. The message gets the parsed object as `value` and the
414
- * object's `field`.
438
+ * object's `field`. The path is checked against the object's type, and so are
439
+ * the errors it produces.
415
440
  * @category Validation Tests
416
441
  */
417
- export declare function check<T, E = never>(test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
418
- export declare function check<T, E = never>(path: string | readonly string[], test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
442
+ export declare function check<T>(test: (value: T) => boolean, message: ValidatorMessage<T>): ValidationTest<T, never>;
443
+ export declare function check<T, const K extends keyof T & string>(path: K, test: (value: T) => boolean, message: ValidatorMessage<T>): ValidationTest<T, { [P in K]?: string; }>;
444
+ export declare function check<T, const Path extends KeyPath<T>>(path: Path, test: (value: T) => boolean, message: ValidatorMessage<T>): ValidationTest<T, AtPath<Path>>;
419
445
  //#endregion
420
446
  //#region src/objects.d.ts
421
447
  type ObjectParameter = Record<string, ValidatorTest>;
@@ -454,7 +480,7 @@ interface ObjectConfig {
454
480
  * every property is valid, and the first failure is the result.
455
481
  * @category Type Validators
456
482
  */
457
- export declare function object<P extends ObjectParameter, V extends ObjectValue<P>, E extends ObjectErrors<P>>(properties: P, config?: ObjectConfig | ValidatorTest<ObjectValue<P>, ObjectErrors<P>>, ...tests: ValidatorTest<ObjectValue<P>, ObjectErrors<P>>[]): TypeValidator<V, E>;
483
+ export declare function object<P extends ObjectParameter>(properties: P, config?: ObjectConfig | ValidationTest<ObjectValue<P>, ObjectErrors<P>>, ...tests: ValidationTest<ObjectValue<P>, ObjectErrors<P>>[]): TypeValidator<ObjectValue<P>, ObjectErrors<P>>;
458
484
  //#endregion
459
485
  //#region src/optional.d.ts
460
486
  /**
@@ -487,7 +513,7 @@ interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<
487
513
  * @param message Error message
488
514
  * @category Validation Tests
489
515
  */
490
- export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T, never>;
516
+ export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, MessageExtras<OneOfValidatorMessageParameters<T>>>): ValidationTest<T, never>;
491
517
  //#endregion
492
518
  //#region src/factories.d.ts
493
519
  /**
@@ -498,14 +524,16 @@ export declare function oneOf<T extends string | number | boolean | Date>(values
498
524
  * @param applyConfig Function that applies configuration to value.
499
525
  * @category Helpers
500
526
  */
501
- export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: NoInfer<C>, applyConfig: (value: unknown, config: C) => T | null | undefined): (config?: Partial<C> | ValidatorTest<unknown, string>, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<T, string>;
527
+ export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: NoInfer<C>, applyConfig: (value: unknown, config: C) => T | null | undefined): (config?: Partial<C> | ValidationTest<T, string>, ...tests: ValidationTest<T, string>[]) => TypeValidator<T, string>;
502
528
  /**
503
- * Captures basic validator test pattern
529
+ * Captures basic validator test pattern. The test gets the parsed value, and
530
+ * fails a missing one, which type validators never pass (#660).
504
531
  * @param test Test function
505
532
  * @param message Error message
506
- * @param getExtras Optional extra fields for error message
533
+ * @param getExtras Extra parameters for the error message
507
534
  */
508
- export declare function createValidatorTest<T = unknown, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(test: (value: unknown, field?: string) => boolean, message: ValidatorMessage<T, P>, getExtras?: (value: T | null | undefined) => Omit<P, 'value' | 'field'>): ValidatorTest<T, E>;
535
+ export declare function createValidatorTest<T, E = never>(test: (value: T, field?: string) => boolean, message: ValidatorMessage<T>): ValidationTest<T, E>;
536
+ export declare function createValidatorTest<T, E, X extends object>(test: (value: T, field?: string) => boolean, message: ValidatorMessage<T, X>, getExtras: (value: T) => X): ValidationTest<T, E>;
509
537
  //#endregion
510
538
  //#region src/limits.d.ts
511
539
  type SharedValueType = string | number | unknown[];
@@ -522,7 +550,7 @@ interface MaxValidatorMessageParameters extends ValidatorMessageParameters<Share
522
550
  * @param exclusive Use exclusive comparison instead of inclusive
523
551
  * @category Validation Tests
524
552
  */
525
- export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
553
+ export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MessageExtras<MaxValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<SharedValueType, never>;
526
554
  interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
527
555
  min: number;
528
556
  limit: number;
@@ -536,7 +564,7 @@ interface MinValidatorMessageParameters extends ValidatorMessageParameters<Share
536
564
  * @param exclusive Use exclusive comparison instead of inclusive
537
565
  * @category Validation Tests
538
566
  */
539
- export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
567
+ export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MessageExtras<MinValidatorMessageParameters>>, exclusive?: boolean): ValidationTest<SharedValueType, never>;
540
568
  interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
541
569
  limit: number;
542
570
  amount: number | null | undefined;
@@ -548,7 +576,33 @@ interface ExactValidatorMessageParameters extends ValidatorMessageParameters<Sha
548
576
  * @param message Error message
549
577
  * @category Validation Tests
550
578
  */
551
- export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType, never>;
579
+ export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, MessageExtras<ExactValidatorMessageParameters>>): ValidationTest<SharedValueType, never>;
580
+ //#endregion
581
+ //#region src/invalid.d.ts
582
+ /** @internal */
583
+ type InvalidFields<E> = {
584
+ value: unknown;
585
+ field: string | undefined;
586
+ errors?: E | undefined;
587
+ };
588
+ /**
589
+ * Creates an invalid result. A message function gets the value as `unknown`,
590
+ * and `extras` must hold the extra parameters it reads.
591
+ * @param result Result fields
592
+ * @param result.message Error message, formatted with `formatMessage`
593
+ * @param result.value Parsed value
594
+ * @param result.field Field name
595
+ * @param result.errors Extra error details, such as per-field errors from `object` and `array`
596
+ * @param result.extras Extra message params
597
+ * @category Helpers
598
+ */
599
+ export declare function invalid<T, E = never>(result: InvalidFields<E> & {
600
+ message: ValidatorMessage<unknown>;
601
+ }): Promise<ValidatorResult<T, E>>;
602
+ export declare function invalid<T, E, X extends object>(result: InvalidFields<E> & {
603
+ message: ValidatorMessage<unknown, X>;
604
+ extras: X;
605
+ }): Promise<ValidatorResult<T, E>>;
552
606
  //#endregion
553
607
  //#region src/strings.d.ts
554
608
  /**
@@ -581,20 +635,20 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
581
635
  * @param message Error message
582
636
  * @category Validation Tests
583
637
  */
584
- export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string, never>;
638
+ export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MessageExtras<MatchesValidatorMessageParameters>>): ValidationTest<string, never>;
585
639
  /**
586
640
  * Ensures a string value matches an email pattern
587
641
  * @param message Error message
588
642
  * @category Validation Tests
589
643
  */
590
- export declare function email(message: ValidatorMessage<string>): ValidatorTest<string, never>;
644
+ export declare function email(message: ValidatorMessage<string>): ValidationTest<string, never>;
591
645
  /**
592
646
  * Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
593
647
  * nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
594
648
  * @param message Error message
595
649
  * @category Validation Tests
596
650
  */
597
- export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string, never>;
651
+ export declare function uuid(message: ValidatorMessage<string>): ValidationTest<string, never>;
598
652
  /**
599
653
  * Ensures a string value is a valid URL
600
654
  * @param message Error message
@@ -602,11 +656,11 @@ export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<s
602
656
  * replaces the default.
603
657
  * @category Validation Tests
604
658
  */
605
- export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string, never>;
659
+ export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidationTest<string, never>;
606
660
  /**
607
661
  * Validates a string value.
608
662
  * @category Type Validators
609
663
  */
610
- export declare const string: (config?: Partial<StringConfig> | ValidatorTest<unknown, string> | undefined, ...tests: ValidatorTest<unknown, string>[]) => TypeValidator<string, string>;
664
+ export declare const string: (config?: Partial<StringConfig> | ValidationTest<string, string> | undefined, ...tests: ValidationTest<string, string>[]) => TypeValidator<string, string>;
611
665
  //#endregion
612
- export type { ArrayConfig, ArrayItemValidatorResult, BooleanConfig, ConfigBase, DateConfig, DeepPartial, ExactValidatorMessageParameters, ExtractError, ExtractValue, InvalidResult, MatchesValidatorMessageParameters, MaxDateValidatorMessageParameters, MaxValidatorMessageParameters, MinDateValidatorMessageParameters, MinValidatorMessageParameters, NumberConfig, ObjectConfig, ObjectParameter as ObjectParam, OneOfValidatorMessageParameters, SharedDateValueType, SharedValueType, StringConfig, TypeValidator, ValidResult, ValidatorFactory, ValidatorMessage, ValidatorMessageParameters, ValidatorResult, ValidatorTest };
666
+ export type { ArrayConfig, ArrayItemValidatorResult, BooleanConfig, ConfigBase, DateConfig, DeepPartial, ExactValidatorMessageParameters, ExtractError, ExtractValue, InvalidResult, MatchesValidatorMessageParameters, MaxDateValidatorMessageParameters, MaxValidatorMessageParameters, MinDateValidatorMessageParameters, MinValidatorMessageParameters, NumberConfig, ObjectConfig, ObjectParameter as ObjectParam, OneOfValidatorMessageParameters, SharedDateValueType, SharedValueType, StringConfig, TypeValidator, ValidResult, ValidationTest, ValidatorFactory, ValidatorMessage, ValidatorMessageParameters, ValidatorResult, ValidatorTest };