@oscarpalmer/jhunal 0.25.0 → 0.27.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/constants.d.mts +15 -7
- package/dist/constants.mjs +15 -6
- package/dist/handler/base.handler.d.mts +6 -0
- package/dist/{validator/base.validator.mjs → handler/base.handler.mjs} +5 -5
- package/dist/handler/function.handler.d.mts +6 -0
- package/dist/handler/function.handler.mjs +9 -0
- package/dist/handler/object.handler.d.mts +7 -0
- package/dist/handler/object.handler.mjs +130 -0
- package/dist/handler/schema.handler.d.mts +7 -0
- package/dist/handler/schema.handler.mjs +16 -0
- package/dist/handler/type.handler.d.mts +9 -0
- package/dist/handler/type.handler.mjs +71 -0
- package/dist/handler/validator.handler.d.mts +10 -0
- package/dist/handler/validator.handler.mjs +34 -0
- package/dist/handler/value.handler.d.mts +14 -0
- package/dist/handler/value.handler.mjs +98 -0
- package/dist/helpers/message.helper.d.mts +9 -7
- package/dist/helpers/message.helper.mjs +34 -16
- package/dist/helpers/misc.helper.d.mts +13 -6
- package/dist/helpers/misc.helper.mjs +12 -4
- package/dist/helpers/report.helper.d.mts +23 -0
- package/dist/helpers/report.helper.mjs +19 -0
- package/dist/helpers/result.helper.d.mts +7 -0
- package/dist/helpers/result.helper.mjs +17 -0
- package/dist/index.d.mts +172 -73
- package/dist/index.mjs +396 -235
- package/dist/models/infer.model.d.mts +11 -8
- package/dist/models/misc.model.d.mts +8 -8
- package/dist/models/schematic.plain.model.d.mts +10 -9
- package/dist/models/schematic.typed.model.d.mts +1 -1
- package/dist/models/transform.model.d.mts +2 -2
- package/dist/models/validation.model.d.mts +56 -25
- package/dist/models/validation.model.mjs +11 -2
- package/dist/schema.d.mts +34 -34
- package/dist/schema.mjs +11 -19
- package/dist/validator.d.mts +83 -0
- package/dist/validator.mjs +25 -0
- package/package.json +2 -2
- package/src/constants.ts +30 -8
- package/src/{validator/base.validator.ts → handler/base.handler.ts} +6 -6
- package/src/handler/function.handler.ts +9 -0
- package/src/handler/object.handler.ts +245 -0
- package/src/handler/schema.handler.ts +25 -0
- package/src/handler/type.handler.ts +160 -0
- package/src/handler/validator.handler.ts +49 -0
- package/src/handler/value.handler.ts +202 -0
- package/src/helpers/message.helper.ts +72 -30
- package/src/helpers/misc.helper.ts +23 -6
- package/src/helpers/report.helper.ts +72 -0
- package/src/helpers/result.helper.ts +33 -0
- package/src/index.ts +1 -0
- package/src/models/infer.model.ts +31 -13
- package/src/models/misc.model.ts +9 -9
- package/src/models/schematic.plain.model.ts +12 -9
- package/src/models/schematic.typed.model.ts +3 -3
- package/src/models/transform.model.ts +2 -2
- package/src/models/validation.model.ts +75 -37
- package/src/schema.ts +44 -70
- package/src/validator.ts +135 -0
- package/dist/validator/base.validator.d.mts +0 -6
- package/dist/validator/function.validator.d.mts +0 -6
- package/dist/validator/function.validator.mjs +0 -9
- package/dist/validator/named.handler.d.mts +0 -6
- package/dist/validator/named.handler.mjs +0 -23
- package/dist/validator/named.validator.d.mts +0 -7
- package/dist/validator/named.validator.mjs +0 -38
- package/dist/validator/object.validator.d.mts +0 -7
- package/dist/validator/object.validator.mjs +0 -185
- package/dist/validator/schematic.validator.d.mts +0 -7
- package/dist/validator/schematic.validator.mjs +0 -16
- package/src/validator/function.validator.ts +0 -9
- package/src/validator/named.handler.ts +0 -65
- package/src/validator/named.validator.ts +0 -61
- package/src/validator/object.validator.ts +0 -366
- package/src/validator/schematic.validator.ts +0 -25
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,86 @@
|
|
|
1
1
|
import { Constructor, GenericCallback, PlainObject, Simplify } from "@oscarpalmer/atoms/models";
|
|
2
2
|
import { Result } from "@oscarpalmer/atoms/result/models";
|
|
3
3
|
|
|
4
|
+
//#region src/validator.d.ts
|
|
5
|
+
declare class Validator<Value> {
|
|
6
|
+
#private;
|
|
7
|
+
private readonly $validator;
|
|
8
|
+
constructor(handler: ValidationHandler, types: ValidationHandlerType[]);
|
|
9
|
+
/**
|
|
10
|
+
* Is the value valid?
|
|
11
|
+
*
|
|
12
|
+
* Will assert that the value is valid and throws an error if it does not
|
|
13
|
+
* @param value Value to validate
|
|
14
|
+
* @returns `true` if the value is valid, otherwise throws an error
|
|
15
|
+
*/
|
|
16
|
+
is(value: unknown, reporting: 'throw'): asserts value is Value;
|
|
17
|
+
/**
|
|
18
|
+
* Is the value valid?
|
|
19
|
+
*
|
|
20
|
+
* Will validate that the value is valid and return a result of `true` or validation information for the first validation failure
|
|
21
|
+
* @param value Value to validate
|
|
22
|
+
* @return Result holding `true` or validation information
|
|
23
|
+
*/
|
|
24
|
+
is(value: unknown, reporting: 'result'): Result<Value, ValueValidation>;
|
|
25
|
+
/**
|
|
26
|
+
* Is the value valid?
|
|
27
|
+
* @param value Value to validate
|
|
28
|
+
* @returns `true` if the value is valid, otherwise `false`
|
|
29
|
+
*/
|
|
30
|
+
is(value: unknown, reporting?: 'none'): value is Value;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a validator for value types
|
|
34
|
+
* @param types Types to validate against
|
|
35
|
+
* @param validators Custom validators to use for validation
|
|
36
|
+
* @returns Validator
|
|
37
|
+
*/
|
|
38
|
+
declare function validator<Types extends Array<Constructor | ((value: unknown) => boolean) | ValueType>>(types: Types, validators?: Validators): Validator<InferValidatorValue<Types>>;
|
|
39
|
+
/**
|
|
40
|
+
* Create a validator for a constructor
|
|
41
|
+
* @param constructor Constructor to validate against
|
|
42
|
+
* @returns Validator
|
|
43
|
+
*/
|
|
44
|
+
declare function validator<Instance>(constructor: Constructor<Instance>): Validator<Instance>;
|
|
45
|
+
/**
|
|
46
|
+
* Create a validator for a callback
|
|
47
|
+
* @param callback Callback for validation
|
|
48
|
+
* @returns Validator
|
|
49
|
+
*/
|
|
50
|
+
declare function validator<Value>(callback: (value: unknown) => value is Value): Validator<Value>;
|
|
51
|
+
/**
|
|
52
|
+
* Create a validator for a callback
|
|
53
|
+
* @param callback Callback for validation
|
|
54
|
+
* @returns Validator
|
|
55
|
+
*/
|
|
56
|
+
declare function validator<Value>(callback: (value: unknown) => boolean): Validator<Value>;
|
|
57
|
+
/**
|
|
58
|
+
* Create a validator for a type
|
|
59
|
+
* @param type Type to validate against
|
|
60
|
+
* @param validators Custom validators to use for validation
|
|
61
|
+
* @returns Validator
|
|
62
|
+
*/
|
|
63
|
+
declare function validator<Type extends ValueType>(type: Type, validators?: ((value: Values[Type]) => boolean) | Array<(value: Values[Type]) => boolean> | Record<Type, ((value: Values[Type]) => boolean) | Array<(value: Values[Type]) => boolean>>): Validator<Values[Type]>;
|
|
64
|
+
/**
|
|
65
|
+
* Create a validator for value types
|
|
66
|
+
* @param types Types to validate against
|
|
67
|
+
* @param validators Custom validators to use for validation
|
|
68
|
+
* @returns Validator
|
|
69
|
+
*/
|
|
70
|
+
declare function validator<Types extends ValueType[]>(types: Types, validators?: Validators): Validator<unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* Create a validator for an array of items
|
|
73
|
+
* @param type Type of items in the array
|
|
74
|
+
* @returns Validator
|
|
75
|
+
*/
|
|
76
|
+
declare function validator<Item>(type: 'array'): Validator<Item[]>;
|
|
77
|
+
//#endregion
|
|
4
78
|
//#region src/models/schematic.plain.model.d.ts
|
|
5
79
|
/**
|
|
6
80
|
* A generic schematic allowing nested schematics, {@link SchematicEntry} values, or arrays of {@link SchematicEntry} as values
|
|
7
81
|
*/
|
|
8
82
|
type PlainSchematic = {
|
|
9
|
-
[key: string]:
|
|
83
|
+
[key: string]: SchematicEntry | SchematicEntry[];
|
|
10
84
|
} & {
|
|
11
85
|
$default?: never;
|
|
12
86
|
$required?: never;
|
|
@@ -29,9 +103,9 @@ type Schematic = PlainSchematic;
|
|
|
29
103
|
/**
|
|
30
104
|
* A union of all valid types for a single schematic entry
|
|
31
105
|
*
|
|
32
|
-
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link
|
|
106
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link ValueType}, or a custom validator function
|
|
33
107
|
*/
|
|
34
|
-
type SchematicEntry = Constructor | PlainSchematic | Schema<unknown> | SchematicProperty |
|
|
108
|
+
type SchematicEntry = Constructor | PlainSchematic | Schema<unknown> | SchematicProperty | Validator<unknown> | ValueType | ((value: unknown) => boolean);
|
|
35
109
|
/**
|
|
36
110
|
* A property definition with explicit type(s), an optional requirement flag, and optional validators
|
|
37
111
|
*
|
|
@@ -58,18 +132,18 @@ type SchematicProperty = {
|
|
|
58
132
|
*/
|
|
59
133
|
$type: SchemaPropertyType | SchemaPropertyType[];
|
|
60
134
|
/**
|
|
61
|
-
* Optional validators keyed by {@link
|
|
135
|
+
* Optional validators keyed by {@link ValueType}, applied during validation
|
|
62
136
|
*/
|
|
63
137
|
$validators?: PropertyValidators<SchemaPropertyType | SchemaPropertyType[]>;
|
|
64
138
|
};
|
|
65
139
|
/**
|
|
66
140
|
* A union of valid types for a {@link SchematicProperty}'s `$type` field
|
|
67
141
|
*
|
|
68
|
-
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link
|
|
142
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link ValueType} string, or a custom validator function
|
|
69
143
|
*/
|
|
70
|
-
type SchemaPropertyType = Constructor | PlainSchematic | Schema<unknown> |
|
|
144
|
+
type SchemaPropertyType = Constructor | PlainSchematic | Schema<unknown> | Validator<unknown> | ValueType | ((value: unknown) => boolean);
|
|
71
145
|
/**
|
|
72
|
-
* A map of optional validator functions keyed by {@link
|
|
146
|
+
* A map of optional validator functions keyed by {@link ValueType}, used to add custom validation to {@link SchemaProperty} definitions
|
|
73
147
|
*
|
|
74
148
|
* Each key may hold a single validator or an array of validators that receive the typed value
|
|
75
149
|
*
|
|
@@ -82,7 +156,7 @@ type SchemaPropertyType = Constructor | PlainSchematic | Schema<unknown> | Value
|
|
|
82
156
|
* };
|
|
83
157
|
* ```
|
|
84
158
|
*/
|
|
85
|
-
type PropertyValidators<Value> = { [Key in
|
|
159
|
+
type PropertyValidators<Value> = { [Key in ExtractValueTypes<Value>]?: ((value: Values[Key]) => boolean) | Array<(value: Values[Key]) => boolean> };
|
|
86
160
|
//#endregion
|
|
87
161
|
//#region src/models/misc.model.d.ts
|
|
88
162
|
/**
|
|
@@ -99,17 +173,17 @@ type PropertyValidators<Value> = { [Key in ExtractValueNames<Value>]?: ((value:
|
|
|
99
173
|
*/
|
|
100
174
|
type DeduplicateTuple<Value extends unknown[], Seen extends unknown[] = []> = Value extends [infer Head, ...infer Tail] ? Head extends Seen[number] ? DeduplicateTuple<Tail, Seen> : DeduplicateTuple<Tail, [...Seen, Head]> : Seen;
|
|
101
175
|
/**
|
|
102
|
-
* Recursively extracts {@link
|
|
176
|
+
* Recursively extracts {@link ValueType} strings from a type, unwrapping arrays and readonly arrays
|
|
103
177
|
*
|
|
104
|
-
* @template Value Type to extract value
|
|
178
|
+
* @template Value Type to extract value types from
|
|
105
179
|
*
|
|
106
180
|
* @example
|
|
107
181
|
* ```ts
|
|
108
|
-
* //
|
|
109
|
-
* //
|
|
182
|
+
* // ExtractValueTypes<'string'> => 'string'
|
|
183
|
+
* // ExtractValueTypes<['string', 'number']> => 'string' | 'number'
|
|
110
184
|
* ```
|
|
111
185
|
*/
|
|
112
|
-
type
|
|
186
|
+
type ExtractValueTypes<Value> = Value extends ValueType ? Value : Value extends (infer Item)[] ? ExtractValueTypes<Item> : Value extends readonly (infer Item)[] ? ExtractValueTypes<Item> : never;
|
|
113
187
|
/**
|
|
114
188
|
* Determines whether a schema entry is optional
|
|
115
189
|
*
|
|
@@ -207,9 +281,9 @@ type UnwrapSingle<Value extends unknown[]> = Value extends [infer Only] ? Only :
|
|
|
207
281
|
/**
|
|
208
282
|
* A union of valid type name strings, e.g. `'string'`, `'number'`, `'date'`
|
|
209
283
|
*/
|
|
210
|
-
type
|
|
284
|
+
type ValueType = keyof Values;
|
|
211
285
|
/**
|
|
212
|
-
* Maps {@link
|
|
286
|
+
* Maps {@link ValueType} strings to their TypeScript equivalents
|
|
213
287
|
*/
|
|
214
288
|
type Values = {
|
|
215
289
|
array: unknown[];
|
|
@@ -259,11 +333,9 @@ type InferPropertyType<Value> = Value extends (infer Item)[] ? InferPropertyValu
|
|
|
259
333
|
/**
|
|
260
334
|
* Maps a single `$type` definition to its TypeScript equivalent
|
|
261
335
|
*
|
|
262
|
-
* Resolves, in order: {@link Constructor}s, {@link Schema} instances, {@link ValueName} values, and nested {@link PlainSchematic} objects
|
|
263
|
-
*
|
|
264
336
|
* @template Value single type definition
|
|
265
337
|
*/
|
|
266
|
-
type InferPropertyValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schema<infer Model> ? Model : Value extends
|
|
338
|
+
type InferPropertyValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schema<infer Model> ? Model : Value extends Validator<infer Type> ? Type : Value extends ValueType ? Values[Value & ValueType] : Value extends PlainSchematic ? Infer<Value> : never;
|
|
267
339
|
/**
|
|
268
340
|
* Extracts keys from a {@link Schematic} whose entries are required _(i.e., `$required` is not `false`)_
|
|
269
341
|
*
|
|
@@ -279,11 +351,15 @@ type InferSchemaEntry<Value> = Value extends (infer Item)[] ? InferSchemaEntryVa
|
|
|
279
351
|
/**
|
|
280
352
|
* Maps a single top-level schema entry to its TypeScript type
|
|
281
353
|
*
|
|
282
|
-
* Resolves, in order: {@link Constructor}s, {@link Schema} instances, {@link SchemaProperty} objects, {@link PlainSchematic} objects, and {@link ValueName} values
|
|
283
|
-
*
|
|
284
354
|
* @template Value single schema entry
|
|
285
355
|
*/
|
|
286
|
-
type InferSchemaEntryValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schema<infer Model> ? Model : Value extends SchematicProperty ? InferPropertyType<Value['$type']> : Value extends PlainSchematic ? Infer<Value & Schematic> : Value extends
|
|
356
|
+
type InferSchemaEntryValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schema<infer Model> ? Model : Value extends SchematicProperty ? InferPropertyType<Value['$type']> : Value extends PlainSchematic ? Infer<Value & Schematic> : Value extends Validator<infer Type> ? Type : Value extends ValueType ? Values[Value & ValueType] : never;
|
|
357
|
+
/**
|
|
358
|
+
* Infers the TypeScript type from a {@link Validator} definition
|
|
359
|
+
*
|
|
360
|
+
* @template Value Validator to infer type from
|
|
361
|
+
*/
|
|
362
|
+
type InferValidatorValue<Value> = Value extends (infer Item)[] ? InferValidatorValue<Item> : Value extends Constructor<infer Instance> ? Instance : Value extends ((value: unknown) => value is infer Type) ? Type : Value extends ((value: unknown) => boolean) ? 'xyz' : Value extends ValueType ? Values[Value & ValueType] : never;
|
|
287
363
|
//#endregion
|
|
288
364
|
//#region src/models/transform.model.d.ts
|
|
289
365
|
/**
|
|
@@ -313,13 +389,13 @@ type ToSchemaPropertyType<Value> = UnwrapSingle<DeduplicateTuple<MapToSchemaProp
|
|
|
313
389
|
*/
|
|
314
390
|
type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject ? TypedSchematic<Value> : ToValueType<Value>;
|
|
315
391
|
/**
|
|
316
|
-
* Converts a TypeScript type to its {@link
|
|
392
|
+
* Converts a TypeScript type to its {@link ValueType} representation, suitable for use as a top-level schema entry
|
|
317
393
|
*
|
|
318
394
|
* @template Value Type to convert
|
|
319
395
|
*/
|
|
320
396
|
type ToSchemaType<Value> = UnwrapSingle<DeduplicateTuple<MapToValueTypes<UnionToTuple<Value>>>>;
|
|
321
397
|
/**
|
|
322
|
-
* Maps a type to its {@link
|
|
398
|
+
* Maps a type to its {@link ValueType} string equivalent
|
|
323
399
|
*
|
|
324
400
|
* Resolves {@link Schema} types as-is, then performs a reverse-lookup against {@link Values} _(excluding `'object'`)_ to find a matching key. If no match is found, `object` types resolve to `'object'` or a type-guard function, and all other unrecognised types resolve to a type-guard function
|
|
325
401
|
*
|
|
@@ -389,7 +465,7 @@ type TypedPropertyRequired<Value> = {
|
|
|
389
465
|
* };
|
|
390
466
|
* ```
|
|
391
467
|
*/
|
|
392
|
-
type TypedSchematic<Model extends PlainObject> = Simplify<{ [Key in RequiredKeys<Model>]: Model[Key] extends PlainObject ? Schema<Model[Key]> : ToSchemaType<Model[Key]> | TypedPropertyRequired<Model[Key]> } & { [Key in OptionalKeys<Model>]: Exclude<Model[Key], undefined> extends PlainObject ?
|
|
468
|
+
type TypedSchematic<Model extends PlainObject> = Simplify<{ [Key in RequiredKeys<Model>]: Model[Key] extends PlainObject ? Schema<Model[Key]> | TypedSchematic<Model[Key]> : ToSchemaType<Model[Key]> | TypedPropertyRequired<Model[Key]> } & { [Key in OptionalKeys<Model>]: Exclude<Model[Key], undefined> extends PlainObject ? TypedPropertyOptional<Model[Key]> : never }>;
|
|
393
469
|
//#endregion
|
|
394
470
|
//#region src/schema.d.ts
|
|
395
471
|
/**
|
|
@@ -398,77 +474,77 @@ type TypedSchematic<Model extends PlainObject> = Simplify<{ [Key in RequiredKeys
|
|
|
398
474
|
declare class Schema<Model> {
|
|
399
475
|
#private;
|
|
400
476
|
private readonly $schema;
|
|
401
|
-
constructor(validator:
|
|
477
|
+
constructor(validator: ValidationHandler);
|
|
402
478
|
/**
|
|
403
479
|
* Parse a value according to the schema
|
|
404
480
|
*
|
|
405
|
-
* Returns
|
|
481
|
+
* Returns value _(deeply cloned, by default)_ or throws an error for the first property that fails validation
|
|
406
482
|
* @param value Value to parse
|
|
407
483
|
* @param options Validation options
|
|
408
|
-
* @returns
|
|
484
|
+
* @returns Value, if it matches the schema, otherwise throws an error
|
|
409
485
|
*/
|
|
410
486
|
get(value: unknown, options: GetOptions<'throw'>): Model;
|
|
411
487
|
/**
|
|
412
488
|
* Parse a value according to the schema
|
|
413
489
|
*
|
|
414
|
-
* Returns
|
|
490
|
+
* Returns value _(deeply cloned, by default)_ or throws an error for the first property that fails validation
|
|
415
491
|
* @param value Value to parse
|
|
416
492
|
* @param errors Reporting type
|
|
417
|
-
* @returns
|
|
493
|
+
* @returns Value, if it matches the schema, otherwise throws an error
|
|
418
494
|
*/
|
|
419
495
|
get(value: unknown, errors: 'throw'): Model;
|
|
420
496
|
/**
|
|
421
497
|
* Parse a value according to the schema
|
|
422
498
|
*
|
|
423
|
-
* Returns
|
|
499
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for validation failures from the same depth in the value
|
|
424
500
|
* @param value Value to parse
|
|
425
501
|
* @param options Validation options
|
|
426
|
-
* @returns Result holding
|
|
502
|
+
* @returns Result holding value or all validation information
|
|
427
503
|
*/
|
|
428
|
-
get(value: unknown, options: GetOptions<'all'>): Result<Model,
|
|
504
|
+
get(value: unknown, options: GetOptions<'all'>): Result<Model, PropertyValidation[]>;
|
|
429
505
|
/**
|
|
430
506
|
* Parse a value according to the schema
|
|
431
507
|
*
|
|
432
|
-
* Returns
|
|
508
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for validation failures from the same depth in the value
|
|
433
509
|
* @param value Value to parse
|
|
434
510
|
* @param errors Reporting type
|
|
435
|
-
* @returns Result holding
|
|
511
|
+
* @returns Result holding value or all validation information
|
|
436
512
|
*/
|
|
437
|
-
get(value: unknown, errors: 'all'): Result<Model,
|
|
513
|
+
get(value: unknown, errors: 'all'): Result<Model, PropertyValidation[]>;
|
|
438
514
|
/**
|
|
439
515
|
* Parse a value according to the schema
|
|
440
516
|
*
|
|
441
|
-
* Returns
|
|
517
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for the first failing property
|
|
442
518
|
* @param value Value to parse
|
|
443
519
|
* @param options Validation options
|
|
444
|
-
* @returns Result holding
|
|
520
|
+
* @returns Result holding value or all validation information
|
|
445
521
|
*/
|
|
446
|
-
get(value: unknown, options: GetOptions<'first'>): Result<Model,
|
|
522
|
+
get(value: unknown, options: GetOptions<'first'>): Result<Model, PropertyValidation>;
|
|
447
523
|
/**
|
|
448
524
|
* Parse a value according to the schema
|
|
449
525
|
*
|
|
450
|
-
* Returns
|
|
526
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for the first failing property
|
|
451
527
|
* @param value Value to parse
|
|
452
528
|
* @param errors Reporting type
|
|
453
|
-
* @returns Result holding
|
|
529
|
+
* @returns Result holding value or all validation information
|
|
454
530
|
*/
|
|
455
|
-
get(value: unknown, errors: 'first'): Result<Model,
|
|
531
|
+
get(value: unknown, errors: 'first'): Result<Model, PropertyValidation>;
|
|
456
532
|
/**
|
|
457
533
|
* Parse a value according to the schema
|
|
458
534
|
*
|
|
459
|
-
* Returns
|
|
535
|
+
* Returns value _(deeply cloned, by default)_ or `undefined` if the value does not match the schema
|
|
460
536
|
* @param value Value to parse
|
|
461
537
|
* @param options Validation options
|
|
462
|
-
* @returns
|
|
538
|
+
* @returns Value, or `undefined` if it's invalid
|
|
463
539
|
*/
|
|
464
|
-
get(value: unknown, options: GetOptions<'none'
|
|
540
|
+
get(value: unknown, options: Partial<GetOptions<'none'>>): Model | undefined;
|
|
465
541
|
/**
|
|
466
542
|
* Parse a value according to the schema
|
|
467
543
|
*
|
|
468
|
-
* Returns
|
|
544
|
+
* Returns value _(deeply cloned, by default)_ or `undefined` if the value does not match the schema
|
|
469
545
|
* @param value Value to parse
|
|
470
546
|
* @param strict Validate if unknown keys are present in the object? _(defaults to `false`)_
|
|
471
|
-
* @returns
|
|
547
|
+
* @returns Value, or `undefined` if it's invalid
|
|
472
548
|
*/
|
|
473
549
|
get(value: unknown, strict?: true): Model | undefined;
|
|
474
550
|
/**
|
|
@@ -497,7 +573,7 @@ declare class Schema<Model> {
|
|
|
497
573
|
* @param options Validation options
|
|
498
574
|
* @returns Result holding `true` or all validation information
|
|
499
575
|
*/
|
|
500
|
-
is(value: unknown, options: IsOptions<'all'>): Result<true,
|
|
576
|
+
is(value: unknown, options: IsOptions<'all'>): Result<true, PropertyValidation[]>;
|
|
501
577
|
/**
|
|
502
578
|
* Does the value match the schema?
|
|
503
579
|
*
|
|
@@ -506,38 +582,38 @@ declare class Schema<Model> {
|
|
|
506
582
|
* @param errors Reporting type
|
|
507
583
|
* @returns Result holding `true` or all validation information
|
|
508
584
|
*/
|
|
509
|
-
is(value: unknown, errors: 'all'): Result<true,
|
|
585
|
+
is(value: unknown, errors: 'all'): Result<true, PropertyValidation[]>;
|
|
510
586
|
/**
|
|
511
587
|
* Does the value match the schema?
|
|
512
588
|
*
|
|
513
589
|
* Will validate that the value matches the schema and return a result of `true` or all validation information for the first failing property
|
|
514
590
|
* @param value Value to validate
|
|
515
591
|
* @param options Validation options
|
|
516
|
-
* @returns `true`
|
|
592
|
+
* @returns Result holding `true` or all validation information
|
|
517
593
|
*/
|
|
518
|
-
is(value: unknown, options: IsOptions<'first'>): Result<true,
|
|
594
|
+
is(value: unknown, options: IsOptions<'first'>): Result<true, PropertyValidation>;
|
|
519
595
|
/**
|
|
520
596
|
* Does the value match the schema?
|
|
521
597
|
*
|
|
522
598
|
* Will validate that the value matches the schema and return a result of `true` or all validation information for the first failing property
|
|
523
599
|
* @param value Value to validate
|
|
524
600
|
* @param errors Reporting type
|
|
525
|
-
* @returns `true`
|
|
601
|
+
* @returns Result holding `true` or all validation information
|
|
526
602
|
*/
|
|
527
|
-
is(value: unknown, errors: 'first'): Result<true,
|
|
603
|
+
is(value: unknown, errors: 'first'): Result<true, PropertyValidation>;
|
|
528
604
|
/**
|
|
529
605
|
* Does the value match the schema?
|
|
530
606
|
*
|
|
531
|
-
* Will validate that the value matches the schema and return `true` or `false
|
|
607
|
+
* Will validate that the value matches the schema and return `true` if it's valid, or `false` if not
|
|
532
608
|
* @param value Value to validate
|
|
533
609
|
* @param options Validation options
|
|
534
610
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
535
611
|
*/
|
|
536
|
-
is(value: unknown, options: IsOptions<'none'
|
|
612
|
+
is(value: unknown, options: Partial<IsOptions<'none'>>): value is Model;
|
|
537
613
|
/**
|
|
538
614
|
* Does the value match the schema?
|
|
539
615
|
*
|
|
540
|
-
* Will validate that the value matches the schema and return `true` or `false
|
|
616
|
+
* Will validate that the value matches the schema and return `true` if it's valid, or `false` if not
|
|
541
617
|
* @param value Value to validate
|
|
542
618
|
* @param strict Validate if unknown keys are present in the object? _(defaults to `false`)_
|
|
543
619
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
@@ -569,11 +645,11 @@ type ReportingInformation = Record<ReportingType, boolean> & {
|
|
|
569
645
|
* Controls how validation failures are reported
|
|
570
646
|
*
|
|
571
647
|
* - `'none'`, returns a boolean _(default)_
|
|
572
|
-
* - `'first'`, returns the first failure as a `Result`
|
|
648
|
+
* - `'first'` or `'result'`, returns the first failure as a `Result`
|
|
573
649
|
* - `'all'`, returns all failures as a `Result` _(from same level)_
|
|
574
650
|
* - `'throw'`, throws a {@link ValidationError} on failure
|
|
575
651
|
*/
|
|
576
|
-
type ReportingType = 'all' | 'first' | 'none' | 'throw';
|
|
652
|
+
type ReportingType = 'all' | 'first' | 'none' | 'result' | 'throw';
|
|
577
653
|
/**
|
|
578
654
|
* Thrown when a schema definition is invalid
|
|
579
655
|
*/
|
|
@@ -584,25 +660,42 @@ declare class SchematicError extends Error {
|
|
|
584
660
|
* Thrown in `'throw'` mode when one or more properties fail validation; `information` holds all failures
|
|
585
661
|
*/
|
|
586
662
|
declare class ValidationError extends Error {
|
|
587
|
-
readonly information:
|
|
588
|
-
constructor(information:
|
|
663
|
+
readonly information: PropertyValidation[];
|
|
664
|
+
constructor(information: PropertyValidation[]);
|
|
589
665
|
}
|
|
590
666
|
/**
|
|
591
|
-
* Describes a single validation failure
|
|
667
|
+
* Describes a single property validation failure
|
|
592
668
|
*/
|
|
593
|
-
type
|
|
594
|
-
/**
|
|
595
|
-
|
|
596
|
-
|
|
669
|
+
type PropertyValidation = {
|
|
670
|
+
/**
|
|
671
|
+
* The key path of the property that failed
|
|
672
|
+
*/
|
|
673
|
+
key?: PropertyValidationKey;
|
|
674
|
+
/**
|
|
675
|
+
* Human-readable description of the failure
|
|
676
|
+
*/
|
|
677
|
+
message: string;
|
|
678
|
+
/**
|
|
679
|
+
* The validator function that failed, if the failure was from a `$validators` entry
|
|
680
|
+
*/
|
|
681
|
+
validator?: GenericCallback;
|
|
682
|
+
/**
|
|
683
|
+
* The value that was provided
|
|
684
|
+
*/
|
|
597
685
|
value: unknown;
|
|
598
686
|
};
|
|
599
687
|
/**
|
|
600
|
-
*
|
|
688
|
+
* The full and short key paths of a property; `full` is the complete path from the root, while `short` is the path from the current schema
|
|
601
689
|
*/
|
|
602
|
-
type
|
|
690
|
+
type PropertyValidationKey = {
|
|
603
691
|
full: string;
|
|
604
692
|
short: string;
|
|
605
693
|
};
|
|
694
|
+
type ValueValidation = {
|
|
695
|
+
message: string;
|
|
696
|
+
validator?: GenericCallback;
|
|
697
|
+
value: unknown;
|
|
698
|
+
};
|
|
606
699
|
type BaseOptions<Errors extends ReportingType> = {
|
|
607
700
|
/**
|
|
608
701
|
* How should validation failures be reported; see {@link ReportingType} _(defaults to `'none'`)_
|
|
@@ -626,14 +719,20 @@ type GetOptions<Errors extends ReportingType> = BaseOptions<Errors> & {
|
|
|
626
719
|
* Options for validation an input value
|
|
627
720
|
*/
|
|
628
721
|
type IsOptions<Errors extends ReportingType> = BaseOptions<Errors>;
|
|
629
|
-
|
|
630
|
-
|
|
722
|
+
/**
|
|
723
|
+
* Object property validators
|
|
724
|
+
*/
|
|
725
|
+
type Validators = { [Key in ValueType]?: Array<(value: unknown) => boolean> };
|
|
726
|
+
type ValidationHandler = (input: unknown, parameters: ValidationHandlerParameters, get: boolean) => true | PropertyValidation[];
|
|
727
|
+
type ValidationHandlerParameters = {
|
|
631
728
|
clone: boolean;
|
|
632
|
-
information?:
|
|
729
|
+
information?: PropertyValidation[];
|
|
730
|
+
key?: string;
|
|
633
731
|
output: PlainObject;
|
|
634
732
|
reporting: ReportingInformation;
|
|
635
733
|
strict: boolean;
|
|
636
734
|
};
|
|
735
|
+
type ValidationHandlerType = Function | PlainObject | Schema<unknown> | Validator<unknown> | ValueType;
|
|
637
736
|
//#endregion
|
|
638
737
|
//#region src/helpers/misc.helper.d.ts
|
|
639
738
|
/**
|
|
@@ -644,10 +743,10 @@ type ValidatorParameters = {
|
|
|
644
743
|
*/
|
|
645
744
|
declare function instanceOf<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
|
|
646
745
|
/**
|
|
647
|
-
* Is the value a
|
|
746
|
+
* Is the value a schema?
|
|
648
747
|
* @param value Value to check
|
|
649
|
-
* @returns `true` if the value is a
|
|
748
|
+
* @returns `true` if the value is a schema, `false` otherwise
|
|
650
749
|
*/
|
|
651
|
-
declare function isSchema(value: unknown): value is Schema<
|
|
750
|
+
declare function isSchema(value: unknown): value is Schema<unknown>;
|
|
652
751
|
//#endregion
|
|
653
|
-
export { type GetOptions, type IsOptions, type Schema, type Schematic, SchematicError, type TypedSchematic, ValidationError, instanceOf, isSchema, schema };
|
|
752
|
+
export { type GetOptions, type IsOptions, type Schema, type Schematic, SchematicError, type TypedSchematic, ValidationError, type Validator, instanceOf, isSchema, schema, validator };
|