validator-fns 2.0.0 → 2.0.2
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.cjs.map +1 -1
- package/dist/index.d.cts +409 -0
- package/dist/index.d.ts +409 -0
- package/dist/index.js.map +1 -1
- package/package.json +36 -17
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
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;
|
|
13
|
+
};
|
|
14
|
+
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;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The result of a validation.
|
|
33
|
+
* @typeParam T The value type
|
|
34
|
+
* @category Types
|
|
35
|
+
*/
|
|
36
|
+
type ValidatorResult<T = any, E = any> = ValidResult<T> | InvalidResult<E>;
|
|
37
|
+
/**
|
|
38
|
+
* Validation test function.
|
|
39
|
+
* @typeParam T The value type
|
|
40
|
+
* @category Types
|
|
41
|
+
*/
|
|
42
|
+
type ValidatorTest<T = any, E = any> = (value?: unknown, field?: string) => Promise<ValidatorResult<T, E>>;
|
|
43
|
+
/**
|
|
44
|
+
* Extracts the generic error type from a ValidatorTest.
|
|
45
|
+
* @typeParam T a ValidatorTest
|
|
46
|
+
* @category Types
|
|
47
|
+
*/
|
|
48
|
+
type ExtractError<T extends ValidatorTest> = T extends ValidatorTest<any, infer E> ? E : unknown;
|
|
49
|
+
/**
|
|
50
|
+
* Extracts the generic value type from a ValidatorTest.
|
|
51
|
+
* @typeParam T a ValidatorTest
|
|
52
|
+
* @category Types
|
|
53
|
+
*/
|
|
54
|
+
type ExtractValue<T extends ValidatorTest> = T extends ValidatorTest<infer V> ? V : unknown;
|
|
55
|
+
/**
|
|
56
|
+
* Make a deep partial type.
|
|
57
|
+
* @typeParam T any object type
|
|
58
|
+
* @category Types
|
|
59
|
+
*/
|
|
60
|
+
type DeepPartial<T> = {
|
|
61
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Validation factory function.
|
|
65
|
+
* @typeParam C The configuration type
|
|
66
|
+
* @category Types
|
|
67
|
+
*/
|
|
68
|
+
type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest, ...tests: ValidatorTest[]) => ValidatorTest<T, E>;
|
|
69
|
+
/**
|
|
70
|
+
* Base configuration type.
|
|
71
|
+
* @typeParam T The target type.
|
|
72
|
+
* @category Types
|
|
73
|
+
*/
|
|
74
|
+
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;
|
|
79
|
+
}
|
|
80
|
+
interface ValidatorMessageParameters<T> {
|
|
81
|
+
value: T | null | undefined;
|
|
82
|
+
field: string | undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Validation message. May be a plain string or a function that accepts arguments.
|
|
86
|
+
* While using a string, values in squiggly brackets `{}` are replaced.
|
|
87
|
+
* @category Types
|
|
88
|
+
*/
|
|
89
|
+
type ValidatorMessage<T, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>> = string | ((parameters: P) => string);
|
|
90
|
+
/**
|
|
91
|
+
* Simple check for objects.
|
|
92
|
+
* @param value Value to test
|
|
93
|
+
*/
|
|
94
|
+
declare function isObject(value: unknown): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Safe hasOwnProperty check
|
|
97
|
+
* @param object Object to check
|
|
98
|
+
* @param property Property to look for
|
|
99
|
+
*/
|
|
100
|
+
declare function hasOwnProperty(object: unknown, property: string | number | symbol): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Formats a message using a squiggly bracket `{}` template.
|
|
103
|
+
* @param template Message template as string or function
|
|
104
|
+
* @param parameters Params to inject into template
|
|
105
|
+
* @category Helpers
|
|
106
|
+
*/
|
|
107
|
+
declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
|
|
108
|
+
/**
|
|
109
|
+
* Creates a valid result
|
|
110
|
+
* @param value Parsed value
|
|
111
|
+
* @param field Field name
|
|
112
|
+
* @category Helpers
|
|
113
|
+
*/
|
|
114
|
+
declare function valid<T, E = never>({ value, field, }: {
|
|
115
|
+
value: T | null | undefined;
|
|
116
|
+
field: string | undefined;
|
|
117
|
+
}): Promise<ValidatorResult<T, E>>;
|
|
118
|
+
/**
|
|
119
|
+
* Creates an invalid result.
|
|
120
|
+
* @param message Error message
|
|
121
|
+
* @param value Parsed value
|
|
122
|
+
* @param field Field name
|
|
123
|
+
* @param extras Extra message params
|
|
124
|
+
* @category Helpers
|
|
125
|
+
*/
|
|
126
|
+
declare function invalid<T, E, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>({ errors, field, message, value, extras, }: {
|
|
127
|
+
message: ValidatorMessage<T, P>;
|
|
128
|
+
value: unknown;
|
|
129
|
+
field: string | undefined;
|
|
130
|
+
errors?: E | undefined;
|
|
131
|
+
extras?: Omit<P, 'field' | 'value'> | undefined;
|
|
132
|
+
}): Promise<ValidatorResult<T, E>>;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a type validator.
|
|
135
|
+
* @typeParam T The basic type for the final value to be validated.
|
|
136
|
+
* @typeParam C The configuration type.
|
|
137
|
+
* @param defaultParser Parser to convert value into type.
|
|
138
|
+
* @param applyConfig Function that applies configuration to value.
|
|
139
|
+
* @category Helpers
|
|
140
|
+
*/
|
|
141
|
+
declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
142
|
+
/**
|
|
143
|
+
* Captures basic validator test pattern
|
|
144
|
+
* @param test Test function
|
|
145
|
+
* @param message Error message
|
|
146
|
+
* @param getExtras Optional extra fields for error message
|
|
147
|
+
*/
|
|
148
|
+
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>;
|
|
149
|
+
/**
|
|
150
|
+
* Ensures a value is not undefined, null, empty string, NaN, nor invalid date.
|
|
151
|
+
* @param message Error message
|
|
152
|
+
* @param nullable Allow null values
|
|
153
|
+
* @category Validation Tests
|
|
154
|
+
*/
|
|
155
|
+
declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
156
|
+
type SharedValueType = string | number | unknown[];
|
|
157
|
+
interface MaxValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
158
|
+
max: number;
|
|
159
|
+
amount: number | null | undefined;
|
|
160
|
+
limit: number;
|
|
161
|
+
exclusive?: boolean | undefined;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Ensures a string, number, or array value is at most a certain amount.
|
|
165
|
+
* @param limit Size or value limit
|
|
166
|
+
* @param message Error message
|
|
167
|
+
* @param exclusive Use exclusive comparison instead of inclusive
|
|
168
|
+
* @category Validation Tests
|
|
169
|
+
*/
|
|
170
|
+
declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
171
|
+
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
172
|
+
min: number;
|
|
173
|
+
limit: number;
|
|
174
|
+
amount: number | null | undefined;
|
|
175
|
+
exclusive?: boolean | undefined;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Ensures a string, number, or array value is at most a certain amount.
|
|
179
|
+
* @param limit Size or value limit
|
|
180
|
+
* @param message Error message
|
|
181
|
+
* @param exclusive Use exclusive comparison instead of inclusive
|
|
182
|
+
* @category Validation Tests
|
|
183
|
+
*/
|
|
184
|
+
declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
185
|
+
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
186
|
+
limit: number;
|
|
187
|
+
amount: number | null | undefined;
|
|
188
|
+
exact: number;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Ensures a string, number, or array value is exactly a certain amount.
|
|
192
|
+
* @param limit Size or value limit
|
|
193
|
+
* @param message Error message
|
|
194
|
+
* @category Validation Tests
|
|
195
|
+
*/
|
|
196
|
+
declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
197
|
+
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
198
|
+
values: readonly T[];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Ensures a value is one in a predefined list.
|
|
202
|
+
* @param values List of allowed values.
|
|
203
|
+
* @param message Error message
|
|
204
|
+
* @category Validation Tests
|
|
205
|
+
*/
|
|
206
|
+
declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Invalid validation result for an item in the array
|
|
210
|
+
* @category Types
|
|
211
|
+
*/
|
|
212
|
+
type ArrayItemValidatorResult<E = unknown> = null | string | E;
|
|
213
|
+
/**
|
|
214
|
+
* Configuration for array validation.
|
|
215
|
+
* @category Types
|
|
216
|
+
*/
|
|
217
|
+
type ArrayConfig = ConfigBase<unknown[]>;
|
|
218
|
+
/**
|
|
219
|
+
* Parses a value into an array.
|
|
220
|
+
* @param value Value to parse
|
|
221
|
+
* @category Parsers
|
|
222
|
+
*/
|
|
223
|
+
declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
224
|
+
/**
|
|
225
|
+
* Applies a configuration to the array value.
|
|
226
|
+
* @param value Value to modify
|
|
227
|
+
* @param config Configuration to apply
|
|
228
|
+
* @category Helpers
|
|
229
|
+
*/
|
|
230
|
+
declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
231
|
+
/**
|
|
232
|
+
* Validates an array. The first passed validator is used to validate individual
|
|
233
|
+
* items. The remaining validators target the array itself.
|
|
234
|
+
* @category Type Validators
|
|
235
|
+
*/
|
|
236
|
+
declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]>;
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Parses a value into a boolean.
|
|
240
|
+
* @param value Value to parse
|
|
241
|
+
* @category Parsers
|
|
242
|
+
*/
|
|
243
|
+
declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
244
|
+
/**
|
|
245
|
+
* Applies a configuration to the boolean value.
|
|
246
|
+
* @param value Value to modify
|
|
247
|
+
* @param config Configuration to apply
|
|
248
|
+
* @category Helpers
|
|
249
|
+
*/
|
|
250
|
+
declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
251
|
+
/**
|
|
252
|
+
* Configuration for boolean validation.
|
|
253
|
+
* @category Types
|
|
254
|
+
*/
|
|
255
|
+
type BooleanConfig = ConfigBase<boolean>;
|
|
256
|
+
/**
|
|
257
|
+
* Validates a boolean value.
|
|
258
|
+
* @category Type Validators
|
|
259
|
+
*/
|
|
260
|
+
declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
261
|
+
|
|
262
|
+
/** Badly formatted date for internal use. */
|
|
263
|
+
declare const invalidDate: Date;
|
|
264
|
+
/**
|
|
265
|
+
* Parses a value into a date.
|
|
266
|
+
* @param value Value to parse
|
|
267
|
+
* @category Parsers
|
|
268
|
+
*/
|
|
269
|
+
declare function parseDate(value: unknown): Date | null | undefined;
|
|
270
|
+
/**
|
|
271
|
+
* Applies a configuration to the date value.
|
|
272
|
+
* @param value Value to modify
|
|
273
|
+
* @param config Configuration to apply
|
|
274
|
+
* @category Helpers
|
|
275
|
+
*/
|
|
276
|
+
declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
277
|
+
type SharedDateValueType = Date | string | number;
|
|
278
|
+
interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
279
|
+
min: Date | null | undefined;
|
|
280
|
+
limit: Date | null | undefined;
|
|
281
|
+
exclusive?: boolean | undefined;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Ensures a date value is on or after a date.
|
|
285
|
+
* @param limit Minimum date
|
|
286
|
+
* @param message Error message
|
|
287
|
+
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
288
|
+
* @category Validation Tests
|
|
289
|
+
*/
|
|
290
|
+
declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, null>;
|
|
291
|
+
interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
292
|
+
max: Date | null | undefined;
|
|
293
|
+
limit: Date | null | undefined;
|
|
294
|
+
exclusive?: boolean | undefined;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Ensures a date value is on or before a date.
|
|
298
|
+
* @param limit Maximum date
|
|
299
|
+
* @param message Error message
|
|
300
|
+
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
301
|
+
* @category Validation Tests
|
|
302
|
+
*/
|
|
303
|
+
declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date>;
|
|
304
|
+
/**
|
|
305
|
+
* Configuration for date validation.
|
|
306
|
+
* @category Types
|
|
307
|
+
*/
|
|
308
|
+
type DateConfig = ConfigBase<Date>;
|
|
309
|
+
/**
|
|
310
|
+
* Validates a date value.
|
|
311
|
+
* @category Type Validators
|
|
312
|
+
*/
|
|
313
|
+
declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Parses a value into a number.
|
|
317
|
+
* @param value Value to parse
|
|
318
|
+
* @category Parsers
|
|
319
|
+
*/
|
|
320
|
+
declare function parseNumber(value: unknown): number | null | undefined;
|
|
321
|
+
/**
|
|
322
|
+
* Applies a configuration to the number value.
|
|
323
|
+
* @param value Value to modify
|
|
324
|
+
* @param config Configuration to apply
|
|
325
|
+
* @category Helpers
|
|
326
|
+
*/
|
|
327
|
+
declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
328
|
+
/**
|
|
329
|
+
* Ensures a number value is an integer.
|
|
330
|
+
* @param message Error message
|
|
331
|
+
* @category Validation Tests
|
|
332
|
+
*/
|
|
333
|
+
declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
334
|
+
/**
|
|
335
|
+
* Configuration for number validation.
|
|
336
|
+
* @category Types
|
|
337
|
+
*/
|
|
338
|
+
interface NumberConfig extends ConfigBase<number> {
|
|
339
|
+
/** Apply rounding mechanism before validation. */
|
|
340
|
+
round?: 'nearest' | 'floor' | 'ceil';
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Validates a number value.
|
|
344
|
+
* @category Type Validators
|
|
345
|
+
*/
|
|
346
|
+
declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
347
|
+
|
|
348
|
+
type ObjectParameter = Record<string, ValidatorTest<unknown, unknown>>;
|
|
349
|
+
/**
|
|
350
|
+
* Validates an object.
|
|
351
|
+
* @category Type Validators
|
|
352
|
+
*/
|
|
353
|
+
declare function object<P extends ObjectParameter, V extends {
|
|
354
|
+
[K in keyof P]?: ExtractValue<P[K]>;
|
|
355
|
+
}, E extends {
|
|
356
|
+
[K in keyof P]?: ExtractError<P[K]>;
|
|
357
|
+
}>(properties: P): ValidatorTest<V, E>;
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Configuration for string validation.
|
|
361
|
+
* @category Types
|
|
362
|
+
*/
|
|
363
|
+
interface StringConfig extends ConfigBase<string> {
|
|
364
|
+
/** Apply trim to string before validation. */
|
|
365
|
+
trim?: boolean;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Parses a value into a string.
|
|
369
|
+
* @param value Value to parse
|
|
370
|
+
* @category Parsers
|
|
371
|
+
*/
|
|
372
|
+
declare function parseString(value: unknown): string | null | undefined;
|
|
373
|
+
/**
|
|
374
|
+
* Applies a configuration to the string value.
|
|
375
|
+
* @param value Value to modify
|
|
376
|
+
* @param config Configuration to apply
|
|
377
|
+
* @category Helpers
|
|
378
|
+
*/
|
|
379
|
+
declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
380
|
+
interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<string> {
|
|
381
|
+
pattern: RegExp;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Ensures a string value matches a pattern
|
|
385
|
+
* @param pattern Pattern to match
|
|
386
|
+
* @param message Error message
|
|
387
|
+
* @category Validation Tests
|
|
388
|
+
*/
|
|
389
|
+
declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
390
|
+
/**
|
|
391
|
+
* Ensures a string value matches an email pattern
|
|
392
|
+
* @param message Error message
|
|
393
|
+
* @category Validation Tests
|
|
394
|
+
*/
|
|
395
|
+
declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
396
|
+
/**
|
|
397
|
+
* Ensures a string value is a valid URL
|
|
398
|
+
* @param message Error message
|
|
399
|
+
* @param protocols Optional list of protocols to allow
|
|
400
|
+
* @category Validation Tests
|
|
401
|
+
*/
|
|
402
|
+
declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
403
|
+
/**
|
|
404
|
+
* Validates a string value.
|
|
405
|
+
* @category Type Validators
|
|
406
|
+
*/
|
|
407
|
+
declare const string: ValidatorFactory<StringConfig, string, string>;
|
|
408
|
+
|
|
409
|
+
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, valid };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/shared.ts","../src/arrays.ts","../src/booleans.ts","../src/dates.ts","../src/numbers.ts","../src/objects.ts","../src/strings.ts"],"sourcesContent":["export type ValidResult<T> = {\n /**\n * True if the original value is valid according to all validation tests\n * @deprecated Use `state` instead.\n */\n isValid: true;\n /** This is preferred to use over `isValid` as it works better for type guards. */\n state: 'valid';\n /** The parsed value. Note that may be different than the original value. */\n value: T | null | undefined;\n /** Field name if provided. Automatically provided if using object validation. */\n field?: string | undefined;\n};\n\nexport type InvalidResult<E> = {\n /**\n * True if the original value is valid according to all validation tests.\n * @deprecated Use `state` instead.\n */\n isValid: false;\n /** This is preferred to use over `isValid` as it works better for type guards. */\n state: 'invalid';\n /** The parsed value. Note that may be different than the original value. */\n value: unknown;\n /** Error message if `state` is invalid. */\n message: string;\n /** Field name if provided. Automatically provided if using object validation. */\n field?: string | undefined;\n /** Extra error details */\n errors?: E | undefined;\n};\n\n/**\n * The result of a validation.\n * @typeParam T The value type\n * @category Types\n */\nexport type ValidatorResult<T = any, E = any> =\n | ValidResult<T>\n | InvalidResult<E>;\n\n/**\n * Validation test function.\n * @typeParam T The value type\n * @category Types\n */\nexport type ValidatorTest<T = any, E = any> = (\n value?: unknown,\n field?: string,\n) => Promise<ValidatorResult<T, E>>;\n\n/**\n * Extracts the generic error type from a ValidatorTest.\n * @typeParam T a ValidatorTest\n * @category Types\n */\nexport type ExtractError<T extends ValidatorTest> =\n T extends ValidatorTest<any, infer E> ? E : unknown;\n\n/**\n * Extracts the generic value type from a ValidatorTest.\n * @typeParam T a ValidatorTest\n * @category Types\n */\nexport type ExtractValue<T extends ValidatorTest> =\n T extends ValidatorTest<infer V> ? V : unknown;\n\n/**\n * Make a deep partial type.\n * @typeParam T any object type\n * @category Types\n */\nexport type DeepPartial<T> = {\n [P in keyof T]?: DeepPartial<T[P]>;\n};\n\n/**\n * Validation factory function.\n * @typeParam C The configuration type\n * @category Types\n */\nexport type ValidatorFactory<C, T, E> = (\n config?: Partial<C> | ValidatorTest,\n ...tests: ValidatorTest[]\n) => ValidatorTest<T, E>;\n\n/**\n * Base configuration type.\n * @typeParam T The target type.\n * @category Types\n */\nexport interface ConfigBase<T> {\n /** Parser to convert value into the designated type. */\n parser: (value: unknown) => T | null | undefined;\n /** Provide a fallback value in case the original value is undefined. */\n default?: T;\n}\n\nexport interface ValidatorMessageParameters<T> {\n value: T | null | undefined;\n field: string | undefined;\n}\n\n/**\n * Validation message. May be a plain string or a function that accepts arguments.\n * While using a string, values in squiggly brackets `{}` are replaced.\n * @category Types\n */\nexport type ValidatorMessage<\n T,\n P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>,\n> = string | ((parameters: P) => string);\n\n/**\n * Simple check for objects.\n * @param value Value to test\n */\nexport function isObject(value: unknown): boolean {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\n/**\n * Safe hasOwnProperty check\n * @param object Object to check\n * @param property Property to look for\n */\nexport function hasOwnProperty(\n object: unknown,\n property: string | number | symbol,\n): boolean {\n return Object.prototype.hasOwnProperty.call(object, property);\n}\n\n/** @internal */\nconst formatPattern = /{(\\w+)}/g;\n\n/**\n * Formats a message using a squiggly bracket `{}` template.\n * @param template Message template as string or function\n * @param parameters Params to inject into template\n * @category Helpers\n */\nexport function formatMessage<T, P extends ValidatorMessageParameters<T>>(\n template: ValidatorMessage<T, P>,\n parameters: P,\n): string {\n if (typeof template === 'string') {\n return template.replaceAll(formatPattern, (_, key: string) =>\n String((parameters as Record<string, unknown>)[key]),\n );\n }\n\n return template(parameters);\n}\n\n/**\n * Creates a valid result\n * @param value Parsed value\n * @param field Field name\n * @category Helpers\n */\nexport async function valid<T, E = never>({\n value,\n field,\n}: {\n value: T | null | undefined;\n field: string | undefined;\n}): Promise<ValidatorResult<T, E>> {\n return {\n isValid: true,\n state: 'valid',\n value,\n field,\n };\n}\n\n/**\n * Creates an invalid result.\n * @param message Error message\n * @param value Parsed value\n * @param field Field name\n * @param extras Extra message params\n * @category Helpers\n */\nexport async function invalid<\n T,\n E,\n P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>,\n>({\n errors,\n field,\n message,\n value,\n extras,\n}: {\n message: ValidatorMessage<T, P>;\n value: unknown;\n field: string | undefined;\n errors?: E | undefined;\n extras?: Omit<P, 'field' | 'value'> | undefined;\n}): Promise<ValidatorResult<T, E>> {\n return {\n isValid: false,\n state: 'invalid',\n message: formatMessage(message, { ...extras, value, field } as P),\n value,\n field,\n errors,\n };\n}\n\n/**\n * Creates a type validator.\n * @typeParam T The basic type for the final value to be validated.\n * @typeParam C The configuration type.\n * @param defaultParser Parser to convert value into type.\n * @param applyConfig Function that applies configuration to value.\n * @category Helpers\n */\nexport function createTypeValidatorTest<T, C extends ConfigBase<T>>(\n defaultConfig: C,\n applyConfig: (value: unknown, config: C) => T | null | undefined,\n): ValidatorFactory<C, T, string> {\n return (config, ...tests) => {\n const cache: Record<string, ValidatorResult<T>> = {};\n let finalConfig = defaultConfig;\n let allTests = tests;\n\n if (config !== undefined) {\n if (typeof config === 'function') {\n allTests = [config, ...tests];\n } else {\n finalConfig = {\n ...defaultConfig,\n ...config,\n };\n }\n }\n\n return async (value, field) => {\n try {\n const parsedValue = applyConfig(value, finalConfig);\n const parsedValueString = String(parsedValue);\n if (hasOwnProperty(cache, parsedValueString)) {\n return cache[parsedValueString] as ValidatorResult<T, any>;\n }\n\n for (const validatorTest of allTests) {\n const result = await validatorTest(parsedValue, field);\n if (result.state === 'invalid') {\n cache[parsedValueString] = result;\n return result;\n }\n }\n\n cache[parsedValueString] = await valid({ value: parsedValue, field });\n return cache[parsedValueString] as ValidatorResult<T, any>;\n } catch (error) {\n return invalid({\n message: error instanceof Error ? error.message : String(error),\n value,\n field,\n });\n }\n };\n };\n}\n\n/**\n * Captures basic validator test pattern\n * @param test Test function\n * @param message Error message\n * @param getExtras Optional extra fields for error message\n */\nexport function createValidatorTest<\n T = any,\n E = any,\n P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>,\n>(\n test: (value: unknown, field?: string) => boolean,\n message: ValidatorMessage<T, P>,\n getExtras?: (value: T | null | undefined) => Omit<P, 'value' | 'field'>,\n): ValidatorTest<T, E> {\n return async (value, field) => {\n if (test(value, field)) {\n return valid({ value: value as T, field });\n }\n\n return invalid({\n message,\n value,\n field,\n extras: getExtras ? getExtras(value as T) : undefined,\n });\n };\n}\n\n/**\n * Ensures a value is not undefined, null, empty string, NaN, nor invalid date.\n * @param message Error message\n * @param nullable Allow null values\n * @category Validation Tests\n */\nexport function required<T, P extends ValidatorMessageParameters<T>>(\n message: ValidatorMessage<T, P>,\n nullable?: boolean,\n): ValidatorTest<T> {\n return createValidatorTest(\n (value) =>\n (nullable && value === null) ||\n (value !== null &&\n value !== undefined &&\n !(typeof value === 'string' && value === '') &&\n !(typeof value === 'number' && Number.isNaN(value)) &&\n !(value instanceof Date && Number.isNaN(value.valueOf()))),\n message,\n );\n}\n\nexport type SharedValueType = string | number | unknown[];\n\n/**\n * Extracts a numeric amount from value, i.e. string length, array length, or number.\n * @internal\n */\nfunction getAmount(value: SharedValueType | null | undefined) {\n return typeof value === 'string' || Array.isArray(value)\n ? value.length\n : value;\n}\n\nexport interface MaxValidatorMessageParameters\n extends ValidatorMessageParameters<SharedValueType> {\n max: number;\n amount: number | null | undefined;\n limit: number;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a string, number, or array value is at most a certain amount.\n * @param limit Size or value limit\n * @param message Error message\n * @param exclusive Use exclusive comparison instead of inclusive\n * @category Validation Tests\n */\nexport function max(\n limit: number,\n message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<SharedValueType> {\n return createValidatorTest(\n (value) => {\n const amount = getAmount(value as SharedValueType);\n return (\n amount === undefined ||\n amount === null ||\n value === '' ||\n (exclusive ? amount < limit : amount <= limit)\n );\n },\n message,\n (value) => ({\n max: limit,\n limit,\n amount: getAmount(value),\n exclusive,\n }),\n );\n}\n\nexport interface MinValidatorMessageParameters\n extends ValidatorMessageParameters<SharedValueType> {\n min: number;\n limit: number;\n amount: number | null | undefined;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a string, number, or array value is at most a certain amount.\n * @param limit Size or value limit\n * @param message Error message\n * @param exclusive Use exclusive comparison instead of inclusive\n * @category Validation Tests\n */\nexport function min(\n limit: number,\n message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<SharedValueType> {\n return createValidatorTest(\n (value) => {\n const amount = getAmount(value as SharedValueType);\n return (\n amount === undefined ||\n amount === null ||\n value === '' ||\n (exclusive ? amount > limit : amount >= limit)\n );\n },\n message,\n (value) => ({\n min: limit,\n limit,\n amount: getAmount(value),\n exclusive,\n }),\n );\n}\n\nexport interface ExactValidatorMessageParameters\n extends ValidatorMessageParameters<SharedValueType> {\n limit: number;\n amount: number | null | undefined;\n exact: number;\n}\n\n/**\n * Ensures a string, number, or array value is exactly a certain amount.\n * @param limit Size or value limit\n * @param message Error message\n * @category Validation Tests\n */\nexport function exact(\n limit: number,\n message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>,\n): ValidatorTest<SharedValueType> {\n return createValidatorTest(\n (value) => {\n const amount = getAmount(value as SharedValueType);\n return (\n amount === undefined ||\n amount === null ||\n value === '' ||\n amount === limit\n );\n },\n message,\n (value) => ({\n amount: getAmount(value),\n limit,\n exact: limit,\n }),\n );\n}\n\nexport interface OneOfValidatorMessageParameters<T>\n extends ValidatorMessageParameters<T> {\n values: readonly T[];\n}\n\n/**\n * Ensures a value is one in a predefined list.\n * @param values List of allowed values.\n * @param message Error message\n * @category Validation Tests\n */\nexport function oneOf<T extends string | number | boolean | Date>(\n values: readonly T[],\n message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>,\n): ValidatorTest<T> {\n return createValidatorTest(\n (value) =>\n value === undefined ||\n value === null ||\n values.includes(value as T) ||\n (value instanceof Date &&\n values.some((x) => x.valueOf() === value.valueOf())),\n message,\n () => ({ values }),\n );\n}\n","import {\n invalid,\n valid,\n type ConfigBase,\n type ValidatorResult,\n type ValidatorTest,\n} from './shared.js';\n\n/**\n * Invalid validation result for an item in the array\n * @category Types\n */\nexport type ArrayItemValidatorResult<E = unknown> = null | string | E;\n\n/**\n * Configuration for array validation.\n * @category Types\n */\nexport type ArrayConfig = ConfigBase<unknown[]>;\n\n/**\n * Parses a value into an array.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseArray(value: unknown): unknown[] | null | undefined {\n if (Array.isArray(value)) {\n return value as unknown[];\n }\n\n if (value === null || value === undefined) {\n return value;\n }\n\n const iterable = value as Iterable<unknown>;\n\n if (iterable[Symbol.iterator]) {\n return [...iterable];\n }\n\n return [];\n}\n\n/**\n * Applies a configuration to the array value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyArrayConfig<T>(\n config: ArrayConfig,\n value: unknown,\n): T[] | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined ||\n parsedValue === null ||\n parsedValue.length === 0)\n ) {\n parsedValue = config.default;\n }\n\n return parsedValue as T[];\n}\n\n/**\n * Validates an array. The first passed validator is used to validate individual\n * items. The remaining validators target the array itself.\n * @category Type Validators\n */\nexport function array<T, E>(\n config?: Partial<ArrayConfig> | ValidatorTest<T, E>,\n ...tests: ValidatorTest[]\n): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]> {\n let allTests = tests;\n let finalConfig: ArrayConfig = {\n parser: parseArray,\n };\n\n if (config !== undefined) {\n if (typeof config === 'function') {\n allTests = [config, ...tests];\n } else {\n finalConfig = {\n ...finalConfig,\n ...config,\n };\n }\n }\n\n return async (value, field) => {\n const arrayValue = applyArrayConfig<T>(finalConfig, value);\n let arrayInvalidResult: ValidatorResult | undefined;\n let isValid = true;\n const errors: ArrayItemValidatorResult[] = [];\n const [validateItem, ...arrayTests] = allTests;\n\n for (const validatorTest of arrayTests) {\n const result = await validatorTest(arrayValue, field);\n if (result.state === 'invalid') {\n isValid = false;\n arrayInvalidResult = result;\n break;\n }\n }\n\n if (arrayValue && validateItem) {\n for (const [index, item] of arrayValue.entries()) {\n const result = await validateItem(\n item,\n field ? `${field}[${index}]` : `[${index}]`,\n );\n\n if (result.state === 'invalid') {\n isValid = false;\n errors.push(result.message || result.errors);\n } else {\n errors.push(null);\n }\n }\n }\n\n if (isValid) {\n return valid({\n value: arrayValue,\n field,\n });\n }\n\n return invalid<T[], ArrayItemValidatorResult<E>[]>({\n message:\n arrayInvalidResult?.state === 'invalid'\n ? arrayInvalidResult.message\n : '',\n value,\n field,\n errors: errors as ArrayItemValidatorResult<E>[],\n });\n };\n}\n","import { type ConfigBase, createTypeValidatorTest } from './shared.js';\n\n/**\n * Parses a value into a boolean.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseBoolean(value: unknown): boolean | null | undefined {\n if (value === undefined || value === null) {\n return value;\n }\n\n if (typeof value === 'boolean') {\n return value;\n }\n\n return Boolean(value);\n}\n\n/**\n * Applies a configuration to the boolean value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyBooleanConfig(\n value: unknown,\n config: BooleanConfig,\n): boolean | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined || parsedValue === null)\n ) {\n parsedValue = config.default;\n }\n\n return parsedValue;\n}\n\n/**\n * Configuration for boolean validation.\n * @category Types\n */\nexport type BooleanConfig = ConfigBase<boolean>;\n\n/**\n * Validates a boolean value.\n * @category Type Validators\n */\nexport const boolean = createTypeValidatorTest(\n { parser: parseBoolean },\n applyBooleanConfig,\n);\n","import {\n createTypeValidatorTest,\n createValidatorTest,\n type ConfigBase,\n type ValidatorMessage,\n type ValidatorMessageParameters,\n type ValidatorTest,\n} from './shared.js';\n\n/** Badly formatted date for internal use. */\nexport const invalidDate = new Date('');\n\n/** @internal */\nconst isoDatePattern =\n /^(\\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)?)?)?)?)?$/;\n\n/** @internal */\nconst timezonePattern = /^([+-])([0-2]\\d):?([0-5]\\d)$/;\n\n/**\n * Parses a timezone string.\n * @param timezone Timezone to parse\n * @internal\n */\nfunction parseTimezone(timezone: string): number {\n const parts = timezonePattern.exec(timezone);\n\n if (timezone === 'Z' || !parts) {\n return 0;\n }\n\n const [, sign, hour = '0', minute = '0'] = parts;\n const multiplier = sign === '-' ? -1 : 1;\n const hoursInMinutes = Number.parseInt(hour, 10) * 60;\n const offset = hoursInMinutes + Number.parseInt(minute, 10);\n\n return multiplier * offset;\n}\n\nconst BASE = 10;\n\nconst dateParts = [\n // Skip\n () => 0,\n // Year\n (part = '0') => Number.parseInt(part, BASE),\n // Month\n (part = '1') => Number.parseInt(part, BASE) - 1,\n // Day\n (part = '1') => Number.parseInt(part, BASE),\n // Hour\n (part = '0') => Number.parseInt(part, BASE),\n // Minute\n (part = '0') => Number.parseInt(part, BASE),\n // Second\n (part = '0') => Number.parseInt(part, BASE),\n // Millisecond\n (part = '0') => Number.parseFloat(part) * 1e3,\n];\n\n/**\n * Parses a value into a date.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseDate(value: unknown): Date | null | undefined {\n if (value === null || value === undefined) {\n return value;\n }\n\n if (value instanceof Date) {\n return value;\n }\n\n if (typeof value === 'number') {\n return new Date(value);\n }\n\n if (typeof value !== 'string') {\n return invalidDate;\n }\n\n const parts = isoDatePattern.exec(value);\n\n if (parts === null) {\n return invalidDate;\n }\n\n const [\n ,\n year = 0,\n month = 0,\n day = 0,\n hour = 0,\n minute = 0,\n second = 0,\n millisecond = 0,\n ] = dateParts.map((parse, index) => parse(parts[index]));\n\n const timezone = parts[8];\n\n if (!timezone) {\n return new Date(year, month, day, hour, minute, second, millisecond);\n }\n\n const minuteOffset = parseTimezone(timezone);\n\n return new Date(\n Date.UTC(\n year,\n month,\n day,\n hour,\n minute + minuteOffset,\n second,\n millisecond,\n ),\n );\n}\n\n/**\n * Applies a configuration to the date value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyDateConfig(\n value: unknown,\n config: DateConfig,\n): Date | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined ||\n parsedValue === null ||\n Number.isNaN(parsedValue.getTime()))\n ) {\n parsedValue = config.default;\n }\n\n return parsedValue;\n}\n\nexport type SharedDateValueType = Date | string | number;\n\nexport interface MinDateValidatorMessageParameters\n extends ValidatorMessageParameters<Date> {\n min: Date | null | undefined;\n limit: Date | null | undefined;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a date value is on or after a date.\n * @param limit Minimum date\n * @param message Error message\n * @param exclusive Makes comparison exclusive instead of inclusive\n * @category Validation Tests\n */\nexport function minDate(\n limit: SharedDateValueType,\n message: ValidatorMessage<Date, MinDateValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<Date, null> {\n const parsedDate = parseDate(limit);\n return createValidatorTest(\n (value) =>\n parsedDate !== null &&\n parsedDate !== undefined &&\n (value === null ||\n value === undefined ||\n (value instanceof Date &&\n (exclusive ? value > parsedDate : value >= parsedDate))),\n message,\n () => ({\n min: parsedDate,\n limit: parsedDate,\n exclusive,\n }),\n );\n}\n\nexport interface MaxDateValidatorMessageParameters\n extends ValidatorMessageParameters<Date> {\n max: Date | null | undefined;\n limit: Date | null | undefined;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a date value is on or before a date.\n * @param limit Maximum date\n * @param message Error message\n * @param exclusive Makes comparison exclusive instead of inclusive\n * @category Validation Tests\n */\nexport function maxDate(\n limit: SharedDateValueType,\n message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<Date> {\n const parsedDate = parseDate(limit);\n return createValidatorTest(\n (value) =>\n parsedDate !== null &&\n parsedDate !== undefined &&\n (value === null ||\n value === undefined ||\n (value instanceof Date &&\n (exclusive ? value < parsedDate : value <= parsedDate))),\n message,\n () => ({\n max: parsedDate,\n limit: parsedDate,\n exclusive,\n }),\n );\n}\n\n/**\n * Configuration for date validation.\n * @category Types\n */\nexport type DateConfig = ConfigBase<Date>;\n\n/**\n * Validates a date value.\n * @category Type Validators\n */\nexport const date = createTypeValidatorTest(\n { parser: parseDate },\n applyDateConfig,\n);\n","import {\n type ConfigBase,\n createTypeValidatorTest,\n invalid,\n valid,\n type ValidatorMessage,\n type ValidatorTest,\n} from './shared.js';\n\n/**\n * Parses a value into a number.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseNumber(value: unknown): number | null | undefined {\n if (value === null || value === undefined) {\n return value;\n }\n\n if (typeof value === 'number') {\n return value;\n }\n\n if (value instanceof Date) {\n return value.getTime();\n }\n\n if (typeof value === 'string') {\n return Number.parseFloat(value);\n }\n\n return Number.parseFloat(String(value));\n}\n\n/**\n * Applies a configuration to the number value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyNumberConfig(\n value: unknown,\n config: NumberConfig,\n): number | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined ||\n parsedValue === null ||\n Number.isNaN(parsedValue))\n ) {\n parsedValue = config.default;\n }\n\n if (config.round && typeof parsedValue === 'number') {\n switch (config.round) {\n case 'nearest': {\n parsedValue = Math.round(parsedValue);\n break;\n }\n case 'ceil': {\n parsedValue = Math.ceil(parsedValue);\n break;\n }\n case 'floor': {\n parsedValue = Math.floor(parsedValue);\n break;\n }\n default: {\n throw new Error(`Unknown round type: ${String(config.round)}`);\n }\n }\n }\n\n return parsedValue;\n}\n\n/**\n * Ensures a number value is an integer.\n * @param message Error message\n * @category Validation Tests\n */\nexport function integer(\n message: ValidatorMessage<number>,\n): ValidatorTest<number> {\n return async (value, field) => {\n if (\n value === undefined ||\n value === null ||\n (typeof value === 'number' &&\n Number.isFinite(value) &&\n Math.floor(value) === value)\n ) {\n return valid({ value, field });\n }\n\n return invalid({\n message,\n value,\n field,\n });\n };\n}\n\n/**\n * Configuration for number validation.\n * @category Types\n */\nexport interface NumberConfig extends ConfigBase<number> {\n /** Apply rounding mechanism before validation. */\n round?: 'nearest' | 'floor' | 'ceil';\n}\n\n/**\n * Validates a number value.\n * @category Type Validators\n */\nexport const number = createTypeValidatorTest(\n {\n parser: parseNumber,\n },\n applyNumberConfig,\n);\n","import {\n type ExtractError,\n type ExtractValue,\n hasOwnProperty,\n invalid,\n valid,\n type ValidatorTest,\n} from './shared.js';\n\nexport type ObjectParameter = Record<string, ValidatorTest<unknown, unknown>>;\n\n/**\n * Validates an object.\n * @category Type Validators\n */\nexport function object<\n P extends ObjectParameter,\n V extends { [K in keyof P]?: ExtractValue<P[K]> },\n E extends { [K in keyof P]?: ExtractError<P[K]> },\n>(properties: P): ValidatorTest<V, E> {\n if (typeof properties !== 'object' || properties === null) {\n throw new TypeError('`properties` must be a configuration object');\n }\n\n return async (values, field) => {\n const definedValues = (values ?? {}) as V;\n const errors = {} as E;\n const resolvedValues = {} as V;\n let isValid = true;\n\n for (const key in properties) {\n if (hasOwnProperty(properties, key)) {\n const validator = properties[key];\n const value = definedValues[key];\n const result =\n typeof validator === 'function'\n ? await validator(value, key)\n : await invalid({ field: key, message: 'No validator set', value });\n\n resolvedValues[key] = result.value as V[Extract<keyof P, string>];\n\n if (result.state === 'invalid') {\n isValid = false;\n errors[key] = (result.message || result.errors) as E[Extract<\n keyof P,\n string\n >];\n }\n }\n }\n\n if (isValid) {\n return valid({ value: resolvedValues, field });\n }\n\n return invalid({\n message: '',\n value: values,\n field,\n errors,\n });\n };\n}\n","import {\n type ConfigBase,\n createTypeValidatorTest,\n createValidatorTest,\n isObject,\n type ValidatorMessage,\n type ValidatorMessageParameters,\n type ValidatorTest,\n} from './shared.js';\n\n/** @internal */\nconst emailPattern =\n /^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/;\n\n/**\n * Configuration for string validation.\n * @category Types\n */\nexport interface StringConfig extends ConfigBase<string> {\n /** Apply trim to string before validation. */\n trim?: boolean;\n}\n\n/**\n * Parses a value into a string.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseString(value: unknown): string | null | undefined {\n if (value === null || value === undefined) {\n return value;\n }\n\n if (typeof value === 'string') {\n return value;\n }\n\n if (value instanceof Date) {\n return value.toISOString();\n }\n\n if (isObject(value)) {\n throw new TypeError('Failed to parse value to string.');\n }\n\n return String(value);\n}\n\n/**\n * Applies a configuration to the string value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyStringConfig(\n value: unknown,\n config: StringConfig,\n): string | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined || parsedValue === null || parsedValue === '')\n ) {\n parsedValue = config.default;\n }\n\n if (config.trim && typeof parsedValue === 'string') {\n parsedValue = parsedValue.trim();\n }\n\n return parsedValue;\n}\n\nexport interface MatchesValidatorMessageParameters\n extends ValidatorMessageParameters<string> {\n pattern: RegExp;\n}\n\n/**\n * Ensures a string value matches a pattern\n * @param pattern Pattern to match\n * @param message Error message\n * @category Validation Tests\n */\nexport function matches(\n pattern: RegExp,\n message: ValidatorMessage<string, MatchesValidatorMessageParameters>,\n): ValidatorTest<string> {\n return createValidatorTest(\n (value) =>\n value === undefined ||\n value === null ||\n value === '' ||\n (typeof value === 'string' && pattern.test(value)),\n message,\n () => ({ pattern }),\n );\n}\n\n/**\n * Ensures a string value matches an email pattern\n * @param message Error message\n * @category Validation Tests\n */\nexport function email(\n message: ValidatorMessage<string>,\n): ValidatorTest<string> {\n return matches(emailPattern, message);\n}\n\n/**\n * Ensures a string value is a valid URL\n * @param message Error message\n * @param protocols Optional list of protocols to allow\n * @category Validation Tests\n */\nexport function url(\n message: ValidatorMessage<string>,\n protocols?: string[],\n): ValidatorTest<string> {\n return createValidatorTest(\n (value) => {\n try {\n let url: URL | undefined;\n if (\n value === undefined ||\n value === null ||\n value === '' ||\n (typeof value === 'string' && (url = new URL(value)))\n ) {\n return !(url && protocols && !protocols.includes(url.protocol));\n }\n } catch {\n // Do nothing\n }\n\n return false;\n },\n message,\n () => ({ protocols }),\n );\n}\n\n/**\n * Validates a string value.\n * @category Type Validators\n */\nexport const string = createTypeValidatorTest(\n {\n parser: parseString,\n },\n applyStringConfig,\n);\n"],"mappings":"AAqHO,SAASA,EAASC,EAAyB,CAChD,OAAO,OAAO,UAAU,SAAS,KAAKA,CAAK,IAAM,iBACnD,CAOO,SAASC,EACdC,EACAC,EACS,CACT,OAAO,OAAO,UAAU,eAAe,KAAKD,EAAQC,CAAQ,CAC9D,CAGA,IAAMC,EAAgB,WAQf,SAASC,EACdC,EACAC,EACQ,CACR,OAAI,OAAOD,GAAa,SACfA,EAAS,WAAWF,EAAe,CAACI,EAAGC,IAC5C,OAAQF,EAAuCE,CAAG,CAAC,CACrD,EAGKH,EAASC,CAAU,CAC5B,CAQA,eAAsBG,EAAoB,CACxC,MAAAV,EACA,MAAAW,CACF,EAGmC,CACjC,MAAO,CACL,QAAS,GACT,MAAO,QACP,MAAAX,EACA,MAAAW,CACF,CACF,CAUA,eAAsBC,EAIpB,CACA,OAAAC,EACA,MAAAF,EACA,QAAAG,EACA,MAAAd,EACA,OAAAe,CACF,EAMmC,CACjC,MAAO,CACL,QAAS,GACT,MAAO,UACP,QAASV,EAAcS,EAAS,CAAE,GAAGC,EAAQ,MAAAf,EAAO,MAAAW,CAAM,CAAM,EAChE,MAAAX,EACA,MAAAW,EACA,OAAAE,CACF,CACF,CAUO,SAASG,EACdC,EACAC,EACgC,CAChC,MAAO,CAACC,KAAWC,IAAU,CAC3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ACuB,CACvB,OAAOF,EAAQP,GAAcS,CAAO,CACtC,CAQO,SAASG,GACdH,EACAI,EACuB,CACvB,OAAOH,EACJR,GAAU,CACT,GAAI,CACF,IAAIU,EACJ,GAEEV,GAAU,MACVA,IAAU,IACT,OAAOA,GAAU,WAAaU,EAAM,IAAI,IAAIV,CAAK,GAElD,MAAO,EAAEU,GAAOC,GAAa,CAACA,EAAU,SAASD,EAAI,QAAQ,EAEjE,MAAQ,CAER,CAEA,MAAO,EACT,EACAH,EACA,KAAO,CAAE,UAAAI,CAAU,EACrB,CACF,CAMO,IAAMC,GAASC,EACpB,CACE,OAAQd,CACV,EACAG,CACF","names":["isObject","value","hasOwnProperty","object","property","formatPattern","formatMessage","template","parameters","_","key","valid","field","invalid","errors","message","extras","createTypeValidatorTest","defaultConfig","applyConfig","config","tests","cache","finalConfig","allTests","parsedValue","parsedValueString","validatorTest","result","error","createValidatorTest","test","getExtras","required","nullable","getAmount","max","limit","exclusive","amount","min","exact","oneOf","values","x","parseArray","value","iterable","applyArrayConfig","config","parsedValue","array","tests","allTests","finalConfig","field","arrayValue","arrayInvalidResult","isValid","errors","validateItem","arrayTests","validatorTest","result","index","item","valid","invalid","parseBoolean","value","applyBooleanConfig","config","parsedValue","boolean","createTypeValidatorTest","invalidDate","isoDatePattern","timezonePattern","parseTimezone","timezone","parts","sign","hour","minute","multiplier","offset","BASE","dateParts","part","parseDate","value","year","month","day","second","millisecond","parse","index","minuteOffset","applyDateConfig","config","parsedValue","minDate","limit","message","exclusive","parsedDate","createValidatorTest","maxDate","date","createTypeValidatorTest","parseNumber","value","applyNumberConfig","config","parsedValue","integer","message","field","valid","invalid","number","createTypeValidatorTest","object","properties","values","field","definedValues","errors","resolvedValues","isValid","key","hasOwnProperty","validator","value","result","invalid","valid","emailPattern","parseString","value","isObject","applyStringConfig","config","parsedValue","matches","pattern","message","createValidatorTest","email","url","protocols","string","createTypeValidatorTest"]}
|
|
1
|
+
{"version":3,"sources":["../src/shared.ts","../src/arrays.ts","../src/booleans.ts","../src/dates.ts","../src/numbers.ts","../src/objects.ts","../src/strings.ts"],"sourcesContent":["export type ValidResult<T> = {\n /**\n * True if the original value is valid according to all validation tests\n * @deprecated Use `state` instead.\n */\n isValid: true;\n /** This is preferred to use over `isValid` as it works better for type guards. */\n state: 'valid';\n /** The parsed value. Note that may be different than the original value. */\n value: T | null | undefined;\n /** Field name if provided. Automatically provided if using object validation. */\n field?: string | undefined;\n};\n\nexport type InvalidResult<E> = {\n /**\n * True if the original value is valid according to all validation tests.\n * @deprecated Use `state` instead.\n */\n isValid: false;\n /** This is preferred to use over `isValid` as it works better for type guards. */\n state: 'invalid';\n /** The parsed value. Note that may be different than the original value. */\n value: unknown;\n /** Error message if `state` is invalid. */\n message: string;\n /** Field name if provided. Automatically provided if using object validation. */\n field?: string | undefined;\n /** Extra error details */\n errors?: E | undefined;\n};\n\n/**\n * The result of a validation.\n * @typeParam T The value type\n * @category Types\n */\nexport type ValidatorResult<T = any, E = any> =\n | ValidResult<T>\n | InvalidResult<E>;\n\n/**\n * Validation test function.\n * @typeParam T The value type\n * @category Types\n */\nexport type ValidatorTest<T = any, E = any> = (\n value?: unknown,\n field?: string,\n) => Promise<ValidatorResult<T, E>>;\n\n/**\n * Extracts the generic error type from a ValidatorTest.\n * @typeParam T a ValidatorTest\n * @category Types\n */\nexport type ExtractError<T extends ValidatorTest> =\n T extends ValidatorTest<any, infer E> ? E : unknown;\n\n/**\n * Extracts the generic value type from a ValidatorTest.\n * @typeParam T a ValidatorTest\n * @category Types\n */\nexport type ExtractValue<T extends ValidatorTest> =\n T extends ValidatorTest<infer V> ? V : unknown;\n\n/**\n * Make a deep partial type.\n * @typeParam T any object type\n * @category Types\n */\nexport type DeepPartial<T> = {\n [P in keyof T]?: DeepPartial<T[P]>;\n};\n\n/**\n * Validation factory function.\n * @typeParam C The configuration type\n * @category Types\n */\nexport type ValidatorFactory<C, T, E> = (\n config?: Partial<C> | ValidatorTest,\n ...tests: ValidatorTest[]\n) => ValidatorTest<T, E>;\n\n/**\n * Base configuration type.\n * @typeParam T The target type.\n * @category Types\n */\nexport interface ConfigBase<T> {\n /** Parser to convert value into the designated type. */\n parser: (value: unknown) => T | null | undefined;\n /** Provide a fallback value in case the original value is undefined. */\n default?: T;\n}\n\nexport interface ValidatorMessageParameters<T> {\n value: T | null | undefined;\n field: string | undefined;\n}\n\n/**\n * Validation message. May be a plain string or a function that accepts arguments.\n * While using a string, values in squiggly brackets `{}` are replaced.\n * @category Types\n */\nexport type ValidatorMessage<\n T,\n P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>,\n> = string | ((parameters: P) => string);\n\n/**\n * Simple check for objects.\n * @param value Value to test\n */\nexport function isObject(value: unknown): boolean {\n return Object.prototype.toString.call(value) === '[object Object]';\n}\n\n/**\n * Safe hasOwnProperty check\n * @param object Object to check\n * @param property Property to look for\n */\nexport function hasOwnProperty(\n object: unknown,\n property: string | number | symbol,\n): boolean {\n return Object.prototype.hasOwnProperty.call(object, property);\n}\n\n/** @internal */\nconst formatPattern = /{(\\w+)}/g;\n\n/**\n * Formats a message using a squiggly bracket `{}` template.\n * @param template Message template as string or function\n * @param parameters Params to inject into template\n * @category Helpers\n */\nexport function formatMessage<T, P extends ValidatorMessageParameters<T>>(\n template: ValidatorMessage<T, P>,\n parameters: P,\n): string {\n if (typeof template === 'string') {\n return template.replaceAll(formatPattern, (_, key: string) =>\n String((parameters as Record<string, unknown>)[key]),\n );\n }\n\n return template(parameters);\n}\n\n/**\n * Creates a valid result\n * @param value Parsed value\n * @param field Field name\n * @category Helpers\n */\nexport async function valid<T, E = never>({\n value,\n field,\n}: {\n value: T | null | undefined;\n field: string | undefined;\n}): Promise<ValidatorResult<T, E>> {\n return {\n isValid: true,\n state: 'valid',\n value,\n field,\n };\n}\n\n/**\n * Creates an invalid result.\n * @param message Error message\n * @param value Parsed value\n * @param field Field name\n * @param extras Extra message params\n * @category Helpers\n */\nexport async function invalid<\n T,\n E,\n P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>,\n>({\n errors,\n field,\n message,\n value,\n extras,\n}: {\n message: ValidatorMessage<T, P>;\n value: unknown;\n field: string | undefined;\n errors?: E | undefined;\n extras?: Omit<P, 'field' | 'value'> | undefined;\n}): Promise<ValidatorResult<T, E>> {\n return {\n isValid: false,\n state: 'invalid',\n message: formatMessage(message, { ...extras, value, field } as P),\n value,\n field,\n errors,\n };\n}\n\n/**\n * Creates a type validator.\n * @typeParam T The basic type for the final value to be validated.\n * @typeParam C The configuration type.\n * @param defaultParser Parser to convert value into type.\n * @param applyConfig Function that applies configuration to value.\n * @category Helpers\n */\nexport function createTypeValidatorTest<T, C extends ConfigBase<T>>(\n defaultConfig: C,\n applyConfig: (value: unknown, config: C) => T | null | undefined,\n): ValidatorFactory<C, T, string> {\n return (config, ...tests) => {\n const cache: Record<string, ValidatorResult<T>> = {};\n let finalConfig = defaultConfig;\n let allTests = tests;\n\n if (config !== undefined) {\n if (typeof config === 'function') {\n allTests = [config, ...tests];\n } else {\n finalConfig = {\n ...defaultConfig,\n ...config,\n };\n }\n }\n\n return async (value, field) => {\n try {\n const parsedValue = applyConfig(value, finalConfig);\n const parsedValueString = String(parsedValue);\n if (hasOwnProperty(cache, parsedValueString)) {\n return cache[parsedValueString] as ValidatorResult<T, any>;\n }\n\n for (const validatorTest of allTests) {\n const result = await validatorTest(parsedValue, field);\n if (result.state === 'invalid') {\n cache[parsedValueString] = result;\n return result;\n }\n }\n\n cache[parsedValueString] = await valid({ value: parsedValue, field });\n return cache[parsedValueString] as ValidatorResult<T, any>;\n } catch (error) {\n return invalid({\n message: error instanceof Error ? error.message : String(error),\n value,\n field,\n });\n }\n };\n };\n}\n\n/**\n * Captures basic validator test pattern\n * @param test Test function\n * @param message Error message\n * @param getExtras Optional extra fields for error message\n */\nexport function createValidatorTest<\n T = any,\n E = any,\n P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>,\n>(\n test: (value: unknown, field?: string) => boolean,\n message: ValidatorMessage<T, P>,\n getExtras?: (value: T | null | undefined) => Omit<P, 'value' | 'field'>,\n): ValidatorTest<T, E> {\n return async (value, field) => {\n if (test(value, field)) {\n return valid({ value: value as T, field });\n }\n\n return invalid({\n message,\n value,\n field,\n extras: getExtras ? getExtras(value as T) : undefined,\n });\n };\n}\n\n/**\n * Ensures a value is not undefined, null, empty string, NaN, nor invalid date.\n * @param message Error message\n * @param nullable Allow null values\n * @category Validation Tests\n */\nexport function required<T, P extends ValidatorMessageParameters<T>>(\n message: ValidatorMessage<T, P>,\n nullable?: boolean,\n): ValidatorTest<T> {\n return createValidatorTest(\n (value) =>\n (nullable && value === null) ||\n (value !== null &&\n value !== undefined &&\n !(typeof value === 'string' && value === '') &&\n !(typeof value === 'number' && Number.isNaN(value)) &&\n !(value instanceof Date && Number.isNaN(value.valueOf()))),\n message,\n );\n}\n\nexport type SharedValueType = string | number | unknown[];\n\n/**\n * Extracts a numeric amount from value, i.e. string length, array length, or number.\n * @internal\n */\nfunction getAmount(value: SharedValueType | null | undefined) {\n return typeof value === 'string' || Array.isArray(value)\n ? value.length\n : value;\n}\n\nexport interface MaxValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {\n max: number;\n amount: number | null | undefined;\n limit: number;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a string, number, or array value is at most a certain amount.\n * @param limit Size or value limit\n * @param message Error message\n * @param exclusive Use exclusive comparison instead of inclusive\n * @category Validation Tests\n */\nexport function max(\n limit: number,\n message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<SharedValueType> {\n return createValidatorTest(\n (value) => {\n const amount = getAmount(value as SharedValueType);\n return (\n amount === undefined ||\n amount === null ||\n value === '' ||\n (exclusive ? amount < limit : amount <= limit)\n );\n },\n message,\n (value) => ({\n max: limit,\n limit,\n amount: getAmount(value),\n exclusive,\n }),\n );\n}\n\nexport interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {\n min: number;\n limit: number;\n amount: number | null | undefined;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a string, number, or array value is at most a certain amount.\n * @param limit Size or value limit\n * @param message Error message\n * @param exclusive Use exclusive comparison instead of inclusive\n * @category Validation Tests\n */\nexport function min(\n limit: number,\n message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<SharedValueType> {\n return createValidatorTest(\n (value) => {\n const amount = getAmount(value as SharedValueType);\n return (\n amount === undefined ||\n amount === null ||\n value === '' ||\n (exclusive ? amount > limit : amount >= limit)\n );\n },\n message,\n (value) => ({\n min: limit,\n limit,\n amount: getAmount(value),\n exclusive,\n }),\n );\n}\n\nexport interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {\n limit: number;\n amount: number | null | undefined;\n exact: number;\n}\n\n/**\n * Ensures a string, number, or array value is exactly a certain amount.\n * @param limit Size or value limit\n * @param message Error message\n * @category Validation Tests\n */\nexport function exact(\n limit: number,\n message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>,\n): ValidatorTest<SharedValueType> {\n return createValidatorTest(\n (value) => {\n const amount = getAmount(value as SharedValueType);\n return (\n amount === undefined ||\n amount === null ||\n value === '' ||\n amount === limit\n );\n },\n message,\n (value) => ({\n amount: getAmount(value),\n limit,\n exact: limit,\n }),\n );\n}\n\nexport interface OneOfValidatorMessageParameters<\n T,\n> extends ValidatorMessageParameters<T> {\n values: readonly T[];\n}\n\n/**\n * Ensures a value is one in a predefined list.\n * @param values List of allowed values.\n * @param message Error message\n * @category Validation Tests\n */\nexport function oneOf<T extends string | number | boolean | Date>(\n values: readonly T[],\n message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>,\n): ValidatorTest<T> {\n return createValidatorTest(\n (value) =>\n value === undefined ||\n value === null ||\n values.includes(value as T) ||\n (value instanceof Date &&\n values.some((x) => x.valueOf() === value.valueOf())),\n message,\n () => ({ values }),\n );\n}\n","import {\n invalid,\n valid,\n type ConfigBase,\n type ValidatorResult,\n type ValidatorTest,\n} from './shared.js';\n\n/**\n * Invalid validation result for an item in the array\n * @category Types\n */\nexport type ArrayItemValidatorResult<E = unknown> = null | string | E;\n\n/**\n * Configuration for array validation.\n * @category Types\n */\nexport type ArrayConfig = ConfigBase<unknown[]>;\n\n/**\n * Parses a value into an array.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseArray(value: unknown): unknown[] | null | undefined {\n if (Array.isArray(value)) {\n return value as unknown[];\n }\n\n if (value === null || value === undefined) {\n return value;\n }\n\n const iterable = value as Iterable<unknown>;\n\n if (iterable[Symbol.iterator]) {\n return [...iterable];\n }\n\n return [];\n}\n\n/**\n * Applies a configuration to the array value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyArrayConfig<T>(\n config: ArrayConfig,\n value: unknown,\n): T[] | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined ||\n parsedValue === null ||\n parsedValue.length === 0)\n ) {\n parsedValue = config.default;\n }\n\n return parsedValue as T[];\n}\n\n/**\n * Validates an array. The first passed validator is used to validate individual\n * items. The remaining validators target the array itself.\n * @category Type Validators\n */\nexport function array<T, E>(\n config?: Partial<ArrayConfig> | ValidatorTest<T, E>,\n ...tests: ValidatorTest[]\n): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]> {\n let allTests = tests;\n let finalConfig: ArrayConfig = {\n parser: parseArray,\n };\n\n if (config !== undefined) {\n if (typeof config === 'function') {\n allTests = [config, ...tests];\n } else {\n finalConfig = {\n ...finalConfig,\n ...config,\n };\n }\n }\n\n // oxlint-disable-next-line eslint/complexity -- array and item tests share one closure; split in #483\n return async (value, field) => {\n const arrayValue = applyArrayConfig<T>(finalConfig, value);\n let arrayInvalidResult: ValidatorResult | undefined;\n let isValid = true;\n const errors: ArrayItemValidatorResult[] = [];\n const [validateItem, ...arrayTests] = allTests;\n\n for (const validatorTest of arrayTests) {\n const result = await validatorTest(arrayValue, field);\n if (result.state === 'invalid') {\n isValid = false;\n arrayInvalidResult = result;\n break;\n }\n }\n\n if (arrayValue && validateItem) {\n for (const [index, item] of arrayValue.entries()) {\n const result = await validateItem(\n item,\n field ? `${field}[${index}]` : `[${index}]`,\n );\n\n if (result.state === 'invalid') {\n isValid = false;\n errors.push(result.message || result.errors);\n } else {\n errors.push(null);\n }\n }\n }\n\n if (isValid) {\n return valid({\n value: arrayValue,\n field,\n });\n }\n\n return invalid<T[], ArrayItemValidatorResult<E>[]>({\n message:\n arrayInvalidResult?.state === 'invalid'\n ? arrayInvalidResult.message\n : '',\n value,\n field,\n errors: errors as ArrayItemValidatorResult<E>[],\n });\n };\n}\n","import { type ConfigBase, createTypeValidatorTest } from './shared.js';\n\n/**\n * Parses a value into a boolean.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseBoolean(value: unknown): boolean | null | undefined {\n if (value === undefined || value === null) {\n return value;\n }\n\n if (typeof value === 'boolean') {\n return value;\n }\n\n return Boolean(value);\n}\n\n/**\n * Applies a configuration to the boolean value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyBooleanConfig(\n value: unknown,\n config: BooleanConfig,\n): boolean | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined || parsedValue === null)\n ) {\n parsedValue = config.default;\n }\n\n return parsedValue;\n}\n\n/**\n * Configuration for boolean validation.\n * @category Types\n */\nexport type BooleanConfig = ConfigBase<boolean>;\n\n/**\n * Validates a boolean value.\n * @category Type Validators\n */\nexport const boolean = createTypeValidatorTest(\n { parser: parseBoolean },\n applyBooleanConfig,\n);\n","import {\n createTypeValidatorTest,\n createValidatorTest,\n type ConfigBase,\n type ValidatorMessage,\n type ValidatorMessageParameters,\n type ValidatorTest,\n} from './shared.js';\n\n/** Badly formatted date for internal use. */\nexport const invalidDate = new Date('');\n\n/** @internal */\nconst isoDatePattern =\n /^(\\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)?)?)?)?)?$/;\n\n/** @internal */\nconst timezonePattern = /^([+-])([0-2]\\d):?([0-5]\\d)$/;\n\n/**\n * Parses a timezone string.\n * @param timezone Timezone to parse\n * @internal\n */\nfunction parseTimezone(timezone: string): number {\n const parts = timezonePattern.exec(timezone);\n\n if (timezone === 'Z' || !parts) {\n return 0;\n }\n\n const [, sign, hour = '0', minute = '0'] = parts;\n const multiplier = sign === '-' ? -1 : 1;\n const hoursInMinutes = Number.parseInt(hour, 10) * 60;\n const offset = hoursInMinutes + Number.parseInt(minute, 10);\n\n return multiplier * offset;\n}\n\nconst BASE = 10;\n\nconst dateParts = [\n // Skip\n () => 0,\n // Year\n (part = '0') => Number.parseInt(part, BASE),\n // Month\n (part = '1') => Number.parseInt(part, BASE) - 1,\n // Day\n (part = '1') => Number.parseInt(part, BASE),\n // Hour\n (part = '0') => Number.parseInt(part, BASE),\n // Minute\n (part = '0') => Number.parseInt(part, BASE),\n // Second\n (part = '0') => Number.parseInt(part, BASE),\n // Millisecond\n (part = '0') => Number.parseFloat(part) * 1e3,\n];\n\n/**\n * Parses a value into a date.\n * @param value Value to parse\n * @category Parsers\n */\n// oxlint-disable-next-line eslint/complexity -- one guard per input type plus ISO parsing; split in #483\nexport function parseDate(value: unknown): Date | null | undefined {\n if (value === null || value === undefined) {\n return value;\n }\n\n if (value instanceof Date) {\n return value;\n }\n\n if (typeof value === 'number') {\n return new Date(value);\n }\n\n if (typeof value !== 'string') {\n return invalidDate;\n }\n\n const parts = isoDatePattern.exec(value);\n\n if (parts === null) {\n return invalidDate;\n }\n\n const [\n ,\n year = 0,\n month = 0,\n day = 0,\n hour = 0,\n minute = 0,\n second = 0,\n millisecond = 0,\n ] = dateParts.map((parse, index) => parse(parts[index]));\n\n const timezone = parts[8];\n\n if (!timezone) {\n return new Date(year, month, day, hour, minute, second, millisecond);\n }\n\n const minuteOffset = parseTimezone(timezone);\n\n return new Date(\n Date.UTC(\n year,\n month,\n day,\n hour,\n minute + minuteOffset,\n second,\n millisecond,\n ),\n );\n}\n\n/**\n * Applies a configuration to the date value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyDateConfig(\n value: unknown,\n config: DateConfig,\n): Date | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined ||\n parsedValue === null ||\n Number.isNaN(parsedValue.getTime()))\n ) {\n parsedValue = config.default;\n }\n\n return parsedValue;\n}\n\nexport type SharedDateValueType = Date | string | number;\n\nexport interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {\n min: Date | null | undefined;\n limit: Date | null | undefined;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a date value is on or after a date.\n * @param limit Minimum date\n * @param message Error message\n * @param exclusive Makes comparison exclusive instead of inclusive\n * @category Validation Tests\n */\nexport function minDate(\n limit: SharedDateValueType,\n message: ValidatorMessage<Date, MinDateValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<Date, null> {\n const parsedDate = parseDate(limit);\n return createValidatorTest(\n (value) =>\n parsedDate !== null &&\n parsedDate !== undefined &&\n (value === null ||\n value === undefined ||\n (value instanceof Date &&\n (exclusive ? value > parsedDate : value >= parsedDate))),\n message,\n () => ({\n min: parsedDate,\n limit: parsedDate,\n exclusive,\n }),\n );\n}\n\nexport interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {\n max: Date | null | undefined;\n limit: Date | null | undefined;\n exclusive?: boolean | undefined;\n}\n\n/**\n * Ensures a date value is on or before a date.\n * @param limit Maximum date\n * @param message Error message\n * @param exclusive Makes comparison exclusive instead of inclusive\n * @category Validation Tests\n */\nexport function maxDate(\n limit: SharedDateValueType,\n message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>,\n exclusive?: boolean,\n): ValidatorTest<Date> {\n const parsedDate = parseDate(limit);\n return createValidatorTest(\n (value) =>\n parsedDate !== null &&\n parsedDate !== undefined &&\n (value === null ||\n value === undefined ||\n (value instanceof Date &&\n (exclusive ? value < parsedDate : value <= parsedDate))),\n message,\n () => ({\n max: parsedDate,\n limit: parsedDate,\n exclusive,\n }),\n );\n}\n\n/**\n * Configuration for date validation.\n * @category Types\n */\nexport type DateConfig = ConfigBase<Date>;\n\n/**\n * Validates a date value.\n * @category Type Validators\n */\nexport const date = createTypeValidatorTest(\n { parser: parseDate },\n applyDateConfig,\n);\n","import {\n type ConfigBase,\n createTypeValidatorTest,\n invalid,\n valid,\n type ValidatorMessage,\n type ValidatorTest,\n} from './shared.js';\n\n/**\n * Parses a value into a number.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseNumber(value: unknown): number | null | undefined {\n if (value === null || value === undefined) {\n return value;\n }\n\n if (typeof value === 'number') {\n return value;\n }\n\n if (value instanceof Date) {\n return value.getTime();\n }\n\n if (typeof value === 'string') {\n return Number.parseFloat(value);\n }\n\n // oxlint-disable-next-line typescript/no-base-to-string -- coerces all other input on purpose; #484 decides non-primitives\n return Number.parseFloat(String(value));\n}\n\n/**\n * Applies a configuration to the number value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyNumberConfig(\n value: unknown,\n config: NumberConfig,\n): number | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined ||\n parsedValue === null ||\n Number.isNaN(parsedValue))\n ) {\n parsedValue = config.default;\n }\n\n if (config.round && typeof parsedValue === 'number') {\n switch (config.round) {\n case 'nearest': {\n parsedValue = Math.round(parsedValue);\n break;\n }\n case 'ceil': {\n parsedValue = Math.ceil(parsedValue);\n break;\n }\n case 'floor': {\n parsedValue = Math.floor(parsedValue);\n break;\n }\n default: {\n throw new Error(`Unknown round type: ${String(config.round)}`);\n }\n }\n }\n\n return parsedValue;\n}\n\n/**\n * Ensures a number value is an integer.\n * @param message Error message\n * @category Validation Tests\n */\nexport function integer(\n message: ValidatorMessage<number>,\n): ValidatorTest<number> {\n return async (value, field) => {\n if (\n value === undefined ||\n value === null ||\n (typeof value === 'number' &&\n Number.isFinite(value) &&\n Math.floor(value) === value)\n ) {\n return valid({ value, field });\n }\n\n return invalid({\n message,\n value,\n field,\n });\n };\n}\n\n/**\n * Configuration for number validation.\n * @category Types\n */\nexport interface NumberConfig extends ConfigBase<number> {\n /** Apply rounding mechanism before validation. */\n round?: 'nearest' | 'floor' | 'ceil';\n}\n\n/**\n * Validates a number value.\n * @category Type Validators\n */\nexport const number = createTypeValidatorTest(\n {\n parser: parseNumber,\n },\n applyNumberConfig,\n);\n","import {\n type ExtractError,\n type ExtractValue,\n hasOwnProperty,\n invalid,\n valid,\n type ValidatorTest,\n} from './shared.js';\n\nexport type ObjectParameter = Record<string, ValidatorTest<unknown, unknown>>;\n\n/**\n * Validates an object.\n * @category Type Validators\n */\nexport function object<\n P extends ObjectParameter,\n V extends { [K in keyof P]?: ExtractValue<P[K]> },\n E extends { [K in keyof P]?: ExtractError<P[K]> },\n>(properties: P): ValidatorTest<V, E> {\n if (typeof properties !== 'object' || properties === null) {\n throw new TypeError('`properties` must be a configuration object');\n }\n\n return async (values, field) => {\n const definedValues = (values ?? {}) as V;\n const errors = {} as E;\n const resolvedValues = {} as V;\n let isValid = true;\n\n for (const key in properties) {\n if (hasOwnProperty(properties, key)) {\n const validator = properties[key];\n const value = definedValues[key];\n const result =\n typeof validator === 'function'\n ? await validator(value, key)\n : await invalid({ field: key, message: 'No validator set', value });\n\n resolvedValues[key] = result.value as V[Extract<keyof P, string>];\n\n if (result.state === 'invalid') {\n isValid = false;\n errors[key] = (result.message || result.errors) as E[Extract<\n keyof P,\n string\n >];\n }\n }\n }\n\n if (isValid) {\n return valid({ value: resolvedValues, field });\n }\n\n return invalid({\n message: '',\n value: values,\n field,\n errors,\n });\n };\n}\n","import {\n type ConfigBase,\n createTypeValidatorTest,\n createValidatorTest,\n isObject,\n type ValidatorMessage,\n type ValidatorMessageParameters,\n type ValidatorTest,\n} from './shared.js';\n\n/** @internal */\nconst emailPattern =\n /^[\\w!#$%&'*+./=?^`{|}~-]+@[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?(?:\\.[\\dA-Za-z](?:[\\dA-Za-z-]{0,61}[\\dA-Za-z])?)*$/;\n\n/**\n * Configuration for string validation.\n * @category Types\n */\nexport interface StringConfig extends ConfigBase<string> {\n /** Apply trim to string before validation. */\n trim?: boolean;\n}\n\n/**\n * Parses a value into a string.\n * @param value Value to parse\n * @category Parsers\n */\nexport function parseString(value: unknown): string | null | undefined {\n if (value === null || value === undefined) {\n return value;\n }\n\n if (typeof value === 'string') {\n return value;\n }\n\n if (value instanceof Date) {\n return value.toISOString();\n }\n\n if (isObject(value)) {\n throw new TypeError('Failed to parse value to string.');\n }\n\n // oxlint-disable-next-line typescript/no-base-to-string -- only plain objects throw above; #484 decides other non-primitives\n return String(value);\n}\n\n/**\n * Applies a configuration to the string value.\n * @param value Value to modify\n * @param config Configuration to apply\n * @category Helpers\n */\nexport function applyStringConfig(\n value: unknown,\n config: StringConfig,\n): string | null | undefined {\n let parsedValue = config.parser(value);\n if (\n config.default !== undefined &&\n (parsedValue === undefined || parsedValue === null || parsedValue === '')\n ) {\n parsedValue = config.default;\n }\n\n if (config.trim && typeof parsedValue === 'string') {\n parsedValue = parsedValue.trim();\n }\n\n return parsedValue;\n}\n\nexport interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<string> {\n pattern: RegExp;\n}\n\n/**\n * Ensures a string value matches a pattern\n * @param pattern Pattern to match\n * @param message Error message\n * @category Validation Tests\n */\nexport function matches(\n pattern: RegExp,\n message: ValidatorMessage<string, MatchesValidatorMessageParameters>,\n): ValidatorTest<string> {\n return createValidatorTest(\n (value) =>\n value === undefined ||\n value === null ||\n value === '' ||\n (typeof value === 'string' && pattern.test(value)),\n message,\n () => ({ pattern }),\n );\n}\n\n/**\n * Ensures a string value matches an email pattern\n * @param message Error message\n * @category Validation Tests\n */\nexport function email(\n message: ValidatorMessage<string>,\n): ValidatorTest<string> {\n return matches(emailPattern, message);\n}\n\n/**\n * Ensures a string value is a valid URL\n * @param message Error message\n * @param protocols Optional list of protocols to allow\n * @category Validation Tests\n */\nexport function url(\n message: ValidatorMessage<string>,\n protocols?: string[],\n): ValidatorTest<string> {\n return createValidatorTest(\n (value) => {\n try {\n let url: URL | undefined;\n if (\n value === undefined ||\n value === null ||\n value === '' ||\n (typeof value === 'string' && (url = new URL(value)))\n ) {\n return !(url && protocols && !protocols.includes(url.protocol));\n }\n } catch {\n // Do nothing\n }\n\n return false;\n },\n message,\n () => ({ protocols }),\n );\n}\n\n/**\n * Validates a string value.\n * @category Type Validators\n */\nexport const string = createTypeValidatorTest(\n {\n parser: parseString,\n },\n applyStringConfig,\n);\n"],"mappings":"AAqHO,SAASA,EAASC,EAAyB,CAChD,OAAO,OAAO,UAAU,SAAS,KAAKA,CAAK,IAAM,iBACnD,CAOO,SAASC,EACdC,EACAC,EACS,CACT,OAAO,OAAO,UAAU,eAAe,KAAKD,EAAQC,CAAQ,CAC9D,CAGA,IAAMC,EAAgB,WAQf,SAASC,EACdC,EACAC,EACQ,CACR,OAAI,OAAOD,GAAa,SACfA,EAAS,WAAWF,EAAe,CAACI,EAAGC,IAC5C,OAAQF,EAAuCE,CAAG,CAAC,CACrD,EAGKH,EAASC,CAAU,CAC5B,CAQA,eAAsBG,EAAoB,CACxC,MAAAV,EACA,MAAAW,CACF,EAGmC,CACjC,MAAO,CACL,QAAS,GACT,MAAO,QACP,MAAAX,EACA,MAAAW,CACF,CACF,CAUA,eAAsBC,EAIpB,CACA,OAAAC,EACA,MAAAF,EACA,QAAAG,EACA,MAAAd,EACA,OAAAe,CACF,EAMmC,CACjC,MAAO,CACL,QAAS,GACT,MAAO,UACP,QAASV,EAAcS,EAAS,CAAE,GAAGC,EAAQ,MAAAf,EAAO,MAAAW,CAAM,CAAM,EAChE,MAAAX,EACA,MAAAW,EACA,OAAAE,CACF,CACF,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DO,EACA,KAAO,CAAE,QAAAD,CAAQ,EACnB,CACF,CAOO,SAASG,GACdF,EACuB,CACvB,OAAOF,EAAQP,GAAcS,CAAO,CACtC,CAQO,SAASG,GACdH,EACAI,EACuB,CACvB,OAAOH,EACJR,GAAU,CACT,GAAI,CACF,IAAIU,EACJ,GAEEV,GAAU,MACVA,IAAU,IACT,OAAOA,GAAU,WAAaU,EAAM,IAAI,IAAIV,CAAK,GAElD,MAAO,EAAEU,GAAOC,GAAa,CAACA,EAAU,SAASD,EAAI,QAAQ,EAEjE,MAAQ,CAER,CAEA,MAAO,EACT,EACAH,EACA,KAAO,CAAE,UAAAI,CAAU,EACrB,CACF,CAMO,IAAMC,GAASC,EACpB,CACE,OAAQd,CACV,EACAG,CACF","names":["isObject","value","hasOwnProperty","object","property","formatPattern","formatMessage","template","parameters","_","key","valid","field","invalid","errors","message","extras","createTypeValidatorTest","defaultConfig","applyConfig","config","tests","cache","finalConfig","allTests","parsedValue","parsedValueString","validatorTest","result","error","createValidatorTest","test","getExtras","required","nullable","getAmount","max","limit","exclusive","amount","min","exact","oneOf","values","x","parseArray","value","iterable","applyArrayConfig","config","parsedValue","array","tests","allTests","finalConfig","field","arrayValue","arrayInvalidResult","isValid","errors","validateItem","arrayTests","validatorTest","result","index","item","valid","invalid","parseBoolean","value","applyBooleanConfig","config","parsedValue","boolean","createTypeValidatorTest","invalidDate","isoDatePattern","timezonePattern","parseTimezone","timezone","parts","sign","hour","minute","multiplier","offset","BASE","dateParts","part","parseDate","value","year","month","day","second","millisecond","parse","index","minuteOffset","applyDateConfig","config","parsedValue","minDate","limit","message","exclusive","parsedDate","createValidatorTest","maxDate","date","createTypeValidatorTest","parseNumber","value","applyNumberConfig","config","parsedValue","integer","message","field","valid","invalid","number","createTypeValidatorTest","object","properties","values","field","definedValues","errors","resolvedValues","isValid","key","hasOwnProperty","validator","value","result","invalid","valid","emailPattern","parseString","value","isObject","applyStringConfig","config","parsedValue","matches","pattern","message","createValidatorTest","email","url","protocols","string","createTypeValidatorTest"]}
|