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/dist/index.d.ts CHANGED
@@ -1,51 +1,52 @@
1
+ //#region src/shared.d.ts
1
2
  type ValidResult<T> = {
2
- /**
3
- * True if the original value is valid according to all validation tests
4
- * @deprecated Use `state` instead.
5
- */
6
- isValid: true;
7
- /** This is preferred to use over `isValid` as it works better for type guards. */
8
- state: 'valid';
9
- /** The parsed value. Note that may be different than the original value. */
10
- value: T | null | undefined;
11
- /** Field name if provided. Automatically provided if using object validation. */
12
- field?: string | undefined;
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
- * True if the original value is valid according to all validation tests.
17
- * @deprecated Use `state` instead.
18
- */
19
- isValid: false;
20
- /** This is preferred to use over `isValid` as it works better for type guards. */
21
- state: 'invalid';
22
- /** The parsed value. Note that may be different than the original value. */
23
- value: unknown;
24
- /** Error message if `state` is invalid. */
25
- message: string;
26
- /** Field name if provided. Automatically provided if using object validation. */
27
- field?: string | undefined;
28
- /** Extra error details */
29
- errors?: E | undefined;
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 = any, E = any> = ValidResult<T> | InvalidResult<E>;
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 = any, E = any> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
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<any, infer E> ? E : unknown;
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
- /** Parser to convert value into the designated type. */
76
- parser: (value: unknown) => T | null | undefined;
77
- /** Provide a fallback value in case the original value is undefined. */
78
- default?: T;
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
- value: T | null | undefined;
82
- field: string | undefined;
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
- value: T | null | undefined;
117
- field: string | undefined;
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
- message: ValidatorMessage<T, P>;
131
- value: unknown;
132
- field: string | undefined;
133
- errors?: E | undefined;
134
- extras?: Omit<P, 'field' | 'value'> | undefined;
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
- min: Date | null | undefined;
201
- limit: Date | null | undefined;
202
- exclusive?: boolean | undefined;
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, null>;
213
+ export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, never>;
212
214
  interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
213
- max: Date | null | undefined;
214
- limit: Date | null | undefined;
215
- exclusive?: boolean | undefined;
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
- /** Apply rounding mechanism before validation. */
261
- round?: 'nearest' | 'floor' | 'ceil';
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
- type ObjectParameter = Record<string, ValidatorTest<unknown, unknown>>;
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
- [K in keyof P]?: ExtractValue<P[K]>;
285
- }, E extends {
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
- values: readonly T[];
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 = any, E = any, 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>;
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
- max: number;
327
- amount: number | null | undefined;
328
- limit: number;
329
- exclusive?: boolean | undefined;
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
- min: number;
341
- limit: number;
342
- amount: number | null | undefined;
343
- exclusive?: boolean | undefined;
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
- limit: number;
355
- amount: number | null | undefined;
356
- exact: number;
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
- /** Apply trim to string before validation. */
372
- trim?: boolean;
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
- pattern: RegExp;
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 { type ArrayConfig, type ArrayItemValidatorResult, type BooleanConfig, type ConfigBase, type DateConfig, type DeepPartial, type ExactValidatorMessageParameters, type ExtractError, type ExtractValue, type InvalidResult, type MatchesValidatorMessageParameters, type MaxDateValidatorMessageParameters, type MaxValidatorMessageParameters, type MinDateValidatorMessageParameters, type MinValidatorMessageParameters, type NumberConfig, type ObjectParameter as ObjectParam, type OneOfValidatorMessageParameters, type SharedDateValueType, type SharedValueType, type StringConfig, type ValidResult, type ValidatorFactory, type ValidatorMessage, type ValidatorMessageParameters, type ValidatorResult, type ValidatorTest, applyArrayConfig, applyBooleanConfig, applyDateConfig, applyNumberConfig, applyStringConfig, array, boolean, createTypeValidatorTest, createValidatorTest, date, email, exact, formatMessage, hasOwnProperty, integer, invalid, invalidDate, isObject, matches, max, maxDate, min, minDate, number, object, oneOf, parseArray, parseBoolean, parseDate, parseNumber, parseString, required, string, url, uuid, valid };
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 T(e){return Object.prototype.toString.call(e)==="[object Object]"}function x(e,r){return Object.prototype.hasOwnProperty.call(e,r)}var I=/{(\w+)}/g;function M(e,r){return typeof e=="string"?e.replaceAll(I,(t,a)=>String(r[a])):e(r)}async function m({value:e,field:r}){return{isValid:!0,state:"valid",value:e,field:r}}async function p({errors:e,field:r,message:t,value:a,extras:n}){return{isValid:!1,state:"invalid",message:M(t,{...n,value:a,field:r}),value:a,field:r,errors:e}}function D(e){if(Array.isArray(e)||e==null)return e;let r=e;return r[Symbol.iterator]?[...r]:[]}function w(e,r){let t=e.parser(r);return e.default!==void 0&&(t==null||t.length===0)&&(t=e.default),t}async function R(e,r,t){for(let a of e){let n=await a(r,t);if(n.state==="invalid")return n}}async function B(e,r,t){let a=!0,n=[];if(!r||!e)return{isValid:a,errors:n};for(let[s,i]of r.entries()){let o=await e(i,t?`${t}[${s}]`:`[${s}]`);o.state==="invalid"?(a=!1,n.push(o.message||o.errors)):n.push(null)}return{isValid:a,errors:n}}function j(e,...r){let t=r,a={parser:D};return e!==void 0&&(typeof e=="function"?t=[e,...r]:a={...a,...e}),async(n,s)=>{let i=w(a,n),[o,...d]=t,f=await R(d,i,s),u=await B(o,i,s);return!f&&u.isValid?m({value:i,field:s}):p({message:f?f.message:"",value:n,field:s,errors:u.errors})}}function g(e,r){return(t,...a)=>{let n=e,s=a;return t!==void 0&&(typeof t=="function"?s=[t,...a]:n={...e,...t}),async(i,o)=>{try{let d=r(i,n);for(let f of s){let u=await f(d,o);if(u.state==="invalid")return u}return await m({value:d,field:o})}catch(d){return p({message:d instanceof Error?d.message:String(d),value:i,field:o})}}}}function l(e,r,t){return async(a,n)=>e(a,n)?m({value:a,field:n}):p({message:r,value:a,field:n,extras:t?t(a):void 0})}function C(e){return e==null||typeof e=="boolean"?e:!!e}function h(e,r){let t=r.parser(e);return r.default!==void 0&&t==null&&(t=r.default),t}var F=g({parser:C},h);var b=new Date(""),$=/^(\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)?)?)?)?)?$/,z=/^([+-])([0-2]\d):?([0-5]\d)$/;function Z(e){let r=z.exec(e);if(e==="Z"||!r)return 0;let[,t,a="0",n="0"]=r,s=t==="-"?-1:1,o=Number.parseInt(a,10)*60+Number.parseInt(n,10);return s*o}var V=10,K=[()=>0,(e="0")=>Number.parseInt(e,V),(e="1")=>Number.parseInt(e,V)-1,(e="1")=>Number.parseInt(e,V),(e="0")=>Number.parseInt(e,V),(e="0")=>Number.parseInt(e,V),(e="0")=>Number.parseInt(e,V),(e="0")=>Number.parseFloat(e)*1e3];function y(e){return e==null||e instanceof Date?e:typeof e=="number"?new Date(e):typeof e=="string"?U(e):b}function U(e){let r=$.exec(e);if(r===null)return b;let[,t=0,a=0,n=0,s=0,i=0,o=0,d=0]=K.map((u,O)=>u(r[O])),f=r[8];return f?new Date(Date.UTC(t,a,n,s,i+Z(f),o,d)):new Date(t,a,n,s,i,o,d)}function E(e,r){let t=r.parser(e);return r.default!==void 0&&(t==null||Number.isNaN(t.getTime()))&&(t=r.default),t}function q(e,r,t){let a=y(e);return l(n=>a!=null&&(n==null||n instanceof Date&&(t?n>a:n>=a)),r,()=>({min:a,limit:a,exclusive:t}))}function L(e,r,t){let a=y(e);return l(n=>a!=null&&(n==null||n instanceof Date&&(t?n<a:n<=a)),r,()=>({max:a,limit:a,exclusive:t}))}var _=g({parser:y},E);function k(e){return e==null||typeof e=="number"?e:e instanceof Date?e.getTime():typeof e=="string"?Number.parseFloat(e):Number.parseFloat(String(e))}function S(e,r){let t=r.parser(e);if(r.default!==void 0&&(t==null||Number.isNaN(t))&&(t=r.default),r.round&&typeof t=="number")switch(r.round){case"nearest":{t=Math.round(t);break}case"ceil":{t=Math.ceil(t);break}case"floor":{t=Math.floor(t);break}default:throw new Error(`Unknown round type: ${String(r.round)}`)}return t}function G(e){return async(r,t)=>r==null||typeof r=="number"&&Number.isFinite(r)&&Math.floor(r)===r?m({value:r,field:t}):p({message:e,value:r,field:t})}var H=g({parser:k},S);function J(e){if(typeof e!="object"||e===null)throw new TypeError("`properties` must be a configuration object");return async(r,t)=>{let a=r??{},n={},s={},i=!0;for(let o in e)if(x(e,o)){let d=e[o],f=a[o],u=typeof d=="function"?await d(f,o):await p({field:o,message:"No validator set",value:f});s[o]=u.value,u.state==="invalid"&&(i=!1,n[o]=u.message||u.errors)}return i?m({value:s,field:t}):p({message:"",value:r,field:t,errors:n})}}function Q(e,r){return l(t=>r&&t===null||t!=null&&!(typeof t=="string"&&t==="")&&!(typeof t=="number"&&Number.isNaN(t))&&!(t instanceof Date&&Number.isNaN(t.valueOf())),e)}function W(e,r){return l(t=>t==null||e.includes(t)||t instanceof Date&&e.some(a=>a.valueOf()===t.valueOf()),r,()=>({values:e}))}function c(e){return typeof e=="string"||Array.isArray(e)?e.length:e}function X(e,r,t){return l(a=>{let n=c(a);return n==null||a===""||(t?n<e:n<=e)},r,a=>({max:e,limit:e,amount:c(a),exclusive:t}))}function Y(e,r,t){return l(a=>{let n=c(a);return n==null||a===""||(t?n>e:n>=e)},r,a=>({min:e,limit:e,amount:c(a),exclusive:t}))}function v(e,r){return l(t=>{let a=c(t);return a==null||t===""||a===e},r,t=>({amount:c(t),limit:e,exact:e}))}var ee=/^[\w!#$%&'*+./=?^`{|}~-]+@[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?)*$/,te=/^(?:[\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 N(e){if(e==null||typeof e=="string")return e;if(e instanceof Date)return e.toISOString();if(T(e))throw new TypeError("Failed to parse value to string.");return String(e)}function A(e,r){let t=r.parser(e);return r.default!==void 0&&(t==null||t==="")&&(t=r.default),r.trim&&typeof t=="string"&&(t=t.trim()),t}function P(e,r){let t=new RegExp(e);return l(a=>(t.lastIndex=0,a==null||a===""||typeof a=="string"&&t.test(a)),r,()=>({pattern:e}))}function re(e){return P(ee,e)}function ae(e){return P(te,e)}function ne(e,r){return l(t=>{try{let a;if(t==null||t===""||typeof t=="string"&&(a=new URL(t)))return!(a&&r&&!r.includes(a.protocol))}catch{}return!1},e,()=>({protocols:r}))}var oe=g({parser:N},A);export{w as applyArrayConfig,h as applyBooleanConfig,E as applyDateConfig,S as applyNumberConfig,A as applyStringConfig,j as array,F as boolean,g as createTypeValidatorTest,l as createValidatorTest,_ as date,re as email,v as exact,M as formatMessage,x as hasOwnProperty,G as integer,p as invalid,b as invalidDate,T as isObject,P as matches,X as max,L as maxDate,Y as min,q as minDate,H as number,J as object,W as oneOf,D as parseArray,C as parseBoolean,y as parseDate,k as parseNumber,N as parseString,Q as required,oe as string,ne as url,ae as uuid,m as valid};
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