validator-fns 2.0.5 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -16
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +326 -174
- package/dist/index.d.ts +326 -174
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +24 -14
package/dist/index.d.cts
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
|
+
//#region src/shared.d.ts
|
|
1
2
|
type ValidResult<T> = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
/**
|
|
4
|
+
* True if the original value is valid according to all validation tests
|
|
5
|
+
* @deprecated Use `state` instead.
|
|
6
|
+
*/
|
|
7
|
+
isValid: true;
|
|
8
|
+
/** This is preferred to use over `isValid` as it works better for type guards. */
|
|
9
|
+
state: 'valid';
|
|
10
|
+
/** The parsed value. Note that may be different than the original value. */
|
|
11
|
+
value: T | null | undefined;
|
|
12
|
+
/** Field name if provided. Automatically provided if using object validation. */
|
|
13
|
+
field?: string | undefined;
|
|
13
14
|
};
|
|
14
15
|
type InvalidResult<E> = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
/**
|
|
17
|
+
* True if the original value is valid according to all validation tests.
|
|
18
|
+
* @deprecated Use `state` instead.
|
|
19
|
+
*/
|
|
20
|
+
isValid: false;
|
|
21
|
+
/** This is preferred to use over `isValid` as it works better for type guards. */
|
|
22
|
+
state: 'invalid';
|
|
23
|
+
/** The parsed value. Note that may be different than the original value. */
|
|
24
|
+
value: unknown;
|
|
25
|
+
/** Error message if `state` is invalid. */
|
|
26
|
+
message: string;
|
|
27
|
+
/** Field name if provided. Automatically provided if using object validation. */
|
|
28
|
+
field?: string | undefined;
|
|
29
|
+
/** Extra error details */
|
|
30
|
+
errors?: E | undefined;
|
|
30
31
|
};
|
|
31
32
|
/**
|
|
32
33
|
* The result of a validation.
|
|
@@ -57,9 +58,7 @@ 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
|
|
@@ -72,14 +71,14 @@ type ValidatorFactory<C, T, E> = (config?: Partial<C> | ValidatorTest, ...tests:
|
|
|
72
71
|
* @category Types
|
|
73
72
|
*/
|
|
74
73
|
interface ConfigBase<T> {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
/** Parser to convert value into the designated type. */
|
|
75
|
+
parser: (value: unknown) => T | null | undefined;
|
|
76
|
+
/** Provide a fallback value in case the original value is undefined. */
|
|
77
|
+
default?: T;
|
|
79
78
|
}
|
|
80
79
|
interface ValidatorMessageParameters<T> {
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
value: T | null | undefined;
|
|
81
|
+
field: string | undefined;
|
|
83
82
|
}
|
|
84
83
|
/**
|
|
85
84
|
* Validation message. May be a plain string or a function that accepts arguments.
|
|
@@ -91,20 +90,20 @@ type ValidatorMessage<T, P extends ValidatorMessageParameters<T> = ValidatorMess
|
|
|
91
90
|
* Simple check for objects.
|
|
92
91
|
* @param value Value to test
|
|
93
92
|
*/
|
|
94
|
-
declare function isObject(value: unknown): boolean;
|
|
93
|
+
export declare function isObject(value: unknown): boolean;
|
|
95
94
|
/**
|
|
96
95
|
* Safe hasOwnProperty check
|
|
97
96
|
* @param object Object to check
|
|
98
97
|
* @param property Property to look for
|
|
99
98
|
*/
|
|
100
|
-
declare function hasOwnProperty(object: unknown, property: string | number | symbol): boolean;
|
|
99
|
+
export declare function hasOwnProperty(object: unknown, property: string | number | symbol): boolean;
|
|
101
100
|
/**
|
|
102
101
|
* Formats a message using a squiggly bracket `{}` template.
|
|
103
102
|
* @param template Message template as string or function
|
|
104
103
|
* @param parameters Params to inject into template
|
|
105
104
|
* @category Helpers
|
|
106
105
|
*/
|
|
107
|
-
declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
|
|
106
|
+
export declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(template: ValidatorMessage<T, P>, parameters: P): string;
|
|
108
107
|
/**
|
|
109
108
|
* Creates a valid result
|
|
110
109
|
* @param result Result fields
|
|
@@ -112,9 +111,9 @@ declare function formatMessage<T, P extends ValidatorMessageParameters<T>>(templ
|
|
|
112
111
|
* @param result.field Field name
|
|
113
112
|
* @category Helpers
|
|
114
113
|
*/
|
|
115
|
-
declare function valid<T, E = never>(
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
export declare function valid<T, E = never>(result: {
|
|
115
|
+
value: T | null | undefined;
|
|
116
|
+
field: string | undefined;
|
|
118
117
|
}): Promise<ValidatorResult<T, E>>;
|
|
119
118
|
/**
|
|
120
119
|
* Creates an invalid result.
|
|
@@ -126,88 +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>>(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
128
|
+
export declare function invalid<T, E, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(result: {
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
* @typeParam T The basic type for the final value to be validated.
|
|
139
|
-
* @typeParam C The configuration type.
|
|
140
|
-
* @param defaultParser Parser to convert value into type.
|
|
141
|
-
* @param applyConfig Function that applies configuration to value.
|
|
142
|
-
* @category Helpers
|
|
143
|
-
*/
|
|
144
|
-
declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
145
|
-
/**
|
|
146
|
-
* Captures basic validator test pattern
|
|
147
|
-
* @param test Test function
|
|
148
|
-
* @param message Error message
|
|
149
|
-
* @param getExtras Optional extra fields for error message
|
|
150
|
-
*/
|
|
151
|
-
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>;
|
|
152
|
-
/**
|
|
153
|
-
* Ensures a value is not undefined, null, empty string, NaN, nor invalid date.
|
|
154
|
-
* @param message Error message
|
|
155
|
-
* @param nullable Allow null values
|
|
156
|
-
* @category Validation Tests
|
|
157
|
-
*/
|
|
158
|
-
declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
159
|
-
type SharedValueType = string | number | unknown[];
|
|
160
|
-
interface MaxValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
161
|
-
max: number;
|
|
162
|
-
amount: number | null | undefined;
|
|
163
|
-
limit: number;
|
|
164
|
-
exclusive?: boolean | undefined;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Ensures a string, number, or array value is at most a certain amount.
|
|
168
|
-
* @param limit Size or value limit
|
|
169
|
-
* @param message Error message
|
|
170
|
-
* @param exclusive Use exclusive comparison instead of inclusive
|
|
171
|
-
* @category Validation Tests
|
|
172
|
-
*/
|
|
173
|
-
declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
174
|
-
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
175
|
-
min: number;
|
|
176
|
-
limit: number;
|
|
177
|
-
amount: number | null | undefined;
|
|
178
|
-
exclusive?: boolean | undefined;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Ensures a string, number, or array value is at most a certain amount.
|
|
182
|
-
* @param limit Size or value limit
|
|
183
|
-
* @param message Error message
|
|
184
|
-
* @param exclusive Use exclusive comparison instead of inclusive
|
|
185
|
-
* @category Validation Tests
|
|
186
|
-
*/
|
|
187
|
-
declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
188
|
-
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
189
|
-
limit: number;
|
|
190
|
-
amount: number | null | undefined;
|
|
191
|
-
exact: number;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Ensures a string, number, or array value is exactly a certain amount.
|
|
195
|
-
* @param limit Size or value limit
|
|
196
|
-
* @param message Error message
|
|
197
|
-
* @category Validation Tests
|
|
198
|
-
*/
|
|
199
|
-
declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
200
|
-
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
201
|
-
values: readonly T[];
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Ensures a value is one in a predefined list.
|
|
205
|
-
* @param values List of allowed values.
|
|
206
|
-
* @param message Error message
|
|
207
|
-
* @category Validation Tests
|
|
208
|
-
*/
|
|
209
|
-
declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
210
|
-
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/arrays.d.ts
|
|
211
137
|
/**
|
|
212
138
|
* Invalid validation result for an item in the array
|
|
213
139
|
* @category Types
|
|
@@ -223,34 +149,35 @@ type ArrayConfig = ConfigBase<unknown[]>;
|
|
|
223
149
|
* @param value Value to parse
|
|
224
150
|
* @category Parsers
|
|
225
151
|
*/
|
|
226
|
-
declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
152
|
+
export declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
227
153
|
/**
|
|
228
154
|
* Applies a configuration to the array value.
|
|
229
155
|
* @param value Value to modify
|
|
230
156
|
* @param config Configuration to apply
|
|
231
157
|
* @category Helpers
|
|
232
158
|
*/
|
|
233
|
-
declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
159
|
+
export declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
234
160
|
/**
|
|
235
161
|
* Validates an array. The first passed validator is used to validate individual
|
|
236
162
|
* items. The remaining validators target the array itself.
|
|
237
163
|
* @category Type Validators
|
|
238
164
|
*/
|
|
239
|
-
declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]>;
|
|
240
|
-
|
|
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
|
|
241
168
|
/**
|
|
242
169
|
* Parses a value into a boolean.
|
|
243
170
|
* @param value Value to parse
|
|
244
171
|
* @category Parsers
|
|
245
172
|
*/
|
|
246
|
-
declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
173
|
+
export declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
247
174
|
/**
|
|
248
175
|
* Applies a configuration to the boolean value.
|
|
249
176
|
* @param value Value to modify
|
|
250
177
|
* @param config Configuration to apply
|
|
251
178
|
* @category Helpers
|
|
252
179
|
*/
|
|
253
|
-
declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
180
|
+
export declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
254
181
|
/**
|
|
255
182
|
* Configuration for boolean validation.
|
|
256
183
|
* @category Types
|
|
@@ -260,28 +187,21 @@ type BooleanConfig = ConfigBase<boolean>;
|
|
|
260
187
|
* Validates a boolean value.
|
|
261
188
|
* @category Type Validators
|
|
262
189
|
*/
|
|
263
|
-
declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
declare const invalidDate: Date;
|
|
267
|
-
/**
|
|
268
|
-
* Parses a value into a date.
|
|
269
|
-
* @param value Value to parse
|
|
270
|
-
* @category Parsers
|
|
271
|
-
*/
|
|
272
|
-
declare function parseDate(value: unknown): Date | null | undefined;
|
|
190
|
+
export declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/dates.d.ts
|
|
273
193
|
/**
|
|
274
194
|
* Applies a configuration to the date value.
|
|
275
195
|
* @param value Value to modify
|
|
276
196
|
* @param config Configuration to apply
|
|
277
197
|
* @category Helpers
|
|
278
198
|
*/
|
|
279
|
-
declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
199
|
+
export declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
280
200
|
type SharedDateValueType = Date | string | number;
|
|
281
201
|
interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
202
|
+
min: Date | null | undefined;
|
|
203
|
+
limit: Date | null | undefined;
|
|
204
|
+
exclusive?: boolean | undefined;
|
|
285
205
|
}
|
|
286
206
|
/**
|
|
287
207
|
* Ensures a date value is on or after a date.
|
|
@@ -290,11 +210,11 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
290
210
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
291
211
|
* @category Validation Tests
|
|
292
212
|
*/
|
|
293
|
-
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, null>;
|
|
294
214
|
interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
215
|
+
max: Date | null | undefined;
|
|
216
|
+
limit: Date | null | undefined;
|
|
217
|
+
exclusive?: boolean | undefined;
|
|
298
218
|
}
|
|
299
219
|
/**
|
|
300
220
|
* Ensures a date value is on or before a date.
|
|
@@ -303,7 +223,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
303
223
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
304
224
|
* @category Validation Tests
|
|
305
225
|
*/
|
|
306
|
-
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>;
|
|
307
227
|
/**
|
|
308
228
|
* Configuration for date validation.
|
|
309
229
|
* @category Types
|
|
@@ -313,75 +233,300 @@ type DateConfig = ConfigBase<Date>;
|
|
|
313
233
|
* Validates a date value.
|
|
314
234
|
* @category Type Validators
|
|
315
235
|
*/
|
|
316
|
-
declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
317
|
-
|
|
236
|
+
export declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/numbers.d.ts
|
|
318
239
|
/**
|
|
319
240
|
* Parses a value into a number.
|
|
320
241
|
* @param value Value to parse
|
|
321
242
|
* @category Parsers
|
|
322
243
|
*/
|
|
323
|
-
declare function parseNumber(value: unknown): number | null | undefined;
|
|
244
|
+
export declare function parseNumber(value: unknown): number | null | undefined;
|
|
324
245
|
/**
|
|
325
246
|
* Applies a configuration to the number value.
|
|
326
247
|
* @param value Value to modify
|
|
327
248
|
* @param config Configuration to apply
|
|
328
249
|
* @category Helpers
|
|
329
250
|
*/
|
|
330
|
-
declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
251
|
+
export declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
331
252
|
/**
|
|
332
253
|
* Ensures a number value is an integer.
|
|
333
254
|
* @param message Error message
|
|
334
255
|
* @category Validation Tests
|
|
335
256
|
*/
|
|
336
|
-
declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
257
|
+
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
337
258
|
/**
|
|
338
259
|
* Configuration for number validation.
|
|
339
260
|
* @category Types
|
|
340
261
|
*/
|
|
341
262
|
interface NumberConfig extends ConfigBase<number> {
|
|
342
|
-
|
|
343
|
-
|
|
263
|
+
/** Apply rounding mechanism before validation. */
|
|
264
|
+
round?: 'nearest' | 'floor' | 'ceil';
|
|
344
265
|
}
|
|
345
266
|
/**
|
|
346
267
|
* Validates a number value.
|
|
347
268
|
* @category Type Validators
|
|
348
269
|
*/
|
|
349
|
-
declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
350
|
-
|
|
270
|
+
export declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/parse-date.d.ts
|
|
273
|
+
/** Badly formatted date for internal use. */
|
|
274
|
+
export declare const invalidDate: Date;
|
|
275
|
+
/**
|
|
276
|
+
* Parses a value into a date.
|
|
277
|
+
* @param value Value to parse
|
|
278
|
+
* @category Parsers
|
|
279
|
+
*/
|
|
280
|
+
export declare function parseDate(value: unknown): Date | null | undefined;
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/check.d.ts
|
|
283
|
+
/**
|
|
284
|
+
* An object-level test for `object()`. It fails when `test` returns false for
|
|
285
|
+
* the parsed object. The message goes to the error at `path`, a property name
|
|
286
|
+
* or a list of names into nested objects, or to the object's own message
|
|
287
|
+
* without a path. The message gets the parsed object as `value` and the
|
|
288
|
+
* object's `field`.
|
|
289
|
+
* @category Validation Tests
|
|
290
|
+
*/
|
|
291
|
+
export declare function check<T, E = never>(test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
|
|
292
|
+
export declare function check<T, E = never>(path: string | readonly string[], test: (value: T) => boolean, message: ValidatorMessage<T>): ValidatorTest<T, E>;
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/objects.d.ts
|
|
351
295
|
type ObjectParameter = Record<string, ValidatorTest<unknown, unknown>>;
|
|
296
|
+
/** @internal */
|
|
297
|
+
type ObjectValue<P extends ObjectParameter> = { [K in keyof P]?: ExtractValue<P[K]>; };
|
|
298
|
+
/** @internal */
|
|
299
|
+
type ObjectErrors<P extends ObjectParameter> = { [K in keyof P]?: ExtractError<P[K]>; };
|
|
352
300
|
/**
|
|
353
|
-
* Validates an object.
|
|
301
|
+
* Validates an object. The remaining `tests` run on the parsed object, in
|
|
302
|
+
* order, once every property is valid, and the first failure is the result.
|
|
354
303
|
* @category Type Validators
|
|
355
304
|
*/
|
|
356
|
-
declare function object<P extends ObjectParameter, V extends
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
305
|
+
export declare function object<P extends ObjectParameter, V extends ObjectValue<P>, E extends ObjectErrors<P>>(properties: P, ...tests: ValidatorTest<ObjectValue<P>, ObjectErrors<P>>[]): ValidatorTest<V, E>;
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/standard-schema.d.ts
|
|
308
|
+
/**
|
|
309
|
+
* @license
|
|
310
|
+
* The StandardTypedV1 and StandardSchemaV1 types below are copied from
|
|
311
|
+
* @standard-schema/spec 1.1.0 (https://standardschema.dev), which invites
|
|
312
|
+
* libraries to copy them instead of depending on the package. Two changes:
|
|
313
|
+
* `validate`'s optional `options` parameter drops its redundant `| undefined`,
|
|
314
|
+
* which lint rejects, with the same type; and only StandardSchemaV1 is
|
|
315
|
+
* exported, since nothing else imports StandardTypedV1. They are under its
|
|
316
|
+
* license:
|
|
317
|
+
*
|
|
318
|
+
* MIT License
|
|
319
|
+
*
|
|
320
|
+
* Copyright (c) 2024 Colin McDonnell
|
|
321
|
+
*
|
|
322
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
323
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
324
|
+
* in the Software without restriction, including without limitation the rights
|
|
325
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
326
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
327
|
+
* furnished to do so, subject to the following conditions:
|
|
328
|
+
*
|
|
329
|
+
* The above copyright notice and this permission notice shall be included in
|
|
330
|
+
* all copies or substantial portions of the Software.
|
|
331
|
+
*
|
|
332
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
333
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
334
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
335
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
336
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
337
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
338
|
+
* SOFTWARE.
|
|
339
|
+
*/
|
|
340
|
+
/** The Standard Typed interface. This is a base type extended by other specs. */
|
|
341
|
+
interface StandardTypedV1<Input = unknown, Output = Input> {
|
|
342
|
+
/** The Standard properties. */
|
|
343
|
+
readonly '~standard': StandardTypedV1.Props<Input, Output>;
|
|
344
|
+
}
|
|
345
|
+
declare namespace StandardTypedV1 {
|
|
346
|
+
/** The Standard Typed properties interface. */
|
|
347
|
+
interface Props<Input = unknown, Output = Input> {
|
|
348
|
+
/** The version number of the standard. */
|
|
349
|
+
readonly version: 1;
|
|
350
|
+
/** The vendor name of the schema library. */
|
|
351
|
+
readonly vendor: string;
|
|
352
|
+
/** Inferred types associated with the schema. */
|
|
353
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
354
|
+
}
|
|
355
|
+
/** The Standard Typed types interface. */
|
|
356
|
+
interface Types<Input = unknown, Output = Input> {
|
|
357
|
+
/** The input type of the schema. */
|
|
358
|
+
readonly input: Input;
|
|
359
|
+
/** The output type of the schema. */
|
|
360
|
+
readonly output: Output;
|
|
361
|
+
}
|
|
362
|
+
/** Infers the input type of a Standard Typed. */
|
|
363
|
+
type InferInput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['input'];
|
|
364
|
+
/** Infers the output type of a Standard Typed. */
|
|
365
|
+
type InferOutput<Schema extends StandardTypedV1> = NonNullable<Schema['~standard']['types']>['output'];
|
|
366
|
+
}
|
|
367
|
+
/** The Standard Schema interface. */
|
|
368
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
369
|
+
/** The Standard Schema properties. */
|
|
370
|
+
readonly '~standard': StandardSchemaV1.Props<Input, Output>;
|
|
371
|
+
}
|
|
372
|
+
declare namespace StandardSchemaV1 {
|
|
373
|
+
/** The Standard Schema properties interface. */
|
|
374
|
+
interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
|
|
375
|
+
/** Validates unknown input values. */
|
|
376
|
+
readonly validate: (value: unknown, options?: StandardSchemaV1.Options) => Result<Output> | Promise<Result<Output>>;
|
|
377
|
+
}
|
|
378
|
+
/** The result interface of the validate function. */
|
|
379
|
+
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
380
|
+
/** The result interface if validation succeeds. */
|
|
381
|
+
interface SuccessResult<Output> {
|
|
382
|
+
/** The typed output value. */
|
|
383
|
+
readonly value: Output;
|
|
384
|
+
/** A falsy value for `issues` indicates success. */
|
|
385
|
+
readonly issues?: undefined;
|
|
386
|
+
}
|
|
387
|
+
interface Options {
|
|
388
|
+
/** Explicit support for additional vendor-specific parameters, if needed. */
|
|
389
|
+
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
390
|
+
}
|
|
391
|
+
/** The result interface if validation fails. */
|
|
392
|
+
interface FailureResult {
|
|
393
|
+
/** The issues of failed validation. */
|
|
394
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
395
|
+
}
|
|
396
|
+
/** The issue interface of the failure output. */
|
|
397
|
+
interface Issue {
|
|
398
|
+
/** The error message of the issue. */
|
|
399
|
+
readonly message: string;
|
|
400
|
+
/** The path of the issue, if any. */
|
|
401
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
402
|
+
}
|
|
403
|
+
/** The path segment interface of the issue. */
|
|
404
|
+
interface PathSegment {
|
|
405
|
+
/** The key representing a path segment. */
|
|
406
|
+
readonly key: PropertyKey;
|
|
407
|
+
}
|
|
408
|
+
/** The Standard types interface. */
|
|
409
|
+
interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {}
|
|
410
|
+
/** Infers the input type of a Standard. */
|
|
411
|
+
type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
|
|
412
|
+
/** Infers the output type of a Standard. */
|
|
413
|
+
type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
|
|
414
|
+
}
|
|
415
|
+
//#endregion
|
|
416
|
+
//#region src/standard.d.ts
|
|
417
|
+
/**
|
|
418
|
+
* Adds the Standard Schema interface (https://standardschema.dev) to a
|
|
419
|
+
* validator, so libraries that accept any Standard Schema can use it. Returns a
|
|
420
|
+
* new validator; the one passed in is unchanged. `validate` always returns a
|
|
421
|
+
* promise, since every validator here is async.
|
|
422
|
+
* @category Helpers
|
|
423
|
+
*/
|
|
424
|
+
export declare function standard<T, E>(validator: ValidatorTest<T, E>): ValidatorTest<T, E> & StandardSchemaV1<unknown, T | null | undefined>;
|
|
425
|
+
//#endregion
|
|
426
|
+
//#region src/common.d.ts
|
|
427
|
+
/**
|
|
428
|
+
* Ensures a value is not undefined, null, empty string, NaN, nor invalid date.
|
|
429
|
+
* @param message Error message
|
|
430
|
+
* @param nullable Allow null values
|
|
431
|
+
* @category Validation Tests
|
|
432
|
+
*/
|
|
433
|
+
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
434
|
+
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
435
|
+
values: readonly T[];
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Ensures a value is one in a predefined list.
|
|
439
|
+
* @param values List of allowed values.
|
|
440
|
+
* @param message Error message
|
|
441
|
+
* @category Validation Tests
|
|
442
|
+
*/
|
|
443
|
+
export declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
444
|
+
//#endregion
|
|
445
|
+
//#region src/factories.d.ts
|
|
446
|
+
/**
|
|
447
|
+
* Creates a type validator.
|
|
448
|
+
* @typeParam T The basic type for the final value to be validated.
|
|
449
|
+
* @typeParam C The configuration type.
|
|
450
|
+
* @param defaultParser Parser to convert value into type.
|
|
451
|
+
* @param applyConfig Function that applies configuration to value.
|
|
452
|
+
* @category Helpers
|
|
453
|
+
*/
|
|
454
|
+
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
455
|
+
/**
|
|
456
|
+
* Captures basic validator test pattern
|
|
457
|
+
* @param test Test function
|
|
458
|
+
* @param message Error message
|
|
459
|
+
* @param getExtras Optional extra fields for error message
|
|
460
|
+
*/
|
|
461
|
+
export 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>;
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/limits.d.ts
|
|
464
|
+
type SharedValueType = string | number | unknown[];
|
|
465
|
+
interface MaxValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
466
|
+
max: number;
|
|
467
|
+
amount: number | null | undefined;
|
|
468
|
+
limit: number;
|
|
469
|
+
exclusive?: boolean | undefined;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Ensures a string, number, or array value is at most a certain amount.
|
|
473
|
+
* @param limit Size or value limit
|
|
474
|
+
* @param message Error message
|
|
475
|
+
* @param exclusive Use exclusive comparison instead of inclusive
|
|
476
|
+
* @category Validation Tests
|
|
477
|
+
*/
|
|
478
|
+
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
479
|
+
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
480
|
+
min: number;
|
|
481
|
+
limit: number;
|
|
482
|
+
amount: number | null | undefined;
|
|
483
|
+
exclusive?: boolean | undefined;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Ensures a string, number, or array value is at most a certain amount.
|
|
487
|
+
* @param limit Size or value limit
|
|
488
|
+
* @param message Error message
|
|
489
|
+
* @param exclusive Use exclusive comparison instead of inclusive
|
|
490
|
+
* @category Validation Tests
|
|
491
|
+
*/
|
|
492
|
+
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
493
|
+
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
494
|
+
limit: number;
|
|
495
|
+
amount: number | null | undefined;
|
|
496
|
+
exact: number;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Ensures a string, number, or array value is exactly a certain amount.
|
|
500
|
+
* @param limit Size or value limit
|
|
501
|
+
* @param message Error message
|
|
502
|
+
* @category Validation Tests
|
|
503
|
+
*/
|
|
504
|
+
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/strings.d.ts
|
|
362
507
|
/**
|
|
363
508
|
* Configuration for string validation.
|
|
364
509
|
* @category Types
|
|
365
510
|
*/
|
|
366
511
|
interface StringConfig extends ConfigBase<string> {
|
|
367
|
-
|
|
368
|
-
|
|
512
|
+
/** Apply trim to string before validation. */
|
|
513
|
+
trim?: boolean;
|
|
369
514
|
}
|
|
370
515
|
/**
|
|
371
516
|
* Parses a value into a string.
|
|
372
517
|
* @param value Value to parse
|
|
373
518
|
* @category Parsers
|
|
374
519
|
*/
|
|
375
|
-
declare function parseString(value: unknown): string | null | undefined;
|
|
520
|
+
export declare function parseString(value: unknown): string | null | undefined;
|
|
376
521
|
/**
|
|
377
522
|
* Applies a configuration to the string value.
|
|
378
523
|
* @param value Value to modify
|
|
379
524
|
* @param config Configuration to apply
|
|
380
525
|
* @category Helpers
|
|
381
526
|
*/
|
|
382
|
-
declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
527
|
+
export declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
383
528
|
interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<string> {
|
|
384
|
-
|
|
529
|
+
pattern: RegExp;
|
|
385
530
|
}
|
|
386
531
|
/**
|
|
387
532
|
* Ensures a string value matches a pattern
|
|
@@ -389,13 +534,20 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
|
|
|
389
534
|
* @param message Error message
|
|
390
535
|
* @category Validation Tests
|
|
391
536
|
*/
|
|
392
|
-
declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
537
|
+
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
393
538
|
/**
|
|
394
539
|
* Ensures a string value matches an email pattern
|
|
395
540
|
* @param message Error message
|
|
396
541
|
* @category Validation Tests
|
|
397
542
|
*/
|
|
398
|
-
declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
543
|
+
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
544
|
+
/**
|
|
545
|
+
* Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
|
|
546
|
+
* nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
|
|
547
|
+
* @param message Error message
|
|
548
|
+
* @category Validation Tests
|
|
549
|
+
*/
|
|
550
|
+
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
399
551
|
/**
|
|
400
552
|
* Ensures a string value is a valid URL
|
|
401
553
|
* @param message Error message
|
|
@@ -403,11 +555,11 @@ declare function email(message: ValidatorMessage<string>): ValidatorTest<string>
|
|
|
403
555
|
* and `data:` URLs are valid; use `['http:', 'https:']` for values rendered as links.
|
|
404
556
|
* @category Validation Tests
|
|
405
557
|
*/
|
|
406
|
-
declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
558
|
+
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
407
559
|
/**
|
|
408
560
|
* Validates a string value.
|
|
409
561
|
* @category Type Validators
|
|
410
562
|
*/
|
|
411
|
-
declare const string: ValidatorFactory<StringConfig, string, string>;
|
|
412
|
-
|
|
413
|
-
export {
|
|
563
|
+
export declare const string: ValidatorFactory<StringConfig, string, string>;
|
|
564
|
+
//#endregion
|
|
565
|
+
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 };
|