@dzeio/schema 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Schema.d.mts +184 -59
- package/dist/Schema.d.ts +184 -59
- package/dist/Schema.js +361 -224
- package/dist/Schema.mjs +359 -230
- package/package.json +3 -3
package/dist/Schema.js
CHANGED
|
@@ -32,19 +32,27 @@ __export(Schema_exports, {
|
|
|
32
32
|
SchemaArray: () => SchemaArray,
|
|
33
33
|
SchemaBoolean: () => SchemaBoolean,
|
|
34
34
|
SchemaDate: () => SchemaDate,
|
|
35
|
+
SchemaDefault: () => SchemaDefault,
|
|
35
36
|
SchemaEnum: () => SchemaEnum,
|
|
37
|
+
SchemaFile: () => SchemaFile,
|
|
38
|
+
SchemaIntersection: () => SchemaIntersection,
|
|
36
39
|
SchemaItem: () => SchemaItem,
|
|
37
40
|
SchemaLiteral: () => SchemaLiteral,
|
|
41
|
+
SchemaNever: () => SchemaNever,
|
|
42
|
+
SchemaNull: () => SchemaNull,
|
|
38
43
|
SchemaNullable: () => SchemaNullable,
|
|
44
|
+
SchemaNullish: () => SchemaNullish,
|
|
39
45
|
SchemaNumber: () => SchemaNumber,
|
|
40
46
|
SchemaObject: () => SchemaObject,
|
|
41
47
|
SchemaRecord: () => SchemaRecord,
|
|
42
48
|
SchemaString: () => SchemaString,
|
|
49
|
+
SchemaUndefined: () => SchemaUndefined,
|
|
43
50
|
SchemaUnion: () => SchemaUnion,
|
|
51
|
+
SchemaVoid: () => SchemaVoid,
|
|
44
52
|
Types: () => Types,
|
|
45
53
|
default: () => Schema,
|
|
46
54
|
isNull: () => isNull,
|
|
47
|
-
|
|
55
|
+
parsable: () => parsable,
|
|
48
56
|
parseForm: () => parseForm,
|
|
49
57
|
parseFormData: () => parseFormData,
|
|
50
58
|
parseQuery: () => parseQuery,
|
|
@@ -106,19 +114,11 @@ var _SchemaItem = class _SchemaItem {
|
|
|
106
114
|
*/
|
|
107
115
|
this.attributes = [];
|
|
108
116
|
this.invalidError = "the field is invalid";
|
|
117
|
+
this.default = this.defaultValue;
|
|
109
118
|
if (items && items.length > 0) {
|
|
110
119
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
111
120
|
}
|
|
112
121
|
}
|
|
113
|
-
defaultValue(value, strict = true) {
|
|
114
|
-
this.addPreProcess((input) => {
|
|
115
|
-
if (strict && isNull(input) || !strict && !input) {
|
|
116
|
-
return value;
|
|
117
|
-
}
|
|
118
|
-
return input;
|
|
119
|
-
});
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
122
|
in(...values) {
|
|
123
123
|
this.addValidation((input) => values.includes(input));
|
|
124
124
|
return this;
|
|
@@ -137,40 +137,87 @@ var _SchemaItem = class _SchemaItem {
|
|
|
137
137
|
clone() {
|
|
138
138
|
return Schema.fromJSON(this.toJSON());
|
|
139
139
|
}
|
|
140
|
+
defaultValue(value, strict = true) {
|
|
141
|
+
return new SchemaDefault(this, value, strict);
|
|
142
|
+
}
|
|
140
143
|
nullable() {
|
|
141
144
|
return new SchemaNullable(this);
|
|
142
145
|
}
|
|
146
|
+
or(other) {
|
|
147
|
+
return new SchemaUnion(this, other);
|
|
148
|
+
}
|
|
149
|
+
and(other) {
|
|
150
|
+
return new SchemaIntersection(this, other);
|
|
151
|
+
}
|
|
152
|
+
array() {
|
|
153
|
+
return new SchemaArray(this);
|
|
154
|
+
}
|
|
155
|
+
/** Returns the sub values of the input, the schema item to handle them, and the path to it */
|
|
156
|
+
getSubInputs(_input) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
160
|
+
updateSubItemInput(path, input, value) {
|
|
161
|
+
if (path && (0, import_object_util.isObject)(input)) {
|
|
162
|
+
(0, import_object_util.objectSet)(input, path.split("."), value);
|
|
163
|
+
return input;
|
|
164
|
+
} else {
|
|
165
|
+
return value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
parseSubItems(input, options) {
|
|
169
|
+
const result = { errors: [], input };
|
|
170
|
+
for (const { item, value, path } of this.getSubInputs(result.input)) {
|
|
171
|
+
const parsed = item.parse(value, options);
|
|
172
|
+
if (parsed.valid) {
|
|
173
|
+
result.input = this.updateSubItemInput(path, result.input, parsed.object);
|
|
174
|
+
} else {
|
|
175
|
+
for (const error of parsed.errors) {
|
|
176
|
+
error.field = error.field ? `${path}.${error.field}` : path;
|
|
177
|
+
result.errors.push(error);
|
|
178
|
+
}
|
|
179
|
+
if (options == null ? void 0 : options.fast) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
143
186
|
parse(input, options) {
|
|
144
|
-
var _a;
|
|
187
|
+
var _a, _b;
|
|
145
188
|
for (const preProcess of this.preProcess) {
|
|
146
189
|
input = preProcess(input);
|
|
147
190
|
}
|
|
191
|
+
const result = this.parseSubItems(input, options);
|
|
192
|
+
if ((_a = result.errors) == null ? void 0 : _a.length) {
|
|
193
|
+
return {
|
|
194
|
+
valid: false,
|
|
195
|
+
errors: result.errors
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
input = result.input;
|
|
148
199
|
if (!this.isOfType(input)) {
|
|
149
200
|
return {
|
|
150
201
|
valid: false,
|
|
151
202
|
errors: [{ message: "invalid type" }]
|
|
152
203
|
};
|
|
153
204
|
}
|
|
154
|
-
const
|
|
205
|
+
const validationErrors = [];
|
|
155
206
|
for (const validation of this.validations) {
|
|
156
207
|
if (!validation.fn(input)) {
|
|
157
|
-
|
|
158
|
-
message: (
|
|
208
|
+
validationErrors.push({
|
|
209
|
+
message: (_b = validation.error) != null ? _b : this.invalidError
|
|
159
210
|
});
|
|
160
211
|
if (options == null ? void 0 : options.fast) {
|
|
161
|
-
|
|
162
|
-
valid: false,
|
|
163
|
-
object: input,
|
|
164
|
-
errors
|
|
165
|
-
};
|
|
212
|
+
break;
|
|
166
213
|
}
|
|
167
214
|
}
|
|
168
215
|
}
|
|
169
|
-
if (
|
|
216
|
+
if (validationErrors.length > 0) {
|
|
170
217
|
return {
|
|
171
218
|
valid: false,
|
|
172
219
|
object: input,
|
|
173
|
-
errors
|
|
220
|
+
errors: validationErrors
|
|
174
221
|
};
|
|
175
222
|
}
|
|
176
223
|
for (const postProcess of this.postProcess) {
|
|
@@ -205,13 +252,23 @@ var _SchemaItem = class _SchemaItem {
|
|
|
205
252
|
this.postProcess.push(fn);
|
|
206
253
|
return this;
|
|
207
254
|
}
|
|
255
|
+
parseJSON() {
|
|
256
|
+
this.addPreProcess((input) => {
|
|
257
|
+
try {
|
|
258
|
+
return typeof input === "string" ? JSON.parse(input) : input;
|
|
259
|
+
} catch {
|
|
260
|
+
return input;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
208
265
|
};
|
|
209
266
|
__decorateClass([
|
|
210
|
-
|
|
211
|
-
], _SchemaItem.prototype, "defaultValue", 1);
|
|
212
|
-
__decorateClass([
|
|
213
|
-
parceable()
|
|
267
|
+
parsable()
|
|
214
268
|
], _SchemaItem.prototype, "in", 1);
|
|
269
|
+
__decorateClass([
|
|
270
|
+
parsable()
|
|
271
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
215
272
|
var SchemaItem = _SchemaItem;
|
|
216
273
|
|
|
217
274
|
// src/items/array.ts
|
|
@@ -228,77 +285,37 @@ var SchemaArray = class extends SchemaItem {
|
|
|
228
285
|
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
229
286
|
return this;
|
|
230
287
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (!valid) {
|
|
234
|
-
return {
|
|
235
|
-
valid,
|
|
236
|
-
object,
|
|
237
|
-
errors
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
const clone = [];
|
|
241
|
-
const errs = [];
|
|
242
|
-
for (let idx = 0; idx < object.length; idx++) {
|
|
243
|
-
const item = object[idx];
|
|
244
|
-
const res = this.values.parse(item);
|
|
245
|
-
if (res.valid) {
|
|
246
|
-
clone.push(res.object);
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
const errss = res.errors.map((it) => ({
|
|
250
|
-
...it,
|
|
251
|
-
field: it.field ? `${idx}.${it.field}` : idx.toString()
|
|
252
|
-
}));
|
|
253
|
-
if (options == null ? void 0 : options.fast) {
|
|
254
|
-
return {
|
|
255
|
-
valid: false,
|
|
256
|
-
object: clone,
|
|
257
|
-
errors: errss
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
errs.push(...errss);
|
|
261
|
-
}
|
|
262
|
-
if (errs.length > 0) {
|
|
263
|
-
return {
|
|
264
|
-
valid: false,
|
|
265
|
-
object: clone,
|
|
266
|
-
errors: errs
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
return {
|
|
270
|
-
valid: true,
|
|
271
|
-
object: clone
|
|
272
|
-
};
|
|
288
|
+
getSubInputs(input) {
|
|
289
|
+
return Array.isArray(input) ? input.map((e, i) => ({ item: this.values, value: e, path: i.toString() })) : [];
|
|
273
290
|
}
|
|
274
291
|
unwrap() {
|
|
275
292
|
return this.values;
|
|
276
293
|
}
|
|
277
294
|
isOfType(input) {
|
|
278
|
-
return Array.isArray(input);
|
|
295
|
+
return Array.isArray(input) && input.every((e) => this.values.isOfType(e));
|
|
279
296
|
}
|
|
280
297
|
};
|
|
281
298
|
__decorateClass([
|
|
282
|
-
|
|
299
|
+
parsable()
|
|
283
300
|
], SchemaArray.prototype, "unique", 1);
|
|
284
301
|
__decorateClass([
|
|
285
|
-
|
|
302
|
+
parsable()
|
|
286
303
|
], SchemaArray.prototype, "split", 1);
|
|
287
304
|
|
|
288
305
|
// src/items/boolean.ts
|
|
289
306
|
var SchemaBoolean = class extends SchemaItem {
|
|
290
307
|
parseString(trueValue = "true", falseValue = "false") {
|
|
291
|
-
this.addPreProcess((
|
|
292
|
-
if (typeof
|
|
293
|
-
return
|
|
308
|
+
this.addPreProcess((input) => {
|
|
309
|
+
if (typeof input !== "string") {
|
|
310
|
+
return input;
|
|
294
311
|
}
|
|
295
|
-
if (
|
|
312
|
+
if (input === trueValue) {
|
|
296
313
|
return true;
|
|
297
314
|
}
|
|
298
|
-
if (
|
|
315
|
+
if (input === falseValue) {
|
|
299
316
|
return false;
|
|
300
317
|
}
|
|
301
|
-
return
|
|
318
|
+
return input;
|
|
302
319
|
});
|
|
303
320
|
return this;
|
|
304
321
|
}
|
|
@@ -307,7 +324,7 @@ var SchemaBoolean = class extends SchemaItem {
|
|
|
307
324
|
}
|
|
308
325
|
};
|
|
309
326
|
__decorateClass([
|
|
310
|
-
|
|
327
|
+
parsable()
|
|
311
328
|
], SchemaBoolean.prototype, "parseString", 1);
|
|
312
329
|
|
|
313
330
|
// src/items/nullable.ts
|
|
@@ -316,36 +333,29 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
316
333
|
super([child]);
|
|
317
334
|
this.child = child;
|
|
318
335
|
}
|
|
319
|
-
|
|
336
|
+
falsyAsNull() {
|
|
320
337
|
this.addPreProcess((it) => !it ? void 0 : it);
|
|
321
338
|
return this;
|
|
322
339
|
}
|
|
323
340
|
unwrap() {
|
|
324
341
|
return this.child;
|
|
325
342
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
return {
|
|
333
|
-
valid: true,
|
|
334
|
-
object: void 0
|
|
335
|
-
};
|
|
343
|
+
getSubInputs(input) {
|
|
344
|
+
return [{ value: input, item: this.child, path: void 0 }];
|
|
345
|
+
}
|
|
346
|
+
parseSubItems(input, options) {
|
|
347
|
+
if (isNull(input)) {
|
|
348
|
+
return { input: void 0 };
|
|
336
349
|
}
|
|
337
|
-
return
|
|
350
|
+
return super.parseSubItems(input, options);
|
|
338
351
|
}
|
|
339
352
|
isOfType(input) {
|
|
340
|
-
return
|
|
341
|
-
}
|
|
342
|
-
isNull(value) {
|
|
343
|
-
return typeof value === "undefined" || value === null;
|
|
353
|
+
return typeof input === "undefined" || this.child.isOfType(input);
|
|
344
354
|
}
|
|
345
355
|
};
|
|
346
356
|
__decorateClass([
|
|
347
|
-
|
|
348
|
-
], SchemaNullable.prototype, "
|
|
357
|
+
parsable()
|
|
358
|
+
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
349
359
|
|
|
350
360
|
// src/items/object.ts
|
|
351
361
|
var import_object_util2 = require("@dzeio/object-util");
|
|
@@ -355,37 +365,14 @@ var SchemaObject = class extends SchemaItem {
|
|
|
355
365
|
this.model = model;
|
|
356
366
|
this.id = "object";
|
|
357
367
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
object,
|
|
364
|
-
errors
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
const clone = (0, import_object_util2.objectClone)(object);
|
|
368
|
-
const res = {};
|
|
369
|
-
(0, import_object_util2.objectLoop)(this.model, (childSchema, key) => {
|
|
370
|
-
const childValue = clone[key];
|
|
371
|
-
const child = childSchema.parse(childValue);
|
|
372
|
-
if (!child.valid) {
|
|
373
|
-
errors.push(...child.errors.map((it) => ({
|
|
374
|
-
...it,
|
|
375
|
-
field: it.field ? `${key}.${it.field}` : key
|
|
376
|
-
})));
|
|
377
|
-
}
|
|
378
|
-
res[key] = child.object;
|
|
379
|
-
return child.valid || !(options == null ? void 0 : options.fast);
|
|
380
|
-
});
|
|
381
|
-
return {
|
|
382
|
-
valid: errors.length === 0,
|
|
383
|
-
errors,
|
|
384
|
-
object: res
|
|
385
|
-
};
|
|
368
|
+
getSubInputs(input) {
|
|
369
|
+
return (0, import_object_util2.isObject)(input) ? (0, import_object_util2.objectMap)(
|
|
370
|
+
input,
|
|
371
|
+
(v, k) => this.model[k] ? { item: this.model[k], value: v, path: k.toString() } : void 0
|
|
372
|
+
).filter((e) => !isNull(e)) : [];
|
|
386
373
|
}
|
|
387
374
|
isOfType(input) {
|
|
388
|
-
return (0, import_object_util2.isObject)(input);
|
|
375
|
+
return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item, k) => !item.isOfType(input[k]));
|
|
389
376
|
}
|
|
390
377
|
};
|
|
391
378
|
|
|
@@ -396,11 +383,12 @@ function isNull(value) {
|
|
|
396
383
|
function parseQuery(model, query, opts) {
|
|
397
384
|
const record = {};
|
|
398
385
|
for (const [key, value] of query) {
|
|
399
|
-
|
|
386
|
+
const hasMultipleValues = query.getAll(key).length > 1;
|
|
387
|
+
(0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? query.getAll(key) : value);
|
|
400
388
|
}
|
|
401
389
|
return model.parse(record, opts);
|
|
402
390
|
}
|
|
403
|
-
function parseFormData(
|
|
391
|
+
function parseFormData(schema, data, opts) {
|
|
404
392
|
const record = {};
|
|
405
393
|
for (const [key, value] of data) {
|
|
406
394
|
const hasMultipleValues = data.getAll(key).length > 1;
|
|
@@ -424,15 +412,15 @@ function parseFormData(model, data, opts) {
|
|
|
424
412
|
(0, import_object_util3.objectSet)(record, keys, !!data.get(keys.join(".")));
|
|
425
413
|
}
|
|
426
414
|
};
|
|
427
|
-
const handleSchemaForBoolean = (
|
|
428
|
-
(0, import_object_util3.objectLoop)(
|
|
415
|
+
const handleSchemaForBoolean = (model, keys = []) => {
|
|
416
|
+
(0, import_object_util3.objectLoop)(model, (value, key) => {
|
|
429
417
|
handleBoolean(value, [...keys, key]);
|
|
430
418
|
});
|
|
431
419
|
};
|
|
432
|
-
if (
|
|
433
|
-
handleSchemaForBoolean(
|
|
420
|
+
if (schema instanceof SchemaObject) {
|
|
421
|
+
handleSchemaForBoolean(schema.model);
|
|
434
422
|
}
|
|
435
|
-
return
|
|
423
|
+
return schema.parse(record, opts);
|
|
436
424
|
}
|
|
437
425
|
function parseForm(model, form, opts) {
|
|
438
426
|
return parseFormData(model, new FormData(form), opts);
|
|
@@ -440,47 +428,47 @@ function parseForm(model, form, opts) {
|
|
|
440
428
|
|
|
441
429
|
// src/items/any.ts
|
|
442
430
|
var SchemaAny = class extends SchemaItem {
|
|
443
|
-
|
|
444
|
-
isOfType(input) {
|
|
431
|
+
isOfType(_input) {
|
|
445
432
|
return true;
|
|
446
433
|
}
|
|
447
434
|
};
|
|
448
435
|
|
|
449
436
|
// src/items/date.ts
|
|
450
437
|
var SchemaDate = class extends SchemaItem {
|
|
451
|
-
parseString(format = "
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
this.addPreProcess((it) => {
|
|
456
|
-
if (typeof it !== "string") {
|
|
457
|
-
return it;
|
|
458
|
-
}
|
|
459
|
-
const date = new Date(it);
|
|
460
|
-
if (isNaN(date.getDate())) {
|
|
461
|
-
return it;
|
|
462
|
-
}
|
|
463
|
-
return date;
|
|
464
|
-
});
|
|
465
|
-
break;
|
|
438
|
+
parseString(format = "default") {
|
|
439
|
+
this.addPreProcess((input) => {
|
|
440
|
+
if (typeof input !== "string") {
|
|
441
|
+
return input;
|
|
466
442
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
443
|
+
let date = void 0;
|
|
444
|
+
switch (format) {
|
|
445
|
+
case "default": {
|
|
446
|
+
date = new Date(input);
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
case "yy-mm-dd":
|
|
450
|
+
case "iso8601": {
|
|
451
|
+
const splitted = input.split("-").map((it) => Number.parseInt(it, 10));
|
|
452
|
+
if (splitted.length !== 3) {
|
|
453
|
+
break;
|
|
471
454
|
}
|
|
455
|
+
date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case "jj/mm/yy": {
|
|
472
459
|
const splitted = input.split("/").map((it) => Number.parseInt(it, 10));
|
|
473
460
|
if (splitted.length !== 3) {
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
const date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
477
|
-
if (isNaN(date.getDate())) {
|
|
478
|
-
return input;
|
|
461
|
+
break;
|
|
479
462
|
}
|
|
480
|
-
|
|
481
|
-
|
|
463
|
+
date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
482
466
|
}
|
|
483
|
-
|
|
467
|
+
if (!date || isNaN(date.getDate())) {
|
|
468
|
+
return input;
|
|
469
|
+
}
|
|
470
|
+
return date;
|
|
471
|
+
});
|
|
484
472
|
return this;
|
|
485
473
|
}
|
|
486
474
|
isOfType(input) {
|
|
@@ -488,7 +476,7 @@ var SchemaDate = class extends SchemaItem {
|
|
|
488
476
|
}
|
|
489
477
|
};
|
|
490
478
|
__decorateClass([
|
|
491
|
-
|
|
479
|
+
parsable()
|
|
492
480
|
], SchemaDate.prototype, "parseString", 1);
|
|
493
481
|
|
|
494
482
|
// src/items/record.ts
|
|
@@ -499,35 +487,27 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
499
487
|
this.keys = keys;
|
|
500
488
|
this.values = values;
|
|
501
489
|
}
|
|
502
|
-
|
|
503
|
-
const {
|
|
504
|
-
if (
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
490
|
+
parseSubItems(input, options) {
|
|
491
|
+
const result = { input, errors: [] };
|
|
492
|
+
if ((0, import_object_util4.isObject)(input)) {
|
|
493
|
+
const clone = {};
|
|
494
|
+
(0, import_object_util4.objectLoop)(input, (value, key) => {
|
|
495
|
+
var _a, _b;
|
|
496
|
+
const res1 = this.keys.parse(key);
|
|
497
|
+
const res2 = this.values.parse(value);
|
|
498
|
+
if (!res1.valid || !res2.valid) {
|
|
499
|
+
result.errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
|
|
500
|
+
message: it.message,
|
|
501
|
+
field: it.field ? `${key}.${it.field}` : key.toString()
|
|
502
|
+
})));
|
|
503
|
+
} else {
|
|
504
|
+
clone[res1.object] = res2.object;
|
|
505
|
+
}
|
|
506
|
+
return result.errors.length === 0 || !(options == null ? void 0 : options.fast);
|
|
507
|
+
});
|
|
508
|
+
result.input = clone;
|
|
510
509
|
}
|
|
511
|
-
|
|
512
|
-
(0, import_object_util4.objectLoop)(object, (value, key) => {
|
|
513
|
-
var _a, _b;
|
|
514
|
-
const res1 = this.keys.parse(key);
|
|
515
|
-
const res2 = this.values.parse(value);
|
|
516
|
-
if (!res1.valid || !res2.valid) {
|
|
517
|
-
errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
|
|
518
|
-
message: it.message,
|
|
519
|
-
field: it.field ? `${key}.${it.field}` : key.toString()
|
|
520
|
-
})));
|
|
521
|
-
} else {
|
|
522
|
-
clone[res1.object] = res2.object;
|
|
523
|
-
}
|
|
524
|
-
return errors.length === 0 || !(options == null ? void 0 : options.fast);
|
|
525
|
-
});
|
|
526
|
-
return {
|
|
527
|
-
valid: errors.length === 0,
|
|
528
|
-
errors,
|
|
529
|
-
object: clone
|
|
530
|
-
};
|
|
510
|
+
return result;
|
|
531
511
|
}
|
|
532
512
|
isOfType(input) {
|
|
533
513
|
return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
@@ -605,9 +585,7 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
605
585
|
return this;
|
|
606
586
|
}
|
|
607
587
|
parseString() {
|
|
608
|
-
this.addPreProcess(
|
|
609
|
-
(input) => typeof input === "string" ? Number.parseFloat(input) : input
|
|
610
|
-
);
|
|
588
|
+
this.addPreProcess((input) => typeof input === "string" ? parseFloat(input) : input);
|
|
611
589
|
return this;
|
|
612
590
|
}
|
|
613
591
|
isOfType(input) {
|
|
@@ -621,19 +599,19 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
621
599
|
}
|
|
622
600
|
};
|
|
623
601
|
__decorateClass([
|
|
624
|
-
|
|
602
|
+
parsable()
|
|
625
603
|
], SchemaNumber.prototype, "lte", 1);
|
|
626
604
|
__decorateClass([
|
|
627
|
-
|
|
605
|
+
parsable()
|
|
628
606
|
], SchemaNumber.prototype, "gte", 1);
|
|
629
607
|
__decorateClass([
|
|
630
|
-
|
|
608
|
+
parsable()
|
|
631
609
|
], SchemaNumber.prototype, "lt", 1);
|
|
632
610
|
__decorateClass([
|
|
633
|
-
|
|
611
|
+
parsable()
|
|
634
612
|
], SchemaNumber.prototype, "gt", 1);
|
|
635
613
|
__decorateClass([
|
|
636
|
-
|
|
614
|
+
parsable()
|
|
637
615
|
], SchemaNumber.prototype, "parseString", 1);
|
|
638
616
|
|
|
639
617
|
// src/items/string.ts
|
|
@@ -690,57 +668,170 @@ var SchemaString = class extends SchemaItem {
|
|
|
690
668
|
}
|
|
691
669
|
};
|
|
692
670
|
__decorateClass([
|
|
693
|
-
|
|
671
|
+
parsable()
|
|
694
672
|
], SchemaString.prototype, "min", 1);
|
|
695
673
|
__decorateClass([
|
|
696
|
-
|
|
674
|
+
parsable()
|
|
697
675
|
], SchemaString.prototype, "toCasing", 1);
|
|
698
676
|
__decorateClass([
|
|
699
|
-
|
|
677
|
+
parsable()
|
|
700
678
|
], SchemaString.prototype, "max", 1);
|
|
701
679
|
__decorateClass([
|
|
702
|
-
|
|
680
|
+
parsable()
|
|
703
681
|
], SchemaString.prototype, "notEmpty", 1);
|
|
704
682
|
__decorateClass([
|
|
705
|
-
|
|
683
|
+
parsable()
|
|
706
684
|
], SchemaString.prototype, "regex", 1);
|
|
707
685
|
|
|
708
686
|
// src/items/union.ts
|
|
709
687
|
var SchemaUnion = class extends SchemaItem {
|
|
710
688
|
constructor(...schemas) {
|
|
711
|
-
super();
|
|
689
|
+
super(schemas);
|
|
712
690
|
this.schemas = schemas;
|
|
713
691
|
}
|
|
714
|
-
|
|
715
|
-
const {
|
|
716
|
-
if (!valid) {
|
|
717
|
-
return {
|
|
718
|
-
valid,
|
|
719
|
-
object,
|
|
720
|
-
errors
|
|
721
|
-
};
|
|
722
|
-
}
|
|
692
|
+
parseSubItems(input, options) {
|
|
693
|
+
const result = { input, errors: [] };
|
|
723
694
|
for (const schema of this.schemas) {
|
|
724
|
-
const validation = schema.parse(input, options);
|
|
695
|
+
const validation = schema.parse(result.input, options);
|
|
725
696
|
if (validation.valid) {
|
|
726
|
-
return validation;
|
|
697
|
+
return { input: validation.object };
|
|
727
698
|
} else {
|
|
728
|
-
errors.push(...validation.errors);
|
|
699
|
+
result.errors.push(...validation.errors);
|
|
729
700
|
}
|
|
730
701
|
}
|
|
731
|
-
return
|
|
732
|
-
valid: false,
|
|
733
|
-
errors,
|
|
734
|
-
object
|
|
735
|
-
};
|
|
702
|
+
return result;
|
|
736
703
|
}
|
|
737
704
|
isOfType(input) {
|
|
738
705
|
return this.schemas.some((it) => it.isOfType(input));
|
|
739
706
|
}
|
|
740
707
|
};
|
|
741
708
|
|
|
709
|
+
// src/items/intersection.ts
|
|
710
|
+
var import_object_util5 = require("@dzeio/object-util");
|
|
711
|
+
var SchemaIntersection = class extends SchemaItem {
|
|
712
|
+
constructor(...schemas) {
|
|
713
|
+
super(schemas);
|
|
714
|
+
this.schemas = schemas;
|
|
715
|
+
}
|
|
716
|
+
parseSubItems(input, options) {
|
|
717
|
+
const result = { input, errors: [] };
|
|
718
|
+
for (const schema of this.schemas) {
|
|
719
|
+
const validation = schema.parse(result.input, options);
|
|
720
|
+
if (!validation.valid) {
|
|
721
|
+
result.errors.push(...validation.errors);
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
if ((0, import_object_util5.isObject)(result.input)) {
|
|
725
|
+
Object.assign(result.input, validation.object);
|
|
726
|
+
} else {
|
|
727
|
+
result.input = validation.object;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return result;
|
|
731
|
+
}
|
|
732
|
+
isOfType(input) {
|
|
733
|
+
return this.schemas.every((it) => it.isOfType(input));
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
// src/items/default.ts
|
|
738
|
+
var SchemaDefault = class extends SchemaItem {
|
|
739
|
+
constructor(child, defaultVal, strict = true) {
|
|
740
|
+
super([child, defaultVal, strict]);
|
|
741
|
+
this.child = child;
|
|
742
|
+
this.defaultVal = defaultVal;
|
|
743
|
+
this.strict = strict;
|
|
744
|
+
}
|
|
745
|
+
unwrap() {
|
|
746
|
+
return this.child;
|
|
747
|
+
}
|
|
748
|
+
getSubInputs(input) {
|
|
749
|
+
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
750
|
+
}
|
|
751
|
+
isOfType(input) {
|
|
752
|
+
return this.child.isOfType(input);
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
// src/items/file.ts
|
|
757
|
+
var SchemaFile = class extends SchemaItem {
|
|
758
|
+
constructor() {
|
|
759
|
+
super();
|
|
760
|
+
this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
|
|
761
|
+
}
|
|
762
|
+
extension(ext, error) {
|
|
763
|
+
this.addValidation({ fn: (input) => {
|
|
764
|
+
var _a;
|
|
765
|
+
return (_a = input.name) == null ? void 0 : _a.endsWith(ext);
|
|
766
|
+
}, error });
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
maxSize(size, error) {
|
|
770
|
+
this.addValidation({ fn: (file) => file.size <= size, error });
|
|
771
|
+
return this;
|
|
772
|
+
}
|
|
773
|
+
isOfType(input) {
|
|
774
|
+
return input instanceof File;
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
__decorateClass([
|
|
778
|
+
parsable()
|
|
779
|
+
], SchemaFile.prototype, "extension", 1);
|
|
780
|
+
__decorateClass([
|
|
781
|
+
parsable()
|
|
782
|
+
], SchemaFile.prototype, "maxSize", 1);
|
|
783
|
+
|
|
784
|
+
// src/items/tuple.ts
|
|
785
|
+
var SchemaTuple = class extends SchemaItem {
|
|
786
|
+
constructor(...children) {
|
|
787
|
+
super(children);
|
|
788
|
+
this.children = children;
|
|
789
|
+
}
|
|
790
|
+
getSubInputs(input) {
|
|
791
|
+
return Array.isArray(input) ? this.children.map((e, i) => ({ value: input[i], item: e, path: i.toString() })) : [];
|
|
792
|
+
}
|
|
793
|
+
isOfType(input) {
|
|
794
|
+
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
795
|
+
return false;
|
|
796
|
+
}
|
|
797
|
+
for (const [i, item] of this.children.entries()) {
|
|
798
|
+
if (item instanceof SchemaItem && !item.isOfType(input[i])) {
|
|
799
|
+
return false;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
|
|
806
|
+
// src/items/nullish.ts
|
|
807
|
+
var SchemaUndefined = class extends SchemaItem {
|
|
808
|
+
isOfType(input) {
|
|
809
|
+
return typeof input === "undefined";
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
var SchemaNull = class extends SchemaItem {
|
|
813
|
+
isOfType(input) {
|
|
814
|
+
return input === null;
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
var SchemaVoid = class extends SchemaItem {
|
|
818
|
+
isOfType(input) {
|
|
819
|
+
return typeof input === "undefined";
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
var SchemaNullish = class extends SchemaItem {
|
|
823
|
+
isOfType(input) {
|
|
824
|
+
return isNull(input);
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
var SchemaNever = class extends SchemaItem {
|
|
828
|
+
isOfType(_input) {
|
|
829
|
+
return false;
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
|
|
742
833
|
// src/Schema.ts
|
|
743
|
-
function
|
|
834
|
+
function parsable() {
|
|
744
835
|
return (target, propertyKey, descriptor) => {
|
|
745
836
|
if (!(propertyKey in target)) {
|
|
746
837
|
throw new Error("property not set in object");
|
|
@@ -757,6 +848,7 @@ function parceable() {
|
|
|
757
848
|
var Types = {
|
|
758
849
|
Any: SchemaAny,
|
|
759
850
|
Array: SchemaArray,
|
|
851
|
+
Tuple: SchemaTuple,
|
|
760
852
|
Boolean: SchemaBoolean,
|
|
761
853
|
Date: SchemaDate,
|
|
762
854
|
Enum: SchemaEnum,
|
|
@@ -765,7 +857,13 @@ var Types = {
|
|
|
765
857
|
Object: SchemaObject,
|
|
766
858
|
Record: SchemaRecord,
|
|
767
859
|
String: SchemaString,
|
|
768
|
-
Union: SchemaUnion
|
|
860
|
+
Union: SchemaUnion,
|
|
861
|
+
Intersection: SchemaIntersection,
|
|
862
|
+
Default: SchemaDefault,
|
|
863
|
+
Undefined: SchemaUndefined,
|
|
864
|
+
Void: SchemaVoid,
|
|
865
|
+
Null: SchemaNull,
|
|
866
|
+
Nullish: SchemaNullish
|
|
769
867
|
};
|
|
770
868
|
var _Schema = class _Schema extends SchemaObject {
|
|
771
869
|
static register(module2) {
|
|
@@ -815,9 +913,33 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
815
913
|
static string(...inputs) {
|
|
816
914
|
return new SchemaString(...inputs);
|
|
817
915
|
}
|
|
916
|
+
static file(...inputs) {
|
|
917
|
+
return new SchemaFile(...inputs);
|
|
918
|
+
}
|
|
818
919
|
static union(...inputs) {
|
|
819
920
|
return new SchemaUnion(...inputs);
|
|
820
921
|
}
|
|
922
|
+
static intersection(...inputs) {
|
|
923
|
+
return new SchemaIntersection(...inputs);
|
|
924
|
+
}
|
|
925
|
+
static tuple(...inputs) {
|
|
926
|
+
return new SchemaTuple(...inputs);
|
|
927
|
+
}
|
|
928
|
+
static undefined(...inputs) {
|
|
929
|
+
return new SchemaUndefined(...inputs);
|
|
930
|
+
}
|
|
931
|
+
static null(...inputs) {
|
|
932
|
+
return new SchemaNull(...inputs);
|
|
933
|
+
}
|
|
934
|
+
static nullish(...inputs) {
|
|
935
|
+
return new SchemaNullish(...inputs);
|
|
936
|
+
}
|
|
937
|
+
static void(...inputs) {
|
|
938
|
+
return new SchemaVoid(...inputs);
|
|
939
|
+
}
|
|
940
|
+
static never(...inputs) {
|
|
941
|
+
return new SchemaNever(...inputs);
|
|
942
|
+
}
|
|
821
943
|
static fromJSON(json) {
|
|
822
944
|
var _a, _b, _c, _d, _e, _f;
|
|
823
945
|
const fn = this.getModule(json.i);
|
|
@@ -870,6 +992,7 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
870
992
|
};
|
|
871
993
|
_Schema.registeredModules = [
|
|
872
994
|
SchemaArray,
|
|
995
|
+
SchemaTuple,
|
|
873
996
|
SchemaBoolean,
|
|
874
997
|
SchemaDate,
|
|
875
998
|
SchemaEnum,
|
|
@@ -878,7 +1001,13 @@ _Schema.registeredModules = [
|
|
|
878
1001
|
SchemaObject,
|
|
879
1002
|
SchemaRecord,
|
|
880
1003
|
SchemaString,
|
|
881
|
-
SchemaUnion
|
|
1004
|
+
SchemaUnion,
|
|
1005
|
+
SchemaIntersection,
|
|
1006
|
+
SchemaDefault,
|
|
1007
|
+
SchemaUndefined,
|
|
1008
|
+
SchemaVoid,
|
|
1009
|
+
SchemaNull,
|
|
1010
|
+
SchemaNullish
|
|
882
1011
|
];
|
|
883
1012
|
var Schema = _Schema;
|
|
884
1013
|
var s = Schema;
|
|
@@ -888,18 +1017,26 @@ var s = Schema;
|
|
|
888
1017
|
SchemaArray,
|
|
889
1018
|
SchemaBoolean,
|
|
890
1019
|
SchemaDate,
|
|
1020
|
+
SchemaDefault,
|
|
891
1021
|
SchemaEnum,
|
|
1022
|
+
SchemaFile,
|
|
1023
|
+
SchemaIntersection,
|
|
892
1024
|
SchemaItem,
|
|
893
1025
|
SchemaLiteral,
|
|
1026
|
+
SchemaNever,
|
|
1027
|
+
SchemaNull,
|
|
894
1028
|
SchemaNullable,
|
|
1029
|
+
SchemaNullish,
|
|
895
1030
|
SchemaNumber,
|
|
896
1031
|
SchemaObject,
|
|
897
1032
|
SchemaRecord,
|
|
898
1033
|
SchemaString,
|
|
1034
|
+
SchemaUndefined,
|
|
899
1035
|
SchemaUnion,
|
|
1036
|
+
SchemaVoid,
|
|
900
1037
|
Types,
|
|
901
1038
|
isNull,
|
|
902
|
-
|
|
1039
|
+
parsable,
|
|
903
1040
|
parseForm,
|
|
904
1041
|
parseFormData,
|
|
905
1042
|
parseQuery,
|