@cleverbrush/schema 1.1.11 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1721 -104
- package/dist/builders/AnySchemaBuilder.d.ts +54 -14
- package/dist/builders/AnySchemaBuilder.js +2 -112
- package/dist/builders/AnySchemaBuilder.js.map +1 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +126 -23
- package/dist/builders/ArraySchemaBuilder.js +2 -284
- package/dist/builders/ArraySchemaBuilder.js.map +1 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +109 -19
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +154 -34
- package/dist/builders/DateSchemaBuilder.js +2 -433
- package/dist/builders/DateSchemaBuilder.js.map +1 -0
- package/dist/builders/ExternSchemaBuilder.d.ts +202 -0
- package/dist/builders/ExternSchemaBuilder.js +2 -0
- package/dist/builders/ExternSchemaBuilder.js.map +1 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +205 -18
- package/dist/builders/FunctionSchemaBuilder.js +2 -113
- package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
- package/dist/builders/GenericSchemaBuilder.d.ts +294 -0
- package/dist/builders/LazySchemaBuilder.d.ts +169 -0
- package/dist/builders/NullSchemaBuilder.d.ts +162 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -31
- package/dist/builders/NumberSchemaBuilder.js +2 -386
- package/dist/builders/NumberSchemaBuilder.js.map +1 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +486 -61
- package/dist/builders/ObjectSchemaBuilder.js +2 -589
- package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
- package/dist/builders/ParseStringSchemaBuilder.d.ts +204 -0
- package/dist/builders/ParseStringSchemaBuilder.js +2 -0
- package/dist/builders/ParseStringSchemaBuilder.js.map +1 -0
- package/dist/builders/PromiseSchemaBuilder.d.ts +213 -0
- package/dist/builders/PromiseSchemaBuilder.js +2 -0
- package/dist/builders/PromiseSchemaBuilder.js.map +1 -0
- package/dist/builders/PropertyValidationResult.d.ts +68 -0
- package/dist/builders/RecordSchemaBuilder.d.ts +343 -0
- package/dist/builders/RecordSchemaBuilder.js +2 -0
- package/dist/builders/RecordSchemaBuilder.js.map +1 -0
- package/dist/builders/SchemaBuilder.d.ts +907 -30
- package/dist/builders/StringSchemaBuilder.d.ts +154 -37
- package/dist/builders/StringSchemaBuilder.js +2 -414
- package/dist/builders/StringSchemaBuilder.js.map +1 -0
- package/dist/builders/TupleSchemaBuilder.d.ts +250 -0
- package/dist/builders/TupleSchemaBuilder.js +2 -0
- package/dist/builders/TupleSchemaBuilder.js.map +1 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +141 -39
- package/dist/builders/UnionSchemaBuilder.js +2 -216
- package/dist/builders/UnionSchemaBuilder.js.map +1 -0
- package/dist/chunk-3JMDGYDT.js +2 -0
- package/dist/chunk-3JMDGYDT.js.map +1 -0
- package/dist/chunk-BUEVZ3KA.js +2 -0
- package/dist/chunk-BUEVZ3KA.js.map +1 -0
- package/dist/chunk-CFIJQ4GP.js +2 -0
- package/dist/chunk-CFIJQ4GP.js.map +1 -0
- package/dist/chunk-DY7J6RNN.js +2 -0
- package/dist/chunk-DY7J6RNN.js.map +1 -0
- package/dist/chunk-EIVZX4ZO.js +2 -0
- package/dist/chunk-EIVZX4ZO.js.map +1 -0
- package/dist/chunk-GXPV6UQK.js +2 -0
- package/dist/chunk-GXPV6UQK.js.map +1 -0
- package/dist/chunk-HN774HD7.js +2 -0
- package/dist/chunk-HN774HD7.js.map +1 -0
- package/dist/chunk-K6Z47OQY.js +2 -0
- package/dist/chunk-K6Z47OQY.js.map +1 -0
- package/dist/chunk-NUW3VXZV.js +2 -0
- package/dist/chunk-NUW3VXZV.js.map +1 -0
- package/dist/chunk-PHE4LIAN.js +2 -0
- package/dist/chunk-PHE4LIAN.js.map +1 -0
- package/dist/chunk-QARCEYGO.js +2 -0
- package/dist/chunk-QARCEYGO.js.map +1 -0
- package/dist/chunk-WDMJBGBD.js +2 -0
- package/dist/chunk-WDMJBGBD.js.map +1 -0
- package/dist/chunk-WQDYWDOE.js +2 -0
- package/dist/chunk-WQDYWDOE.js.map +1 -0
- package/dist/chunk-YQZHDMRF.js +2 -0
- package/dist/chunk-YQZHDMRF.js.map +1 -0
- package/dist/chunk-ZC6YBKCP.js +2 -0
- package/dist/chunk-ZC6YBKCP.js.map +1 -0
- package/dist/chunk-ZFI27R3L.js +2 -0
- package/dist/chunk-ZFI27R3L.js.map +1 -0
- package/dist/core.d.ts +28 -0
- package/dist/core.js +2 -0
- package/dist/core.js.map +1 -0
- package/dist/extension.d.ts +421 -0
- package/dist/extensions/array.d.ts +112 -0
- package/dist/extensions/enum.d.ts +190 -0
- package/dist/extensions/index.d.ts +112 -0
- package/dist/extensions/nullable.d.ts +26 -0
- package/dist/extensions/number.d.ts +228 -0
- package/dist/extensions/string.d.ts +332 -0
- package/dist/extensions/util.d.ts +45 -0
- package/dist/index.d.ts +10 -20
- package/dist/index.js +2 -19
- package/dist/index.js.map +1 -0
- package/dist/utils/transaction.d.ts +27 -4
- package/package.json +83 -7
- package/dist/builders/SchemaBuilder.js +0 -275
- package/dist/utils/transaction.js +0 -178
|
@@ -1,18 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { ObjectSchemaBuilder, ObjectSchemaValidationResult } from './ObjectSchemaBuilder.js';
|
|
2
|
+
import { type BRAND, type InferType, type NestedValidationResult, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
3
|
+
type UnionSchemaBuilderCreateProps<T extends readonly SchemaBuilder<any, any, any, any, any>[], R extends boolean = true> = Partial<ReturnType<UnionSchemaBuilder<T, R>['introspect']>>;
|
|
4
|
+
/**
|
|
5
|
+
* Mapped tuple type that converts a tuple of SchemaBuilder options into
|
|
6
|
+
* a tuple of their corresponding validation results.
|
|
7
|
+
* Union schema options get `UnionSchemaValidationResult` with recursive
|
|
8
|
+
* `getNestedErrors` navigation; object schema options get
|
|
9
|
+
* `ObjectSchemaValidationResult`; other types get `ValidationResult`.
|
|
10
|
+
*/
|
|
11
|
+
export type OptionValidationResults<TOptions extends readonly SchemaBuilder<any, any, any, any, any>[]> = {
|
|
12
|
+
[K in keyof TOptions]: TOptions[K] extends UnionSchemaBuilder<infer UOptions extends readonly SchemaBuilder<any, any, any, any, any>[], any, any> ? UnionSchemaValidationResult<InferType<TOptions[K]>, UOptions> : TOptions[K] extends ObjectSchemaBuilder<any, any, any, any> ? ObjectSchemaValidationResult<InferType<TOptions[K]>, TOptions[K]> : ValidationResult<InferType<TOptions[K]>>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Validation result type returned by `UnionSchemaBuilder.validate()`.
|
|
16
|
+
* Extends `ValidationResult` with:
|
|
17
|
+
* - `getNestedErrors` for root-level union errors and per-branch validation results
|
|
18
|
+
*/
|
|
19
|
+
export type UnionSchemaValidationResult<T, TOptions extends readonly SchemaBuilder<any, any, any, any, any>[]> = ValidationResult<T> & {
|
|
20
|
+
/**
|
|
21
|
+
* Returns root-level union validation errors combined with
|
|
22
|
+
* per-branch validation results.
|
|
23
|
+
* The returned value has both `NestedValidationResult` properties
|
|
24
|
+
* (`errors`, `isValid`, `descriptor`, `seenValue`) and tuple-indexed
|
|
25
|
+
* branch results (`[0]`, `[1]`, etc.).
|
|
26
|
+
*/
|
|
27
|
+
getNestedErrors(): OptionValidationResults<TOptions> & NestedValidationResult<any, any, any>;
|
|
28
|
+
};
|
|
29
|
+
type SchemaArrayToUnion<TArr extends readonly SchemaBuilder<any, any, any, any, any>[]> = TArr['length'] extends 1 ? InferType<TArr[0]> : TArr extends readonly [
|
|
30
|
+
infer TFirst extends SchemaBuilder<any, any, any, any, any>,
|
|
31
|
+
...infer TRest extends SchemaBuilder<any, any, any, any, any>[]
|
|
6
32
|
] ? InferType<TFirst> | SchemaArrayToUnion<[...TRest]> : never;
|
|
7
|
-
type TakeBeforeIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex extends number> = TArr extends [
|
|
8
|
-
...infer TRest extends SchemaBuilder<any, any>[],
|
|
9
|
-
infer
|
|
33
|
+
type TakeBeforeIndex<TArr extends readonly SchemaBuilder<any, any, any, any, any>[], TIndex extends number> = TArr extends [
|
|
34
|
+
...infer TRest extends SchemaBuilder<any, any, any, any, any>[],
|
|
35
|
+
infer _ extends SchemaBuilder<any, any, any, any, any>
|
|
10
36
|
] ? TRest['length'] extends TIndex ? TRest : TakeBeforeIndex<TRest, TIndex> : never;
|
|
11
|
-
type TakeAfterIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex extends number, TAcc extends readonly SchemaBuilder<any, any>[] = []> = TArr extends [
|
|
12
|
-
...infer TRest extends SchemaBuilder<any, any>[],
|
|
13
|
-
infer TLast extends SchemaBuilder<any, any>
|
|
37
|
+
type TakeAfterIndex<TArr extends readonly SchemaBuilder<any, any, any, any, any>[], TIndex extends number, TAcc extends readonly SchemaBuilder<any, any, any, any, any>[] = []> = TArr extends [
|
|
38
|
+
...infer TRest extends SchemaBuilder<any, any, any, any, any>[],
|
|
39
|
+
infer TLast extends SchemaBuilder<any, any, any, any, any>
|
|
14
40
|
] ? TRest['length'] extends TIndex ? TAcc : TakeAfterIndex<TRest, TIndex, [TLast, ...TAcc]> : never;
|
|
15
|
-
type TakeExceptIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex extends number> = [...TakeBeforeIndex<TArr, TIndex>, ...TakeAfterIndex<TArr, TIndex>];
|
|
41
|
+
type TakeExceptIndex<TArr extends readonly SchemaBuilder<any, any, any, any, any>[], TIndex extends number> = [...TakeBeforeIndex<TArr, TIndex>, ...TakeAfterIndex<TArr, TIndex>];
|
|
16
42
|
/**
|
|
17
43
|
* Union schema builder class. Allows to create schemas
|
|
18
44
|
* containing alternatives. E.g. string | number | Date.
|
|
@@ -30,7 +56,7 @@ type TakeExceptIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex ext
|
|
|
30
56
|
* @example
|
|
31
57
|
* ```ts
|
|
32
58
|
* const schema = union(string('foo')).or(string('bar'));
|
|
33
|
-
* const result =
|
|
59
|
+
* const result = schema.validate('foo');
|
|
34
60
|
* // result.valid === true
|
|
35
61
|
* // result.object === 'foo'
|
|
36
62
|
* ```
|
|
@@ -38,7 +64,7 @@ type TakeExceptIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex ext
|
|
|
38
64
|
* @example
|
|
39
65
|
* ```ts
|
|
40
66
|
* const schema = union(string('foo')).or(string('bar'));
|
|
41
|
-
* const result =
|
|
67
|
+
* const result = schema.validate('baz');
|
|
42
68
|
* // result.valid === false
|
|
43
69
|
* ```
|
|
44
70
|
*
|
|
@@ -46,88 +72,164 @@ type TakeExceptIndex<TArr extends readonly SchemaBuilder<any, any>[], TIndex ext
|
|
|
46
72
|
* ```ts
|
|
47
73
|
* const schema = union(string('yes')).or(string('no')).or(number(0)).or(number(1));
|
|
48
74
|
* // equals to 'yes' | 'no' | 0 | 1 in TS
|
|
49
|
-
* const result =
|
|
75
|
+
* const result = schema.validate('yes');
|
|
50
76
|
* // result.valid === true
|
|
51
77
|
* // result.object === 'yes'
|
|
52
78
|
*
|
|
53
|
-
* const result2 =
|
|
79
|
+
* const result2 = schema.validate(0);
|
|
54
80
|
* // result2.valid === true
|
|
55
81
|
* // result2.object === 0
|
|
56
82
|
*
|
|
57
|
-
* const result3 =
|
|
83
|
+
* const result3 = schema.validate('baz');
|
|
58
84
|
* // result3.valid === false
|
|
59
85
|
*
|
|
60
|
-
* const result4 =
|
|
86
|
+
* const result4 = schema.validate(2);
|
|
61
87
|
* // result4.valid === false
|
|
62
88
|
* ```
|
|
63
89
|
*
|
|
64
90
|
* @see {@link union}
|
|
65
91
|
*/
|
|
66
|
-
export declare class UnionSchemaBuilder<TOptions extends readonly SchemaBuilder<any, any>[], TRequired extends boolean = true, TExplicitType = undefined> extends SchemaBuilder<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, TRequired> {
|
|
92
|
+
export declare class UnionSchemaBuilder<TOptions extends readonly SchemaBuilder<any, any, any, any, any>[], TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}> extends SchemaBuilder<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, TRequired, TNullable, THasDefault, TExtensions> {
|
|
67
93
|
#private;
|
|
68
|
-
|
|
69
|
-
|
|
94
|
+
/**
|
|
95
|
+
* @hidden
|
|
96
|
+
*/
|
|
97
|
+
static create(props: UnionSchemaBuilderCreateProps<any>): UnionSchemaBuilder<any, true, false, undefined, false, {}>;
|
|
98
|
+
protected constructor(props: UnionSchemaBuilderCreateProps<TOptions, TRequired>);
|
|
70
99
|
introspect(): {
|
|
71
100
|
/**
|
|
72
101
|
* Array of schemas participating in the union.
|
|
73
102
|
*/
|
|
74
103
|
options: TOptions;
|
|
104
|
+
/**
|
|
105
|
+
* When the union is a discriminated union (all branches are objects
|
|
106
|
+
* sharing a required property with unique literal values), this is
|
|
107
|
+
* the name of that property. `undefined` otherwise.
|
|
108
|
+
*/
|
|
109
|
+
discriminatorPropertyName: string | undefined;
|
|
75
110
|
type: string;
|
|
76
111
|
isRequired: boolean;
|
|
77
|
-
|
|
78
|
-
|
|
112
|
+
isNullable: boolean;
|
|
113
|
+
isReadonly: boolean;
|
|
114
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType>[];
|
|
115
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType>[];
|
|
116
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
117
|
+
extensions: {
|
|
118
|
+
[x: string]: unknown;
|
|
119
|
+
};
|
|
120
|
+
hasDefault: boolean;
|
|
121
|
+
defaultValue: (TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType) | (() => TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType) | undefined;
|
|
122
|
+
description: string | undefined;
|
|
123
|
+
schemaName: string | undefined;
|
|
124
|
+
hasCatch: boolean;
|
|
125
|
+
catchValue: (TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType) | (() => TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType) | undefined;
|
|
126
|
+
example: unknown;
|
|
79
127
|
};
|
|
128
|
+
/**
|
|
129
|
+
* Null is a legitimate JavaScript value that a union option (e.g.
|
|
130
|
+
* `NullSchemaBuilder`) may accept. Override the base-class behaviour so
|
|
131
|
+
* that a required union does **not** reject `null` during pre-validation;
|
|
132
|
+
* instead, each option gets the opportunity to validate it.
|
|
133
|
+
* @override
|
|
134
|
+
*/
|
|
135
|
+
protected get isNullRequiredViolation(): boolean;
|
|
136
|
+
/**
|
|
137
|
+
* @inheritdoc
|
|
138
|
+
*/
|
|
139
|
+
hasType<T>(_notUsed?: T): UnionSchemaBuilder<TOptions, true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
140
|
+
/**
|
|
141
|
+
* @inheritdoc
|
|
142
|
+
*/
|
|
143
|
+
clearHasType(): UnionSchemaBuilder<TOptions, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
144
|
+
/**
|
|
145
|
+
* Performs synchronous validation of the union schema over `object`. {@inheritDoc SchemaBuilder.validate}
|
|
146
|
+
*/
|
|
147
|
+
validate(object: TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, context?: ValidationContext): UnionSchemaValidationResult<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, TOptions>;
|
|
148
|
+
/**
|
|
149
|
+
* Performs async validation of the union schema over `object`. {@inheritDoc SchemaBuilder.validateAsync}
|
|
150
|
+
*/
|
|
151
|
+
validateAsync(object: TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, context?: ValidationContext): Promise<UnionSchemaValidationResult<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, TOptions>>;
|
|
152
|
+
/**
|
|
153
|
+
* Performs synchronous validation of the union schema over `object`.
|
|
154
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
155
|
+
* @param context Optional `ValidationContext` settings.
|
|
156
|
+
*/
|
|
157
|
+
protected _validate(object: TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, context?: ValidationContext): UnionSchemaValidationResult<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, TOptions>;
|
|
158
|
+
/**
|
|
159
|
+
* Performs async validation of the union schema over `object`.
|
|
160
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
161
|
+
* @param context Optional `ValidationContext` settings.
|
|
162
|
+
*/
|
|
163
|
+
protected _validateAsync(object: TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, context?: ValidationContext): Promise<UnionSchemaValidationResult<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType, TOptions>>;
|
|
164
|
+
protected createFromProps<T extends readonly SchemaBuilder<any, any, any, any, any>[], TReq extends boolean>(props: UnionSchemaBuilderCreateProps<T, TReq>): this;
|
|
80
165
|
/**
|
|
81
166
|
* @hidden
|
|
82
167
|
*/
|
|
83
|
-
|
|
168
|
+
required(errorMessage?: ValidationErrorMessageProvider): UnionSchemaBuilder<TOptions, true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
84
169
|
/**
|
|
85
170
|
* @hidden
|
|
86
171
|
*/
|
|
87
|
-
|
|
172
|
+
optional(): UnionSchemaBuilder<TOptions, false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
88
173
|
/**
|
|
89
|
-
*
|
|
90
|
-
* @param context Optional `ValidationContext` settings.
|
|
174
|
+
* @hidden
|
|
91
175
|
*/
|
|
92
|
-
|
|
93
|
-
protected createFromProps<T extends readonly SchemaBuilder<any, any>[], TReq extends boolean>(props: UnionSchemaBuilderCreateProps<T, TReq>): this;
|
|
176
|
+
default(value: (TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType) | (() => TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType)): UnionSchemaBuilder<TOptions, true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
94
177
|
/**
|
|
95
178
|
* @hidden
|
|
96
179
|
*/
|
|
97
|
-
|
|
180
|
+
clearDefault(): UnionSchemaBuilder<TOptions, TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
98
181
|
/**
|
|
99
182
|
* @hidden
|
|
100
183
|
*/
|
|
101
|
-
|
|
184
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): UnionSchemaBuilder<TOptions, TRequired, TNullable, (TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType) & {
|
|
185
|
+
readonly [K in BRAND]: TBrand;
|
|
186
|
+
}, THasDefault, TExtensions> & TExtensions;
|
|
187
|
+
/**
|
|
188
|
+
* @hidden
|
|
189
|
+
*/
|
|
190
|
+
readonly(): UnionSchemaBuilder<TOptions, TRequired, TNullable, Readonly<TExplicitType extends undefined ? SchemaArrayToUnion<TOptions> : TExplicitType>, THasDefault, TExtensions> & TExtensions;
|
|
102
191
|
/**
|
|
103
192
|
* Adds a new schema option described by `schema`.
|
|
104
193
|
* schema must be an instance of `SchemaBuilder` class ancestor.
|
|
105
194
|
* @param schema schema to be added as an option.
|
|
106
195
|
*/
|
|
107
|
-
or<T extends SchemaBuilder<any, any>>(schema: T): UnionSchemaBuilder<[
|
|
196
|
+
or<T extends SchemaBuilder<any, any, any, any, any>>(schema: T): UnionSchemaBuilder<[
|
|
197
|
+
...TOptions,
|
|
198
|
+
T
|
|
199
|
+
], TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
108
200
|
/**
|
|
109
201
|
* Removes option by its `index`. If `index` is out of bounds,
|
|
110
202
|
* an error is thrown.
|
|
111
203
|
* @param index index of the option, starting from `0`.
|
|
112
204
|
*/
|
|
113
|
-
removeOption<T extends number>(index: T): UnionSchemaBuilder<TakeExceptIndex<TOptions, T>, TRequired, TExplicitType
|
|
205
|
+
removeOption<T extends number>(index: T): UnionSchemaBuilder<TakeExceptIndex<TOptions, T>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
114
206
|
/**
|
|
115
207
|
* Removes first option from the union schema.
|
|
116
208
|
*/
|
|
117
209
|
removeFirstOption(): TOptions extends [
|
|
118
|
-
infer
|
|
119
|
-
...infer TRest extends SchemaBuilder<any, any>[]
|
|
120
|
-
] ? UnionSchemaBuilder<TRest, TRequired, TExplicitType> : never;
|
|
210
|
+
infer _,
|
|
211
|
+
...infer TRest extends SchemaBuilder<any, any, any, any, any>[]
|
|
212
|
+
] ? UnionSchemaBuilder<TRest, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions : never;
|
|
121
213
|
/**
|
|
122
214
|
* Removes all options and replaces them by single `schema` option.
|
|
123
215
|
* Equivalent to `union(schema)` function, but could be useful in some cases.
|
|
124
216
|
* @param schema schema to be added as a single option to the new schema.
|
|
125
217
|
*/
|
|
126
|
-
reset<T extends SchemaBuilder<any, any>>(schema: T): UnionSchemaBuilder<[
|
|
218
|
+
reset<T extends SchemaBuilder<any, any, any, any, any>>(schema: T): UnionSchemaBuilder<[
|
|
219
|
+
T
|
|
220
|
+
], TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
221
|
+
/**
|
|
222
|
+
* @hidden
|
|
223
|
+
*/
|
|
224
|
+
nullable(): UnionSchemaBuilder<TOptions, TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
225
|
+
/**
|
|
226
|
+
* @hidden
|
|
227
|
+
*/
|
|
228
|
+
notNullable(): UnionSchemaBuilder<TOptions, TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
127
229
|
}
|
|
128
230
|
/**
|
|
129
231
|
* Creates a union schema.
|
|
130
|
-
* @param schema required and will be considered as a first option for the union
|
|
232
|
+
* @param schema required and will be considered as a first option for the union schema.
|
|
131
233
|
*/
|
|
132
|
-
export declare const union: <T extends SchemaBuilder<any, any>>(schema: T) => UnionSchemaBuilder<[T]>;
|
|
234
|
+
export declare const union: <T extends SchemaBuilder<any, any, any, any, any>>(schema: T) => UnionSchemaBuilder<[T]>;
|
|
133
235
|
export {};
|
|
@@ -1,216 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Union schema builder class. Allows to create schemas
|
|
4
|
-
* containing alternatives. E.g. string | number | Date.
|
|
5
|
-
* Use it when you want to define a schema for a value
|
|
6
|
-
* that can be of different types. The type of the value
|
|
7
|
-
* will be determined by the first schema that succeeds
|
|
8
|
-
* validation. Any schema type can be supplied as variant.
|
|
9
|
-
* Which means that you are not limited to primitive types and
|
|
10
|
-
* can construct complex types as well, e.g. object | array.
|
|
11
|
-
*
|
|
12
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
13
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
14
|
-
* directly. Use {@link union | union()} function instead.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const schema = union(string('foo')).or(string('bar'));
|
|
19
|
-
* const result = await schema.validate('foo');
|
|
20
|
-
* // result.valid === true
|
|
21
|
-
* // result.object === 'foo'
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* const schema = union(string('foo')).or(string('bar'));
|
|
27
|
-
* const result = await schema.validate('baz');
|
|
28
|
-
* // result.valid === false
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* const schema = union(string('yes')).or(string('no')).or(number(0)).or(number(1));
|
|
34
|
-
* // equals to 'yes' | 'no' | 0 | 1 in TS
|
|
35
|
-
* const result = await schema.validate('yes');
|
|
36
|
-
* // result.valid === true
|
|
37
|
-
* // result.object === 'yes'
|
|
38
|
-
*
|
|
39
|
-
* const result2 = await schema.validate(0);
|
|
40
|
-
* // result2.valid === true
|
|
41
|
-
* // result2.object === 0
|
|
42
|
-
*
|
|
43
|
-
* const result3 = await schema.validate('baz');
|
|
44
|
-
* // result3.valid === false
|
|
45
|
-
*
|
|
46
|
-
* const result4 = await schema.validate(2);
|
|
47
|
-
* // result4.valid === false
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @see {@link union}
|
|
51
|
-
*/
|
|
52
|
-
export class UnionSchemaBuilder extends SchemaBuilder {
|
|
53
|
-
#options;
|
|
54
|
-
static create(props) {
|
|
55
|
-
return new UnionSchemaBuilder({
|
|
56
|
-
type: 'union',
|
|
57
|
-
...props
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
constructor(props) {
|
|
61
|
-
super(props);
|
|
62
|
-
if (Array.isArray(props.options)) {
|
|
63
|
-
this.#options = props.options;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
introspect() {
|
|
67
|
-
return {
|
|
68
|
-
...super.introspect(),
|
|
69
|
-
/**
|
|
70
|
-
* Array of schemas participating in the union.
|
|
71
|
-
*/
|
|
72
|
-
options: this.#options
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* @hidden
|
|
77
|
-
*/
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
79
|
-
hasType(notUsed) {
|
|
80
|
-
return this.createFromProps({
|
|
81
|
-
...this.introspect()
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* @hidden
|
|
86
|
-
*/
|
|
87
|
-
clearHasType() {
|
|
88
|
-
return this.createFromProps({
|
|
89
|
-
...this.introspect()
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Performs validion of the union schema over `object`.
|
|
94
|
-
* @param context Optional `ValidationContext` settings.
|
|
95
|
-
*/
|
|
96
|
-
async validate(object, context) {
|
|
97
|
-
const superResult = await super.preValidate(object, context);
|
|
98
|
-
const { valid, transaction: preValidationTransaction, context: prevalidationContext, errors } = superResult;
|
|
99
|
-
const { path } = prevalidationContext;
|
|
100
|
-
if (!valid) {
|
|
101
|
-
return {
|
|
102
|
-
valid,
|
|
103
|
-
errors
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
let { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
107
|
-
if (!this.isRequired &&
|
|
108
|
-
(typeof objToValidate === 'undefined' || objToValidate === null)) {
|
|
109
|
-
return {
|
|
110
|
-
valid: true,
|
|
111
|
-
object: objToValidate
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
let minErrorsCount = Number.MAX_SAFE_INTEGER;
|
|
115
|
-
let resultingErrors = [];
|
|
116
|
-
for (let i = 0; i < this.#options.length; i++) {
|
|
117
|
-
const { valid, errors, object: validatedOption } = await this.#options[i].validate(objToValidate, {
|
|
118
|
-
...prevalidationContext,
|
|
119
|
-
path: `${path}[option ${i}]`
|
|
120
|
-
});
|
|
121
|
-
if (valid) {
|
|
122
|
-
return {
|
|
123
|
-
valid: true,
|
|
124
|
-
object: validatedOption
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
if (Array.isArray(errors) &&
|
|
129
|
-
errors.length > 0 &&
|
|
130
|
-
errors.length < minErrorsCount) {
|
|
131
|
-
resultingErrors = errors;
|
|
132
|
-
minErrorsCount = errors.length;
|
|
133
|
-
}
|
|
134
|
-
objToValidate =
|
|
135
|
-
preValidationTransaction.rollback().validatedObject;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
valid: false,
|
|
140
|
-
errors: resultingErrors
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
createFromProps(props) {
|
|
144
|
-
return UnionSchemaBuilder.create(props);
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* @hidden
|
|
148
|
-
*/
|
|
149
|
-
required() {
|
|
150
|
-
return super.required();
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* @hidden
|
|
154
|
-
*/
|
|
155
|
-
optional() {
|
|
156
|
-
return super.optional();
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Adds a new schema option described by `schema`.
|
|
160
|
-
* schema must be an instance of `SchemaBuilder` class ancestor.
|
|
161
|
-
* @param schema schema to be added as an option.
|
|
162
|
-
*/
|
|
163
|
-
or(schema) {
|
|
164
|
-
if (!(schema instanceof SchemaBuilder)) {
|
|
165
|
-
throw new Error('schema must be an instance of the SchemaBuilder class');
|
|
166
|
-
}
|
|
167
|
-
return this.createFromProps({
|
|
168
|
-
...this.introspect(),
|
|
169
|
-
options: [...this.#options, schema]
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Removes option by its `index`. If `index` is out of bounds,
|
|
174
|
-
* an error is thrown.
|
|
175
|
-
* @param index index of the option, starting from `0`.
|
|
176
|
-
*/
|
|
177
|
-
removeOption(index) {
|
|
178
|
-
if (typeof index !== 'number' ||
|
|
179
|
-
index < 0 ||
|
|
180
|
-
index > this.#options.length) {
|
|
181
|
-
throw new Error('index must be >= 0 and <= count of the options');
|
|
182
|
-
}
|
|
183
|
-
return this.createFromProps({
|
|
184
|
-
...this.introspect(),
|
|
185
|
-
options: this.#options.filter((v, i) => i !== index)
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Removes first option from the union schema.
|
|
190
|
-
*/
|
|
191
|
-
removeFirstOption() {
|
|
192
|
-
return this.removeOption(0);
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Removes all options and replaces them by single `schema` option.
|
|
196
|
-
* Equivalent to `union(schema)` function, but could be useful in some cases.
|
|
197
|
-
* @param schema schema to be added as a single option to the new schema.
|
|
198
|
-
*/
|
|
199
|
-
reset(schema) {
|
|
200
|
-
if (!(schema instanceof SchemaBuilder)) {
|
|
201
|
-
throw new Error('schema must be an instance of the SchemaBuilder class');
|
|
202
|
-
}
|
|
203
|
-
return this.createFromProps({
|
|
204
|
-
...this.introspect(),
|
|
205
|
-
options: [schema]
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Creates a union schema.
|
|
211
|
-
* @param schema required and will be considered as a first option for the union shchema.
|
|
212
|
-
*/
|
|
213
|
-
export const union = (schema) => UnionSchemaBuilder.create({
|
|
214
|
-
isRequired: true,
|
|
215
|
-
options: [schema]
|
|
216
|
-
});
|
|
1
|
+
import{a,b}from"../chunk-WQDYWDOE.js";import"../chunk-3JMDGYDT.js";export{a as UnionSchemaBuilder,b as union};
|
|
2
|
+
//# sourceMappingURL=UnionSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var y=Symbol("transaction"),P=[Error,RegExp,Date],R={shouldNotWrapWithTransaction:u=>!!P.find(e=>u instanceof e)},h=(u,e)=>{let r={},a=new Map;e=Object.assign({},R,e||{});let{shouldNotWrapWithTransaction:i}=e,l=()=>!!Object.keys(r).find(s=>r[s]?.[y]?r[s][y].isDirty():!0)||a.size>0,c=()=>{let s={};Object.keys(u).forEach(t=>{s[t]=u[t]}),Object.keys(r).forEach(t=>{let o=r[t];if(o[y]){let{commit:d}=o[y];s[t]=d()}else s[t]=r[t]});for(let t of a.keys())delete s[t];return r={},a=new Map,s},p=()=>{for(let s in r){let t=r[s];t&&typeof t=="object"&&t[y]&&t[y].rollback()}return r={},a=new Map,u};if(Array.isArray(u)){let s=u.map(d=>typeof d=="object"&&d&&!i(d)?h(d).object:d),t=()=>s.map(d=>d&&typeof d[y]=="object"?d[y].commit():d),o=()=>!!s.find((d,f)=>d&&typeof d[y]=="object"?d[y].isDirty():s[f]!==u[f]);return Object.defineProperty(s,y,{writable:!1,configurable:!1,value:{initial:u,object:s,commit:t,rollback:()=>u,isDirty:o}}),{object:s,commit:t,rollback:()=>u,isDirty:o}}let n=new Proxy(u,{set:(s,t,o)=>s&&s[t]===o?(delete r[t],!0):(r[t]=o,a.delete(t),!0),ownKeys:s=>[...Object.keys(s).filter(t=>!a.has(t)),...Object.keys(r).filter(t=>!(t in s))],getOwnPropertyDescriptor:(s,t)=>{if(!a.has(t))return t in r?Object.getOwnPropertyDescriptor(r,t):Object.getOwnPropertyDescriptor(s,t)},has:(s,t)=>a.has(t)?!1:t in r?!0:t in s,get:(s,t)=>{if(typeof t=="symbol")return t===y?{initial:u,object:n,commit:c,rollback:p,isDirty:l}:s[t];if(t in r)return r[t];if(!a.has(t)){if(!g(s[t])&&typeof s[t]=="object"&&s[t]&&!i(s[t])){let{object:o}=h(s[t],e);return r[t]=o,o}return s[t]}},deleteProperty:(s,t)=>(t in r&&(r[t]&&typeof r[t][y]=="object"&&r[t][y].rollback(),delete r[t]),t in s&&a.set(t,!0),!0)});return{object:n,commit:c,rollback:p,isDirty:l}},T=u=>({object:u,commit:()=>u,rollback:()=>u,isDirty:()=>!1}),g=u=>u&&typeof u=="object"&&Object.hasOwn(u,y);var m=class extends Error{errors;constructor(e){let r=e.length>0?e.map(a=>a.message).join("; "):"Validation failed";super(r),this.name="SchemaValidationError",this.errors=e}},x=Symbol(),v=Symbol();function E(u,e,r,a){return Object.defineProperties(u,{seenValue:{get:e,enumerable:!1},errors:{get:r,enumerable:!1},isValid:{get:()=>r().length===0,enumerable:!1},descriptor:{get:a,enumerable:!1}}),u}var b=class{#d=!0;#a=!1;#y=!1;#p;#h;#e=[];#t=[];#n=!1;#T=!0;#c={};#f="base";#m="is required";#o="is required";#s=void 0;#i=void 0;#l=!1;#b=void 0;#u;get"~standard"(){if(this.#u)return this.#u;let e=this;return this.#u={version:1,vendor:"@cleverbrush/schema",validate(r){let a=e.validate(r);return a.valid?{value:a.object}:{issues:(a.errors??[]).map(i=>({message:i.message}))}}},this.#u}get type(){return this.#f}set type(e){if(typeof e!="string"||!e)throw new Error("value should be non empty string");this.#f=e}get preprocessors(){return this.#e}get validators(){return this.#t}get isRequired(){return this.#d}get isNullable(){return this.#a}set isRequired(e){if(typeof e!="boolean")throw new Error("should be a boolean value");this.#d=e}get requiredErrorMessage(){return this.#o}get hasDefault(){return this.#s!==void 0}get hasCatch(){return this.#l}resolveCatchValue(){return typeof this.#i=="function"?this.#i():this.#i}get isReadonly(){return this.#y}resolveDefaultValue(){return typeof this.#s=="function"?this.#s():this.#s}get canSkipPreValidation(){return this.#T}get isNullRequiredViolation(){return!0}#P(e,r){let a=r?.doNotStopOnFirstError??!1,i={doNotStopOnFirstError:a,rootPropertyDescriptor:r?.rootPropertyDescriptor,currentPropertyDescriptor:r?.currentPropertyDescriptor},l=this.#n;return{doNotStopOnFirstError:a,resultingContext:i,transaction:l?h({validatedObject:e}):T({validatedObject:e}),errors:[]}}#r(e,r){return{valid:!1,errors:[e[0]].filter(a=>a),context:r}}#R(e,r,a){return Array.isArray(a)&&a.length?a:[{message:`Validator #${e}${r?` (${r})`:""} didn't pass.`}]}#g(e,r,a,i){return e.length>0?{valid:!1,errors:e.filter(l=>l).filter((l,c)=>r?!0:c===0),context:a,transaction:i}:{valid:!0,context:a,transaction:i}}preValidateSync(e,r){let a=this.#P(e,r),{doNotStopOnFirstError:i,resultingContext:l,errors:c}=a,p=a.transaction,n=p.object.validatedObject;if(this.#e.length>0){let s=0;for(let t of this.#e)try{let o=t.fn(n);if(o instanceof Promise)throw new Error(`Preprocessor #${s}${t.fn.name?` (${t.fn.name})`:""} returned a Promise. Use validateAsync() for schemas with async preprocessors.`);n=o}catch(o){if(o.message?.includes("Use validateAsync()"))throw o;if(c.push({message:`Preprocessor #${s}${t.fn.name?` (${t.fn.name})`:""} thrown an error: ${o.message}`}),!i)return this.#r(c,l)}finally{s++}p=this.#n?h({validatedObject:n}):T({validatedObject:n})}if(typeof n>"u"&&this.hasDefault&&(n=this.resolveDefaultValue(),p=this.#n?h({validatedObject:n}):T({validatedObject:n})),this.#t.length>0&&!(n==null&&!this.isRequired)&&!(n===null&&this.#a)){let s=0;for(let t of this.#t)try{let o=t.fn(n);if(o instanceof Promise)throw new Error(`Validator #${s}${t.fn.name?` (${t.fn.name})`:""} returned a Promise. Use validateAsync() for schemas with async validators.`);let{valid:d,errors:f}=o;if(!d&&(c.push(...this.#R(s,t.fn.name,f)),!i))return this.#r(c,l)}catch(o){if(o.message?.includes("Use validateAsync()"))throw o;if(c.push({message:`Validator #${s}${t.fn.name?` (${t.fn.name})`:""} thrown an error: ${o.message}`}),!i)return this.#r(c,l)}finally{s++}}return this.isRequired&&(typeof n>"u"||n===null&&this.isNullRequiredViolation&&!this.#a)&&(c.push({message:this.getValidationErrorMessageSync(this.#o,n)}),!i)?(p.rollback(),this.#r(c,l)):this.#g(c,i,l,p)}async preValidateAsync(e,r){let a=this.#P(e,r),{doNotStopOnFirstError:i,resultingContext:l,errors:c}=a,p=a.transaction,n=p.object.validatedObject;if(this.#e.length>0){let s=0;for(let t of this.#e)try{n=await Promise.resolve(t.fn(n))}catch(o){if(c.push({message:`Preprocessor #${s}${t.fn.name?` (${t.fn.name})`:""} thrown an error: ${o.message}`}),!i)return this.#r(c,l)}finally{s++}p=this.#n?h({validatedObject:n}):T({validatedObject:n})}if(typeof n>"u"&&this.hasDefault&&(n=this.resolveDefaultValue(),p=this.#n?h({validatedObject:n}):T({validatedObject:n})),this.#t.length>0&&!(n==null&&!this.isRequired)&&!(n===null&&this.#a)){let s=0;for(let t of this.#t)try{let{valid:o,errors:d}=await Promise.resolve(t.fn(n));if(!o&&(c.push(...this.#R(s,t.fn.name,d)),!i))return this.#r(c,l)}catch(o){if(c.push({message:`Validator #${s}${t.fn.name?` (${t.fn.name})`:""} thrown an error: ${o.message}`}),!i)return this.#r(c,l)}finally{s++}}return this.isRequired&&(typeof n>"u"||n===null&&this.isNullRequiredViolation&&!this.#a)&&(c.push({message:await this.getValidationErrorMessage(this.#o,n)}),!i)?(p.rollback(),this.#r(c,l)):this.#g(c,i,l,p)}preValidate(e,r){return this.preValidateAsync(e,r)}introspect(){return{type:this.type,isRequired:this.#d,isNullable:this.#a,isReadonly:this.#y,preprocessors:[...this.preprocessors],validators:[...this.validators],requiredValidationErrorMessageProvider:this.#o,extensions:{...this.#c},hasDefault:this.#s!==void 0,defaultValue:this.#s,description:this.#p,schemaName:this.#h,hasCatch:this.#l,catchValue:this.#i,example:this.#b}}optional(){return this.createFromProps({...this.introspect(),isRequired:!1})}nullable(){return this.createFromProps({...this.introspect(),isNullable:!0})}notNullable(){return this.createFromProps({...this.introspect(),isNullable:!1})}default(e){return this.createFromProps({...this.introspect(),defaultValue:e})}catch(e){return this.createFromProps({...this.introspect(),catchValue:e,hasCatch:!0})}clearDefault(){return this.createFromProps({...this.introspect(),defaultValue:void 0})}describe(e){return this.createFromProps({...this.introspect(),description:e})}example(e){return this.createFromProps({...this.introspect(),example:e})}schemaName(e){return this.createFromProps({...this.introspect(),schemaName:e})}brand(e){return this.createFromProps({...this.introspect()})}readonly(){return this.createFromProps({...this.introspect(),isReadonly:!0})}required(e){return this.createFromProps({...this.introspect(),isRequired:!0,...e!==void 0?{requiredValidationErrorMessageProvider:this.assureValidationErrorMessageProvider(e,this.#m)}:{}})}addPreprocessor(e,r){if(typeof e!="function")throw new Error("preprocessor must be a function");return this.createFromProps({...this.introspect(),preprocessors:[...this.preprocessors,{fn:e,mutates:r?.mutates??!0}]})}clearPreprocessors(){return this.createFromProps({...this.introspect(),preprocessors:[]})}addValidator(e,r){if(typeof e!="function")throw new Error("validator must be a function");return this.createFromProps({...this.introspect(),validators:[...this.validators,{fn:e,mutates:r?.mutates??!1}]})}clearValidators(){return this.createFromProps({...this.introspect(),validators:[]})}validate(e,r){let a=this._validate(e,r);if(!a.valid&&this.#l){let i=this.resolveCatchValue(),l=this._validate(i,r);return l.valid?l:{valid:!0,object:i}}return a}async validateAsync(e,r){let a=await this._validateAsync(e,r);if(!a.valid&&this.#l){let i=this.resolveCatchValue(),l=await this._validateAsync(i,r);return l.valid?l:{valid:!0,object:i}}return a}getValidationErrorMessageSync(e,r){if(typeof e=="string")return e;if(typeof e=="function"){let a=e(r,this);if(a instanceof Promise)throw new Error("Async error message providers require validateAsync(). Use a string or sync function instead.");return a}throw new Error("Invalid error message provider must be a string or a function returning a string")}async getValidationErrorMessage(e,r){if(typeof e=="string")return e;if(typeof e=="function")return e(r,this);throw new Error("Invalid error message provider must be a string or a function returning a string or a promise of a string")}assureValidationErrorMessageProvider(e,r){return typeof e=="string"?e:typeof e=="function"?e.bind(this):typeof r=="function"?r.bind(this):r}withExtension(e,r){return this.createFromProps({...this.introspect(),extensions:{...this.#c,[e]:r}})}getExtension(e){return this.#c[e]}parse(e,r){let a=this.validate(e,r);if(!a.valid)throw new m(a.errors||[]);return a.object}async parseAsync(e,r){let a=await this.validateAsync(e,r);if(!a.valid)throw new m(a.errors||[]);return a.object}safeParse(e,r){return this.validate(e,r)}safeParseAsync(e,r){return this.validateAsync(e,r)}constructor(e){if(!(typeof e=="object"&&e))throw new Error("SchemaBuilder props must be an object");let{type:r,preprocessors:a,validators:i,isRequired:l}=e;this.type=r,typeof l=="boolean"&&(this.isRequired=l),typeof e.isNullable=="boolean"&&(this.#a=e.isNullable),typeof e.isReadonly=="boolean"&&(this.#y=e.isReadonly),Array.isArray(a)&&(this.#e=[...a]),Array.isArray(i)&&(this.#t=[...i]),this.#n=this.#e.some(c=>c.mutates)||this.#t.some(c=>c.mutates),this.#T=this.#e.length===0&&this.#t.length===0,typeof e.extensions=="object"&&e.extensions&&(this.#c={...e.extensions}),e.defaultValue!==void 0&&(this.#s=e.defaultValue),e.hasCatch&&(this.#l=!0,this.#i=e.catchValue),typeof e.description=="string"&&(this.#p=e.description),typeof e.schemaName=="string"&&(this.#h=e.schemaName),e.example!==void 0&&(this.#b=e.example),this.#o=this.assureValidationErrorMessageProvider(e.requiredValidationErrorMessageProvider,this.#m)}};export{h as a,m as b,x as c,v as d,E as e,b as f};
|
|
2
|
+
//# sourceMappingURL=chunk-3JMDGYDT.js.map
|