@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,284 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Similar to the `Array` type in TypeScript. It can be used to validate arrays of any type.
|
|
4
|
-
* It can also be used to validate arrays of specific type.
|
|
5
|
-
* For example, if you want to validate an array of numbers,
|
|
6
|
-
* you can use `array(number())` to create a schema builder.
|
|
7
|
-
* If you want to validate an array of users, you can use
|
|
8
|
-
* `array().of(object({ name: string(), age: number() }))` to create a schema builder.
|
|
9
|
-
* If you want to validate an array of numbers or strings,
|
|
10
|
-
* you can use `array(union(number()).or(string()))`.
|
|
11
|
-
*
|
|
12
|
-
* Also you can limit the length of the array by using `minLength`
|
|
13
|
-
* and `maxLength` methods.
|
|
14
|
-
*
|
|
15
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
16
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
17
|
-
* directly. Use {@link array | array()} function instead.
|
|
18
|
-
* @see {@link array}
|
|
19
|
-
*/
|
|
20
|
-
export class ArraySchemaBuilder extends SchemaBuilder {
|
|
21
|
-
#minLength;
|
|
22
|
-
#maxLength;
|
|
23
|
-
#elementSchema;
|
|
24
|
-
/**
|
|
25
|
-
* @hidden
|
|
26
|
-
*/
|
|
27
|
-
static create(props) {
|
|
28
|
-
return new ArraySchemaBuilder({
|
|
29
|
-
type: 'array',
|
|
30
|
-
...props
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
constructor(props) {
|
|
34
|
-
super(props);
|
|
35
|
-
if (typeof props.minLength === 'number') {
|
|
36
|
-
this.#minLength = props.minLength;
|
|
37
|
-
}
|
|
38
|
-
if (typeof props.maxLength === 'number') {
|
|
39
|
-
this.#maxLength = props.maxLength;
|
|
40
|
-
}
|
|
41
|
-
if (props.elementSchema instanceof SchemaBuilder) {
|
|
42
|
-
this.#elementSchema = props.elementSchema;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* @hidden
|
|
47
|
-
*/
|
|
48
|
-
hasType(
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
50
|
-
notUsed) {
|
|
51
|
-
return this.createFromProps({
|
|
52
|
-
...this.introspect()
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* @hidden
|
|
57
|
-
*/
|
|
58
|
-
clearHasType() {
|
|
59
|
-
return this.createFromProps({
|
|
60
|
-
...this.introspect()
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Performs validion of the schema over `object`. Basically runs
|
|
65
|
-
* validators, preprocessors and checks for required (if schema is not optional).
|
|
66
|
-
* @param context Optional `ValidationContext` settings.
|
|
67
|
-
*/
|
|
68
|
-
async validate(object, context) {
|
|
69
|
-
const superResult = await super.preValidate(object, context);
|
|
70
|
-
const { valid, context: prevalidationContext, errors, transaction: preValidationTransaction } = superResult;
|
|
71
|
-
const { path } = prevalidationContext;
|
|
72
|
-
if (!valid) {
|
|
73
|
-
return {
|
|
74
|
-
valid,
|
|
75
|
-
errors
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
79
|
-
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
80
|
-
this.isRequired === false) {
|
|
81
|
-
return {
|
|
82
|
-
valid: true,
|
|
83
|
-
object: objToValidate
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
if (!Array.isArray(objToValidate)) {
|
|
87
|
-
return {
|
|
88
|
-
valid: false,
|
|
89
|
-
errors: [{ message: 'array expected', path: path }]
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
if (typeof this.#maxLength === 'number' &&
|
|
93
|
-
objToValidate.length > this.#maxLength) {
|
|
94
|
-
return {
|
|
95
|
-
valid: false,
|
|
96
|
-
errors: [
|
|
97
|
-
{
|
|
98
|
-
message: `cannot contain more than ${this.#maxLength} elements`,
|
|
99
|
-
path: path
|
|
100
|
-
}
|
|
101
|
-
]
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
if (typeof this.#minLength === 'number' &&
|
|
105
|
-
objToValidate.length < this.#minLength) {
|
|
106
|
-
return {
|
|
107
|
-
valid: false,
|
|
108
|
-
errors: [
|
|
109
|
-
{
|
|
110
|
-
message: `cannot contain less than ${this.#minLength} elements`,
|
|
111
|
-
path: path
|
|
112
|
-
}
|
|
113
|
-
]
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
if (objToValidate.length > 0 &&
|
|
117
|
-
this.#elementSchema instanceof SchemaBuilder) {
|
|
118
|
-
if (prevalidationContext.doNotStopOnFirstError) {
|
|
119
|
-
const results = await Promise.all(objToValidate.map((o) => this.#elementSchema?.validate(o, prevalidationContext)));
|
|
120
|
-
let valid = true;
|
|
121
|
-
for (let i = 0; i < results.length; i++) {
|
|
122
|
-
if (!results[i]?.valid) {
|
|
123
|
-
valid = false;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return Object.assign({
|
|
127
|
-
valid
|
|
128
|
-
}, valid
|
|
129
|
-
? {}
|
|
130
|
-
: {
|
|
131
|
-
errors: results
|
|
132
|
-
.map((r) => r?.errors)
|
|
133
|
-
.filter((r) => r)
|
|
134
|
-
.map((e) => e?.map((r, index) => ({
|
|
135
|
-
...r,
|
|
136
|
-
path: `${r.path}[${index}]`
|
|
137
|
-
})))
|
|
138
|
-
.flat()
|
|
139
|
-
}, valid
|
|
140
|
-
? {
|
|
141
|
-
object: results.map((r) => r?.object)
|
|
142
|
-
}
|
|
143
|
-
: {});
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
for (let i = 0; i < objToValidate.length; i++) {
|
|
147
|
-
const { valid, errors, object: validatedItem } = await this.#elementSchema.validate(objToValidate[i], {
|
|
148
|
-
...prevalidationContext,
|
|
149
|
-
path: `${path}[${i}]`
|
|
150
|
-
});
|
|
151
|
-
if (valid) {
|
|
152
|
-
objToValidate[i] = validatedItem;
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
return {
|
|
156
|
-
valid: false,
|
|
157
|
-
errors: Array.isArray(errors) && errors.length > 0
|
|
158
|
-
? [errors[0]]
|
|
159
|
-
: []
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
valid: true,
|
|
167
|
-
object: objToValidate
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* @hidden
|
|
172
|
-
*/
|
|
173
|
-
createFromProps(props) {
|
|
174
|
-
return ArraySchemaBuilder.create(props);
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* @hidden
|
|
178
|
-
*/
|
|
179
|
-
required() {
|
|
180
|
-
return super.required();
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* @hidden
|
|
184
|
-
*/
|
|
185
|
-
optional() {
|
|
186
|
-
return super.optional();
|
|
187
|
-
}
|
|
188
|
-
introspect() {
|
|
189
|
-
return {
|
|
190
|
-
...super.introspect(),
|
|
191
|
-
/**
|
|
192
|
-
* Schema of array item (if defined)
|
|
193
|
-
*/
|
|
194
|
-
elementSchema: this.#elementSchema,
|
|
195
|
-
/**
|
|
196
|
-
* Min length of a valid array
|
|
197
|
-
*/
|
|
198
|
-
minLength: this.#minLength,
|
|
199
|
-
/**
|
|
200
|
-
* Max length of a valid array
|
|
201
|
-
*/
|
|
202
|
-
maxLength: this.#maxLength
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Set a schema that every array item has to satisfy. If it is not set,
|
|
207
|
-
* Item of any type is allowed.
|
|
208
|
-
* @param schema Schema that every array item has to satisfy
|
|
209
|
-
*/
|
|
210
|
-
of(schema) {
|
|
211
|
-
return ArraySchemaBuilder.create({
|
|
212
|
-
...this.introspect(),
|
|
213
|
-
elementSchema: schema
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
clearOf() {
|
|
217
|
-
return ArraySchemaBuilder.create({
|
|
218
|
-
...this.introspect(),
|
|
219
|
-
elementSchema: undefined
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Set minimal length of the valid array value for schema.
|
|
224
|
-
*/
|
|
225
|
-
minLength(length) {
|
|
226
|
-
if (typeof length !== 'number' || length < 0)
|
|
227
|
-
throw new Error('length is expected to be a number which is >= 0');
|
|
228
|
-
return ArraySchemaBuilder.create({
|
|
229
|
-
...this.introspect(),
|
|
230
|
-
minLength: length
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Clear minimal length of the valid array value for schema.
|
|
235
|
-
*/
|
|
236
|
-
clearMinLength() {
|
|
237
|
-
const schema = this.introspect();
|
|
238
|
-
delete schema.minLength;
|
|
239
|
-
return this.createFromProps({
|
|
240
|
-
...schema
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Set max length of the valid array value for schema.
|
|
245
|
-
*/
|
|
246
|
-
maxLength(length) {
|
|
247
|
-
if (typeof length !== 'number' || length < 0)
|
|
248
|
-
throw new Error('length is expected to be a number which is >= 0');
|
|
249
|
-
return ArraySchemaBuilder.create({
|
|
250
|
-
...this.introspect(),
|
|
251
|
-
maxLength: length
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Clear max length of the valid array value for schema.
|
|
256
|
-
*/
|
|
257
|
-
clearMaxLength() {
|
|
258
|
-
const schema = this.introspect();
|
|
259
|
-
delete schema.maxLength;
|
|
260
|
-
return this.createFromProps({
|
|
261
|
-
...schema
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Creates a `Array` schema.
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```typescript
|
|
270
|
-
* const schema = array().minLength(2).maxLength(5).of(string());
|
|
271
|
-
* // [] - invalid
|
|
272
|
-
* // ['a', 'b'] - valid
|
|
273
|
-
* // ['a', 'b', 'c', 'd', 'e', 'f'] - invalid
|
|
274
|
-
* // ['a', 'b', 'c', 'd', 'e'] - valid
|
|
275
|
-
* // ['a', 'b', 'c', 'd', 1] - invalid
|
|
276
|
-
* // ['a', 'b', null] - invalid
|
|
277
|
-
* // null - invalid
|
|
278
|
-
* // undefined - invalid
|
|
279
|
-
* ```
|
|
280
|
-
*/
|
|
281
|
-
export const array = (elementSchema) => ArraySchemaBuilder.create({
|
|
282
|
-
isRequired: true,
|
|
283
|
-
elementSchema
|
|
284
|
-
});
|
|
1
|
+
import{a,b}from"../chunk-NUW3VXZV.js";import"../chunk-3JMDGYDT.js";export{a as ArraySchemaBuilder,b as array};
|
|
2
|
+
//# sourceMappingURL=ArraySchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type BRAND, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
2
2
|
type BooleanSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<BooleanSchemaBuilder<R>['introspect']>>;
|
|
3
3
|
/**
|
|
4
4
|
* Similar to `boolean` type in TypeScript.
|
|
@@ -11,58 +11,148 @@ type BooleanSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnT
|
|
|
11
11
|
*
|
|
12
12
|
* @example ```ts
|
|
13
13
|
* const schema = boolean().equals(true);
|
|
14
|
-
* const result =
|
|
14
|
+
* const result = schema.validate(true);
|
|
15
15
|
* // result.valid === true
|
|
16
16
|
* // result.object === true
|
|
17
17
|
* ```
|
|
18
18
|
* @example ```ts
|
|
19
19
|
* const schema = boolean().equals(false);
|
|
20
|
-
* const result =
|
|
20
|
+
* const result = schema.validate(true);
|
|
21
21
|
* // result.valid === false
|
|
22
22
|
* // result.errors[0].message === 'is expected to be equal to 'false''
|
|
23
23
|
* ```
|
|
24
24
|
* @example ```ts
|
|
25
25
|
* const schema = boolean().equals(true).optional();
|
|
26
|
-
* const result =
|
|
26
|
+
* const result = schema.validate(undefined);
|
|
27
27
|
* // result.valid === true
|
|
28
28
|
* // result.object === undefined
|
|
29
29
|
* ```
|
|
30
30
|
*
|
|
31
31
|
* @see {@link boolean}
|
|
32
32
|
*/
|
|
33
|
-
export declare class BooleanSchemaBuilder<TResult = boolean, TRequired extends boolean = true, TExplicitType = undefined, TFinalResult = TExplicitType extends undefined ? TResult : TExplicitType> extends SchemaBuilder<TFinalResult, TRequired> {
|
|
33
|
+
export declare class BooleanSchemaBuilder<TResult = boolean, TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}, TFinalResult = TExplicitType extends undefined ? TResult : TExplicitType> extends SchemaBuilder<TFinalResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
34
34
|
#private;
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* @hidden
|
|
37
|
+
*/
|
|
38
|
+
static create(props: BooleanSchemaBuilderCreateProps<any>): BooleanSchemaBuilder<boolean, any, false, undefined, false, {}, boolean>;
|
|
39
|
+
protected constructor(props: BooleanSchemaBuilderCreateProps<TRequired>);
|
|
37
40
|
introspect(): {
|
|
38
41
|
/**
|
|
39
42
|
* If set, restrict object to be equal to a certain value.
|
|
40
43
|
*/
|
|
41
44
|
equalsTo: boolean | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Equals to validation error message provider.
|
|
47
|
+
* If not provided, default error message will be used.
|
|
48
|
+
*/
|
|
49
|
+
equalsToValidationErrorMessageProvider: ValidationErrorMessageProvider<BooleanSchemaBuilder<TResult, TRequired, false, undefined, false, {}, TResult>>;
|
|
42
50
|
type: string;
|
|
43
51
|
isRequired: boolean;
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
isNullable: boolean;
|
|
53
|
+
isReadonly: boolean;
|
|
54
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TFinalResult>[];
|
|
55
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TFinalResult>[];
|
|
56
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
57
|
+
extensions: {
|
|
58
|
+
[x: string]: unknown;
|
|
59
|
+
};
|
|
60
|
+
hasDefault: boolean;
|
|
61
|
+
defaultValue: TFinalResult | (() => TFinalResult) | undefined;
|
|
62
|
+
description: string | undefined;
|
|
63
|
+
schemaName: string | undefined;
|
|
64
|
+
hasCatch: boolean;
|
|
65
|
+
catchValue: TFinalResult | (() => TFinalResult) | undefined;
|
|
66
|
+
example: unknown;
|
|
46
67
|
};
|
|
47
|
-
hasType<T>(notUsed?: T): BooleanSchemaBuilder<TResult, true, T>;
|
|
48
|
-
clearHasType(): BooleanSchemaBuilder<TResult, TRequired, undefined>;
|
|
49
68
|
/**
|
|
50
|
-
*
|
|
51
|
-
|
|
69
|
+
* @inheritdoc
|
|
70
|
+
*/
|
|
71
|
+
hasType<T>(_notUsed?: T): BooleanSchemaBuilder<TResult, true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
72
|
+
/**
|
|
73
|
+
* @inheritdoc
|
|
74
|
+
*/
|
|
75
|
+
clearHasType(): BooleanSchemaBuilder<TResult, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
76
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
77
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
78
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
79
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
80
|
+
/**
|
|
81
|
+
* Performs synchronous validation of the schema over `object`.
|
|
82
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
52
83
|
* @param context Optional `ValidationContext` settings.
|
|
53
84
|
*/
|
|
54
|
-
|
|
85
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
86
|
+
/**
|
|
87
|
+
* Performs async validation of the schema over `object`.
|
|
88
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
89
|
+
* @param context Optional `ValidationContext` settings.
|
|
90
|
+
*/
|
|
91
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
55
92
|
protected createFromProps<TReq extends boolean>(props: BooleanSchemaBuilderCreateProps<TReq>): this;
|
|
56
|
-
|
|
57
|
-
|
|
93
|
+
/**
|
|
94
|
+
* @hidden
|
|
95
|
+
*/
|
|
96
|
+
required(errorMessage?: ValidationErrorMessageProvider): BooleanSchemaBuilder<TResult, true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
97
|
+
/**
|
|
98
|
+
* @hidden
|
|
99
|
+
*/
|
|
100
|
+
optional(): BooleanSchemaBuilder<TResult, false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
101
|
+
/**
|
|
102
|
+
* @hidden
|
|
103
|
+
*/
|
|
104
|
+
default(value: TFinalResult | (() => TFinalResult)): BooleanSchemaBuilder<TResult, true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
105
|
+
/**
|
|
106
|
+
* @hidden
|
|
107
|
+
*/
|
|
108
|
+
clearDefault(): BooleanSchemaBuilder<TResult, TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
109
|
+
/**
|
|
110
|
+
* @hidden
|
|
111
|
+
*/
|
|
112
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): BooleanSchemaBuilder<TResult, TRequired, TNullable, TFinalResult & {
|
|
113
|
+
readonly [K in BRAND]: TBrand;
|
|
114
|
+
}, THasDefault, TExtensions> & TExtensions;
|
|
115
|
+
/**
|
|
116
|
+
* Marks the inferred type as `Readonly<boolean>`. Since booleans are
|
|
117
|
+
* already immutable this is an identity operation, but it sets the
|
|
118
|
+
* `isReadonly` introspection flag for tooling consistency.
|
|
119
|
+
*
|
|
120
|
+
* @see {@link SchemaBuilder.readonly}
|
|
121
|
+
*/
|
|
122
|
+
readonly(): BooleanSchemaBuilder<TResult, TRequired, TNullable, Readonly<TFinalResult>, THasDefault, TExtensions> & TExtensions;
|
|
58
123
|
/**
|
|
59
124
|
* Restricts object to be equal to `value`.
|
|
60
125
|
*/
|
|
61
|
-
equals<T extends boolean>(value: T
|
|
126
|
+
equals<T extends boolean>(value: T,
|
|
127
|
+
/**
|
|
128
|
+
* Custom error message provider.
|
|
129
|
+
*/
|
|
130
|
+
errorMessage?: ValidationErrorMessageProvider<BooleanSchemaBuilder<TResult, TRequired>>): BooleanSchemaBuilder<T, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
131
|
+
/**
|
|
132
|
+
* Removes a `value` defined by `equals()` call.
|
|
133
|
+
*/
|
|
134
|
+
clearEquals(): BooleanSchemaBuilder<boolean, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
135
|
+
/**
|
|
136
|
+
* Adds a preprocessor that coerces a string value to a boolean.
|
|
137
|
+
* Accepts `"true"` → `true` and `"false"` → `false`; other values are
|
|
138
|
+
* left unchanged so the boolean schema rejects them.
|
|
139
|
+
*
|
|
140
|
+
* @example ```ts
|
|
141
|
+
* const schema = boolean().coerce();
|
|
142
|
+
* const result = schema.validate('true');
|
|
143
|
+
* // result.valid === true
|
|
144
|
+
* // result.object === true
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
coerce(): BooleanSchemaBuilder<TResult, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
148
|
+
/**
|
|
149
|
+
* @hidden
|
|
150
|
+
*/
|
|
151
|
+
nullable(): BooleanSchemaBuilder<TResult, TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
62
152
|
/**
|
|
63
|
-
*
|
|
153
|
+
* @hidden
|
|
64
154
|
*/
|
|
65
|
-
|
|
155
|
+
notNullable(): BooleanSchemaBuilder<TResult, TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
66
156
|
}
|
|
67
157
|
/**
|
|
68
158
|
* Creates a `boolean` schema.
|
|
@@ -1,150 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Similar to `boolean` type in TypeScript.
|
|
4
|
-
* Allows to define a schema for a boolean value. It can be required or optional.
|
|
5
|
-
* It can be restricted to be equal to a certain value.
|
|
6
|
-
*
|
|
7
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
8
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
9
|
-
* directly. Use {@link boolean | boolean()} function instead.
|
|
10
|
-
*
|
|
11
|
-
* @example ```ts
|
|
12
|
-
* const schema = boolean().equals(true);
|
|
13
|
-
* const result = await schema.validate(true);
|
|
14
|
-
* // result.valid === true
|
|
15
|
-
* // result.object === true
|
|
16
|
-
* ```
|
|
17
|
-
* @example ```ts
|
|
18
|
-
* const schema = boolean().equals(false);
|
|
19
|
-
* const result = await schema.validate(true);
|
|
20
|
-
* // result.valid === false
|
|
21
|
-
* // result.errors[0].message === 'is expected to be equal to 'false''
|
|
22
|
-
* ```
|
|
23
|
-
* @example ```ts
|
|
24
|
-
* const schema = boolean().equals(true).optional();
|
|
25
|
-
* const result = await schema.validate(undefined);
|
|
26
|
-
* // result.valid === true
|
|
27
|
-
* // result.object === undefined
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @see {@link boolean}
|
|
31
|
-
*/
|
|
32
|
-
export class BooleanSchemaBuilder extends SchemaBuilder {
|
|
33
|
-
#equalsTo;
|
|
34
|
-
static create(props) {
|
|
35
|
-
return new BooleanSchemaBuilder({
|
|
36
|
-
type: 'boolean',
|
|
37
|
-
...props
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
constructor(props) {
|
|
41
|
-
super(props);
|
|
42
|
-
if (typeof props.equalsTo === 'boolean' ||
|
|
43
|
-
typeof props.equalsTo === 'undefined') {
|
|
44
|
-
this.#equalsTo = props.equalsTo;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
introspect() {
|
|
48
|
-
return {
|
|
49
|
-
...super.introspect(),
|
|
50
|
-
/**
|
|
51
|
-
* If set, restrict object to be equal to a certain value.
|
|
52
|
-
*/
|
|
53
|
-
equalsTo: this.#equalsTo
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
57
|
-
hasType(notUsed) {
|
|
58
|
-
return this.createFromProps({
|
|
59
|
-
...this.introspect()
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
clearHasType() {
|
|
63
|
-
return this.createFromProps({
|
|
64
|
-
...this.introspect()
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Performs validion of the schema over `object`. Basically runs
|
|
69
|
-
* validators, preprocessors and checks for required (if schema is not optional).
|
|
70
|
-
* @param context Optional `ValidationContext` settings.
|
|
71
|
-
*/
|
|
72
|
-
async validate(object, context) {
|
|
73
|
-
const superResult = await super.preValidate(object, context);
|
|
74
|
-
const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
|
|
75
|
-
const { path } = prevalidationContext;
|
|
76
|
-
if (!valid) {
|
|
77
|
-
return { valid, errors };
|
|
78
|
-
}
|
|
79
|
-
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
80
|
-
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
81
|
-
this.isRequired === false) {
|
|
82
|
-
return {
|
|
83
|
-
valid: true,
|
|
84
|
-
object: objToValidate
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
if (typeof objToValidate !== 'boolean') {
|
|
88
|
-
return {
|
|
89
|
-
valid: false,
|
|
90
|
-
errors: [
|
|
91
|
-
{
|
|
92
|
-
message: 'expected to be boolean',
|
|
93
|
-
path: path
|
|
94
|
-
}
|
|
95
|
-
]
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
if (typeof this.#equalsTo !== 'undefined' &&
|
|
99
|
-
objToValidate !== this.#equalsTo) {
|
|
100
|
-
return {
|
|
101
|
-
valid: false,
|
|
102
|
-
errors: [
|
|
103
|
-
{
|
|
104
|
-
message: `is expected to be equal to '${this.#equalsTo}'`,
|
|
105
|
-
path: path
|
|
106
|
-
}
|
|
107
|
-
]
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
valid: true,
|
|
112
|
-
object: objToValidate
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
createFromProps(props) {
|
|
116
|
-
return BooleanSchemaBuilder.create(props);
|
|
117
|
-
}
|
|
118
|
-
required() {
|
|
119
|
-
return super.required();
|
|
120
|
-
}
|
|
121
|
-
optional() {
|
|
122
|
-
return super.optional();
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Restricts object to be equal to `value`.
|
|
126
|
-
*/
|
|
127
|
-
equals(value) {
|
|
128
|
-
if (typeof value !== 'boolean')
|
|
129
|
-
throw new Error('boolean expected');
|
|
130
|
-
return this.createFromProps({
|
|
131
|
-
...this.introspect(),
|
|
132
|
-
equalsTo: value
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Removes a `value` defeined by `equals()` call.
|
|
137
|
-
*/
|
|
138
|
-
clearEquals() {
|
|
139
|
-
return this.createFromProps({
|
|
140
|
-
...this.introspect(),
|
|
141
|
-
equalsTo: undefined
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Creates a `boolean` schema.
|
|
147
|
-
*/
|
|
148
|
-
export const boolean = () => BooleanSchemaBuilder.create({
|
|
149
|
-
isRequired: true
|
|
150
|
-
});
|
|
1
|
+
import{a,b}from"../chunk-CFIJQ4GP.js";import"../chunk-3JMDGYDT.js";export{a as BooleanSchemaBuilder,b as boolean};
|
|
2
|
+
//# sourceMappingURL=BooleanSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|