validator-fns 2.1.0 → 3.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -15
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +127 -122
- package/dist/index.d.ts +127 -122
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +9 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,51 +1,52 @@
|
|
|
1
|
+
//#region src/shared.d.ts
|
|
1
2
|
type ValidResult<T> = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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. */
|
|
9
|
+
state: 'valid';
|
|
10
|
+
/** The parsed value. Note that may be different than the original value. */
|
|
11
|
+
value: T | null | undefined;
|
|
12
|
+
/** Field name if provided. Automatically provided if using object validation. */
|
|
13
|
+
field?: string | undefined;
|
|
13
14
|
};
|
|
14
15
|
type InvalidResult<E> = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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. */
|
|
22
|
+
state: 'invalid';
|
|
23
|
+
/** The parsed value. Note that may be different than the original value. */
|
|
24
|
+
value: unknown;
|
|
25
|
+
/** Error message if `state` is invalid. */
|
|
26
|
+
message: string;
|
|
27
|
+
/** Field name if provided. Automatically provided if using object validation. */
|
|
28
|
+
field?: string | undefined;
|
|
29
|
+
/** Extra error details */
|
|
30
|
+
errors?: E | undefined;
|
|
30
31
|
};
|
|
31
32
|
/**
|
|
32
33
|
* The result of a validation.
|
|
33
34
|
* @typeParam T The value type
|
|
34
35
|
* @category Types
|
|
35
36
|
*/
|
|
36
|
-
type ValidatorResult<T =
|
|
37
|
+
type ValidatorResult<T = unknown, E = unknown> = ValidResult<T> | InvalidResult<E>;
|
|
37
38
|
/**
|
|
38
39
|
* Validation test function.
|
|
39
40
|
* @typeParam T The value type
|
|
40
41
|
* @category Types
|
|
41
42
|
*/
|
|
42
|
-
type ValidatorTest<T =
|
|
43
|
+
type ValidatorTest<T = unknown, E = unknown> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
|
|
43
44
|
/**
|
|
44
45
|
* Extracts the generic error type from a ValidatorTest.
|
|
45
46
|
* @typeParam T a ValidatorTest
|
|
46
47
|
* @category Types
|
|
47
48
|
*/
|
|
48
|
-
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<
|
|
49
|
+
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<unknown, infer E> ? E : unknown;
|
|
49
50
|
/**
|
|
50
51
|
* Extracts the generic value type from a ValidatorTest.
|
|
51
52
|
* @typeParam T a ValidatorTest
|
|
@@ -57,29 +58,27 @@ type ExtractValue<T extends ValidatorTest> = T extends ValidatorTest<infer V> ?
|
|
|
57
58
|
* @typeParam T any object type
|
|
58
59
|
* @category Types
|
|
59
60
|
*/
|
|
60
|
-
type DeepPartial<T> = {
|
|
61
|
-
[P in keyof T]?: DeepPartial<T[P]>;
|
|
62
|
-
};
|
|
61
|
+
type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]>; };
|
|
63
62
|
/**
|
|
64
63
|
* Validation factory function.
|
|
65
64
|
* @typeParam C The configuration type
|
|
66
65
|
* @category Types
|
|
67
66
|
*/
|
|
68
|
-
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest, ...tests: ValidatorTest[]) => ValidatorTest<T, E>;
|
|
67
|
+
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest<unknown, E>, ...tests: ValidatorTest<unknown, E>[]) => ValidatorTest<T, E>;
|
|
69
68
|
/**
|
|
70
69
|
* Base configuration type.
|
|
71
70
|
* @typeParam T The target type.
|
|
72
71
|
* @category Types
|
|
73
72
|
*/
|
|
74
73
|
interface ConfigBase<T> {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
/** Parser to convert value into the designated type. */
|
|
75
|
+
parser: (value: unknown) => T | null | undefined;
|
|
76
|
+
/** Provide a fallback value in case the original value is undefined. */
|
|
77
|
+
default?: T;
|
|
79
78
|
}
|
|
80
79
|
interface ValidatorMessageParameters<T> {
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
value: T | null | undefined;
|
|
81
|
+
field: string | undefined;
|
|
83
82
|
}
|
|
84
83
|
/**
|
|
85
84
|
* Validation message. May be a plain string or a function that accepts arguments.
|
|
@@ -91,20 +90,20 @@ type ValidatorMessage<T, P extends ValidatorMessageParameters<T> = ValidatorMess
|
|
|
91
90
|
* Simple check for objects.
|
|
92
91
|
* @param value Value to test
|
|
93
92
|
*/
|
|
94
|
-
declare function isObject(value: unknown): boolean;
|
|
93
|
+
export declare function isObject(value: unknown): boolean;
|
|
95
94
|
/**
|
|
96
95
|
* Safe hasOwnProperty check
|
|
97
96
|
* @param object Object to check
|
|
98
97
|
* @param property Property to look for
|
|
99
98
|
*/
|
|
100
|
-
declare function hasOwnProperty(object: unknown, property: string | number | symbol): boolean;
|
|
99
|
+
export declare function hasOwnProperty(object: unknown, property: string | number | symbol): boolean;
|
|
101
100
|
/**
|
|
102
101
|
* Formats a message using a squiggly bracket `{}` template.
|
|
103
102
|
* @param template Message template as string or function
|
|
104
103
|
* @param parameters Params to inject into template
|
|
105
104
|
* @category Helpers
|
|
106
105
|
*/
|
|
107
|
-
declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
|
|
106
|
+
export declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
|
|
108
107
|
/**
|
|
109
108
|
* Creates a valid result
|
|
110
109
|
* @param result Result fields
|
|
@@ -112,9 +111,9 @@ declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(templ
|
|
|
112
111
|
* @param result.field Field name
|
|
113
112
|
* @category Helpers
|
|
114
113
|
*/
|
|
115
|
-
declare function valid<T, E = never>({ value, field
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
export declare function valid<T, E = never>({ value, field }: {
|
|
115
|
+
value: T | null | undefined;
|
|
116
|
+
field: string | undefined;
|
|
118
117
|
}): Promise<ValidatorResult<T, E>>;
|
|
119
118
|
/**
|
|
120
119
|
* Creates an invalid result.
|
|
@@ -126,14 +125,15 @@ declare function valid<T, E = never>({ value, field, }: {
|
|
|
126
125
|
* @param result.extras Extra message params
|
|
127
126
|
* @category Helpers
|
|
128
127
|
*/
|
|
129
|
-
declare function invalid<T, E, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>({ errors, field, message, value, extras
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
128
|
+
export declare function invalid<T, E = never, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>({ errors, field, message, value, extras }: {
|
|
129
|
+
message: ValidatorMessage<T, P>;
|
|
130
|
+
value: unknown;
|
|
131
|
+
field: string | undefined;
|
|
132
|
+
errors?: E | undefined;
|
|
133
|
+
extras?: Omit<P, 'field' | 'value'> | undefined;
|
|
135
134
|
}): Promise<ValidatorResult<T, E>>;
|
|
136
|
-
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/arrays.d.ts
|
|
137
137
|
/**
|
|
138
138
|
* Invalid validation result for an item in the array
|
|
139
139
|
* @category Types
|
|
@@ -149,34 +149,35 @@ type ArrayConfig = ConfigBase<unknown[]>;
|
|
|
149
149
|
* @param value Value to parse
|
|
150
150
|
* @category Parsers
|
|
151
151
|
*/
|
|
152
|
-
declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
152
|
+
export declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
153
153
|
/**
|
|
154
154
|
* Applies a configuration to the array value.
|
|
155
155
|
* @param value Value to modify
|
|
156
156
|
* @param config Configuration to apply
|
|
157
157
|
* @category Helpers
|
|
158
158
|
*/
|
|
159
|
-
declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
159
|
+
export declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
160
160
|
/**
|
|
161
161
|
* Validates an array. The first passed validator is used to validate individual
|
|
162
162
|
* items. The remaining validators target the array itself.
|
|
163
163
|
* @category Type Validators
|
|
164
164
|
*/
|
|
165
|
-
declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]>;
|
|
166
|
-
|
|
165
|
+
export declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]>;
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/booleans.d.ts
|
|
167
168
|
/**
|
|
168
169
|
* Parses a value into a boolean.
|
|
169
170
|
* @param value Value to parse
|
|
170
171
|
* @category Parsers
|
|
171
172
|
*/
|
|
172
|
-
declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
173
|
+
export declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
173
174
|
/**
|
|
174
175
|
* Applies a configuration to the boolean value.
|
|
175
176
|
* @param value Value to modify
|
|
176
177
|
* @param config Configuration to apply
|
|
177
178
|
* @category Helpers
|
|
178
179
|
*/
|
|
179
|
-
declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
180
|
+
export declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
180
181
|
/**
|
|
181
182
|
* Configuration for boolean validation.
|
|
182
183
|
* @category Types
|
|
@@ -186,20 +187,21 @@ type BooleanConfig = ConfigBase<boolean>;
|
|
|
186
187
|
* Validates a boolean value.
|
|
187
188
|
* @category Type Validators
|
|
188
189
|
*/
|
|
189
|
-
declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
190
|
-
|
|
190
|
+
export declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/dates.d.ts
|
|
191
193
|
/**
|
|
192
194
|
* Applies a configuration to the date value.
|
|
193
195
|
* @param value Value to modify
|
|
194
196
|
* @param config Configuration to apply
|
|
195
197
|
* @category Helpers
|
|
196
198
|
*/
|
|
197
|
-
declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
199
|
+
export declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
198
200
|
type SharedDateValueType = Date | string | number;
|
|
199
201
|
interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
202
|
+
min: Date | null | undefined;
|
|
203
|
+
limit: Date | null | undefined;
|
|
204
|
+
exclusive?: boolean | undefined;
|
|
203
205
|
}
|
|
204
206
|
/**
|
|
205
207
|
* Ensures a date value is on or after a date.
|
|
@@ -208,11 +210,11 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
208
210
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
209
211
|
* @category Validation Tests
|
|
210
212
|
*/
|
|
211
|
-
declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date,
|
|
213
|
+
export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
|
|
212
214
|
interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
max: Date | null | undefined;
|
|
216
|
+
limit: Date | null | undefined;
|
|
217
|
+
exclusive?: boolean | undefined;
|
|
216
218
|
}
|
|
217
219
|
/**
|
|
218
220
|
* Ensures a date value is on or before a date.
|
|
@@ -221,7 +223,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
221
223
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
222
224
|
* @category Validation Tests
|
|
223
225
|
*/
|
|
224
|
-
declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date>;
|
|
226
|
+
export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
|
|
225
227
|
/**
|
|
226
228
|
* Configuration for date validation.
|
|
227
229
|
* @category Types
|
|
@@ -231,70 +233,70 @@ type DateConfig = ConfigBase<Date>;
|
|
|
231
233
|
* Validates a date value.
|
|
232
234
|
* @category Type Validators
|
|
233
235
|
*/
|
|
234
|
-
declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
235
|
-
|
|
236
|
+
export declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/numbers.d.ts
|
|
236
239
|
/**
|
|
237
240
|
* Parses a value into a number.
|
|
238
241
|
* @param value Value to parse
|
|
239
242
|
* @category Parsers
|
|
240
243
|
*/
|
|
241
|
-
declare function parseNumber(value: unknown): number | null | undefined;
|
|
244
|
+
export declare function parseNumber(value: unknown): number | null | undefined;
|
|
242
245
|
/**
|
|
243
246
|
* Applies a configuration to the number value.
|
|
244
247
|
* @param value Value to modify
|
|
245
248
|
* @param config Configuration to apply
|
|
246
249
|
* @category Helpers
|
|
247
250
|
*/
|
|
248
|
-
declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
251
|
+
export declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
249
252
|
/**
|
|
250
253
|
* Ensures a number value is an integer.
|
|
251
254
|
* @param message Error message
|
|
252
255
|
* @category Validation Tests
|
|
253
256
|
*/
|
|
254
|
-
declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
257
|
+
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number, never>;
|
|
255
258
|
/**
|
|
256
259
|
* Configuration for number validation.
|
|
257
260
|
* @category Types
|
|
258
261
|
*/
|
|
259
262
|
interface NumberConfig extends ConfigBase<number> {
|
|
260
|
-
|
|
261
|
-
|
|
263
|
+
/** Apply rounding mechanism before validation. */
|
|
264
|
+
round?: 'nearest' | 'floor' | 'ceil';
|
|
262
265
|
}
|
|
263
266
|
/**
|
|
264
267
|
* Validates a number value.
|
|
265
268
|
* @category Type Validators
|
|
266
269
|
*/
|
|
267
|
-
declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
268
|
-
|
|
270
|
+
export declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/parse-date.d.ts
|
|
269
273
|
/** Badly formatted date for internal use. */
|
|
270
|
-
declare const invalidDate: Date;
|
|
274
|
+
export declare const invalidDate: Date;
|
|
271
275
|
/**
|
|
272
276
|
* Parses a value into a date.
|
|
273
277
|
* @param value Value to parse
|
|
274
278
|
* @category Parsers
|
|
275
279
|
*/
|
|
276
|
-
declare function parseDate(value: unknown): Date | null | undefined;
|
|
277
|
-
|
|
278
|
-
|
|
280
|
+
export declare function parseDate(value: unknown): Date | null | undefined;
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/objects.d.ts
|
|
283
|
+
type ObjectParameter = Record<string, ValidatorTest>;
|
|
279
284
|
/**
|
|
280
285
|
* Validates an object.
|
|
281
286
|
* @category Type Validators
|
|
282
287
|
*/
|
|
283
|
-
declare function object<P extends ObjectParameter, V extends {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
[K in keyof P]?: ExtractError<P[K]>;
|
|
287
|
-
}>(properties: P): ValidatorTest<V, E>;
|
|
288
|
-
|
|
288
|
+
export declare function object<P extends ObjectParameter, V extends { [K in keyof P]?: ExtractValue<P[K]>; }, E extends { [K in keyof P]?: ExtractError<P[K]>; }>(properties: P): ValidatorTest<V, E>;
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/common.d.ts
|
|
289
291
|
/**
|
|
290
292
|
* Ensures a value is not undefined, null, empty string, NaN, nor invalid date.
|
|
291
293
|
* @param message Error message
|
|
292
294
|
* @param nullable Allow null values
|
|
293
295
|
* @category Validation Tests
|
|
294
296
|
*/
|
|
295
|
-
declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
297
|
+
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T, never>;
|
|
296
298
|
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
297
|
-
|
|
299
|
+
values: readonly T[];
|
|
298
300
|
}
|
|
299
301
|
/**
|
|
300
302
|
* Ensures a value is one in a predefined list.
|
|
@@ -302,8 +304,9 @@ interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<
|
|
|
302
304
|
* @param message Error message
|
|
303
305
|
* @category Validation Tests
|
|
304
306
|
*/
|
|
305
|
-
declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
306
|
-
|
|
307
|
+
export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T, never>;
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/factories.d.ts
|
|
307
310
|
/**
|
|
308
311
|
* Creates a type validator.
|
|
309
312
|
* @typeParam T The basic type for the final value to be validated.
|
|
@@ -312,21 +315,22 @@ declare function oneOf<T extends string | number | boolean | Date>(values: reado
|
|
|
312
315
|
* @param applyConfig Function that applies configuration to value.
|
|
313
316
|
* @category Helpers
|
|
314
317
|
*/
|
|
315
|
-
declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
318
|
+
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
316
319
|
/**
|
|
317
320
|
* Captures basic validator test pattern
|
|
318
321
|
* @param test Test function
|
|
319
322
|
* @param message Error message
|
|
320
323
|
* @param getExtras Optional extra fields for error message
|
|
321
324
|
*/
|
|
322
|
-
declare function createValidatorTest<T =
|
|
323
|
-
|
|
325
|
+
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>;
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/limits.d.ts
|
|
324
328
|
type SharedValueType = string | number | unknown[];
|
|
325
329
|
interface MaxValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
+
max: number;
|
|
331
|
+
amount: number | null | undefined;
|
|
332
|
+
limit: number;
|
|
333
|
+
exclusive?: boolean | undefined;
|
|
330
334
|
}
|
|
331
335
|
/**
|
|
332
336
|
* Ensures a string, number, or array value is at most a certain amount.
|
|
@@ -335,12 +339,12 @@ interface MaxValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
335
339
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
336
340
|
* @category Validation Tests
|
|
337
341
|
*/
|
|
338
|
-
declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
342
|
+
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
|
|
339
343
|
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
+
min: number;
|
|
345
|
+
limit: number;
|
|
346
|
+
amount: number | null | undefined;
|
|
347
|
+
exclusive?: boolean | undefined;
|
|
344
348
|
}
|
|
345
349
|
/**
|
|
346
350
|
* Ensures a string, number, or array value is at most a certain amount.
|
|
@@ -349,11 +353,11 @@ interface MinValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
349
353
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
350
354
|
* @category Validation Tests
|
|
351
355
|
*/
|
|
352
|
-
declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
356
|
+
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType, never>;
|
|
353
357
|
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
358
|
+
limit: number;
|
|
359
|
+
amount: number | null | undefined;
|
|
360
|
+
exact: number;
|
|
357
361
|
}
|
|
358
362
|
/**
|
|
359
363
|
* Ensures a string, number, or array value is exactly a certain amount.
|
|
@@ -361,31 +365,32 @@ interface ExactValidatorMessageParameters extends ValidatorMessageParameters<Sha
|
|
|
361
365
|
* @param message Error message
|
|
362
366
|
* @category Validation Tests
|
|
363
367
|
*/
|
|
364
|
-
declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
365
|
-
|
|
368
|
+
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType, never>;
|
|
369
|
+
//#endregion
|
|
370
|
+
//#region src/strings.d.ts
|
|
366
371
|
/**
|
|
367
372
|
* Configuration for string validation.
|
|
368
373
|
* @category Types
|
|
369
374
|
*/
|
|
370
375
|
interface StringConfig extends ConfigBase<string> {
|
|
371
|
-
|
|
372
|
-
|
|
376
|
+
/** Apply trim to string before validation. */
|
|
377
|
+
trim?: boolean;
|
|
373
378
|
}
|
|
374
379
|
/**
|
|
375
380
|
* Parses a value into a string.
|
|
376
381
|
* @param value Value to parse
|
|
377
382
|
* @category Parsers
|
|
378
383
|
*/
|
|
379
|
-
declare function parseString(value: unknown): string | null | undefined;
|
|
384
|
+
export declare function parseString(value: unknown): string | null | undefined;
|
|
380
385
|
/**
|
|
381
386
|
* Applies a configuration to the string value.
|
|
382
387
|
* @param value Value to modify
|
|
383
388
|
* @param config Configuration to apply
|
|
384
389
|
* @category Helpers
|
|
385
390
|
*/
|
|
386
|
-
declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
391
|
+
export declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
387
392
|
interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<string> {
|
|
388
|
-
|
|
393
|
+
pattern: RegExp;
|
|
389
394
|
}
|
|
390
395
|
/**
|
|
391
396
|
* Ensures a string value matches a pattern
|
|
@@ -393,20 +398,20 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
|
|
|
393
398
|
* @param message Error message
|
|
394
399
|
* @category Validation Tests
|
|
395
400
|
*/
|
|
396
|
-
declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
401
|
+
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string, never>;
|
|
397
402
|
/**
|
|
398
403
|
* Ensures a string value matches an email pattern
|
|
399
404
|
* @param message Error message
|
|
400
405
|
* @category Validation Tests
|
|
401
406
|
*/
|
|
402
|
-
declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
407
|
+
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string, never>;
|
|
403
408
|
/**
|
|
404
409
|
* Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
|
|
405
410
|
* nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
|
|
406
411
|
* @param message Error message
|
|
407
412
|
* @category Validation Tests
|
|
408
413
|
*/
|
|
409
|
-
declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
414
|
+
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string, never>;
|
|
410
415
|
/**
|
|
411
416
|
* Ensures a string value is a valid URL
|
|
412
417
|
* @param message Error message
|
|
@@ -414,11 +419,11 @@ declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
|
414
419
|
* and `data:` URLs are valid; use `['http:', 'https:']` for values rendered as links.
|
|
415
420
|
* @category Validation Tests
|
|
416
421
|
*/
|
|
417
|
-
declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
422
|
+
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string, never>;
|
|
418
423
|
/**
|
|
419
424
|
* Validates a string value.
|
|
420
425
|
* @category Type Validators
|
|
421
426
|
*/
|
|
422
|
-
declare const string: ValidatorFactory<StringConfig, string, string>;
|
|
423
|
-
|
|
424
|
-
export {
|
|
427
|
+
export declare const string: ValidatorFactory<StringConfig, string, string>;
|
|
428
|
+
//#endregion
|
|
429
|
+
export type { ArrayConfig, ArrayItemValidatorResult, BooleanConfig, ConfigBase, DateConfig, DeepPartial, ExactValidatorMessageParameters, ExtractError, ExtractValue, InvalidResult, MatchesValidatorMessageParameters, MaxDateValidatorMessageParameters, MaxValidatorMessageParameters, MinDateValidatorMessageParameters, MinValidatorMessageParameters, NumberConfig, ObjectParameter as ObjectParam, OneOfValidatorMessageParameters, SharedDateValueType, SharedValueType, StringConfig, ValidResult, ValidatorFactory, ValidatorMessage, ValidatorMessageParameters, ValidatorResult, ValidatorTest };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function
|
|
1
|
+
function e(e){return Object.prototype.toString.call(e)===`[object Object]`}function t(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function n(e){return!!Reflect.get(Object(e),Symbol.iterator)}const r=/{(\w+)}/g;function i(e,t){return typeof e==`string`?e.replaceAll(r,(e,n)=>String(Reflect.get(t,n))):e(t)}async function a({value:e,field:t}){return{isValid:!0,state:`valid`,value:e,field:t}}async function o({errors:e,field:t,message:n,value:r,extras:a}){return{isValid:!1,state:`invalid`,message:i(n,{...a,value:r,field:t}),value:r,field:t,errors:e}}function s(e){return Array.isArray(e)||e==null?e:n(e)?[...e]:[]}function c(e,t){let n=e.parser(t);return e.default!==void 0&&(n==null||n.length===0)&&(n=e.default),n}async function l(e,t,n){for(let r of e){let e=await r(t,n);if(e.state===`invalid`)return e}}async function u(e,t,n){let r=!0,i=[];if(!t||!e)return{isValid:r,errors:i};for(let[a,o]of t.entries()){let t=await e(o,n?`${n}[${a}]`:`[${a}]`);t.state===`invalid`?(r=!1,i.push(t.message||t.errors)):i.push(null)}return{isValid:r,errors:i}}function d(e,...t){let n=t,r={parser:s};return e!==void 0&&(typeof e==`function`?n=[e,...t]:r={...r,...e}),async(e,t)=>{let i=c(r,e),[s,...d]=n,f=await l(d,i,t),p=await u(s,i,t);return!f&&p.isValid?a({value:i,field:t}):o({message:f?f.message:``,value:e,field:t,errors:p.errors})}}function f(e,t){return(n,...r)=>{let i=e,s=r;return n!==void 0&&(typeof n==`function`?s=[n,...r]:i={...e,...n}),async(e,n)=>{try{let r=t(e,i);for(let e of s){let t=await e(r,n);if(t.state===`invalid`)return t}return await a({value:r,field:n})}catch(t){return o({message:t instanceof Error?t.message:String(t),value:e,field:n})}}}}function p(e,t,n){return async(r,i)=>e(r,i)?a({value:r,field:i}):o({message:t,value:r,field:i,extras:n?n(r):void 0})}function m(e){return e==null||typeof e==`boolean`?e:!!e}function h(e,t){let n=t.parser(e);return t.default!==void 0&&n==null&&(n=t.default),n}const g=f({parser:m},h),_=/* @__PURE__ */ new Date(``),v=/^(\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)?)?)?)?)?$/,y=/^([+-])([0-2]\d):?([0-5]\d)$/;function b(e){let t=y.exec(e);if(e===`Z`||!t)return 0;let[,n,r=`0`,i=`0`]=t;return(n===`-`?-1:1)*(Number.parseInt(r,10)*60+Number.parseInt(i,10))}const x=[()=>0,(e=`0`)=>Number.parseInt(e,10),(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 S(e){return e==null||e instanceof Date?e:typeof e==`number`?new Date(e):typeof e==`string`?C(e):_}function C(e){let t=v.exec(e);if(t===null)return _;let[,n=0,r=0,i=0,a=0,o=0,s=0,c=0]=x.map((e,n)=>e(t[n])),l=t[8];return l?new Date(Date.UTC(n,r,i,a,o+b(l),s,c)):new Date(n,r,i,a,o,s,c)}function w(e,t){let n=t.parser(e);return t.default!==void 0&&(n==null||Number.isNaN(n.getTime()))&&(n=t.default),n}function T(e,t,n){let r=S(e);return p(e=>r!=null&&(e==null||e instanceof Date&&(n?e>r:e>=r)),t,()=>({min:r,limit:r,exclusive:n}))}function E(e,t,n){let r=S(e);return p(e=>r!=null&&(e==null||e instanceof Date&&(n?e<r:e<=r)),t,()=>({max:r,limit:r,exclusive:n}))}const D=f({parser:S},w);function O(e){return e==null||typeof e==`number`?e:e instanceof Date?e.getTime():typeof e==`string`?Number.parseFloat(e):Number.parseFloat(String(e))}function k(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 A(e){return async(t,n)=>t==null||typeof t==`number`&&Number.isFinite(t)&&Math.floor(t)===t?a({value:t,field:n}):o({message:e,value:t,field:n})}const j=f({parser:O},k);function M(e){if(typeof e!=`object`||!e)throw TypeError("`properties` must be a configuration object");return async(n,r)=>{let i={},s={},c=!0;for(let r in e)if(t(e,r)){let t=e[r],a=Reflect.get(Object(n),r),l=await(typeof t==`function`?t(a,r):o({field:r,message:`No validator set`,value:a}));s[r]=l.value,l.state===`invalid`&&(c=!1,i[r]=l.message||l.errors)}return c?a({value:s,field:r}):o({message:``,value:n,field:r,errors:i})}}function N(e,t){return p(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 P(e,t){let n=e;return p(t=>t==null||n.includes(t)||t instanceof Date&&e.some(e=>e.valueOf()===t.valueOf()),t,()=>({values:e}))}function F(e){return typeof e==`string`||Array.isArray(e)?e.length:e}function I(e,t,n){return p(t=>{let r=F(t);return r==null||t===``||(n?r<e:r<=e)},t,t=>({max:e,limit:e,amount:F(t),exclusive:n}))}function L(e,t,n){return p(t=>{let r=F(t);return r==null||t===``||(n?r>e:r>=e)},t,t=>({min:e,limit:e,amount:F(t),exclusive:n}))}function R(e,t){return p(t=>{let n=F(t);return n==null||t===``||n===e},t,t=>({amount:F(t),limit:e,exact:e}))}const z=/^[\w!#$%&'*+./=?^`{|}~-]+@[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?)*$/,B=/^(?:[\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 V(t){if(t==null||typeof t==`string`)return t;if(t instanceof Date)return t.toISOString();if(e(t))throw TypeError(`Failed to parse value to string.`);return String(t)}function H(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 U(e,t){let n=new RegExp(e);return p(e=>(n.lastIndex=0,e==null||e===``||typeof e==`string`&&n.test(e)),t,()=>({pattern:e}))}function W(e){return U(z,e)}function G(e){return U(B,e)}function K(e,t){return p(e=>{try{let n;if(e==null||e===``||typeof e==`string`&&(n=new URL(e)))return!(n&&t&&!t.includes(n.protocol))}catch{}return!1},e,()=>({protocols:t}))}const q=f({parser:V},H);export{c as applyArrayConfig,h as applyBooleanConfig,w as applyDateConfig,k as applyNumberConfig,H as applyStringConfig,d as array,g as boolean,f as createTypeValidatorTest,p as createValidatorTest,D as date,W as email,R as exact,i as formatMessage,t as hasOwnProperty,A as integer,o as invalid,_ as invalidDate,e as isObject,U as matches,I as max,E as maxDate,L as min,T as minDate,j as number,M as object,P as oneOf,s as parseArray,m as parseBoolean,S as parseDate,O as parseNumber,V as parseString,N as required,q as string,K as url,G as uuid,a as valid};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|