validator-fns 2.1.0 → 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 +97 -15
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +259 -118
- package/dist/index.d.ts +259 -118
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
package/dist/index.d.ts
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,14 +125,15 @@ declare function valid<T, E = never>({ value, field, }: {
|
|
|
126
125
|
* @param result.extras Extra message params
|
|
127
126
|
* @category Helpers
|
|
128
127
|
*/
|
|
129
|
-
declare function invalid<T, E, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(
|
|
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
|
-
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/arrays.d.ts
|
|
137
137
|
/**
|
|
138
138
|
* Invalid validation result for an item in the array
|
|
139
139
|
* @category Types
|
|
@@ -149,34 +149,35 @@ type ArrayConfig = ConfigBase<unknown[]>;
|
|
|
149
149
|
* @param value Value to parse
|
|
150
150
|
* @category Parsers
|
|
151
151
|
*/
|
|
152
|
-
declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
152
|
+
export declare function parseArray(value: unknown): unknown[] | null | undefined;
|
|
153
153
|
/**
|
|
154
154
|
* Applies a configuration to the array value.
|
|
155
155
|
* @param value Value to modify
|
|
156
156
|
* @param config Configuration to apply
|
|
157
157
|
* @category Helpers
|
|
158
158
|
*/
|
|
159
|
-
declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
159
|
+
export declare function applyArrayConfig<T>(config: ArrayConfig, value: unknown): T[] | null | undefined;
|
|
160
160
|
/**
|
|
161
161
|
* Validates an array. The first passed validator is used to validate individual
|
|
162
162
|
* items. The remaining validators target the array itself.
|
|
163
163
|
* @category Type Validators
|
|
164
164
|
*/
|
|
165
|
-
declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]>;
|
|
166
|
-
|
|
165
|
+
export declare function array<T, E>(config?: Partial<ArrayConfig> | ValidatorTest<T, E>, ...tests: ValidatorTest[]): ValidatorTest<T[], string | ArrayItemValidatorResult<E>[]>;
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/booleans.d.ts
|
|
167
168
|
/**
|
|
168
169
|
* Parses a value into a boolean.
|
|
169
170
|
* @param value Value to parse
|
|
170
171
|
* @category Parsers
|
|
171
172
|
*/
|
|
172
|
-
declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
173
|
+
export declare function parseBoolean(value: unknown): boolean | null | undefined;
|
|
173
174
|
/**
|
|
174
175
|
* Applies a configuration to the boolean value.
|
|
175
176
|
* @param value Value to modify
|
|
176
177
|
* @param config Configuration to apply
|
|
177
178
|
* @category Helpers
|
|
178
179
|
*/
|
|
179
|
-
declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
180
|
+
export declare function applyBooleanConfig(value: unknown, config: BooleanConfig): boolean | null | undefined;
|
|
180
181
|
/**
|
|
181
182
|
* Configuration for boolean validation.
|
|
182
183
|
* @category Types
|
|
@@ -186,20 +187,21 @@ type BooleanConfig = ConfigBase<boolean>;
|
|
|
186
187
|
* Validates a boolean value.
|
|
187
188
|
* @category Type Validators
|
|
188
189
|
*/
|
|
189
|
-
declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
190
|
-
|
|
190
|
+
export declare const boolean: ValidatorFactory<BooleanConfig, boolean, string>;
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/dates.d.ts
|
|
191
193
|
/**
|
|
192
194
|
* Applies a configuration to the date value.
|
|
193
195
|
* @param value Value to modify
|
|
194
196
|
* @param config Configuration to apply
|
|
195
197
|
* @category Helpers
|
|
196
198
|
*/
|
|
197
|
-
declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
199
|
+
export declare function applyDateConfig(value: unknown, config: DateConfig): Date | null | undefined;
|
|
198
200
|
type SharedDateValueType = Date | string | number;
|
|
199
201
|
interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
202
|
+
min: Date | null | undefined;
|
|
203
|
+
limit: Date | null | undefined;
|
|
204
|
+
exclusive?: boolean | undefined;
|
|
203
205
|
}
|
|
204
206
|
/**
|
|
205
207
|
* Ensures a date value is on or after a date.
|
|
@@ -208,11 +210,11 @@ interface MinDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
208
210
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
209
211
|
* @category Validation Tests
|
|
210
212
|
*/
|
|
211
|
-
declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, null>;
|
|
213
|
+
export declare function minDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MinDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date, null>;
|
|
212
214
|
interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<Date> {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
215
|
+
max: Date | null | undefined;
|
|
216
|
+
limit: Date | null | undefined;
|
|
217
|
+
exclusive?: boolean | undefined;
|
|
216
218
|
}
|
|
217
219
|
/**
|
|
218
220
|
* Ensures a date value is on or before a date.
|
|
@@ -221,7 +223,7 @@ interface MaxDateValidatorMessageParameters extends ValidatorMessageParameters<D
|
|
|
221
223
|
* @param exclusive Makes comparison exclusive instead of inclusive
|
|
222
224
|
* @category Validation Tests
|
|
223
225
|
*/
|
|
224
|
-
declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date>;
|
|
226
|
+
export declare function maxDate(limit: SharedDateValueType, message: ValidatorMessage<Date, MaxDateValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<Date>;
|
|
225
227
|
/**
|
|
226
228
|
* Configuration for date validation.
|
|
227
229
|
* @category Types
|
|
@@ -231,70 +233,206 @@ type DateConfig = ConfigBase<Date>;
|
|
|
231
233
|
* Validates a date value.
|
|
232
234
|
* @category Type Validators
|
|
233
235
|
*/
|
|
234
|
-
declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
235
|
-
|
|
236
|
+
export declare const date: ValidatorFactory<DateConfig, Date, string>;
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/numbers.d.ts
|
|
236
239
|
/**
|
|
237
240
|
* Parses a value into a number.
|
|
238
241
|
* @param value Value to parse
|
|
239
242
|
* @category Parsers
|
|
240
243
|
*/
|
|
241
|
-
declare function parseNumber(value: unknown): number | null | undefined;
|
|
244
|
+
export declare function parseNumber(value: unknown): number | null | undefined;
|
|
242
245
|
/**
|
|
243
246
|
* Applies a configuration to the number value.
|
|
244
247
|
* @param value Value to modify
|
|
245
248
|
* @param config Configuration to apply
|
|
246
249
|
* @category Helpers
|
|
247
250
|
*/
|
|
248
|
-
declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
251
|
+
export declare function applyNumberConfig(value: unknown, config: NumberConfig): number | null | undefined;
|
|
249
252
|
/**
|
|
250
253
|
* Ensures a number value is an integer.
|
|
251
254
|
* @param message Error message
|
|
252
255
|
* @category Validation Tests
|
|
253
256
|
*/
|
|
254
|
-
declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
257
|
+
export declare function integer(message: ValidatorMessage<number>): ValidatorTest<number>;
|
|
255
258
|
/**
|
|
256
259
|
* Configuration for number validation.
|
|
257
260
|
* @category Types
|
|
258
261
|
*/
|
|
259
262
|
interface NumberConfig extends ConfigBase<number> {
|
|
260
|
-
|
|
261
|
-
|
|
263
|
+
/** Apply rounding mechanism before validation. */
|
|
264
|
+
round?: 'nearest' | 'floor' | 'ceil';
|
|
262
265
|
}
|
|
263
266
|
/**
|
|
264
267
|
* Validates a number value.
|
|
265
268
|
* @category Type Validators
|
|
266
269
|
*/
|
|
267
|
-
declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
268
|
-
|
|
270
|
+
export declare const number: ValidatorFactory<NumberConfig, number, string>;
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/parse-date.d.ts
|
|
269
273
|
/** Badly formatted date for internal use. */
|
|
270
|
-
declare const invalidDate: Date;
|
|
274
|
+
export declare const invalidDate: Date;
|
|
271
275
|
/**
|
|
272
276
|
* Parses a value into a date.
|
|
273
277
|
* @param value Value to parse
|
|
274
278
|
* @category Parsers
|
|
275
279
|
*/
|
|
276
|
-
declare function parseDate(value: unknown): Date | null | undefined;
|
|
277
|
-
|
|
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
|
|
278
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]>; };
|
|
279
300
|
/**
|
|
280
|
-
* 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.
|
|
281
303
|
* @category Type Validators
|
|
282
304
|
*/
|
|
283
|
-
declare function object<P extends ObjectParameter, V extends
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
|
289
427
|
/**
|
|
290
428
|
* Ensures a value is not undefined, null, empty string, NaN, nor invalid date.
|
|
291
429
|
* @param message Error message
|
|
292
430
|
* @param nullable Allow null values
|
|
293
431
|
* @category Validation Tests
|
|
294
432
|
*/
|
|
295
|
-
declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
433
|
+
export declare function required<T, P extends ValidatorMessageParameters<T>>(message: ValidatorMessage<T, P>, nullable?: boolean): ValidatorTest<T>;
|
|
296
434
|
interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<T> {
|
|
297
|
-
|
|
435
|
+
values: readonly T[];
|
|
298
436
|
}
|
|
299
437
|
/**
|
|
300
438
|
* Ensures a value is one in a predefined list.
|
|
@@ -302,8 +440,9 @@ interface OneOfValidatorMessageParameters<T> extends ValidatorMessageParameters<
|
|
|
302
440
|
* @param message Error message
|
|
303
441
|
* @category Validation Tests
|
|
304
442
|
*/
|
|
305
|
-
declare function oneOf<T extends string | number | boolean | Date>(values: readonly T[], message: ValidatorMessage<T, OneOfValidatorMessageParameters<T>>): ValidatorTest<T>;
|
|
306
|
-
|
|
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
|
|
307
446
|
/**
|
|
308
447
|
* Creates a type validator.
|
|
309
448
|
* @typeParam T The basic type for the final value to be validated.
|
|
@@ -312,21 +451,22 @@ declare function oneOf<T extends string | number | boolean | Date>(values: reado
|
|
|
312
451
|
* @param applyConfig Function that applies configuration to value.
|
|
313
452
|
* @category Helpers
|
|
314
453
|
*/
|
|
315
|
-
declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
454
|
+
export declare function createTypeValidatorTest<T, C extends ConfigBase<T>>(defaultConfig: C, applyConfig: (value: unknown, config: C) => T | null | undefined): ValidatorFactory<C, T, string>;
|
|
316
455
|
/**
|
|
317
456
|
* Captures basic validator test pattern
|
|
318
457
|
* @param test Test function
|
|
319
458
|
* @param message Error message
|
|
320
459
|
* @param getExtras Optional extra fields for error message
|
|
321
460
|
*/
|
|
322
|
-
declare function createValidatorTest<T = any, E = any, P extends ValidatorMessageParameters<T> = ValidatorMessageParameters<T>>(test: (value: unknown, field?: string) => boolean, message: ValidatorMessage<T, P>, getExtras?: (value: T | null | undefined) => Omit<P, 'value' | 'field'>): ValidatorTest<T, E>;
|
|
323
|
-
|
|
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
|
|
324
464
|
type SharedValueType = string | number | unknown[];
|
|
325
465
|
interface MaxValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
466
|
+
max: number;
|
|
467
|
+
amount: number | null | undefined;
|
|
468
|
+
limit: number;
|
|
469
|
+
exclusive?: boolean | undefined;
|
|
330
470
|
}
|
|
331
471
|
/**
|
|
332
472
|
* Ensures a string, number, or array value is at most a certain amount.
|
|
@@ -335,12 +475,12 @@ interface MaxValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
335
475
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
336
476
|
* @category Validation Tests
|
|
337
477
|
*/
|
|
338
|
-
declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
478
|
+
export declare function max(limit: number, message: ValidatorMessage<SharedValueType, MaxValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
339
479
|
interface MinValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
480
|
+
min: number;
|
|
481
|
+
limit: number;
|
|
482
|
+
amount: number | null | undefined;
|
|
483
|
+
exclusive?: boolean | undefined;
|
|
344
484
|
}
|
|
345
485
|
/**
|
|
346
486
|
* Ensures a string, number, or array value is at most a certain amount.
|
|
@@ -349,11 +489,11 @@ interface MinValidatorMessageParameters extends ValidatorMessageParameters<Share
|
|
|
349
489
|
* @param exclusive Use exclusive comparison instead of inclusive
|
|
350
490
|
* @category Validation Tests
|
|
351
491
|
*/
|
|
352
|
-
declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
492
|
+
export declare function min(limit: number, message: ValidatorMessage<SharedValueType, MinValidatorMessageParameters>, exclusive?: boolean): ValidatorTest<SharedValueType>;
|
|
353
493
|
interface ExactValidatorMessageParameters extends ValidatorMessageParameters<SharedValueType> {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
494
|
+
limit: number;
|
|
495
|
+
amount: number | null | undefined;
|
|
496
|
+
exact: number;
|
|
357
497
|
}
|
|
358
498
|
/**
|
|
359
499
|
* Ensures a string, number, or array value is exactly a certain amount.
|
|
@@ -361,31 +501,32 @@ interface ExactValidatorMessageParameters extends ValidatorMessageParameters<Sha
|
|
|
361
501
|
* @param message Error message
|
|
362
502
|
* @category Validation Tests
|
|
363
503
|
*/
|
|
364
|
-
declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
365
|
-
|
|
504
|
+
export declare function exact(limit: number, message: ValidatorMessage<SharedValueType, ExactValidatorMessageParameters>): ValidatorTest<SharedValueType>;
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/strings.d.ts
|
|
366
507
|
/**
|
|
367
508
|
* Configuration for string validation.
|
|
368
509
|
* @category Types
|
|
369
510
|
*/
|
|
370
511
|
interface StringConfig extends ConfigBase<string> {
|
|
371
|
-
|
|
372
|
-
|
|
512
|
+
/** Apply trim to string before validation. */
|
|
513
|
+
trim?: boolean;
|
|
373
514
|
}
|
|
374
515
|
/**
|
|
375
516
|
* Parses a value into a string.
|
|
376
517
|
* @param value Value to parse
|
|
377
518
|
* @category Parsers
|
|
378
519
|
*/
|
|
379
|
-
declare function parseString(value: unknown): string | null | undefined;
|
|
520
|
+
export declare function parseString(value: unknown): string | null | undefined;
|
|
380
521
|
/**
|
|
381
522
|
* Applies a configuration to the string value.
|
|
382
523
|
* @param value Value to modify
|
|
383
524
|
* @param config Configuration to apply
|
|
384
525
|
* @category Helpers
|
|
385
526
|
*/
|
|
386
|
-
declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
527
|
+
export declare function applyStringConfig(value: unknown, config: StringConfig): string | null | undefined;
|
|
387
528
|
interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<string> {
|
|
388
|
-
|
|
529
|
+
pattern: RegExp;
|
|
389
530
|
}
|
|
390
531
|
/**
|
|
391
532
|
* Ensures a string value matches a pattern
|
|
@@ -393,20 +534,20 @@ interface MatchesValidatorMessageParameters extends ValidatorMessageParameters<s
|
|
|
393
534
|
* @param message Error message
|
|
394
535
|
* @category Validation Tests
|
|
395
536
|
*/
|
|
396
|
-
declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
537
|
+
export declare function matches(pattern: RegExp, message: ValidatorMessage<string, MatchesValidatorMessageParameters>): ValidatorTest<string>;
|
|
397
538
|
/**
|
|
398
539
|
* Ensures a string value matches an email pattern
|
|
399
540
|
* @param message Error message
|
|
400
541
|
* @category Validation Tests
|
|
401
542
|
*/
|
|
402
|
-
declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
543
|
+
export declare function email(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
403
544
|
/**
|
|
404
545
|
* Ensures a string value is an RFC 9562 UUID, any version from 1 to 8, or the
|
|
405
546
|
* nil or max UUID. Case-insensitive, without braces or a `urn:uuid:` prefix.
|
|
406
547
|
* @param message Error message
|
|
407
548
|
* @category Validation Tests
|
|
408
549
|
*/
|
|
409
|
-
declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
550
|
+
export declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
410
551
|
/**
|
|
411
552
|
* Ensures a string value is a valid URL
|
|
412
553
|
* @param message Error message
|
|
@@ -414,11 +555,11 @@ declare function uuid(message: ValidatorMessage<string>): ValidatorTest<string>;
|
|
|
414
555
|
* and `data:` URLs are valid; use `['http:', 'https:']` for values rendered as links.
|
|
415
556
|
* @category Validation Tests
|
|
416
557
|
*/
|
|
417
|
-
declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
558
|
+
export declare function url(message: ValidatorMessage<string>, protocols?: string[]): ValidatorTest<string>;
|
|
418
559
|
/**
|
|
419
560
|
* Validates a string value.
|
|
420
561
|
* @category Type Validators
|
|
421
562
|
*/
|
|
422
|
-
declare const string: ValidatorFactory<StringConfig, string, string>;
|
|
423
|
-
|
|
424
|
-
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 };
|