@cleverbrush/schema 1.0.0-beta.4 → 1.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 +118 -320
- package/dist/builders/AnySchemaBuilder.d.ts +76 -0
- package/dist/builders/AnySchemaBuilder.js +112 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
- package/dist/builders/ArraySchemaBuilder.js +254 -111
- package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
- package/dist/builders/BooleanSchemaBuilder.js +134 -74
- package/dist/builders/DateSchemaBuilder.d.ts +177 -0
- package/dist/builders/DateSchemaBuilder.js +433 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +59 -23
- package/dist/builders/FunctionSchemaBuilder.js +103 -56
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
- package/dist/builders/NumberSchemaBuilder.js +345 -165
- package/dist/builders/ObjectSchemaBuilder.d.ts +357 -81
- package/dist/builders/ObjectSchemaBuilder.js +519 -283
- package/dist/builders/SchemaBuilder.d.ts +158 -34
- package/dist/builders/SchemaBuilder.js +258 -71
- package/dist/builders/StringSchemaBuilder.d.ts +138 -13
- package/dist/builders/StringSchemaBuilder.js +261 -115
- package/dist/builders/UnionSchemaBuilder.d.ts +129 -9
- package/dist/builders/UnionSchemaBuilder.js +196 -75
- package/dist/index.d.ts +20 -40
- package/dist/index.js +19 -32
- package/dist/utils/transaction.d.ts +46 -0
- package/dist/utils/transaction.js +178 -0
- package/package.json +27 -14
- package/dist/defaultSchemas.d.ts +0 -4
- package/dist/defaultSchemas.js +0 -45
- package/dist/schema.d.ts +0 -230
- package/dist/schema.js +0 -2
- package/dist/schemaRegistry.d.ts +0 -49
- package/dist/schemaRegistry.js +0 -278
- package/dist/validators/validateArray.d.ts +0 -3
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -30
- package/dist/validators/validateFunction.d.ts +0 -2
- package/dist/validators/validateFunction.js +0 -21
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -3
- package/dist/validators/validateObject.js +0 -95
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/dist/validators/validateUnion.d.ts +0 -3
- package/dist/validators/validateUnion.js +0 -30
- package/src/builders/ArraySchemaBuilder.test.ts +0 -270
- package/src/builders/ArraySchemaBuilder.ts +0 -381
- package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
- package/src/builders/BooleanSchemaBuilder.ts +0 -155
- package/src/builders/FunctionSchemaBuilder.test.ts +0 -134
- package/src/builders/FunctionSchemaBuilder.ts +0 -109
- package/src/builders/NumberSchemaBuilder.test.ts +0 -493
- package/src/builders/NumberSchemaBuilder.ts +0 -789
- package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
- package/src/builders/ObjectSchemaBuilder.ts +0 -794
- package/src/builders/SchemaBuilder.test.ts +0 -73
- package/src/builders/SchemaBuilder.ts +0 -135
- package/src/builders/StringSchemaBuilder.test.ts +0 -318
- package/src/builders/StringSchemaBuilder.ts +0 -392
- package/src/builders/UnionSchemaBuilder.test.ts +0 -162
- package/src/builders/UnionSchemaBuilder.ts +0 -154
- package/src/defaultSchemas.ts +0 -44
- package/src/index.ts +0 -66
- package/src/schema.ts +0 -827
- package/src/schemaRegistry.builders.test.ts +0 -1393
- package/src/schemaRegistry.test.ts +0 -118
- package/src/schemaRegistry.ts +0 -461
- package/src/validators/validateArray.ts +0 -76
- package/src/validators/validateBoolean.ts +0 -36
- package/src/validators/validateFunction.ts +0 -25
- package/src/validators/validateNumber.ts +0 -82
- package/src/validators/validateObject.ts +0 -119
- package/src/validators/validateString.ts +0 -59
- package/src/validators/validateUnion.ts +0 -36
- package/tsconfig.json +0 -16
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { transaction } from '../utils/transaction.js';
|
|
2
|
+
import { SchemaBuilder } from './SchemaBuilder.js';
|
|
3
|
+
const parseFromJsonPreprocessor = (value) => {
|
|
4
|
+
if (typeof value === 'undefined')
|
|
5
|
+
return value;
|
|
6
|
+
if (typeof value === 'string') {
|
|
7
|
+
const time = Date.parse(value);
|
|
8
|
+
if (Number.isNaN(time))
|
|
9
|
+
return value;
|
|
10
|
+
return new Date(time);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
14
|
+
const parseFromEpochPreprocessor = (value) => {
|
|
15
|
+
if (typeof value === 'undefined')
|
|
16
|
+
return value;
|
|
17
|
+
if (typeof value === 'number') {
|
|
18
|
+
const time = new Date(value);
|
|
19
|
+
if (Number.isNaN(time.getTime()))
|
|
20
|
+
return value;
|
|
21
|
+
return time;
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Allows to create Date schema. It can be required or optional.
|
|
27
|
+
* It can be restricted to be: equal to a certain value, in future, in past, in a certain range.
|
|
28
|
+
* Supports parsing from JSON string and UNIX epoch (using preprocessors).
|
|
29
|
+
*
|
|
30
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
31
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
32
|
+
* directly. Use {@link date | date()} function instead.
|
|
33
|
+
*
|
|
34
|
+
* @example ```ts
|
|
35
|
+
* const date = new Date(2020, 0, 2);
|
|
36
|
+
* const schema = date().min(new Date(2020, 0, 1));
|
|
37
|
+
* const result = await schema.validate(date);
|
|
38
|
+
* // result.valid === true
|
|
39
|
+
* // result.object === date
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example ```ts
|
|
43
|
+
* const schema = date();
|
|
44
|
+
* const result = await schema.validate('2020-01-01');
|
|
45
|
+
* // result.valid === false
|
|
46
|
+
* // result.errors[0].message === 'is expected to be a date'
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @example ```ts
|
|
50
|
+
* const schema = date().parseFromJson();
|
|
51
|
+
* const result = await schema.validate('2020-01-01T00:00:00.000Z');
|
|
52
|
+
* // result.valid === true
|
|
53
|
+
* // result.object is equal to corresponding Date object
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example ```ts
|
|
57
|
+
* const schema = date().parseFromEpoch();
|
|
58
|
+
* const result = await schema.validate(1577836800000);
|
|
59
|
+
* // result.valid === true
|
|
60
|
+
* // result.object is equal to corresponding Date object
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @see {@link date}
|
|
64
|
+
*/
|
|
65
|
+
export class DateSchemaBuilder extends SchemaBuilder {
|
|
66
|
+
#min;
|
|
67
|
+
#max;
|
|
68
|
+
#equalsTo;
|
|
69
|
+
#ensureIsInFuture = false;
|
|
70
|
+
#ensureIsInPast = false;
|
|
71
|
+
#parseFromJson = false;
|
|
72
|
+
#parseFromEpoch = false;
|
|
73
|
+
static create(props) {
|
|
74
|
+
return new DateSchemaBuilder({
|
|
75
|
+
type: 'date',
|
|
76
|
+
...props
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
constructor(props) {
|
|
80
|
+
super(props);
|
|
81
|
+
if (props.min instanceof Date) {
|
|
82
|
+
this.#min = props.min;
|
|
83
|
+
}
|
|
84
|
+
if (props.max instanceof Date) {
|
|
85
|
+
this.#max = props.max;
|
|
86
|
+
}
|
|
87
|
+
if (typeof props.ensureIsInFuture === 'boolean') {
|
|
88
|
+
this.#ensureIsInFuture = props.ensureIsInFuture;
|
|
89
|
+
}
|
|
90
|
+
if (typeof props.ensureIsInPast === 'boolean') {
|
|
91
|
+
this.#ensureIsInPast = props.ensureIsInPast;
|
|
92
|
+
}
|
|
93
|
+
if (typeof props.parseFromJson === 'boolean') {
|
|
94
|
+
this.#parseFromJson = props.parseFromJson;
|
|
95
|
+
}
|
|
96
|
+
if (typeof props.parseFromEpoch === 'boolean') {
|
|
97
|
+
this.#parseFromEpoch = props.parseFromEpoch;
|
|
98
|
+
}
|
|
99
|
+
if (props.equalsTo instanceof Date ||
|
|
100
|
+
typeof props.equalsTo === 'undefined') {
|
|
101
|
+
this.#equalsTo = props.equalsTo;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
introspect() {
|
|
105
|
+
return {
|
|
106
|
+
...super.introspect(),
|
|
107
|
+
/**
|
|
108
|
+
* Min valid value (if defined).
|
|
109
|
+
*/
|
|
110
|
+
min: this.#min,
|
|
111
|
+
/**
|
|
112
|
+
* Max valid value (if defined).
|
|
113
|
+
*/
|
|
114
|
+
max: this.#max,
|
|
115
|
+
/**
|
|
116
|
+
* Make sure that date is in future. `false` by default.
|
|
117
|
+
*/
|
|
118
|
+
ensureIsInFuture: this.#ensureIsInFuture,
|
|
119
|
+
/**
|
|
120
|
+
* Make sure that date is in past. `false` by default.
|
|
121
|
+
*/
|
|
122
|
+
ensureIsInPast: this.#ensureIsInPast,
|
|
123
|
+
/**
|
|
124
|
+
* If set, restrict date to be equal to a certain value.
|
|
125
|
+
*/
|
|
126
|
+
equalsTo: this.#equalsTo,
|
|
127
|
+
/**
|
|
128
|
+
* If set, schema will try to parse date from the UNIX epoch (number).
|
|
129
|
+
* `false` by default.
|
|
130
|
+
*/
|
|
131
|
+
parseFromEpoch: this.#parseFromEpoch,
|
|
132
|
+
/**
|
|
133
|
+
* If set, schema will try to parse date from JSON string.
|
|
134
|
+
* `false` by default.
|
|
135
|
+
*/
|
|
136
|
+
parseFromJson: this.#parseFromJson,
|
|
137
|
+
/**
|
|
138
|
+
* Array of preprocessor functions
|
|
139
|
+
*/
|
|
140
|
+
preprocessors: this.preprocessors,
|
|
141
|
+
/**
|
|
142
|
+
* Array of validator functions
|
|
143
|
+
*/
|
|
144
|
+
validators: this.validators
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @hidden
|
|
149
|
+
*/
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
151
|
+
hasType(notUsed) {
|
|
152
|
+
return this.createFromProps({
|
|
153
|
+
...this.introspect()
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @hidden
|
|
158
|
+
*/
|
|
159
|
+
clearHasType() {
|
|
160
|
+
return this.createFromProps({
|
|
161
|
+
...this.introspect()
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Performs validion of Date schema over `object`.
|
|
166
|
+
* @param context Optional `ValidationContext` settings.
|
|
167
|
+
*/
|
|
168
|
+
async validate(object, context) {
|
|
169
|
+
const superResult = await super.preValidate(object, context);
|
|
170
|
+
const { valid, context: prevalidationContext, errors } = superResult;
|
|
171
|
+
const { path } = prevalidationContext;
|
|
172
|
+
if (!valid) {
|
|
173
|
+
return {
|
|
174
|
+
valid,
|
|
175
|
+
errors
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
let { transaction: preValidationTransaction } = superResult;
|
|
179
|
+
let { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
180
|
+
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
181
|
+
this.isRequired === false) {
|
|
182
|
+
return {
|
|
183
|
+
valid: true,
|
|
184
|
+
object: objToValidate
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
if (this.#parseFromJson) {
|
|
188
|
+
preValidationTransaction = transaction({
|
|
189
|
+
validatedObject: parseFromJsonPreprocessor(objToValidate)
|
|
190
|
+
});
|
|
191
|
+
objToValidate = preValidationTransaction.object.validatedObject;
|
|
192
|
+
}
|
|
193
|
+
if (this.#parseFromEpoch) {
|
|
194
|
+
preValidationTransaction = transaction({
|
|
195
|
+
validatedObject: parseFromEpochPreprocessor(objToValidate)
|
|
196
|
+
});
|
|
197
|
+
objToValidate = preValidationTransaction.object.validatedObject;
|
|
198
|
+
}
|
|
199
|
+
if (!(objToValidate instanceof Date))
|
|
200
|
+
return {
|
|
201
|
+
valid: false,
|
|
202
|
+
errors: [
|
|
203
|
+
{
|
|
204
|
+
message: `expected instance of Date, but saw ${typeof objToValidate}`,
|
|
205
|
+
path: path
|
|
206
|
+
}
|
|
207
|
+
]
|
|
208
|
+
};
|
|
209
|
+
if (typeof this.#equalsTo !== 'undefined' &&
|
|
210
|
+
objToValidate.getTime() !== this.#equalsTo.getTime()) {
|
|
211
|
+
return {
|
|
212
|
+
valid: false,
|
|
213
|
+
errors: [
|
|
214
|
+
{
|
|
215
|
+
message: `is expected to be equal to ${this.#equalsTo}`,
|
|
216
|
+
path: path
|
|
217
|
+
}
|
|
218
|
+
]
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (this.#ensureIsInFuture && objToValidate <= new Date()) {
|
|
222
|
+
return {
|
|
223
|
+
valid: false,
|
|
224
|
+
errors: [
|
|
225
|
+
{
|
|
226
|
+
message: 'is expected to be in future',
|
|
227
|
+
path: path
|
|
228
|
+
}
|
|
229
|
+
]
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
if (this.#ensureIsInPast && objToValidate >= new Date()) {
|
|
233
|
+
return {
|
|
234
|
+
valid: false,
|
|
235
|
+
errors: [
|
|
236
|
+
{
|
|
237
|
+
message: 'is expected to be in past',
|
|
238
|
+
path: path
|
|
239
|
+
}
|
|
240
|
+
]
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
if (typeof this.#min !== 'undefined') {
|
|
244
|
+
if (objToValidate < this.#min)
|
|
245
|
+
return {
|
|
246
|
+
valid: false,
|
|
247
|
+
errors: [
|
|
248
|
+
{
|
|
249
|
+
message: `expected to be at least ${this.#min}`,
|
|
250
|
+
path: path
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
if (typeof this.#max !== 'undefined') {
|
|
256
|
+
if (objToValidate > this.#max)
|
|
257
|
+
return {
|
|
258
|
+
valid: false,
|
|
259
|
+
errors: [
|
|
260
|
+
{
|
|
261
|
+
message: `expected to be no more than or equal to ${this.#max}`,
|
|
262
|
+
path: path
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
valid: true,
|
|
269
|
+
object: preValidationTransaction.commit()
|
|
270
|
+
.validatedObject
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* @hidden
|
|
275
|
+
*/
|
|
276
|
+
createFromProps(props) {
|
|
277
|
+
return DateSchemaBuilder.create(props);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Restricts Date to be equal to `value`.
|
|
281
|
+
*/
|
|
282
|
+
equals(value) {
|
|
283
|
+
if (!(value instanceof Date))
|
|
284
|
+
throw new Error('Date expected');
|
|
285
|
+
return this.createFromProps({
|
|
286
|
+
...this.introspect(),
|
|
287
|
+
equalsTo: value
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Clears `equals()` call.
|
|
292
|
+
*/
|
|
293
|
+
clearEquals() {
|
|
294
|
+
return this.createFromProps({
|
|
295
|
+
...this.introspect(),
|
|
296
|
+
equalsTo: undefined
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* @hidden
|
|
301
|
+
*/
|
|
302
|
+
required() {
|
|
303
|
+
return super.required();
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* @hidden
|
|
307
|
+
*/
|
|
308
|
+
optional() {
|
|
309
|
+
return super.optional();
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Accept only dates in the future.
|
|
313
|
+
*/
|
|
314
|
+
isInFuture() {
|
|
315
|
+
return this.createFromProps({
|
|
316
|
+
...this.introspect(),
|
|
317
|
+
ensureIsInFuture: true
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Cancel `isInFuture()` call.
|
|
322
|
+
*/
|
|
323
|
+
clearIsInFuture() {
|
|
324
|
+
return this.createFromProps({
|
|
325
|
+
...this.introspect(),
|
|
326
|
+
ensureIsInFuture: false
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Accept only dates in the past.
|
|
331
|
+
*/
|
|
332
|
+
isInPast() {
|
|
333
|
+
return this.createFromProps({
|
|
334
|
+
...this.introspect(),
|
|
335
|
+
ensureIsInPast: true
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Cancel `isInPast()` call.
|
|
340
|
+
*/
|
|
341
|
+
clearIsInPast() {
|
|
342
|
+
return this.createFromProps({
|
|
343
|
+
...this.introspect(),
|
|
344
|
+
ensureIsInPast: false
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Set minimal valid Date value for schema.
|
|
349
|
+
*/
|
|
350
|
+
min(minValue) {
|
|
351
|
+
if (!(minValue instanceof Date))
|
|
352
|
+
throw new Error('minValue must be a Date');
|
|
353
|
+
return this.createFromProps({
|
|
354
|
+
...this.introspect(),
|
|
355
|
+
min: minValue
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Clear `min()` call.
|
|
360
|
+
*/
|
|
361
|
+
clearMin() {
|
|
362
|
+
const schema = this.introspect();
|
|
363
|
+
delete schema.min;
|
|
364
|
+
return this.createFromProps({
|
|
365
|
+
...schema
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Set maximal valid Date value for schema.
|
|
370
|
+
*/
|
|
371
|
+
max(maxValue) {
|
|
372
|
+
if (!(maxValue instanceof Date))
|
|
373
|
+
throw new Error('maxValue must be a Date');
|
|
374
|
+
return this.createFromProps({
|
|
375
|
+
...this.introspect(),
|
|
376
|
+
max: maxValue
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Clear `max()` call.
|
|
381
|
+
*/
|
|
382
|
+
clearMax() {
|
|
383
|
+
const schema = this.introspect();
|
|
384
|
+
delete schema.max;
|
|
385
|
+
return this.createFromProps({
|
|
386
|
+
...schema
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Accepts JSON string as a valid Date.
|
|
391
|
+
* String must be in ISO format and will be parsed using `JSON.parse()`.
|
|
392
|
+
*/
|
|
393
|
+
acceptJsonString() {
|
|
394
|
+
return this.createFromProps({
|
|
395
|
+
...this.introspect(),
|
|
396
|
+
parseFromJson: true
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Cancel `acceptJsonString()` call.
|
|
401
|
+
*/
|
|
402
|
+
doNotAcceptJsonString() {
|
|
403
|
+
return this.createFromProps({
|
|
404
|
+
...this.introspect(),
|
|
405
|
+
parseFromJson: false
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Accepts epoch number as a valid Date.
|
|
410
|
+
* Epoch number will be parsed using `new Date(epoch)`.
|
|
411
|
+
*/
|
|
412
|
+
acceptEpoch() {
|
|
413
|
+
return this.createFromProps({
|
|
414
|
+
...this.introspect(),
|
|
415
|
+
parseFromEpoch: true
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Cancel `acceptEpoch()` call.
|
|
420
|
+
*/
|
|
421
|
+
doNotAcceptEpoch() {
|
|
422
|
+
return this.createFromProps({
|
|
423
|
+
...this.introspect(),
|
|
424
|
+
parseFromEpoch: false
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Creates a Date schema.
|
|
430
|
+
*/
|
|
431
|
+
export const date = () => DateSchemaBuilder.create({
|
|
432
|
+
isRequired: true
|
|
433
|
+
});
|
|
@@ -1,25 +1,61 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
import { SchemaBuilder, ValidationResult, ValidationContext } from './SchemaBuilder.js';
|
|
2
|
+
type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<FunctionSchemaBuilder<R>['introspect']>>;
|
|
3
|
+
/**
|
|
4
|
+
* Schema builder for functions. Allows to define a schema for a function.
|
|
5
|
+
* It can be: required or optional.
|
|
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 func | func()} function instead.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const schema = func();
|
|
14
|
+
* const result = await schema.validate(() => {});
|
|
15
|
+
* // result.valid === true
|
|
16
|
+
* // result.object === () => {}
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const schema = func().optional();
|
|
22
|
+
* const result = await schema.validate(undefined);
|
|
23
|
+
* // result.valid === true
|
|
24
|
+
* // result.object === undefined
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see {@link func}
|
|
28
|
+
*/
|
|
29
|
+
export declare class FunctionSchemaBuilder<TRequired extends boolean = true, TExplicitType = undefined, TResult = TExplicitType extends undefined ? (...args: any[]) => any : TExplicitType> extends SchemaBuilder<TResult, TRequired> {
|
|
30
|
+
static create(props: FunctionSchemaBuilderCreateProps<any>): FunctionSchemaBuilder<true, undefined, (...args: any[]) => any>;
|
|
19
31
|
private constructor();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
/**
|
|
33
|
+
* @hidden
|
|
34
|
+
*/
|
|
35
|
+
hasType<T>(notUsed?: T): FunctionSchemaBuilder<true, T>;
|
|
36
|
+
/**
|
|
37
|
+
* @hidden
|
|
38
|
+
*/
|
|
39
|
+
clearHasType(): FunctionSchemaBuilder<TRequired, undefined>;
|
|
40
|
+
/**
|
|
41
|
+
* Performs validion of the schema over `object`. Basically runs
|
|
42
|
+
* validators, preprocessors and checks for required (if schema is not optional).
|
|
43
|
+
* @param context Optional `ValidationContext` settings.
|
|
44
|
+
*/
|
|
45
|
+
validate(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
46
|
+
protected createFromProps<TReq extends boolean>(props: FunctionSchemaBuilderCreateProps<TReq>): this;
|
|
47
|
+
/**
|
|
48
|
+
* @hidden
|
|
49
|
+
*/
|
|
50
|
+
required(): FunctionSchemaBuilder<true, TExplicitType>;
|
|
51
|
+
/**
|
|
52
|
+
* @hidden
|
|
53
|
+
*/
|
|
54
|
+
optional(): FunctionSchemaBuilder<false, TExplicitType>;
|
|
24
55
|
}
|
|
25
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Creates a `function` schema.
|
|
58
|
+
* @retuns {@link FunctionSchemaBuilder}
|
|
59
|
+
*/
|
|
60
|
+
export declare const func: () => FunctionSchemaBuilder<true, undefined, (...args: any[]) => any>;
|
|
61
|
+
export {};
|
|
@@ -1,66 +1,113 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { SchemaBuilder } from './SchemaBuilder.js';
|
|
2
|
+
/**
|
|
3
|
+
* Schema builder for functions. Allows to define a schema for a function.
|
|
4
|
+
* It can be: required or optional.
|
|
5
|
+
*
|
|
6
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
7
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
8
|
+
* directly. Use {@link func | func()} function instead.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const schema = func();
|
|
13
|
+
* const result = await schema.validate(() => {});
|
|
14
|
+
* // result.valid === true
|
|
15
|
+
* // result.object === () => {}
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const schema = func().optional();
|
|
21
|
+
* const result = await schema.validate(undefined);
|
|
22
|
+
* // result.valid === true
|
|
23
|
+
* // result.object === undefined
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @see {@link func}
|
|
27
|
+
*/
|
|
28
|
+
export class FunctionSchemaBuilder extends SchemaBuilder {
|
|
29
|
+
static create(props) {
|
|
30
|
+
return new FunctionSchemaBuilder({
|
|
31
|
+
type: 'function',
|
|
32
|
+
...props
|
|
33
|
+
});
|
|
15
34
|
}
|
|
16
|
-
|
|
17
|
-
|
|
35
|
+
constructor(props) {
|
|
36
|
+
super(props);
|
|
18
37
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
optional() {
|
|
28
|
-
if (this.isRequired === false) {
|
|
29
|
-
return this;
|
|
30
|
-
}
|
|
31
|
-
return FunctionSchemaBuilder.create({
|
|
32
|
-
...this._schema,
|
|
33
|
-
isRequired: false
|
|
38
|
+
/**
|
|
39
|
+
* @hidden
|
|
40
|
+
*/
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
42
|
+
hasType(notUsed) {
|
|
43
|
+
return this.createFromProps({
|
|
44
|
+
...this.introspect()
|
|
34
45
|
});
|
|
35
46
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return
|
|
41
|
-
...this.
|
|
42
|
-
isRequired: true
|
|
47
|
+
/**
|
|
48
|
+
* @hidden
|
|
49
|
+
*/
|
|
50
|
+
clearHasType() {
|
|
51
|
+
return this.createFromProps({
|
|
52
|
+
...this.introspect()
|
|
43
53
|
});
|
|
44
54
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Performs validion of the schema over `object`. Basically runs
|
|
57
|
+
* validators, preprocessors and checks for required (if schema is not optional).
|
|
58
|
+
* @param context Optional `ValidationContext` settings.
|
|
59
|
+
*/
|
|
60
|
+
async validate(object, context) {
|
|
61
|
+
const superResult = await super.preValidate(object, context);
|
|
62
|
+
const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
|
|
63
|
+
const { path } = prevalidationContext;
|
|
64
|
+
if (!valid) {
|
|
65
|
+
return { valid, errors };
|
|
48
66
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return this;
|
|
67
|
+
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
68
|
+
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
69
|
+
this.isRequired === false) {
|
|
70
|
+
return {
|
|
71
|
+
valid: true,
|
|
72
|
+
object: objToValidate
|
|
73
|
+
};
|
|
57
74
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
75
|
+
if (typeof objToValidate !== 'function') {
|
|
76
|
+
return {
|
|
77
|
+
valid: false,
|
|
78
|
+
errors: [
|
|
79
|
+
{
|
|
80
|
+
message: `expected type function, but saw ${typeof objToValidate}`,
|
|
81
|
+
path: path
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
valid: true,
|
|
88
|
+
object: objToValidate
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
createFromProps(props) {
|
|
92
|
+
return FunctionSchemaBuilder.create(props);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* @hidden
|
|
96
|
+
*/
|
|
97
|
+
required() {
|
|
98
|
+
return super.required();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @hidden
|
|
102
|
+
*/
|
|
103
|
+
optional() {
|
|
104
|
+
return super.optional();
|
|
62
105
|
}
|
|
63
106
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Creates a `function` schema.
|
|
109
|
+
* @retuns {@link FunctionSchemaBuilder}
|
|
110
|
+
*/
|
|
111
|
+
export const func = () => FunctionSchemaBuilder.create({
|
|
112
|
+
isRequired: true
|
|
113
|
+
});
|