@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
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { Validator } from "../validator.mjs";
|
|
1
2
|
import { Schema } from "../schema.mjs";
|
|
2
3
|
import { PlainSchematic, Schematic, SchematicProperty } from "./schematic.plain.model.mjs";
|
|
3
|
-
import { IsOptionalProperty,
|
|
4
|
+
import { IsOptionalProperty, ValueType, Values } from "./misc.model.mjs";
|
|
4
5
|
import { Constructor, Simplify } from "@oscarpalmer/atoms/models";
|
|
5
6
|
|
|
6
7
|
//#region src/models/infer.model.d.ts
|
|
@@ -37,11 +38,9 @@ type InferPropertyType<Value> = Value extends (infer Item)[] ? InferPropertyValu
|
|
|
37
38
|
/**
|
|
38
39
|
* Maps a single `$type` definition to its TypeScript equivalent
|
|
39
40
|
*
|
|
40
|
-
* Resolves, in order: {@link Constructor}s, {@link Schema} instances, {@link ValueName} values, and nested {@link PlainSchematic} objects
|
|
41
|
-
*
|
|
42
41
|
* @template Value single type definition
|
|
43
42
|
*/
|
|
44
|
-
type InferPropertyValue<Value> = Value extends Constructor<infer Instance> ? Instance : Value extends Schema<infer Model> ? Model : Value extends
|
|
43
|
+
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;
|
|
45
44
|
/**
|
|
46
45
|
* Extracts keys from a {@link Schematic} whose entries are required _(i.e., `$required` is not `false`)_
|
|
47
46
|
*
|
|
@@ -57,10 +56,14 @@ type InferSchemaEntry<Value> = Value extends (infer Item)[] ? InferSchemaEntryVa
|
|
|
57
56
|
/**
|
|
58
57
|
* Maps a single top-level schema entry to its TypeScript type
|
|
59
58
|
*
|
|
60
|
-
* Resolves, in order: {@link Constructor}s, {@link Schema} instances, {@link SchemaProperty} objects, {@link PlainSchematic} objects, and {@link ValueName} values
|
|
61
|
-
*
|
|
62
59
|
* @template Value single schema entry
|
|
63
60
|
*/
|
|
64
|
-
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
|
|
61
|
+
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;
|
|
62
|
+
/**
|
|
63
|
+
* Infers the TypeScript type from a {@link Validator} definition
|
|
64
|
+
*
|
|
65
|
+
* @template Value Validator to infer type from
|
|
66
|
+
*/
|
|
67
|
+
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;
|
|
65
68
|
//#endregion
|
|
66
|
-
export { Infer, InferOptionalKeys, InferPropertyType, InferPropertyValue, InferRequiredKeys, InferSchemaEntry, InferSchemaEntryValue };
|
|
69
|
+
export { Infer, InferOptionalKeys, InferPropertyType, InferPropertyValue, InferRequiredKeys, InferSchemaEntry, InferSchemaEntryValue, InferValidatorValue };
|
|
@@ -15,17 +15,17 @@ import { SchematicProperty } from "./schematic.plain.model.mjs";
|
|
|
15
15
|
*/
|
|
16
16
|
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;
|
|
17
17
|
/**
|
|
18
|
-
* Recursively extracts {@link
|
|
18
|
+
* Recursively extracts {@link ValueType} strings from a type, unwrapping arrays and readonly arrays
|
|
19
19
|
*
|
|
20
|
-
* @template Value Type to extract value
|
|
20
|
+
* @template Value Type to extract value types from
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* ```ts
|
|
24
|
-
* //
|
|
25
|
-
* //
|
|
24
|
+
* // ExtractValueTypes<'string'> => 'string'
|
|
25
|
+
* // ExtractValueTypes<['string', 'number']> => 'string' | 'number'
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
type
|
|
28
|
+
type ExtractValueTypes<Value> = Value extends ValueType ? Value : Value extends (infer Item)[] ? ExtractValueTypes<Item> : Value extends readonly (infer Item)[] ? ExtractValueTypes<Item> : never;
|
|
29
29
|
/**
|
|
30
30
|
* Determines whether a schema entry is optional
|
|
31
31
|
*
|
|
@@ -123,9 +123,9 @@ type UnwrapSingle<Value extends unknown[]> = Value extends [infer Only] ? Only :
|
|
|
123
123
|
/**
|
|
124
124
|
* A union of valid type name strings, e.g. `'string'`, `'number'`, `'date'`
|
|
125
125
|
*/
|
|
126
|
-
type
|
|
126
|
+
type ValueType = keyof Values;
|
|
127
127
|
/**
|
|
128
|
-
* Maps {@link
|
|
128
|
+
* Maps {@link ValueType} strings to their TypeScript equivalents
|
|
129
129
|
*/
|
|
130
130
|
type Values = {
|
|
131
131
|
array: unknown[];
|
|
@@ -141,4 +141,4 @@ type Values = {
|
|
|
141
141
|
undefined: undefined;
|
|
142
142
|
};
|
|
143
143
|
//#endregion
|
|
144
|
-
export { DeduplicateTuple,
|
|
144
|
+
export { DeduplicateTuple, ExtractValueTypes, IsOptionalProperty, LastOfUnion, OptionalKeys, RequiredKeys, TuplePermutations, TupleRemoveAt, UnionToIntersection, UnionToTuple, UnwrapSingle, ValueType, Values };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Validator } from "../validator.mjs";
|
|
1
2
|
import { Schema } from "../schema.mjs";
|
|
2
|
-
import {
|
|
3
|
+
import { ExtractValueTypes, ValueType, Values } from "./misc.model.mjs";
|
|
3
4
|
import { Constructor } from "@oscarpalmer/atoms/models";
|
|
4
5
|
|
|
5
6
|
//#region src/models/schematic.plain.model.d.ts
|
|
@@ -7,7 +8,7 @@ import { Constructor } from "@oscarpalmer/atoms/models";
|
|
|
7
8
|
* A generic schematic allowing nested schematics, {@link SchematicEntry} values, or arrays of {@link SchematicEntry} as values
|
|
8
9
|
*/
|
|
9
10
|
type PlainSchematic = {
|
|
10
|
-
[key: string]:
|
|
11
|
+
[key: string]: SchematicEntry | SchematicEntry[];
|
|
11
12
|
} & {
|
|
12
13
|
$default?: never;
|
|
13
14
|
$required?: never;
|
|
@@ -30,9 +31,9 @@ type Schematic = PlainSchematic;
|
|
|
30
31
|
/**
|
|
31
32
|
* A union of all valid types for a single schematic entry
|
|
32
33
|
*
|
|
33
|
-
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link
|
|
34
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link ValueType}, or a custom validator function
|
|
34
35
|
*/
|
|
35
|
-
type SchematicEntry = Constructor | PlainSchematic | Schema<unknown> | SchematicProperty |
|
|
36
|
+
type SchematicEntry = Constructor | PlainSchematic | Schema<unknown> | SchematicProperty | Validator<unknown> | ValueType | ((value: unknown) => boolean);
|
|
36
37
|
/**
|
|
37
38
|
* A property definition with explicit type(s), an optional requirement flag, and optional validators
|
|
38
39
|
*
|
|
@@ -59,18 +60,18 @@ type SchematicProperty = {
|
|
|
59
60
|
*/
|
|
60
61
|
$type: SchemaPropertyType | SchemaPropertyType[];
|
|
61
62
|
/**
|
|
62
|
-
* Optional validators keyed by {@link
|
|
63
|
+
* Optional validators keyed by {@link ValueType}, applied during validation
|
|
63
64
|
*/
|
|
64
65
|
$validators?: PropertyValidators<SchemaPropertyType | SchemaPropertyType[]>;
|
|
65
66
|
};
|
|
66
67
|
/**
|
|
67
68
|
* A union of valid types for a {@link SchematicProperty}'s `$type` field
|
|
68
69
|
*
|
|
69
|
-
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link
|
|
70
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link ValueType} string, or a custom validator function
|
|
70
71
|
*/
|
|
71
|
-
type SchemaPropertyType = Constructor | PlainSchematic | Schema<unknown> |
|
|
72
|
+
type SchemaPropertyType = Constructor | PlainSchematic | Schema<unknown> | Validator<unknown> | ValueType | ((value: unknown) => boolean);
|
|
72
73
|
/**
|
|
73
|
-
* A map of optional validator functions keyed by {@link
|
|
74
|
+
* A map of optional validator functions keyed by {@link ValueType}, used to add custom validation to {@link SchemaProperty} definitions
|
|
74
75
|
*
|
|
75
76
|
* Each key may hold a single validator or an array of validators that receive the typed value
|
|
76
77
|
*
|
|
@@ -83,6 +84,6 @@ type SchemaPropertyType = Constructor | PlainSchematic | Schema<unknown> | Value
|
|
|
83
84
|
* };
|
|
84
85
|
* ```
|
|
85
86
|
*/
|
|
86
|
-
type PropertyValidators<Value> = { [Key in
|
|
87
|
+
type PropertyValidators<Value> = { [Key in ExtractValueTypes<Value>]?: ((value: Values[Key]) => boolean) | Array<(value: Values[Key]) => boolean> };
|
|
87
88
|
//#endregion
|
|
88
89
|
export { PlainSchematic, PropertyValidators, SchemaPropertyType, Schematic, SchematicEntry, SchematicProperty };
|
|
@@ -59,6 +59,6 @@ type TypedPropertyRequired<Value> = {
|
|
|
59
59
|
* };
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
|
-
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 ?
|
|
62
|
+
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 }>;
|
|
63
63
|
//#endregion
|
|
64
64
|
export { TypedPropertyOptional, TypedPropertyRequired, TypedSchematic };
|
|
@@ -31,13 +31,13 @@ type ToSchemaPropertyType<Value> = UnwrapSingle<DeduplicateTuple<MapToSchemaProp
|
|
|
31
31
|
*/
|
|
32
32
|
type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject ? TypedSchematic<Value> : ToValueType<Value>;
|
|
33
33
|
/**
|
|
34
|
-
* Converts a TypeScript type to its {@link
|
|
34
|
+
* Converts a TypeScript type to its {@link ValueType} representation, suitable for use as a top-level schema entry
|
|
35
35
|
*
|
|
36
36
|
* @template Value Type to convert
|
|
37
37
|
*/
|
|
38
38
|
type ToSchemaType<Value> = UnwrapSingle<DeduplicateTuple<MapToValueTypes<UnionToTuple<Value>>>>;
|
|
39
39
|
/**
|
|
40
|
-
* Maps a type to its {@link
|
|
40
|
+
* Maps a type to its {@link ValueType} string equivalent
|
|
41
41
|
*
|
|
42
42
|
* 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
|
|
43
43
|
*
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
import { Validator } from "../validator.mjs";
|
|
1
2
|
import { Schema } from "../schema.mjs";
|
|
2
|
-
import {
|
|
3
|
+
import { ValueType } from "./misc.model.mjs";
|
|
3
4
|
import { GenericCallback, PlainObject } from "@oscarpalmer/atoms/models";
|
|
4
5
|
|
|
5
6
|
//#region src/models/validation.model.d.ts
|
|
6
|
-
type NamedValidatorHandlers = { [Key in ValueName]?: Array<(value: unknown) => boolean> };
|
|
7
|
-
type NamedValidators = Record<ValueName, (value: unknown) => boolean>;
|
|
8
7
|
type ReportingInformation = Record<ReportingType, boolean> & {
|
|
9
8
|
type: ReportingType;
|
|
10
9
|
};
|
|
@@ -12,11 +11,11 @@ type ReportingInformation = Record<ReportingType, boolean> & {
|
|
|
12
11
|
* Controls how validation failures are reported
|
|
13
12
|
*
|
|
14
13
|
* - `'none'`, returns a boolean _(default)_
|
|
15
|
-
* - `'first'`, returns the first failure as a `Result`
|
|
14
|
+
* - `'first'` or `'result'`, returns the first failure as a `Result`
|
|
16
15
|
* - `'all'`, returns all failures as a `Result` _(from same level)_
|
|
17
16
|
* - `'throw'`, throws a {@link ValidationError} on failure
|
|
18
17
|
*/
|
|
19
|
-
type ReportingType = 'all' | 'first' | 'none' | 'throw';
|
|
18
|
+
type ReportingType = 'all' | 'first' | 'none' | 'result' | 'throw';
|
|
20
19
|
/**
|
|
21
20
|
* Thrown when a schema definition is invalid
|
|
22
21
|
*/
|
|
@@ -27,25 +26,48 @@ declare class SchematicError extends Error {
|
|
|
27
26
|
* Thrown in `'throw'` mode when one or more properties fail validation; `information` holds all failures
|
|
28
27
|
*/
|
|
29
28
|
declare class ValidationError extends Error {
|
|
30
|
-
readonly information:
|
|
31
|
-
constructor(information:
|
|
29
|
+
readonly information: PropertyValidation[];
|
|
30
|
+
constructor(information: PropertyValidation[]);
|
|
32
31
|
}
|
|
33
32
|
/**
|
|
34
|
-
*
|
|
33
|
+
* Thrown when a validator definition is invalid
|
|
35
34
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
declare class ValidatorError extends Error {
|
|
36
|
+
constructor(message: string);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Describes a single property validation failure
|
|
40
|
+
*/
|
|
41
|
+
type PropertyValidation = {
|
|
42
|
+
/**
|
|
43
|
+
* The key path of the property that failed
|
|
44
|
+
*/
|
|
45
|
+
key?: PropertyValidationKey;
|
|
46
|
+
/**
|
|
47
|
+
* Human-readable description of the failure
|
|
48
|
+
*/
|
|
49
|
+
message: string;
|
|
50
|
+
/**
|
|
51
|
+
* The validator function that failed, if the failure was from a `$validators` entry
|
|
52
|
+
*/
|
|
53
|
+
validator?: GenericCallback;
|
|
54
|
+
/**
|
|
55
|
+
* The value that was provided
|
|
56
|
+
*/
|
|
40
57
|
value: unknown;
|
|
41
58
|
};
|
|
42
59
|
/**
|
|
43
|
-
*
|
|
60
|
+
* 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
|
|
44
61
|
*/
|
|
45
|
-
type
|
|
62
|
+
type PropertyValidationKey = {
|
|
46
63
|
full: string;
|
|
47
64
|
short: string;
|
|
48
65
|
};
|
|
66
|
+
type ValueValidation = {
|
|
67
|
+
message: string;
|
|
68
|
+
validator?: GenericCallback;
|
|
69
|
+
value: unknown;
|
|
70
|
+
};
|
|
49
71
|
type BaseOptions<Errors extends ReportingType> = {
|
|
50
72
|
/**
|
|
51
73
|
* How should validation failures be reported; see {@link ReportingType} _(defaults to `'none'`)_
|
|
@@ -69,24 +91,33 @@ type GetOptions<Errors extends ReportingType> = BaseOptions<Errors> & {
|
|
|
69
91
|
* Options for validation an input value
|
|
70
92
|
*/
|
|
71
93
|
type IsOptions<Errors extends ReportingType> = BaseOptions<Errors>;
|
|
72
|
-
|
|
73
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Object property validators
|
|
96
|
+
*/
|
|
97
|
+
type Validators = { [Key in ValueType]?: Array<(value: unknown) => boolean> };
|
|
98
|
+
/**
|
|
99
|
+
* Base type validators
|
|
100
|
+
*/
|
|
101
|
+
type TypeValidators = Record<ValueType, (value: unknown) => boolean>;
|
|
102
|
+
type ValidationHandler = (input: unknown, parameters: ValidationHandlerParameters, get: boolean) => true | PropertyValidation[];
|
|
103
|
+
type ValidationHandlerDefaults = {
|
|
74
104
|
value: unknown;
|
|
75
105
|
};
|
|
76
|
-
type
|
|
77
|
-
defaults
|
|
78
|
-
|
|
106
|
+
type ValidationHandlerItem = {
|
|
107
|
+
defaults?: ValidationHandlerDefaults;
|
|
108
|
+
handler: ValidationHandler;
|
|
109
|
+
key: PropertyValidationKey;
|
|
79
110
|
required: boolean;
|
|
80
|
-
types:
|
|
81
|
-
validator: Validator;
|
|
111
|
+
types: ValidationHandlerType[];
|
|
82
112
|
};
|
|
83
|
-
type
|
|
113
|
+
type ValidationHandlerParameters = {
|
|
84
114
|
clone: boolean;
|
|
85
|
-
information?:
|
|
115
|
+
information?: PropertyValidation[];
|
|
116
|
+
key?: string;
|
|
86
117
|
output: PlainObject;
|
|
87
118
|
reporting: ReportingInformation;
|
|
88
119
|
strict: boolean;
|
|
89
120
|
};
|
|
90
|
-
type
|
|
121
|
+
type ValidationHandlerType = Function | PlainObject | Schema<unknown> | Validator<unknown> | ValueType;
|
|
91
122
|
//#endregion
|
|
92
|
-
export { GetOptions, IsOptions,
|
|
123
|
+
export { GetOptions, IsOptions, PropertyValidation, PropertyValidationKey, ReportingInformation, ReportingType, SchematicError, TypeValidators, ValidationError, ValidationHandler, ValidationHandlerDefaults, ValidationHandlerItem, ValidationHandlerParameters, ValidationHandlerType, ValidatorError, Validators, ValueValidation };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION } from "../constants.mjs";
|
|
1
|
+
import { NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_ERROR_VALIDATOR } from "../constants.mjs";
|
|
2
2
|
import { join } from "@oscarpalmer/atoms/string";
|
|
3
3
|
//#region src/models/validation.model.ts
|
|
4
4
|
/**
|
|
@@ -20,5 +20,14 @@ var ValidationError = class extends Error {
|
|
|
20
20
|
this.name = NAME_ERROR_VALIDATION;
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when a validator definition is invalid
|
|
25
|
+
*/
|
|
26
|
+
var ValidatorError = class extends Error {
|
|
27
|
+
constructor(message) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = NAME_ERROR_VALIDATOR;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
23
32
|
//#endregion
|
|
24
|
-
export { SchematicError, ValidationError };
|
|
33
|
+
export { SchematicError, ValidationError, ValidatorError };
|
package/dist/schema.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { GetOptions, IsOptions, PropertyValidation, ValidationHandler } from "./models/validation.model.mjs";
|
|
1
2
|
import { Infer } from "./models/infer.model.mjs";
|
|
2
3
|
import { TypedSchematic } from "./models/schematic.typed.model.mjs";
|
|
3
|
-
import { GetOptions, IsOptions, ValidationInformation, Validator } from "./models/validation.model.mjs";
|
|
4
4
|
import { Schematic } from "./models/schematic.plain.model.mjs";
|
|
5
5
|
import { PlainObject } from "@oscarpalmer/atoms/models";
|
|
6
6
|
import { Result } from "@oscarpalmer/atoms/result/models";
|
|
@@ -12,77 +12,77 @@ import { Result } from "@oscarpalmer/atoms/result/models";
|
|
|
12
12
|
declare class Schema<Model> {
|
|
13
13
|
#private;
|
|
14
14
|
private readonly $schema;
|
|
15
|
-
constructor(validator:
|
|
15
|
+
constructor(validator: ValidationHandler);
|
|
16
16
|
/**
|
|
17
17
|
* Parse a value according to the schema
|
|
18
18
|
*
|
|
19
|
-
* Returns
|
|
19
|
+
* Returns value _(deeply cloned, by default)_ or throws an error for the first property that fails validation
|
|
20
20
|
* @param value Value to parse
|
|
21
21
|
* @param options Validation options
|
|
22
|
-
* @returns
|
|
22
|
+
* @returns Value, if it matches the schema, otherwise throws an error
|
|
23
23
|
*/
|
|
24
24
|
get(value: unknown, options: GetOptions<'throw'>): Model;
|
|
25
25
|
/**
|
|
26
26
|
* Parse a value according to the schema
|
|
27
27
|
*
|
|
28
|
-
* Returns
|
|
28
|
+
* Returns value _(deeply cloned, by default)_ or throws an error for the first property that fails validation
|
|
29
29
|
* @param value Value to parse
|
|
30
30
|
* @param errors Reporting type
|
|
31
|
-
* @returns
|
|
31
|
+
* @returns Value, if it matches the schema, otherwise throws an error
|
|
32
32
|
*/
|
|
33
33
|
get(value: unknown, errors: 'throw'): Model;
|
|
34
34
|
/**
|
|
35
35
|
* Parse a value according to the schema
|
|
36
36
|
*
|
|
37
|
-
* Returns
|
|
37
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for validation failures from the same depth in the value
|
|
38
38
|
* @param value Value to parse
|
|
39
39
|
* @param options Validation options
|
|
40
|
-
* @returns Result holding
|
|
40
|
+
* @returns Result holding value or all validation information
|
|
41
41
|
*/
|
|
42
|
-
get(value: unknown, options: GetOptions<'all'>): Result<Model,
|
|
42
|
+
get(value: unknown, options: GetOptions<'all'>): Result<Model, PropertyValidation[]>;
|
|
43
43
|
/**
|
|
44
44
|
* Parse a value according to the schema
|
|
45
45
|
*
|
|
46
|
-
* Returns
|
|
46
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for validation failures from the same depth in the value
|
|
47
47
|
* @param value Value to parse
|
|
48
48
|
* @param errors Reporting type
|
|
49
|
-
* @returns Result holding
|
|
49
|
+
* @returns Result holding value or all validation information
|
|
50
50
|
*/
|
|
51
|
-
get(value: unknown, errors: 'all'): Result<Model,
|
|
51
|
+
get(value: unknown, errors: 'all'): Result<Model, PropertyValidation[]>;
|
|
52
52
|
/**
|
|
53
53
|
* Parse a value according to the schema
|
|
54
54
|
*
|
|
55
|
-
* Returns
|
|
55
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for the first failing property
|
|
56
56
|
* @param value Value to parse
|
|
57
57
|
* @param options Validation options
|
|
58
|
-
* @returns Result holding
|
|
58
|
+
* @returns Result holding value or all validation information
|
|
59
59
|
*/
|
|
60
|
-
get(value: unknown, options: GetOptions<'first'>): Result<Model,
|
|
60
|
+
get(value: unknown, options: GetOptions<'first'>): Result<Model, PropertyValidation>;
|
|
61
61
|
/**
|
|
62
62
|
* Parse a value according to the schema
|
|
63
63
|
*
|
|
64
|
-
* Returns
|
|
64
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for the first failing property
|
|
65
65
|
* @param value Value to parse
|
|
66
66
|
* @param errors Reporting type
|
|
67
|
-
* @returns Result holding
|
|
67
|
+
* @returns Result holding value or all validation information
|
|
68
68
|
*/
|
|
69
|
-
get(value: unknown, errors: 'first'): Result<Model,
|
|
69
|
+
get(value: unknown, errors: 'first'): Result<Model, PropertyValidation>;
|
|
70
70
|
/**
|
|
71
71
|
* Parse a value according to the schema
|
|
72
72
|
*
|
|
73
|
-
* Returns
|
|
73
|
+
* Returns value _(deeply cloned, by default)_ or `undefined` if the value does not match the schema
|
|
74
74
|
* @param value Value to parse
|
|
75
75
|
* @param options Validation options
|
|
76
|
-
* @returns
|
|
76
|
+
* @returns Value, or `undefined` if it's invalid
|
|
77
77
|
*/
|
|
78
|
-
get(value: unknown, options: GetOptions<'none'
|
|
78
|
+
get(value: unknown, options: Partial<GetOptions<'none'>>): Model | undefined;
|
|
79
79
|
/**
|
|
80
80
|
* Parse a value according to the schema
|
|
81
81
|
*
|
|
82
|
-
* Returns
|
|
82
|
+
* Returns value _(deeply cloned, by default)_ or `undefined` if the value does not match the schema
|
|
83
83
|
* @param value Value to parse
|
|
84
84
|
* @param strict Validate if unknown keys are present in the object? _(defaults to `false`)_
|
|
85
|
-
* @returns
|
|
85
|
+
* @returns Value, or `undefined` if it's invalid
|
|
86
86
|
*/
|
|
87
87
|
get(value: unknown, strict?: true): Model | undefined;
|
|
88
88
|
/**
|
|
@@ -111,7 +111,7 @@ declare class Schema<Model> {
|
|
|
111
111
|
* @param options Validation options
|
|
112
112
|
* @returns Result holding `true` or all validation information
|
|
113
113
|
*/
|
|
114
|
-
is(value: unknown, options: IsOptions<'all'>): Result<true,
|
|
114
|
+
is(value: unknown, options: IsOptions<'all'>): Result<true, PropertyValidation[]>;
|
|
115
115
|
/**
|
|
116
116
|
* Does the value match the schema?
|
|
117
117
|
*
|
|
@@ -120,38 +120,38 @@ declare class Schema<Model> {
|
|
|
120
120
|
* @param errors Reporting type
|
|
121
121
|
* @returns Result holding `true` or all validation information
|
|
122
122
|
*/
|
|
123
|
-
is(value: unknown, errors: 'all'): Result<true,
|
|
123
|
+
is(value: unknown, errors: 'all'): Result<true, PropertyValidation[]>;
|
|
124
124
|
/**
|
|
125
125
|
* Does the value match the schema?
|
|
126
126
|
*
|
|
127
127
|
* Will validate that the value matches the schema and return a result of `true` or all validation information for the first failing property
|
|
128
128
|
* @param value Value to validate
|
|
129
129
|
* @param options Validation options
|
|
130
|
-
* @returns `true`
|
|
130
|
+
* @returns Result holding `true` or all validation information
|
|
131
131
|
*/
|
|
132
|
-
is(value: unknown, options: IsOptions<'first'>): Result<true,
|
|
132
|
+
is(value: unknown, options: IsOptions<'first'>): Result<true, PropertyValidation>;
|
|
133
133
|
/**
|
|
134
134
|
* Does the value match the schema?
|
|
135
135
|
*
|
|
136
136
|
* Will validate that the value matches the schema and return a result of `true` or all validation information for the first failing property
|
|
137
137
|
* @param value Value to validate
|
|
138
138
|
* @param errors Reporting type
|
|
139
|
-
* @returns `true`
|
|
139
|
+
* @returns Result holding `true` or all validation information
|
|
140
140
|
*/
|
|
141
|
-
is(value: unknown, errors: 'first'): Result<true,
|
|
141
|
+
is(value: unknown, errors: 'first'): Result<true, PropertyValidation>;
|
|
142
142
|
/**
|
|
143
143
|
* Does the value match the schema?
|
|
144
144
|
*
|
|
145
|
-
* Will validate that the value matches the schema and return `true` or `false
|
|
145
|
+
* Will validate that the value matches the schema and return `true` if it's valid, or `false` if not
|
|
146
146
|
* @param value Value to validate
|
|
147
147
|
* @param options Validation options
|
|
148
148
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
149
149
|
*/
|
|
150
|
-
is(value: unknown, options: IsOptions<'none'
|
|
150
|
+
is(value: unknown, options: Partial<IsOptions<'none'>>): value is Model;
|
|
151
151
|
/**
|
|
152
152
|
* Does the value match the schema?
|
|
153
153
|
*
|
|
154
|
-
* Will validate that the value matches the schema and return `true` or `false
|
|
154
|
+
* Will validate that the value matches the schema and return `true` if it's valid, or `false` if not
|
|
155
155
|
* @param value Value to validate
|
|
156
156
|
* @param strict Validate if unknown keys are present in the object? _(defaults to `false`)_
|
|
157
157
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
@@ -174,6 +174,6 @@ declare function schema<Model extends Schematic>(schema: Model): Schema<Infer<Mo
|
|
|
174
174
|
* @returns A schema for the given typed schematic
|
|
175
175
|
*/
|
|
176
176
|
declare function schema<Model extends PlainObject>(schema: TypedSchematic<Model>): Schema<Model>;
|
|
177
|
-
declare const
|
|
177
|
+
declare const schemaHandlers: WeakMap<Schema<unknown>, ValidationHandler>;
|
|
178
178
|
//#endregion
|
|
179
|
-
export { Schema, schema,
|
|
179
|
+
export { Schema, schema, schemaHandlers };
|
package/dist/schema.mjs
CHANGED
|
@@ -1,40 +1,32 @@
|
|
|
1
1
|
import { PROPERTY_SCHEMA, SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE } from "./constants.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { isSchema } from "./helpers/misc.helper.mjs";
|
|
3
3
|
import { SchematicError } from "./models/validation.model.mjs";
|
|
4
|
-
import {
|
|
4
|
+
import { getResult, isResult } from "./helpers/result.helper.mjs";
|
|
5
|
+
import { getObjectHandler } from "./handler/object.handler.mjs";
|
|
5
6
|
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
6
|
-
import { error, ok } from "@oscarpalmer/atoms/result/misc";
|
|
7
7
|
//#region src/schema.ts
|
|
8
8
|
/**
|
|
9
9
|
* A schema for validating objects
|
|
10
10
|
*/
|
|
11
11
|
var Schema = class {
|
|
12
|
-
#
|
|
12
|
+
#handler;
|
|
13
13
|
constructor(validator) {
|
|
14
14
|
Object.defineProperty(this, PROPERTY_SCHEMA, { value: true });
|
|
15
|
-
this.#
|
|
16
|
-
|
|
15
|
+
this.#handler = validator;
|
|
16
|
+
schemaHandlers.set(this, validator);
|
|
17
17
|
}
|
|
18
18
|
get(value, options) {
|
|
19
|
-
|
|
20
|
-
const result = this.#validator(value, parameters, true);
|
|
21
|
-
if (result === true) return parameters.reporting.none || parameters.reporting.throw ? parameters.output : ok(parameters.output);
|
|
22
|
-
if (parameters.reporting.none) return;
|
|
23
|
-
return error(parameters.reporting.all ? result : result[0]);
|
|
19
|
+
return getResult(this.#handler, value, options);
|
|
24
20
|
}
|
|
25
21
|
is(value, options) {
|
|
26
|
-
|
|
27
|
-
const result = this.#validator(value, parameters, false);
|
|
28
|
-
if (result === true) return parameters.reporting.none || parameters.reporting.throw ? result : ok(result);
|
|
29
|
-
if (parameters.reporting.none) return false;
|
|
30
|
-
return error(parameters.reporting.all ? result : result[0]);
|
|
22
|
+
return isResult(this.#handler, value, options);
|
|
31
23
|
}
|
|
32
24
|
};
|
|
33
25
|
function schema(schema) {
|
|
34
26
|
if (isSchema(schema)) return schema;
|
|
35
27
|
if (!isPlainObject(schema)) throw new SchematicError(SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE);
|
|
36
|
-
return new Schema(
|
|
28
|
+
return new Schema(getObjectHandler(schema));
|
|
37
29
|
}
|
|
38
|
-
const
|
|
30
|
+
const schemaHandlers = /* @__PURE__ */ new WeakMap();
|
|
39
31
|
//#endregion
|
|
40
|
-
export { Schema, schema,
|
|
32
|
+
export { Schema, schema, schemaHandlers };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ValidationHandler, ValidationHandlerType, Validators, ValueValidation } from "./models/validation.model.mjs";
|
|
2
|
+
import { InferValidatorValue } from "./models/infer.model.mjs";
|
|
3
|
+
import { ValueType, Values } from "./models/misc.model.mjs";
|
|
4
|
+
import { Constructor } from "@oscarpalmer/atoms/models";
|
|
5
|
+
import { Result } from "@oscarpalmer/atoms/result/models";
|
|
6
|
+
|
|
7
|
+
//#region src/validator.d.ts
|
|
8
|
+
declare class Validator<Value> {
|
|
9
|
+
#private;
|
|
10
|
+
private readonly $validator;
|
|
11
|
+
constructor(handler: ValidationHandler, types: ValidationHandlerType[]);
|
|
12
|
+
/**
|
|
13
|
+
* Is the value valid?
|
|
14
|
+
*
|
|
15
|
+
* Will assert that the value is valid and throws an error if it does not
|
|
16
|
+
* @param value Value to validate
|
|
17
|
+
* @returns `true` if the value is valid, otherwise throws an error
|
|
18
|
+
*/
|
|
19
|
+
is(value: unknown, reporting: 'throw'): asserts value is Value;
|
|
20
|
+
/**
|
|
21
|
+
* Is the value valid?
|
|
22
|
+
*
|
|
23
|
+
* Will validate that the value is valid and return a result of `true` or validation information for the first validation failure
|
|
24
|
+
* @param value Value to validate
|
|
25
|
+
* @return Result holding `true` or validation information
|
|
26
|
+
*/
|
|
27
|
+
is(value: unknown, reporting: 'result'): Result<Value, ValueValidation>;
|
|
28
|
+
/**
|
|
29
|
+
* Is the value valid?
|
|
30
|
+
* @param value Value to validate
|
|
31
|
+
* @returns `true` if the value is valid, otherwise `false`
|
|
32
|
+
*/
|
|
33
|
+
is(value: unknown, reporting?: 'none'): value is Value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create a validator for value types
|
|
37
|
+
* @param types Types to validate against
|
|
38
|
+
* @param validators Custom validators to use for validation
|
|
39
|
+
* @returns Validator
|
|
40
|
+
*/
|
|
41
|
+
declare function validator<Types extends Array<Constructor | ((value: unknown) => boolean) | ValueType>>(types: Types, validators?: Validators): Validator<InferValidatorValue<Types>>;
|
|
42
|
+
/**
|
|
43
|
+
* Create a validator for a constructor
|
|
44
|
+
* @param constructor Constructor to validate against
|
|
45
|
+
* @returns Validator
|
|
46
|
+
*/
|
|
47
|
+
declare function validator<Instance>(constructor: Constructor<Instance>): Validator<Instance>;
|
|
48
|
+
/**
|
|
49
|
+
* Create a validator for a callback
|
|
50
|
+
* @param callback Callback for validation
|
|
51
|
+
* @returns Validator
|
|
52
|
+
*/
|
|
53
|
+
declare function validator<Value>(callback: (value: unknown) => value is Value): Validator<Value>;
|
|
54
|
+
/**
|
|
55
|
+
* Create a validator for a callback
|
|
56
|
+
* @param callback Callback for validation
|
|
57
|
+
* @returns Validator
|
|
58
|
+
*/
|
|
59
|
+
declare function validator<Value>(callback: (value: unknown) => boolean): Validator<Value>;
|
|
60
|
+
/**
|
|
61
|
+
* Create a validator for a type
|
|
62
|
+
* @param type Type to validate against
|
|
63
|
+
* @param validators Custom validators to use for validation
|
|
64
|
+
* @returns Validator
|
|
65
|
+
*/
|
|
66
|
+
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]>;
|
|
67
|
+
/**
|
|
68
|
+
* Create a validator for value types
|
|
69
|
+
* @param types Types to validate against
|
|
70
|
+
* @param validators Custom validators to use for validation
|
|
71
|
+
* @returns Validator
|
|
72
|
+
*/
|
|
73
|
+
declare function validator<Types extends ValueType[]>(types: Types, validators?: Validators): Validator<unknown>;
|
|
74
|
+
/**
|
|
75
|
+
* Create a validator for an array of items
|
|
76
|
+
* @param type Type of items in the array
|
|
77
|
+
* @returns Validator
|
|
78
|
+
*/
|
|
79
|
+
declare function validator<Item>(type: 'array'): Validator<Item[]>;
|
|
80
|
+
declare const validatorHandlers: WeakMap<Validator<unknown>, ValidationHandler>;
|
|
81
|
+
declare const validatorTypes: WeakMap<Validator<unknown>, ValidationHandlerType[]>;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { Validator, validator, validatorHandlers, validatorTypes };
|