@oscarpalmer/jhunal 0.26.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 +170 -71
- package/dist/index.mjs +354 -215
- 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 +32 -32
- 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 +42 -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 -207
- package/dist/validator/schema.validator.d.mts +0 -7
- package/dist/validator/schema.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 -426
- package/src/validator/schema.validator.ts +0 -25
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type {Constructor} from '@oscarpalmer/atoms/models';
|
|
2
2
|
import type {Schema} from '../schema';
|
|
3
|
-
import type {
|
|
3
|
+
import type {Validator} from '../validator';
|
|
4
|
+
import type {ExtractValueTypes, ValueType, Values} from './misc.model';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* A generic schematic allowing nested schematics, {@link SchematicEntry} values, or arrays of {@link SchematicEntry} as values
|
|
7
8
|
*/
|
|
8
9
|
export type PlainSchematic = {
|
|
9
|
-
[key: string]:
|
|
10
|
+
[key: string]: SchematicEntry | SchematicEntry[];
|
|
10
11
|
} & {
|
|
11
12
|
$default?: never;
|
|
12
13
|
$required?: never;
|
|
@@ -31,14 +32,15 @@ export type Schematic = PlainSchematic;
|
|
|
31
32
|
/**
|
|
32
33
|
* A union of all valid types for a single schematic entry
|
|
33
34
|
*
|
|
34
|
-
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link
|
|
35
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link SchematicProperty}, {@link Schema}, {@link ValueType}, or a custom validator function
|
|
35
36
|
*/
|
|
36
37
|
export type SchematicEntry =
|
|
37
38
|
| Constructor
|
|
38
39
|
| PlainSchematic
|
|
39
40
|
| Schema<unknown>
|
|
40
41
|
| SchematicProperty
|
|
41
|
-
|
|
|
42
|
+
| Validator<unknown>
|
|
43
|
+
| ValueType
|
|
42
44
|
| ((value: unknown) => boolean);
|
|
43
45
|
|
|
44
46
|
/**
|
|
@@ -67,7 +69,7 @@ export type SchematicProperty = {
|
|
|
67
69
|
*/
|
|
68
70
|
$type: SchemaPropertyType | SchemaPropertyType[];
|
|
69
71
|
/**
|
|
70
|
-
* Optional validators keyed by {@link
|
|
72
|
+
* Optional validators keyed by {@link ValueType}, applied during validation
|
|
71
73
|
*/
|
|
72
74
|
$validators?: PropertyValidators<SchemaPropertyType | SchemaPropertyType[]>;
|
|
73
75
|
};
|
|
@@ -75,17 +77,18 @@ export type SchematicProperty = {
|
|
|
75
77
|
/**
|
|
76
78
|
* A union of valid types for a {@link SchematicProperty}'s `$type` field
|
|
77
79
|
*
|
|
78
|
-
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link
|
|
80
|
+
* Can be a {@link Constructor}, {@link PlainSchematic}, {@link Schema}, {@link ValueType} string, or a custom validator function
|
|
79
81
|
*/
|
|
80
82
|
export type SchemaPropertyType =
|
|
81
83
|
| Constructor
|
|
82
84
|
| PlainSchematic
|
|
83
85
|
| Schema<unknown>
|
|
84
|
-
|
|
|
86
|
+
| Validator<unknown>
|
|
87
|
+
| ValueType
|
|
85
88
|
| ((value: unknown) => boolean);
|
|
86
89
|
|
|
87
90
|
/**
|
|
88
|
-
* A map of optional validator functions keyed by {@link
|
|
91
|
+
* A map of optional validator functions keyed by {@link ValueType}, used to add custom validation to {@link SchemaProperty} definitions
|
|
89
92
|
*
|
|
90
93
|
* Each key may hold a single validator or an array of validators that receive the typed value
|
|
91
94
|
*
|
|
@@ -99,7 +102,7 @@ export type SchemaPropertyType =
|
|
|
99
102
|
* ```
|
|
100
103
|
*/
|
|
101
104
|
export type PropertyValidators<Value> = {
|
|
102
|
-
[Key in
|
|
105
|
+
[Key in ExtractValueTypes<Value>]?:
|
|
103
106
|
| ((value: Values[Key]) => boolean)
|
|
104
107
|
| Array<(value: Values[Key]) => boolean>;
|
|
105
108
|
};
|
|
@@ -63,11 +63,11 @@ export type TypedPropertyRequired<Value> = {
|
|
|
63
63
|
export type TypedSchematic<Model extends PlainObject> = Simplify<
|
|
64
64
|
{
|
|
65
65
|
[Key in RequiredKeys<Model>]: Model[Key] extends PlainObject
|
|
66
|
-
? Schema<Model[Key]>
|
|
66
|
+
? Schema<Model[Key]> | TypedSchematic<Model[Key]>
|
|
67
67
|
: ToSchemaType<Model[Key]> | TypedPropertyRequired<Model[Key]>;
|
|
68
68
|
} & {
|
|
69
69
|
[Key in OptionalKeys<Model>]: Exclude<Model[Key], undefined> extends PlainObject
|
|
70
|
-
?
|
|
71
|
-
:
|
|
70
|
+
? TypedPropertyOptional<Model[Key]>
|
|
71
|
+
: never;
|
|
72
72
|
}
|
|
73
73
|
>;
|
|
@@ -45,7 +45,7 @@ export type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject
|
|
|
45
45
|
: ToValueType<Value>;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
* Converts a TypeScript type to its {@link
|
|
48
|
+
* Converts a TypeScript type to its {@link ValueType} representation, suitable for use as a top-level schema entry
|
|
49
49
|
*
|
|
50
50
|
* @template Value Type to convert
|
|
51
51
|
*/
|
|
@@ -54,7 +54,7 @@ export type ToSchemaType<Value> = UnwrapSingle<
|
|
|
54
54
|
>;
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
* Maps a type to its {@link
|
|
57
|
+
* Maps a type to its {@link ValueType} string equivalent
|
|
58
58
|
*
|
|
59
59
|
* 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
|
|
60
60
|
*
|
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
import type {GenericCallback, PlainObject} from '@oscarpalmer/atoms/models';
|
|
2
2
|
import {join} from '@oscarpalmer/atoms/string';
|
|
3
|
-
import {NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION} from '../constants';
|
|
3
|
+
import {NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_ERROR_VALIDATOR} from '../constants';
|
|
4
4
|
import type {Schema} from '../schema';
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
// #region Named validation
|
|
8
|
-
|
|
9
|
-
export type NamedValidatorHandlers = {
|
|
10
|
-
[Key in ValueName]?: Array<(value: unknown) => boolean>;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type NamedValidators = Record<ValueName, (value: unknown) => boolean>;
|
|
14
|
-
|
|
15
|
-
// #endregion
|
|
5
|
+
import type {Validator} from '../validator';
|
|
6
|
+
import type {ValueType} from './misc.model';
|
|
16
7
|
|
|
17
8
|
// #region Reporting
|
|
18
9
|
|
|
@@ -24,11 +15,11 @@ export type ReportingInformation = Record<ReportingType, boolean> & {
|
|
|
24
15
|
* Controls how validation failures are reported
|
|
25
16
|
*
|
|
26
17
|
* - `'none'`, returns a boolean _(default)_
|
|
27
|
-
* - `'first'`, returns the first failure as a `Result`
|
|
18
|
+
* - `'first'` or `'result'`, returns the first failure as a `Result`
|
|
28
19
|
* - `'all'`, returns all failures as a `Result` _(from same level)_
|
|
29
20
|
* - `'throw'`, throws a {@link ValidationError} on failure
|
|
30
21
|
*/
|
|
31
|
-
export type ReportingType = 'all' | 'first' | 'none' | 'throw';
|
|
22
|
+
export type ReportingType = 'all' | 'first' | 'none' | 'result' | 'throw';
|
|
32
23
|
|
|
33
24
|
// #endregion
|
|
34
25
|
|
|
@@ -49,7 +40,7 @@ export class SchematicError extends Error {
|
|
|
49
40
|
* Thrown in `'throw'` mode when one or more properties fail validation; `information` holds all failures
|
|
50
41
|
*/
|
|
51
42
|
export class ValidationError extends Error {
|
|
52
|
-
constructor(readonly information:
|
|
43
|
+
constructor(readonly information: PropertyValidation[]) {
|
|
53
44
|
super(
|
|
54
45
|
join(
|
|
55
46
|
information.map(item => item.message),
|
|
@@ -61,32 +52,57 @@ export class ValidationError extends Error {
|
|
|
61
52
|
}
|
|
62
53
|
}
|
|
63
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Thrown when a validator definition is invalid
|
|
57
|
+
*/
|
|
58
|
+
export class ValidatorError extends Error {
|
|
59
|
+
constructor(message: string) {
|
|
60
|
+
super(message);
|
|
61
|
+
|
|
62
|
+
this.name = NAME_ERROR_VALIDATOR;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
64
66
|
// #endregion
|
|
65
67
|
|
|
66
68
|
// #region Results
|
|
67
69
|
|
|
68
70
|
/**
|
|
69
|
-
* Describes a single validation failure
|
|
71
|
+
* Describes a single property validation failure
|
|
70
72
|
*/
|
|
71
|
-
export type
|
|
72
|
-
/**
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
export type PropertyValidation = {
|
|
74
|
+
/**
|
|
75
|
+
* The key path of the property that failed
|
|
76
|
+
*/
|
|
77
|
+
key?: PropertyValidationKey;
|
|
78
|
+
/**
|
|
79
|
+
* Human-readable description of the failure
|
|
80
|
+
*/
|
|
75
81
|
message: string;
|
|
76
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* The validator function that failed, if the failure was from a `$validators` entry
|
|
84
|
+
*/
|
|
77
85
|
validator?: GenericCallback;
|
|
78
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* The value that was provided
|
|
88
|
+
*/
|
|
79
89
|
value: unknown;
|
|
80
90
|
};
|
|
81
91
|
|
|
82
92
|
/**
|
|
83
|
-
*
|
|
93
|
+
* 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
|
|
84
94
|
*/
|
|
85
|
-
export type
|
|
95
|
+
export type PropertyValidationKey = {
|
|
86
96
|
full: string;
|
|
87
97
|
short: string;
|
|
88
98
|
};
|
|
89
99
|
|
|
100
|
+
export type ValueValidation = {
|
|
101
|
+
message: string;
|
|
102
|
+
validator?: GenericCallback;
|
|
103
|
+
value: unknown;
|
|
104
|
+
};
|
|
105
|
+
|
|
90
106
|
// #endregion
|
|
91
107
|
|
|
92
108
|
// #region Options
|
|
@@ -119,34 +135,56 @@ export type IsOptions<Errors extends ReportingType> = BaseOptions<Errors>;
|
|
|
119
135
|
|
|
120
136
|
// #endregion
|
|
121
137
|
|
|
122
|
-
// #region
|
|
138
|
+
// #region Type validation
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Object property validators
|
|
142
|
+
*/
|
|
143
|
+
export type Validators = {
|
|
144
|
+
[Key in ValueType]?: Array<(value: unknown) => boolean>;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Base type validators
|
|
149
|
+
*/
|
|
150
|
+
export type TypeValidators = Record<ValueType, (value: unknown) => boolean>;
|
|
151
|
+
|
|
152
|
+
// #endregion
|
|
153
|
+
|
|
154
|
+
// #region Validation handler
|
|
123
155
|
|
|
124
|
-
export type
|
|
156
|
+
export type ValidationHandler = (
|
|
125
157
|
input: unknown,
|
|
126
|
-
parameters:
|
|
158
|
+
parameters: ValidationHandlerParameters,
|
|
127
159
|
get: boolean,
|
|
128
|
-
) => true |
|
|
160
|
+
) => true | PropertyValidation[];
|
|
129
161
|
|
|
130
|
-
export type
|
|
162
|
+
export type ValidationHandlerDefaults = {
|
|
131
163
|
value: unknown;
|
|
132
164
|
};
|
|
133
165
|
|
|
134
|
-
export type
|
|
135
|
-
defaults
|
|
136
|
-
|
|
166
|
+
export type ValidationHandlerItem = {
|
|
167
|
+
defaults?: ValidationHandlerDefaults;
|
|
168
|
+
handler: ValidationHandler;
|
|
169
|
+
key: PropertyValidationKey;
|
|
137
170
|
required: boolean;
|
|
138
|
-
types:
|
|
139
|
-
validator: Validator;
|
|
171
|
+
types: ValidationHandlerType[];
|
|
140
172
|
};
|
|
141
173
|
|
|
142
|
-
export type
|
|
174
|
+
export type ValidationHandlerParameters = {
|
|
143
175
|
clone: boolean;
|
|
144
|
-
information?:
|
|
176
|
+
information?: PropertyValidation[];
|
|
177
|
+
key?: string;
|
|
145
178
|
output: PlainObject;
|
|
146
179
|
reporting: ReportingInformation;
|
|
147
180
|
strict: boolean;
|
|
148
181
|
};
|
|
149
182
|
|
|
150
|
-
export type
|
|
183
|
+
export type ValidationHandlerType =
|
|
184
|
+
| Function
|
|
185
|
+
| PlainObject
|
|
186
|
+
| Schema<unknown>
|
|
187
|
+
| Validator<unknown>
|
|
188
|
+
| ValueType;
|
|
151
189
|
|
|
152
190
|
// #endregion
|
package/src/schema.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
2
|
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
3
|
-
import {error, ok} from '@oscarpalmer/atoms/result/misc';
|
|
4
3
|
import type {Result} from '@oscarpalmer/atoms/result/models';
|
|
5
4
|
import {PROPERTY_SCHEMA, SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE} from './constants';
|
|
6
|
-
import {
|
|
5
|
+
import {getObjectHandler} from './handler/object.handler';
|
|
6
|
+
import {isSchema} from './helpers/misc.helper';
|
|
7
|
+
import {getResult, isResult} from './helpers/result.helper';
|
|
7
8
|
import type {Infer} from './models/infer.model';
|
|
8
9
|
import type {Schematic} from './models/schematic.plain.model';
|
|
9
10
|
import type {TypedSchematic} from './models/schematic.typed.model';
|
|
@@ -11,10 +12,9 @@ import {
|
|
|
11
12
|
SchematicError,
|
|
12
13
|
type GetOptions,
|
|
13
14
|
type IsOptions,
|
|
14
|
-
type
|
|
15
|
-
type
|
|
15
|
+
type ValidationHandler,
|
|
16
|
+
type PropertyValidation,
|
|
16
17
|
} from './models/validation.model';
|
|
17
|
-
import {getObjectValidator} from './validator/object.validator';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* A schema for validating objects
|
|
@@ -22,116 +22,100 @@ import {getObjectValidator} from './validator/object.validator';
|
|
|
22
22
|
export class Schema<Model> {
|
|
23
23
|
declare private readonly $schema: true;
|
|
24
24
|
|
|
25
|
-
#
|
|
25
|
+
readonly #handler: ValidationHandler;
|
|
26
26
|
|
|
27
|
-
constructor(validator:
|
|
27
|
+
constructor(validator: ValidationHandler) {
|
|
28
28
|
Object.defineProperty(this, PROPERTY_SCHEMA, {
|
|
29
29
|
value: true,
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
this.#
|
|
32
|
+
this.#handler = validator;
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
schemaHandlers.set(this, validator);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Parse a value according to the schema
|
|
39
39
|
*
|
|
40
|
-
* Returns
|
|
40
|
+
* Returns value _(deeply cloned, by default)_ or throws an error for the first property that fails validation
|
|
41
41
|
* @param value Value to parse
|
|
42
42
|
* @param options Validation options
|
|
43
|
-
* @returns
|
|
43
|
+
* @returns Value, if it matches the schema, otherwise throws an error
|
|
44
44
|
*/
|
|
45
45
|
get(value: unknown, options: GetOptions<'throw'>): Model;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Parse a value according to the schema
|
|
49
49
|
*
|
|
50
|
-
* Returns
|
|
50
|
+
* Returns value _(deeply cloned, by default)_ or throws an error for the first property that fails validation
|
|
51
51
|
* @param value Value to parse
|
|
52
52
|
* @param errors Reporting type
|
|
53
|
-
* @returns
|
|
53
|
+
* @returns Value, if it matches the schema, otherwise throws an error
|
|
54
54
|
*/
|
|
55
55
|
get(value: unknown, errors: 'throw'): Model;
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Parse a value according to the schema
|
|
59
59
|
*
|
|
60
|
-
* Returns
|
|
60
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for validation failures from the same depth in the value
|
|
61
61
|
* @param value Value to parse
|
|
62
62
|
* @param options Validation options
|
|
63
|
-
* @returns Result holding
|
|
63
|
+
* @returns Result holding value or all validation information
|
|
64
64
|
*/
|
|
65
|
-
get(value: unknown, options: GetOptions<'all'>): Result<Model,
|
|
65
|
+
get(value: unknown, options: GetOptions<'all'>): Result<Model, PropertyValidation[]>;
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* Parse a value according to the schema
|
|
69
69
|
*
|
|
70
|
-
* Returns
|
|
70
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for validation failures from the same depth in the value
|
|
71
71
|
* @param value Value to parse
|
|
72
72
|
* @param errors Reporting type
|
|
73
|
-
* @returns Result holding
|
|
73
|
+
* @returns Result holding value or all validation information
|
|
74
74
|
*/
|
|
75
|
-
get(value: unknown, errors: 'all'): Result<Model,
|
|
75
|
+
get(value: unknown, errors: 'all'): Result<Model, PropertyValidation[]>;
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
78
|
* Parse a value according to the schema
|
|
79
79
|
*
|
|
80
|
-
* Returns
|
|
80
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for the first failing property
|
|
81
81
|
* @param value Value to parse
|
|
82
82
|
* @param options Validation options
|
|
83
|
-
* @returns Result holding
|
|
83
|
+
* @returns Result holding value or all validation information
|
|
84
84
|
*/
|
|
85
|
-
get(value: unknown, options: GetOptions<'first'>): Result<Model,
|
|
85
|
+
get(value: unknown, options: GetOptions<'first'>): Result<Model, PropertyValidation>;
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
88
|
* Parse a value according to the schema
|
|
89
89
|
*
|
|
90
|
-
* Returns
|
|
90
|
+
* Returns value _(deeply cloned, by default)_ or all validation information for the first failing property
|
|
91
91
|
* @param value Value to parse
|
|
92
92
|
* @param errors Reporting type
|
|
93
|
-
* @returns Result holding
|
|
93
|
+
* @returns Result holding value or all validation information
|
|
94
94
|
*/
|
|
95
|
-
get(value: unknown, errors: 'first'): Result<Model,
|
|
95
|
+
get(value: unknown, errors: 'first'): Result<Model, PropertyValidation>;
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
98
|
* Parse a value according to the schema
|
|
99
99
|
*
|
|
100
|
-
* Returns
|
|
100
|
+
* Returns value _(deeply cloned, by default)_ or `undefined` if the value does not match the schema
|
|
101
101
|
* @param value Value to parse
|
|
102
102
|
* @param options Validation options
|
|
103
|
-
* @returns
|
|
103
|
+
* @returns Value, or `undefined` if it's invalid
|
|
104
104
|
*/
|
|
105
105
|
get(value: unknown, options: Partial<GetOptions<'none'>>): Model | undefined;
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
108
|
* Parse a value according to the schema
|
|
109
109
|
*
|
|
110
|
-
* Returns
|
|
110
|
+
* Returns value _(deeply cloned, by default)_ or `undefined` if the value does not match the schema
|
|
111
111
|
* @param value Value to parse
|
|
112
112
|
* @param strict Validate if unknown keys are present in the object? _(defaults to `false`)_
|
|
113
|
-
* @returns
|
|
113
|
+
* @returns Value, or `undefined` if it's invalid
|
|
114
114
|
*/
|
|
115
115
|
get(value: unknown, strict?: true): Model | undefined;
|
|
116
116
|
|
|
117
117
|
get(value: unknown, options?: unknown): unknown {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const result = this.#validator(value, parameters, true);
|
|
121
|
-
|
|
122
|
-
if (result === true) {
|
|
123
|
-
return parameters.reporting.none || parameters.reporting.throw
|
|
124
|
-
? parameters.clone
|
|
125
|
-
? parameters.output
|
|
126
|
-
: value
|
|
127
|
-
: ok(parameters.clone ? parameters.output : value);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (parameters.reporting.none) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return error(parameters.reporting.all ? result : result[0]);
|
|
118
|
+
return getResult(this.#handler, value, options);
|
|
135
119
|
}
|
|
136
120
|
|
|
137
121
|
/**
|
|
@@ -162,7 +146,7 @@ export class Schema<Model> {
|
|
|
162
146
|
* @param options Validation options
|
|
163
147
|
* @returns Result holding `true` or all validation information
|
|
164
148
|
*/
|
|
165
|
-
is(value: unknown, options: IsOptions<'all'>): Result<true,
|
|
149
|
+
is(value: unknown, options: IsOptions<'all'>): Result<true, PropertyValidation[]>;
|
|
166
150
|
|
|
167
151
|
/**
|
|
168
152
|
* Does the value match the schema?
|
|
@@ -172,7 +156,7 @@ export class Schema<Model> {
|
|
|
172
156
|
* @param errors Reporting type
|
|
173
157
|
* @returns Result holding `true` or all validation information
|
|
174
158
|
*/
|
|
175
|
-
is(value: unknown, errors: 'all'): Result<true,
|
|
159
|
+
is(value: unknown, errors: 'all'): Result<true, PropertyValidation[]>;
|
|
176
160
|
|
|
177
161
|
/**
|
|
178
162
|
* Does the value match the schema?
|
|
@@ -180,9 +164,9 @@ export class Schema<Model> {
|
|
|
180
164
|
* Will validate that the value matches the schema and return a result of `true` or all validation information for the first failing property
|
|
181
165
|
* @param value Value to validate
|
|
182
166
|
* @param options Validation options
|
|
183
|
-
* @returns `true`
|
|
167
|
+
* @returns Result holding `true` or all validation information
|
|
184
168
|
*/
|
|
185
|
-
is(value: unknown, options: IsOptions<'first'>): Result<true,
|
|
169
|
+
is(value: unknown, options: IsOptions<'first'>): Result<true, PropertyValidation>;
|
|
186
170
|
|
|
187
171
|
/**
|
|
188
172
|
* Does the value match the schema?
|
|
@@ -190,14 +174,14 @@ export class Schema<Model> {
|
|
|
190
174
|
* Will validate that the value matches the schema and return a result of `true` or all validation information for the first failing property
|
|
191
175
|
* @param value Value to validate
|
|
192
176
|
* @param errors Reporting type
|
|
193
|
-
* @returns `true`
|
|
177
|
+
* @returns Result holding `true` or all validation information
|
|
194
178
|
*/
|
|
195
|
-
is(value: unknown, errors: 'first'): Result<true,
|
|
179
|
+
is(value: unknown, errors: 'first'): Result<true, PropertyValidation>;
|
|
196
180
|
|
|
197
181
|
/**
|
|
198
182
|
* Does the value match the schema?
|
|
199
183
|
*
|
|
200
|
-
* Will validate that the value matches the schema and return `true` or `false
|
|
184
|
+
* Will validate that the value matches the schema and return `true` if it's valid, or `false` if not
|
|
201
185
|
* @param value Value to validate
|
|
202
186
|
* @param options Validation options
|
|
203
187
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
@@ -207,7 +191,7 @@ export class Schema<Model> {
|
|
|
207
191
|
/**
|
|
208
192
|
* Does the value match the schema?
|
|
209
193
|
*
|
|
210
|
-
* Will validate that the value matches the schema and return `true` or `false
|
|
194
|
+
* Will validate that the value matches the schema and return `true` if it's valid, or `false` if not
|
|
211
195
|
* @param value Value to validate
|
|
212
196
|
* @param strict Validate if unknown keys are present in the object? _(defaults to `false`)_
|
|
213
197
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
@@ -215,19 +199,7 @@ export class Schema<Model> {
|
|
|
215
199
|
is(value: unknown, strict?: true): value is Model;
|
|
216
200
|
|
|
217
201
|
is(value: unknown, options?: unknown): unknown {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const result = this.#validator(value, parameters, false);
|
|
221
|
-
|
|
222
|
-
if (result === true) {
|
|
223
|
-
return parameters.reporting.none || parameters.reporting.throw ? result : ok(result);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (parameters.reporting.none) {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return error(parameters.reporting.all ? result : result[0]);
|
|
202
|
+
return isResult(this.#handler, value, options);
|
|
231
203
|
}
|
|
232
204
|
}
|
|
233
205
|
|
|
@@ -251,14 +223,14 @@ export function schema<Model extends PlainObject>(schema: TypedSchematic<Model>)
|
|
|
251
223
|
|
|
252
224
|
export function schema<Model extends Schematic>(schema: Model): Schema<Model> {
|
|
253
225
|
if (isSchema(schema)) {
|
|
254
|
-
return schema
|
|
226
|
+
return schema as Schema<Model>;
|
|
255
227
|
}
|
|
256
228
|
|
|
257
229
|
if (!isPlainObject(schema)) {
|
|
258
230
|
throw new SchematicError(SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE);
|
|
259
231
|
}
|
|
260
232
|
|
|
261
|
-
return new Schema<Model>(
|
|
233
|
+
return new Schema<Model>(getObjectHandler(schema));
|
|
262
234
|
}
|
|
263
235
|
|
|
264
|
-
export const
|
|
236
|
+
export const schemaHandlers = new WeakMap<Schema<unknown>, ValidationHandler>();
|