validator-fns 2.2.0 → 3.0.0-next.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/README.md +27 -20
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -36
- package/dist/index.d.ts +26 -36
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
//#region src/shared.d.ts
|
|
2
2
|
type ValidResult<T> = {
|
|
3
|
-
/**
|
|
4
|
-
* True if the original value is valid according to all validation tests
|
|
5
|
-
* @deprecated Use `state` instead.
|
|
6
|
-
*/
|
|
7
|
-
isValid: true;
|
|
8
|
-
/** This is preferred to use over `isValid` as it works better for type guards. */
|
|
3
|
+
/** `valid` if the value passed every validation test. */
|
|
9
4
|
state: 'valid';
|
|
10
5
|
/** The parsed value. Note that may be different than the original value. */
|
|
11
6
|
value: T | null | undefined;
|
|
@@ -13,12 +8,7 @@ type ValidResult<T> = {
|
|
|
13
8
|
field?: string | undefined;
|
|
14
9
|
};
|
|
15
10
|
type InvalidResult<E> = {
|
|
16
|
-
/**
|
|
17
|
-
* True if the original value is valid according to all validation tests.
|
|
18
|
-
* @deprecated Use `state` instead.
|
|
19
|
-
*/
|
|
20
|
-
isValid: false;
|
|
21
|
-
/** This is preferred to use over `isValid` as it works better for type guards. */
|
|
11
|
+
/** `invalid` if the value failed a validation test. */
|
|
22
12
|
state: 'invalid';
|
|
23
13
|
/** The parsed value. Note that may be different than the original value. */
|
|
24
14
|
value: unknown;
|
|
@@ -34,19 +24,19 @@ type InvalidResult<E> = {
|
|
|
34
24
|
* @typeParam T The value type
|
|
35
25
|
* @category Types
|
|
36
26
|
*/
|
|
37
|
-
type ValidatorResult<T =
|
|
27
|
+
type ValidatorResult<T = unknown, E = unknown> = ValidResult<T> | InvalidResult<E>;
|
|
38
28
|
/**
|
|
39
29
|
* Validation test function.
|
|
40
30
|
* @typeParam T The value type
|
|
41
31
|
* @category Types
|
|
42
32
|
*/
|
|
43
|
-
type ValidatorTest<T =
|
|
33
|
+
type ValidatorTest<T = unknown, E = unknown> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
|
|
44
34
|
/**
|
|
45
35
|
* Extracts the generic error type from a ValidatorTest.
|
|
46
36
|
* @typeParam T a ValidatorTest
|
|
47
37
|
* @category Types
|
|
48
38
|
*/
|
|
49
|
-
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<
|
|
39
|
+
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<unknown, infer E> ? E : unknown;
|
|
50
40
|
/**
|
|
51
41
|
* Extracts the generic value type from a ValidatorTest.
|
|
52
42
|
* @typeParam T a ValidatorTest
|
|
@@ -64,7 +54,7 @@ type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; };
|
|
|
64
54
|
* @typeParam C The configuration type
|
|
65
55
|
* @category Types
|
|
66
56
|
*/
|
|
67
|
-
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest, ...tests: ValidatorTest[]) => ValidatorTest<T, E>;
|
|
57
|
+
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest<unknown, E>, ...tests: ValidatorTest<unknown, E>[]) => ValidatorTest<T, E>;
|
|
68
58
|
/**
|
|
69
59
|
* Base configuration type.
|
|
70
60
|
* @typeParam T The target type.
|
|
@@ -125,7 +115,7 @@ export declare function valid<T, E = never>(result: {
|
|
|
125
115
|
* @param result.extras Extra message params
|
|
126
116
|
* @category Helpers
|
|
127
117
|
*/
|
|
128
|
-
export declare function invalid<T, E, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
|
|
118
|
+
export declare function invalid<T, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
|
|
129
119
|
message: ValidatorMessage<T, P>;
|
|
130
120
|
value: unknown;
|
|
131
121
|
field: string | undefined;
|
|
@@ -156,7 +146,7 @@ export declare function parseArray(value: unknown): unknown[] | null | undefined
|
|
|
156
146
|
* @param config Configuration to apply
|
|
157
147
|
* @category Helpers
|
|
158
148
|
*/
|
|
159
|
-
export declare function applyArrayConfig
|
|
149
|
+
export declare function applyArrayConfig(config: ArrayConfig, value: unknown): unknown[] | null | undefined;
|
|
160
150
|
/**
|
|
161
151
|
* Validates an array. The first passed validator is used to validate individual
|
|
162
152
|
* items. The remaining validators target the array itself.
|
|
@@ -210,7 +200,7 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
210
200
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
211
201
|
* @category Validation Tests
|
|
212
202
|
*/
|
|
213
|
-
export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date,
|
|
203
|
+
export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
|
|
214
204
|
interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
215
205
|
max: Date | null | undefined;
|
|
216
206
|
limit: Date | null | undefined;
|
|
@@ -223,7 +213,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
223
213
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
224
214
|
* @category Validation Tests
|
|
225
215
|
*/
|
|
226
|
-
export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date>;
|
|
216
|
+
export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
|
|
227
217
|
/**
|
|
228
218
|
* Configuration for date validation.
|
|
229
219
|
* @category Types
|
|
@@ -254,7 +244,7 @@ export declare function applyNumberConfig(value: unknown, config: NumberConfig):
|
|
|
254
244
|
* @param message Error message
|
|
255
245
|
* @category Validation Tests
|
|
256
246
|
*/
|
|
257
|
-
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
247
|
+
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number, never>;
|
|
258
248
|
/**
|
|
259
249
|
* Configuration for number validation.
|
|
260
250
|
* @category Types
|
|
@@ -292,9 +282,9 @@ export declare function check<T, E = never>(test: (value: T) => boolean, message
|
|
|
292
282
|
export declare function check<T, E = never>(path: string | readonly string[], test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
|
|
293
283
|
//#endregion
|
|
294
284
|
//#region src/objects.d.ts
|
|
295
|
-
type ObjectParameter = Record<string, ValidatorTest
|
|
285
|
+
type ObjectParameter = Record<string, ValidatorTest>;
|
|
296
286
|
/** @internal */
|
|
297
|
-
type ObjectValue<P extends ObjectParameter> = { [K in keyof P]?: ExtractValue<P[K]
|
|
287
|
+
type ObjectValue<P extends ObjectParameter> = { [K in keyof P]?: ExtractValue<P[K]> | null; };
|
|
298
288
|
/** @internal */
|
|
299
289
|
type ObjectErrors<P extends ObjectParameter> = { [K in keyof P]?: ExtractError<P[K]>; };
|
|
300
290
|
/**
|
|
@@ -430,7 +420,7 @@ export declare function standard<T, E>(validator: ValidatorTest<T, E>): Validato
|
|
|
430
420
|
* @param nullable Allow null values
|
|
431
421
|
* @category Validation Tests
|
|
432
422
|
*/
|
|
433
|
-
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
423
|
+
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T, never>;
|
|
434
424
|
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
435
425
|
values: readonly T[];
|
|
436
426
|
}
|
|
@@ -440,7 +430,7 @@ interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<
|
|
|
440
430
|
* @param message Error message
|
|
441
431
|
* @category Validation Tests
|
|
442
432
|
*/
|
|
443
|
-
export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
433
|
+
export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T, never>;
|
|
444
434
|
//#endregion
|
|
445
435
|
//#region src/factories.d.ts
|
|
446
436
|
/**
|
|
@@ -451,14 +441,14 @@ export declare function oneOf<T extends string | number | boolean | Date>(values
|
|
|
451
441
|
* @param applyConfig Function that applies configuration to value.
|
|
452
442
|
* @category Helpers
|
|
453
443
|
*/
|
|
454
|
-
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C
|
|
444
|
+
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: NoInfer<C>, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
455
445
|
/**
|
|
456
446
|
* Captures basic validator test pattern
|
|
457
447
|
* @param test Test function
|
|
458
448
|
* @param message Error message
|
|
459
449
|
* @param getExtras Optional extra fields for error message
|
|
460
450
|
*/
|
|
461
|
-
export declare function createValidatorTest<T =
|
|
451
|
+
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>;
|
|
462
452
|
//#endregion
|
|
463
453
|
//#region src/limits.d.ts
|
|
464
454
|
type SharedValueType = string | number | unknown[];
|
|
@@ -475,7 +465,7 @@ interface MaxValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
475
465
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
476
466
|
* @category Validation Tests
|
|
477
467
|
*/
|
|
478
|
-
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
468
|
+
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
|
|
479
469
|
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
480
470
|
min: number;
|
|
481
471
|
limit: number;
|
|
@@ -489,7 +479,7 @@ interface MinValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
489
479
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
490
480
|
* @category Validation Tests
|
|
491
481
|
*/
|
|
492
|
-
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
482
|
+
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
|
|
493
483
|
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
494
484
|
limit: number;
|
|
495
485
|
amount: number | null | undefined;
|
|
@@ -501,7 +491,7 @@ interface ExactValidatorMessageParameters extends ValidatorMessageParameters<Sha
|
|
|
501
491
|
* @param message Error message
|
|
502
492
|
* @category Validation Tests
|
|
503
493
|
*/
|
|
504
|
-
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
494
|
+
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType, never>;
|
|
505
495
|
//#endregion
|
|
506
496
|
//#region src/strings.d.ts
|
|
507
497
|
/**
|
|
@@ -534,28 +524,28 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
|
|
|
534
524
|
* @param message Error message
|
|
535
525
|
* @category Validation Tests
|
|
536
526
|
*/
|
|
537
|
-
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
527
|
+
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string, never>;
|
|
538
528
|
/**
|
|
539
529
|
* Ensures a string value matches an email pattern
|
|
540
530
|
* @param message Error message
|
|
541
531
|
* @category Validation Tests
|
|
542
532
|
*/
|
|
543
|
-
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
533
|
+
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string, never>;
|
|
544
534
|
/**
|
|
545
535
|
* Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
|
|
546
536
|
* nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
|
|
547
537
|
* @param message Error message
|
|
548
538
|
* @category Validation Tests
|
|
549
539
|
*/
|
|
550
|
-
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
540
|
+
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string, never>;
|
|
551
541
|
/**
|
|
552
542
|
* Ensures a string value is a valid URL
|
|
553
543
|
* @param message Error message
|
|
554
|
-
* @param protocols
|
|
555
|
-
*
|
|
544
|
+
* @param protocols Protocols to allow. Defaults to `['http:', 'https:']`; a list
|
|
545
|
+
* replaces the default.
|
|
556
546
|
* @category Validation Tests
|
|
557
547
|
*/
|
|
558
|
-
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
548
|
+
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string, never>;
|
|
559
549
|
/**
|
|
560
550
|
* Validates a string value.
|
|
561
551
|
* @category Type Validators
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
//#region src/shared.d.ts
|
|
2
2
|
type ValidResult<T> = {
|
|
3
|
-
/**
|
|
4
|
-
* True if the original value is valid according to all validation tests
|
|
5
|
-
* @deprecated Use `state` instead.
|
|
6
|
-
*/
|
|
7
|
-
isValid: true;
|
|
8
|
-
/** This is preferred to use over `isValid` as it works better for type guards. */
|
|
3
|
+
/** `valid` if the value passed every validation test. */
|
|
9
4
|
state: 'valid';
|
|
10
5
|
/** The parsed value. Note that may be different than the original value. */
|
|
11
6
|
value: T | null | undefined;
|
|
@@ -13,12 +8,7 @@ type ValidResult<T> = {
|
|
|
13
8
|
field?: string | undefined;
|
|
14
9
|
};
|
|
15
10
|
type InvalidResult<E> = {
|
|
16
|
-
/**
|
|
17
|
-
* True if the original value is valid according to all validation tests.
|
|
18
|
-
* @deprecated Use `state` instead.
|
|
19
|
-
*/
|
|
20
|
-
isValid: false;
|
|
21
|
-
/** This is preferred to use over `isValid` as it works better for type guards. */
|
|
11
|
+
/** `invalid` if the value failed a validation test. */
|
|
22
12
|
state: 'invalid';
|
|
23
13
|
/** The parsed value. Note that may be different than the original value. */
|
|
24
14
|
value: unknown;
|
|
@@ -34,19 +24,19 @@ type InvalidResult<E> = {
|
|
|
34
24
|
* @typeParam T The value type
|
|
35
25
|
* @category Types
|
|
36
26
|
*/
|
|
37
|
-
type ValidatorResult<T =
|
|
27
|
+
type ValidatorResult<T = unknown, E = unknown> = ValidResult<T> | InvalidResult<E>;
|
|
38
28
|
/**
|
|
39
29
|
* Validation test function.
|
|
40
30
|
* @typeParam T The value type
|
|
41
31
|
* @category Types
|
|
42
32
|
*/
|
|
43
|
-
type ValidatorTest<T =
|
|
33
|
+
type ValidatorTest<T = unknown, E = unknown> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
|
|
44
34
|
/**
|
|
45
35
|
* Extracts the generic error type from a ValidatorTest.
|
|
46
36
|
* @typeParam T a ValidatorTest
|
|
47
37
|
* @category Types
|
|
48
38
|
*/
|
|
49
|
-
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<
|
|
39
|
+
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<unknown, infer E> ? E : unknown;
|
|
50
40
|
/**
|
|
51
41
|
* Extracts the generic value type from a ValidatorTest.
|
|
52
42
|
* @typeParam T a ValidatorTest
|
|
@@ -64,7 +54,7 @@ type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; };
|
|
|
64
54
|
* @typeParam C The configuration type
|
|
65
55
|
* @category Types
|
|
66
56
|
*/
|
|
67
|
-
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest, ...tests: ValidatorTest[]) => ValidatorTest<T, E>;
|
|
57
|
+
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest<unknown, E>, ...tests: ValidatorTest<unknown, E>[]) => ValidatorTest<T, E>;
|
|
68
58
|
/**
|
|
69
59
|
* Base configuration type.
|
|
70
60
|
* @typeParam T The target type.
|
|
@@ -125,7 +115,7 @@ export declare function valid<T, E = never>(result: {
|
|
|
125
115
|
* @param result.extras Extra message params
|
|
126
116
|
* @category Helpers
|
|
127
117
|
*/
|
|
128
|
-
export declare function invalid<T, E, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
|
|
118
|
+
export declare function invalid<T, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
|
|
129
119
|
message: ValidatorMessage<T, P>;
|
|
130
120
|
value: unknown;
|
|
131
121
|
field: string | undefined;
|
|
@@ -156,7 +146,7 @@ export declare function parseArray(value: unknown): unknown[] | null | undefined
|
|
|
156
146
|
* @param config Configuration to apply
|
|
157
147
|
* @category Helpers
|
|
158
148
|
*/
|
|
159
|
-
export declare function applyArrayConfig
|
|
149
|
+
export declare function applyArrayConfig(config: ArrayConfig, value: unknown): unknown[] | null | undefined;
|
|
160
150
|
/**
|
|
161
151
|
* Validates an array. The first passed validator is used to validate individual
|
|
162
152
|
* items. The remaining validators target the array itself.
|
|
@@ -210,7 +200,7 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
210
200
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
211
201
|
* @category Validation Tests
|
|
212
202
|
*/
|
|
213
|
-
export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date,
|
|
203
|
+
export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
|
|
214
204
|
interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
215
205
|
max: Date | null | undefined;
|
|
216
206
|
limit: Date | null | undefined;
|
|
@@ -223,7 +213,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
223
213
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
224
214
|
* @category Validation Tests
|
|
225
215
|
*/
|
|
226
|
-
export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date>;
|
|
216
|
+
export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
|
|
227
217
|
/**
|
|
228
218
|
* Configuration for date validation.
|
|
229
219
|
* @category Types
|
|
@@ -254,7 +244,7 @@ export declare function applyNumberConfig(value: unknown, config: NumberConfig):
|
|
|
254
244
|
* @param message Error message
|
|
255
245
|
* @category Validation Tests
|
|
256
246
|
*/
|
|
257
|
-
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
247
|
+
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number, never>;
|
|
258
248
|
/**
|
|
259
249
|
* Configuration for number validation.
|
|
260
250
|
* @category Types
|
|
@@ -292,9 +282,9 @@ export declare function check<T, E = never>(test: (value: T) => boolean, message
|
|
|
292
282
|
export declare function check<T, E = never>(path: string | readonly string[], test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
|
|
293
283
|
//#endregion
|
|
294
284
|
//#region src/objects.d.ts
|
|
295
|
-
type ObjectParameter = Record<string, ValidatorTest
|
|
285
|
+
type ObjectParameter = Record<string, ValidatorTest>;
|
|
296
286
|
/** @internal */
|
|
297
|
-
type ObjectValue<P extends ObjectParameter> = { [K in keyof P]?: ExtractValue<P[K]
|
|
287
|
+
type ObjectValue<P extends ObjectParameter> = { [K in keyof P]?: ExtractValue<P[K]> | null; };
|
|
298
288
|
/** @internal */
|
|
299
289
|
type ObjectErrors<P extends ObjectParameter> = { [K in keyof P]?: ExtractError<P[K]>; };
|
|
300
290
|
/**
|
|
@@ -430,7 +420,7 @@ export declare function standard<T, E>(validator: ValidatorTest<T, E>): Validato
|
|
|
430
420
|
* @param nullable Allow null values
|
|
431
421
|
* @category Validation Tests
|
|
432
422
|
*/
|
|
433
|
-
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
423
|
+
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T, never>;
|
|
434
424
|
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
435
425
|
values: readonly T[];
|
|
436
426
|
}
|
|
@@ -440,7 +430,7 @@ interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<
|
|
|
440
430
|
* @param message Error message
|
|
441
431
|
* @category Validation Tests
|
|
442
432
|
*/
|
|
443
|
-
export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
433
|
+
export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T, never>;
|
|
444
434
|
//#endregion
|
|
445
435
|
//#region src/factories.d.ts
|
|
446
436
|
/**
|
|
@@ -451,14 +441,14 @@ export declare function oneOf<T extends string | number | boolean | Date>(values
|
|
|
451
441
|
* @param applyConfig Function that applies configuration to value.
|
|
452
442
|
* @category Helpers
|
|
453
443
|
*/
|
|
454
|
-
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C
|
|
444
|
+
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: NoInfer<C>, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
455
445
|
/**
|
|
456
446
|
* Captures basic validator test pattern
|
|
457
447
|
* @param test Test function
|
|
458
448
|
* @param message Error message
|
|
459
449
|
* @param getExtras Optional extra fields for error message
|
|
460
450
|
*/
|
|
461
|
-
export declare function createValidatorTest<T =
|
|
451
|
+
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>;
|
|
462
452
|
//#endregion
|
|
463
453
|
//#region src/limits.d.ts
|
|
464
454
|
type SharedValueType = string | number | unknown[];
|
|
@@ -475,7 +465,7 @@ interface MaxValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
475
465
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
476
466
|
* @category Validation Tests
|
|
477
467
|
*/
|
|
478
|
-
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
468
|
+
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
|
|
479
469
|
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
480
470
|
min: number;
|
|
481
471
|
limit: number;
|
|
@@ -489,7 +479,7 @@ interface MinValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
489
479
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
490
480
|
* @category Validation Tests
|
|
491
481
|
*/
|
|
492
|
-
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
482
|
+
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
|
|
493
483
|
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
494
484
|
limit: number;
|
|
495
485
|
amount: number | null | undefined;
|
|
@@ -501,7 +491,7 @@ interface ExactValidatorMessageParameters extends ValidatorMessageParameters<Sha
|
|
|
501
491
|
* @param message Error message
|
|
502
492
|
* @category Validation Tests
|
|
503
493
|
*/
|
|
504
|
-
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
494
|
+
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType, never>;
|
|
505
495
|
//#endregion
|
|
506
496
|
//#region src/strings.d.ts
|
|
507
497
|
/**
|
|
@@ -534,28 +524,28 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
|
|
|
534
524
|
* @param message Error message
|
|
535
525
|
* @category Validation Tests
|
|
536
526
|
*/
|
|
537
|
-
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
527
|
+
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string, never>;
|
|
538
528
|
/**
|
|
539
529
|
* Ensures a string value matches an email pattern
|
|
540
530
|
* @param message Error message
|
|
541
531
|
* @category Validation Tests
|
|
542
532
|
*/
|
|
543
|
-
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
533
|
+
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string, never>;
|
|
544
534
|
/**
|
|
545
535
|
* Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
|
|
546
536
|
* nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
|
|
547
537
|
* @param message Error message
|
|
548
538
|
* @category Validation Tests
|
|
549
539
|
*/
|
|
550
|
-
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
540
|
+
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string, never>;
|
|
551
541
|
/**
|
|
552
542
|
* Ensures a string value is a valid URL
|
|
553
543
|
* @param message Error message
|
|
554
|
-
* @param protocols
|
|
555
|
-
*
|
|
544
|
+
* @param protocols Protocols to allow. Defaults to `['http:', 'https:']`; a list
|
|
545
|
+
* replaces the default.
|
|
556
546
|
* @category Validation Tests
|
|
557
547
|
*/
|
|
558
|
-
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
548
|
+
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string, never>;
|
|
559
549
|
/**
|
|
560
550
|
* Validates a string value.
|
|
561
551
|
* @category Type Validators
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function e(e){return Object.prototype.toString.call(e)===`[object Object]`}function
|
|
1
|
+
function e(e){return e.message||e.errors===void 0?e.message:e.errors}function t(e){return Object.prototype.toString.call(e)===`[object Object]`}function n(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function r(e){return!!Reflect.get(Object(e),Symbol.iterator)}const i=/{(\w+)}/g;function a(e,t){return typeof e==`string`?e.replaceAll(i,(e,n)=>String(Reflect.get(t,n))):e(t)}async function o(e){return s(e)}function s({value:e,field:t}){return{state:`valid`,value:e,field:t}}async function c(e){return l(e)}function l({errors:e,field:t,message:n,value:r,extras:i}){return{state:`invalid`,message:a(n,{...i,value:r,field:t}),value:r,field:t,errors:e}}function u(e){return Array.isArray(e)||e==null?e:r(e)?[...e]:[]}function d(e,t){let n=e.parser(t);return e.default!==void 0&&(n==null||n.length===0)&&(n=e.default),n}async function f(e,t,n){for(let r of e){let e=await r(t,n);if(e.state===`invalid`)return e}}async function p(t,n,r){let i=!0,a=[];if(!n||!t)return{isValid:i,errors:a,values:n};let o=[];for(let[s,c]of n.entries()){let n=await t(c,`${r||``}[${s}]`);o.push(n.value),n.state===`invalid`?(i=!1,a.push(e(n))):a.push(null)}return{isValid:i,errors:a,values:o}}function m(e,...t){let n=typeof e==`function`?[e,...t]:t,r={parser:u,...typeof e==`function`?void 0:e};return async(e,t)=>{let i=d(r,e),[a,...s]=n,l=await f(s,i,t),u=await p(a,i,t);return!l&&u.isValid?o({value:u.values,field:t}):c({message:l?l.message:``,value:e,field:t,errors:u.errors})}}const h=/* @__PURE__ */ new WeakMap;function g(e,t){return(n,...r)=>{let i=e,a=r;return n!==void 0&&(typeof n==`function`?a=[n,...r]:i={...e,...n}),async(e,n)=>{try{let r=t(e,i);for(let e of a){let t=h.get(e),i;if(i=t?t(r,n):await e(r,n),i.state===`invalid`)return i}return s({value:r,field:n})}catch(t){return c({message:t instanceof Error?t.message:String(t),value:e,field:n})}}}}function _(e,t,n){let r=(r,i)=>e(r,i)?s({value:r,field:i}):l({message:t,value:r,field:i,extras:n?n(r):void 0}),i=async(e,t)=>r(e,t);return h.set(i,r),i}function v(e){return e==null||typeof e==`boolean`?e:!!e}function y(e,t){let n=t.parser(e);return t.default!==void 0&&n==null&&(n=t.default),n}const b=g({parser:v},y),x=/* @__PURE__ */ new Date(``),S=/^(\d{4})(?:-([0-2]\d)(?:-([0-3]\d)(?:[ T]([0-2]\d):([0-5]\d)(?::([0-5]\d)(\.\d+)?(Z|[+-][0-2]\d:?[0-5]\d)?)?)?)?)?$/,C=/^([+-])([0-2]\d):?([0-5]\d)$/;function w(e){let t=C.exec(e);if(e===`Z`||!t)return 0;let[,n,r,i]=t;return(n===`-`?-1:1)*(Number(r)*60+Number(i))}const T=[()=>0,e=>Number(e),(e=`1`)=>Number.parseInt(e,10)-1,(e=`1`)=>Number.parseInt(e,10),(e=`0`)=>Number.parseInt(e,10),(e=`0`)=>Number.parseInt(e,10),(e=`0`)=>Number.parseInt(e,10),(e=`0`)=>Number.parseFloat(e)*1e3];function E(e){return e==null||e instanceof Date?e:typeof e==`number`?new Date(e):typeof e==`string`?D(e):x}function D(e){let t=S.exec(e);if(t===null)return x;let[,n=0,r=0,i=0,a=0,o=0,s=0,c=0]=T.map((e,n)=>e(t[n])),l=t[8];return l?new Date(Date.UTC(n,r,i,a,o+w(l),s,c)):new Date(n,r,i,a,o,s,c)}function O(e,t){let n=t.parser(e);return t.default!==void 0&&(n==null||Number.isNaN(n.getTime()))&&(n=t.default),n}function k(e,t,n){let r=E(e);return _(e=>r!=null&&(e==null||e instanceof Date&&(n?e>r:e>=r)),t,()=>({min:r,limit:r,exclusive:n}))}function ee(e,t,n){let r=E(e);return _(e=>r!=null&&(e==null||e instanceof Date&&(n?e<r:e<=r)),t,()=>({max:r,limit:r,exclusive:n}))}const te=g({parser:E},O),ne=[e=>String.prototype.valueOf.call(e),e=>Number.prototype.valueOf.call(e),e=>Boolean.prototype.valueOf.call(e)];function A(e){if(typeof e!=`object`||!e)return e;for(let t of ne)try{return t(e)}catch{}return e}function j(e){let t=A(e);switch(typeof t){case`string`:case`number`:case`bigint`:case`boolean`:return String(t);default:return}}function M(e){if(e==null||typeof e==`number`)return e;if(e instanceof Date)return e.getTime();let t=j(e),n=t===void 0?NaN:Number.parseFloat(t);if(Number.isNaN(n)&&(t===void 0||t.trim()!==``))throw TypeError(`Failed to parse value to number.`);return n}function N(e,t){let n=t.parser(e);if(t.default!==void 0&&(n==null||Number.isNaN(n))&&(n=t.default),t.round&&typeof n==`number`)switch(t.round){case`nearest`:n=Math.round(n);break;case`ceil`:n=Math.ceil(n);break;case`floor`:n=Math.floor(n);break;default:throw Error(`Unknown round type: ${String(t.round)}`)}return n}function P(e){return async(t,n)=>t==null||typeof t==`number`&&Number.isFinite(t)&&Math.floor(t)===t?o({value:t,field:n}):c({message:e,value:t,field:n})}const F=g({parser:M},N);function I(...e){let[t,n,r]=e.length===2?[[],...e]:e,i=typeof t==`string`?[t]:t;return async(e,t)=>{let s=e;return n(s)?o({value:s,field:t}):i.length===0?c({message:r,value:s,field:t}):c({message:``,value:s,field:t,errors:i.reduceRight((e,t)=>({[t]:e}),a(r,{value:s,field:t}))})}}function L(e,t,n){Object.defineProperty(e,t,{value:n,enumerable:!0,writable:!0,configurable:!0})}async function R(e,t,n){for(let r of e){let e=await r(t,n);if(e.state===`invalid`)return e}}function z(t,...r){if(typeof t!=`object`||!t)throw TypeError("`properties` must be a configuration object");return async(i,a)=>{let s=Object(i),l={},u={},d=!0;for(let r in t)if(n(t,r)){let i=t[r],a=n(s,r)?Reflect.get(s,r):void 0,o=await(typeof i==`function`?i(a,r):c({field:r,message:`No validator set`,value:a}));L(u,r,o.value),o.state===`invalid`&&(d=!1,L(l,r,e(o)))}if(!d)return c({message:``,value:i,field:a,errors:l});let f=await R(r,u,a);return f?c({message:f.message,value:i,field:a,errors:f.errors}):o({value:u,field:a})}}const B=`Invalid value.`;function V(e,t,n){if(typeof e==`string`)n.push({message:e||B,path:t});else if(Array.isArray(e))for(let[r,i]of e.entries())i!==null&&V(i,[...t,r],n);else if(typeof e==`object`&&e)for(let r of Object.keys(e))V(Reflect.get(e,r),[...t,r],n);else n.push({message:B,path:t})}function H(e){return Object.assign((t,n)=>e(t,n),{"~standard":{version:1,vendor:`validator-fns`,validate:async t=>{let n=await e(t);if(n.state===`valid`)return{value:n.value};let r=n.message?[{message:n.message}]:[];return n.errors!==void 0&&V(n.errors,[],r),{issues:r.length>0?r:[{message:B}]}}}})}function U(e,t){return _(e=>t&&e===null||e!=null&&(typeof e!=`string`||e!==``)&&!(typeof e==`number`&&Number.isNaN(e))&&!(e instanceof Date&&Number.isNaN(e.valueOf())),e)}function W(e,t){let n=e;return _(t=>t==null||n.includes(t)||t instanceof Date&&e.some(e=>e.valueOf()===t.valueOf()),t,()=>({values:e}))}function G(e){return typeof e==`string`||Array.isArray(e)?e.length:e}function K(e,t,n){return _(t=>{let r=G(t);return r==null||t===``||(n?r<e:r<=e)},t,t=>({max:e,limit:e,amount:G(t),exclusive:n}))}function q(e,t,n){return _(t=>{let r=G(t);return r==null||t===``||(n?r>e:r>=e)},t,t=>({min:e,limit:e,amount:G(t),exclusive:n}))}function J(e,t){return _(t=>{let n=G(t);return n==null||t===``||n===e},t,t=>({amount:G(t),limit:e,exact:e}))}const Y=/^[\w!#$%&'*+./=?^`{|}~-]+@[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?)*$/,X=/^(?:[\da-f]{8}-[\da-f]{4}-[1-8][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}|0{8}-0{4}-0{4}-0{4}-0{12}|f{8}-f{4}-f{4}-f{4}-f{12})$/i;function Z(e){if(e==null||typeof e==`string`)return e;if(e instanceof Date)return e.toISOString();let t=j(e);if(t===void 0)throw TypeError(`Failed to parse value to string.`);return t}function Q(e,t){let n=t.parser(e);return t.default!==void 0&&(n==null||n===``)&&(n=t.default),t.trim&&typeof n==`string`&&(n=n.trim()),n}function $(e,t){let n=new RegExp(e);return _(e=>(n.lastIndex=0,e==null||e===``||typeof e==`string`&&n.test(e)),t,()=>({pattern:e}))}function re(e){return $(Y,e)}function ie(e){return $(X,e)}function ae(e,t=[`http:`,`https:`]){return _(e=>{try{let n;if(e==null||e===``||typeof e==`string`&&(n=new URL(e)))return!n||t.includes(n.protocol)}catch{}return!1},e,()=>({protocols:t}))}const oe=g({parser:Z},Q);export{d as applyArrayConfig,y as applyBooleanConfig,O as applyDateConfig,N as applyNumberConfig,Q as applyStringConfig,m as array,b as boolean,I as check,g as createTypeValidatorTest,_ as createValidatorTest,te as date,re as email,J as exact,a as formatMessage,n as hasOwnProperty,P as integer,c as invalid,x as invalidDate,t as isObject,$ as matches,K as max,ee as maxDate,q as min,k as minDate,F as number,z as object,W as oneOf,u as parseArray,v as parseBoolean,E as parseDate,M as parseNumber,Z as parseString,U as required,H as standard,oe as string,ae as url,ie as uuid,o as valid};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|