validator-fns 1.10.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts DELETED
@@ -1,409 +0,0 @@
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;
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;
28
- /** Extra error details */
29
- errors?: E;
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;
131
- extras?: Except<P, 'field' | 'value'>;
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) => Except<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;
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;
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;
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;
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 { 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, 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 };