@dzeio/schema 0.5.0 → 0.7.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 +60 -65
- package/dist/Schema.d.ts +60 -65
- package/dist/Schema.js +193 -131
- package/dist/Schema.mjs +195 -132
- package/package.json +1 -1
package/dist/Schema.js
CHANGED
|
@@ -49,7 +49,6 @@ __export(Schema_exports, {
|
|
|
49
49
|
SchemaUndefined: () => SchemaUndefined,
|
|
50
50
|
SchemaUnion: () => SchemaUnion,
|
|
51
51
|
SchemaVoid: () => SchemaVoid,
|
|
52
|
-
Types: () => Types,
|
|
53
52
|
default: () => Schema,
|
|
54
53
|
isNull: () => isNull,
|
|
55
54
|
parsable: () => parsable,
|
|
@@ -84,14 +83,18 @@ var _SchemaItem = class _SchemaItem {
|
|
|
84
83
|
};
|
|
85
84
|
}
|
|
86
85
|
};
|
|
87
|
-
/**
|
|
88
|
-
* keep public ?
|
|
89
|
-
*/
|
|
90
|
-
this.validations = [];
|
|
91
86
|
/**
|
|
92
87
|
* Function calls saved for serialization
|
|
93
88
|
*/
|
|
94
89
|
this.savedCalls = [];
|
|
90
|
+
/**
|
|
91
|
+
* list of attributes for custom works
|
|
92
|
+
*/
|
|
93
|
+
this.attributes = [];
|
|
94
|
+
/**
|
|
95
|
+
* keep public ?
|
|
96
|
+
*/
|
|
97
|
+
this.validations = [];
|
|
95
98
|
/**
|
|
96
99
|
* Pre process the variable for various reasons
|
|
97
100
|
*
|
|
@@ -109,16 +112,23 @@ var _SchemaItem = class _SchemaItem {
|
|
|
109
112
|
* note: the type of the final post-process MUST be valid
|
|
110
113
|
*/
|
|
111
114
|
this.postProcess = [];
|
|
112
|
-
/**
|
|
113
|
-
* list of attributes for custom works
|
|
114
|
-
*/
|
|
115
|
-
this.attributes = [];
|
|
116
115
|
this.invalidError = "the field is invalid";
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering, @typescript-eslint/unbound-method
|
|
117
117
|
this.default = this.defaultValue;
|
|
118
118
|
if (items && items.length > 0) {
|
|
119
119
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
+
parseJSON() {
|
|
123
|
+
this.addPreProcess((input) => {
|
|
124
|
+
try {
|
|
125
|
+
return typeof input === "string" ? JSON.parse(input) : input;
|
|
126
|
+
} catch {
|
|
127
|
+
return input;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
122
132
|
in(...values) {
|
|
123
133
|
this.addValidation((input) => values.includes(input));
|
|
124
134
|
return this;
|
|
@@ -152,37 +162,7 @@ var _SchemaItem = class _SchemaItem {
|
|
|
152
162
|
array() {
|
|
153
163
|
return new SchemaArray(this);
|
|
154
164
|
}
|
|
155
|
-
|
|
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
|
-
}
|
|
165
|
+
// eslint-disable-next-line complexity
|
|
186
166
|
parse(input, options) {
|
|
187
167
|
var _a, _b;
|
|
188
168
|
for (const preProcess of this.preProcess) {
|
|
@@ -199,7 +179,7 @@ var _SchemaItem = class _SchemaItem {
|
|
|
199
179
|
if (!this.isOfType(input)) {
|
|
200
180
|
return {
|
|
201
181
|
valid: false,
|
|
202
|
-
errors: [{ message:
|
|
182
|
+
errors: [{ message: this.invalidError }]
|
|
203
183
|
};
|
|
204
184
|
}
|
|
205
185
|
const validationErrors = [];
|
|
@@ -231,14 +211,24 @@ var _SchemaItem = class _SchemaItem {
|
|
|
231
211
|
toJSON() {
|
|
232
212
|
var _a;
|
|
233
213
|
const res = {
|
|
234
|
-
|
|
214
|
+
_i: this.constructor.name,
|
|
235
215
|
a: this.attributes.length > 0 ? this.attributes : void 0,
|
|
236
|
-
c: (_a = this.items) == null ? void 0 : _a.map((it) =>
|
|
216
|
+
c: (_a = this.items) == null ? void 0 : _a.map((it) => this.deepSerializeItem(it)),
|
|
237
217
|
f: this.savedCalls.map((it) => it.args ? { n: it.name, a: Array.from(it.args) } : { n: it.name })
|
|
238
218
|
};
|
|
239
219
|
(0, import_object_util.objectClean)(res, { deep: false });
|
|
240
220
|
return res;
|
|
241
221
|
}
|
|
222
|
+
deepSerializeItem(item) {
|
|
223
|
+
if (item instanceof _SchemaItem) {
|
|
224
|
+
return item.toJSON();
|
|
225
|
+
} else if (Array.isArray(item)) {
|
|
226
|
+
return item.map((it) => this.deepSerializeItem(it));
|
|
227
|
+
} else if ((0, import_object_util.isObject)(item)) {
|
|
228
|
+
return (0, import_object_util.objectRemap)(item, (value, key) => ({ value: this.deepSerializeItem(value), key }));
|
|
229
|
+
}
|
|
230
|
+
return item;
|
|
231
|
+
}
|
|
242
232
|
addValidation(fn, error) {
|
|
243
233
|
this.validations.push(typeof fn === "function" ? { fn, error } : fn);
|
|
244
234
|
return this;
|
|
@@ -252,23 +242,44 @@ var _SchemaItem = class _SchemaItem {
|
|
|
252
242
|
this.postProcess.push(fn);
|
|
253
243
|
return this;
|
|
254
244
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
245
|
+
/** Returns the sub values of the input, the schema item to handle them, and the path to it */
|
|
246
|
+
getSubInputs(_input) {
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
249
|
+
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
250
|
+
updateSubItemInput(path, input, value) {
|
|
251
|
+
if (path && (0, import_object_util.isObject)(input)) {
|
|
252
|
+
(0, import_object_util.objectSet)(input, path.split("."), value);
|
|
253
|
+
return input;
|
|
254
|
+
} else {
|
|
255
|
+
return value;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
parseSubItems(input, options) {
|
|
259
|
+
const result = { errors: [], input };
|
|
260
|
+
for (const { item, value, path } of this.getSubInputs(result.input)) {
|
|
261
|
+
const parsed = item.parse(value, options);
|
|
262
|
+
if (parsed.valid) {
|
|
263
|
+
result.input = this.updateSubItemInput(path, result.input, parsed.object);
|
|
264
|
+
} else {
|
|
265
|
+
for (const error of parsed.errors) {
|
|
266
|
+
error.field = error.field ? `${path}.${error.field}` : path;
|
|
267
|
+
result.errors.push(error);
|
|
268
|
+
}
|
|
269
|
+
if (options == null ? void 0 : options.fast) {
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
261
272
|
}
|
|
262
|
-
}
|
|
263
|
-
return
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
264
275
|
}
|
|
265
276
|
};
|
|
266
277
|
__decorateClass([
|
|
267
278
|
parsable()
|
|
268
|
-
], _SchemaItem.prototype, "
|
|
279
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
269
280
|
__decorateClass([
|
|
270
281
|
parsable()
|
|
271
|
-
], _SchemaItem.prototype, "
|
|
282
|
+
], _SchemaItem.prototype, "in", 1);
|
|
272
283
|
var SchemaItem = _SchemaItem;
|
|
273
284
|
|
|
274
285
|
// src/items/array.ts
|
|
@@ -281,23 +292,30 @@ var SchemaArray = class extends SchemaItem {
|
|
|
281
292
|
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
282
293
|
return this;
|
|
283
294
|
}
|
|
295
|
+
clean() {
|
|
296
|
+
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
284
299
|
split(separator = ",") {
|
|
285
300
|
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
286
301
|
return this;
|
|
287
302
|
}
|
|
288
|
-
getSubInputs(input) {
|
|
289
|
-
return Array.isArray(input) ? input.map((e, i) => ({ item: this.values, value: e, path: i.toString() })) : [];
|
|
290
|
-
}
|
|
291
303
|
unwrap() {
|
|
292
304
|
return this.values;
|
|
293
305
|
}
|
|
294
306
|
isOfType(input) {
|
|
295
|
-
return Array.isArray(input) && input.every((
|
|
307
|
+
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
308
|
+
}
|
|
309
|
+
getSubInputs(input) {
|
|
310
|
+
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
296
311
|
}
|
|
297
312
|
};
|
|
298
313
|
__decorateClass([
|
|
299
314
|
parsable()
|
|
300
315
|
], SchemaArray.prototype, "unique", 1);
|
|
316
|
+
__decorateClass([
|
|
317
|
+
parsable()
|
|
318
|
+
], SchemaArray.prototype, "clean", 1);
|
|
301
319
|
__decorateClass([
|
|
302
320
|
parsable()
|
|
303
321
|
], SchemaArray.prototype, "split", 1);
|
|
@@ -337,9 +355,20 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
337
355
|
this.addPreProcess((it) => !it ? void 0 : it);
|
|
338
356
|
return this;
|
|
339
357
|
}
|
|
358
|
+
emptyAsNull() {
|
|
359
|
+
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
emptyFileAsNull() {
|
|
363
|
+
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
340
366
|
unwrap() {
|
|
341
367
|
return this.child;
|
|
342
368
|
}
|
|
369
|
+
isOfType(input) {
|
|
370
|
+
return typeof input === "undefined" || this.child.isOfType(input);
|
|
371
|
+
}
|
|
343
372
|
getSubInputs(input) {
|
|
344
373
|
return [{ value: input, item: this.child, path: void 0 }];
|
|
345
374
|
}
|
|
@@ -349,13 +378,16 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
349
378
|
}
|
|
350
379
|
return super.parseSubItems(input, options);
|
|
351
380
|
}
|
|
352
|
-
isOfType(input) {
|
|
353
|
-
return typeof input === "undefined" || this.child.isOfType(input);
|
|
354
|
-
}
|
|
355
381
|
};
|
|
356
382
|
__decorateClass([
|
|
357
383
|
parsable()
|
|
358
384
|
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
385
|
+
__decorateClass([
|
|
386
|
+
parsable()
|
|
387
|
+
], SchemaNullable.prototype, "emptyAsNull", 1);
|
|
388
|
+
__decorateClass([
|
|
389
|
+
parsable()
|
|
390
|
+
], SchemaNullable.prototype, "emptyFileAsNull", 1);
|
|
359
391
|
|
|
360
392
|
// src/items/object.ts
|
|
361
393
|
var import_object_util2 = require("@dzeio/object-util");
|
|
@@ -365,14 +397,11 @@ var SchemaObject = class extends SchemaItem {
|
|
|
365
397
|
this.model = model;
|
|
366
398
|
this.id = "object";
|
|
367
399
|
}
|
|
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)) : [];
|
|
373
|
-
}
|
|
374
400
|
isOfType(input) {
|
|
375
|
-
return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item,
|
|
401
|
+
return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item, key) => !item.isOfType(input[key]));
|
|
402
|
+
}
|
|
403
|
+
getSubInputs(input) {
|
|
404
|
+
return (0, import_object_util2.isObject)(input) ? (0, import_object_util2.objectMap)(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
376
405
|
}
|
|
377
406
|
};
|
|
378
407
|
|
|
@@ -380,19 +409,25 @@ var SchemaObject = class extends SchemaItem {
|
|
|
380
409
|
function isNull(value) {
|
|
381
410
|
return typeof value === "undefined" || value === null;
|
|
382
411
|
}
|
|
383
|
-
function parseQuery(
|
|
412
|
+
function parseQuery(schema, query, opts) {
|
|
384
413
|
const record = {};
|
|
385
414
|
for (const [key, value] of query) {
|
|
386
|
-
const
|
|
387
|
-
|
|
415
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
416
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
417
|
+
const allValues = query.getAll(key);
|
|
418
|
+
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
419
|
+
(0, import_object_util3.objectSet)(record, keys, finalValue);
|
|
388
420
|
}
|
|
389
|
-
return
|
|
421
|
+
return schema.parse(record, opts);
|
|
390
422
|
}
|
|
391
423
|
function parseFormData(schema, data, opts) {
|
|
392
424
|
const record = {};
|
|
393
425
|
for (const [key, value] of data) {
|
|
394
|
-
const
|
|
395
|
-
|
|
426
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
427
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
428
|
+
const allValues = data.getAll(key);
|
|
429
|
+
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
430
|
+
(0, import_object_util3.objectSet)(record, keys, finalValue);
|
|
396
431
|
}
|
|
397
432
|
const handleBoolean = (value, keys) => {
|
|
398
433
|
var _a;
|
|
@@ -425,6 +460,27 @@ function parseFormData(schema, data, opts) {
|
|
|
425
460
|
function parseForm(model, form, opts) {
|
|
426
461
|
return parseFormData(model, new FormData(form), opts);
|
|
427
462
|
}
|
|
463
|
+
function getSchemaItemAtPath(schema, path) {
|
|
464
|
+
let current = schema;
|
|
465
|
+
const internalPath = (0, import_object_util3.objectClone)(path);
|
|
466
|
+
for (const key of path) {
|
|
467
|
+
if (!current) {
|
|
468
|
+
return void 0;
|
|
469
|
+
}
|
|
470
|
+
internalPath.shift();
|
|
471
|
+
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
472
|
+
current = current.unwrap();
|
|
473
|
+
}
|
|
474
|
+
if (current instanceof SchemaObject) {
|
|
475
|
+
current = current.model[key];
|
|
476
|
+
current = getSchemaItemAtPath(current, internalPath);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
480
|
+
current = current.unwrap();
|
|
481
|
+
}
|
|
482
|
+
return current;
|
|
483
|
+
}
|
|
428
484
|
|
|
429
485
|
// src/items/any.ts
|
|
430
486
|
var SchemaAny = class extends SchemaItem {
|
|
@@ -440,7 +496,7 @@ var SchemaDate = class extends SchemaItem {
|
|
|
440
496
|
if (typeof input !== "string") {
|
|
441
497
|
return input;
|
|
442
498
|
}
|
|
443
|
-
let date
|
|
499
|
+
let date;
|
|
444
500
|
switch (format) {
|
|
445
501
|
case "default": {
|
|
446
502
|
date = new Date(input);
|
|
@@ -487,6 +543,9 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
487
543
|
this.keys = keys;
|
|
488
544
|
this.values = values;
|
|
489
545
|
}
|
|
546
|
+
isOfType(input) {
|
|
547
|
+
return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
548
|
+
}
|
|
490
549
|
parseSubItems(input, options) {
|
|
491
550
|
const result = { input, errors: [] };
|
|
492
551
|
if ((0, import_object_util4.isObject)(input)) {
|
|
@@ -509,9 +568,6 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
509
568
|
}
|
|
510
569
|
return result;
|
|
511
570
|
}
|
|
512
|
-
isOfType(input) {
|
|
513
|
-
return (0, import_object_util4.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
514
|
-
}
|
|
515
571
|
};
|
|
516
572
|
|
|
517
573
|
// src/items/enum.ts
|
|
@@ -536,13 +592,13 @@ var SchemaEnum = class extends SchemaItem {
|
|
|
536
592
|
|
|
537
593
|
// src/items/literal.ts
|
|
538
594
|
var SchemaLiteral = class extends SchemaItem {
|
|
539
|
-
constructor(
|
|
540
|
-
super(
|
|
541
|
-
this.
|
|
542
|
-
this.validations.push({ fn: (it) =>
|
|
595
|
+
constructor(...values) {
|
|
596
|
+
super(values);
|
|
597
|
+
this.values = values;
|
|
598
|
+
this.validations.push({ fn: (it) => this.values.includes(it) });
|
|
543
599
|
}
|
|
544
600
|
isOfType(input) {
|
|
545
|
-
return typeof input === typeof
|
|
601
|
+
return this.values.some((it) => typeof input === typeof it);
|
|
546
602
|
}
|
|
547
603
|
};
|
|
548
604
|
|
|
@@ -585,7 +641,7 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
585
641
|
return this;
|
|
586
642
|
}
|
|
587
643
|
parseString() {
|
|
588
|
-
this.addPreProcess((input) => typeof input === "string" ? parseFloat(input) : input);
|
|
644
|
+
this.addPreProcess((input) => typeof input === "string" ? Number.parseFloat(input) : input);
|
|
589
645
|
return this;
|
|
590
646
|
}
|
|
591
647
|
isOfType(input) {
|
|
@@ -657,6 +713,15 @@ var SchemaString = class extends SchemaItem {
|
|
|
657
713
|
});
|
|
658
714
|
return this;
|
|
659
715
|
}
|
|
716
|
+
email(message) {
|
|
717
|
+
this.addValidation({
|
|
718
|
+
fn(input) {
|
|
719
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(input);
|
|
720
|
+
},
|
|
721
|
+
error: message
|
|
722
|
+
});
|
|
723
|
+
return this;
|
|
724
|
+
}
|
|
660
725
|
minLength(value, message) {
|
|
661
726
|
return this.min(value, message);
|
|
662
727
|
}
|
|
@@ -682,6 +747,9 @@ __decorateClass([
|
|
|
682
747
|
__decorateClass([
|
|
683
748
|
parsable()
|
|
684
749
|
], SchemaString.prototype, "regex", 1);
|
|
750
|
+
__decorateClass([
|
|
751
|
+
parsable()
|
|
752
|
+
], SchemaString.prototype, "email", 1);
|
|
685
753
|
|
|
686
754
|
// src/items/union.ts
|
|
687
755
|
var SchemaUnion = class extends SchemaItem {
|
|
@@ -689,6 +757,9 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
689
757
|
super(schemas);
|
|
690
758
|
this.schemas = schemas;
|
|
691
759
|
}
|
|
760
|
+
isOfType(input) {
|
|
761
|
+
return this.schemas.some((it) => it.isOfType(input));
|
|
762
|
+
}
|
|
692
763
|
parseSubItems(input, options) {
|
|
693
764
|
const result = { input, errors: [] };
|
|
694
765
|
for (const schema of this.schemas) {
|
|
@@ -701,9 +772,6 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
701
772
|
}
|
|
702
773
|
return result;
|
|
703
774
|
}
|
|
704
|
-
isOfType(input) {
|
|
705
|
-
return this.schemas.some((it) => it.isOfType(input));
|
|
706
|
-
}
|
|
707
775
|
};
|
|
708
776
|
|
|
709
777
|
// src/items/intersection.ts
|
|
@@ -713,6 +781,9 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
713
781
|
super(schemas);
|
|
714
782
|
this.schemas = schemas;
|
|
715
783
|
}
|
|
784
|
+
isOfType(input) {
|
|
785
|
+
return this.schemas.every((it) => it.isOfType(input));
|
|
786
|
+
}
|
|
716
787
|
parseSubItems(input, options) {
|
|
717
788
|
const result = { input, errors: [] };
|
|
718
789
|
for (const schema of this.schemas) {
|
|
@@ -729,9 +800,6 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
729
800
|
}
|
|
730
801
|
return result;
|
|
731
802
|
}
|
|
732
|
-
isOfType(input) {
|
|
733
|
-
return this.schemas.every((it) => it.isOfType(input));
|
|
734
|
-
}
|
|
735
803
|
};
|
|
736
804
|
|
|
737
805
|
// src/items/default.ts
|
|
@@ -745,12 +813,12 @@ var SchemaDefault = class extends SchemaItem {
|
|
|
745
813
|
unwrap() {
|
|
746
814
|
return this.child;
|
|
747
815
|
}
|
|
748
|
-
getSubInputs(input) {
|
|
749
|
-
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
750
|
-
}
|
|
751
816
|
isOfType(input) {
|
|
752
817
|
return this.child.isOfType(input);
|
|
753
818
|
}
|
|
819
|
+
getSubInputs(input) {
|
|
820
|
+
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
821
|
+
}
|
|
754
822
|
};
|
|
755
823
|
|
|
756
824
|
// src/items/file.ts
|
|
@@ -760,10 +828,7 @@ var SchemaFile = class extends SchemaItem {
|
|
|
760
828
|
this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
|
|
761
829
|
}
|
|
762
830
|
extension(ext, error) {
|
|
763
|
-
this.addValidation({ fn: (input) =>
|
|
764
|
-
var _a;
|
|
765
|
-
return (_a = input.name) == null ? void 0 : _a.endsWith(ext);
|
|
766
|
-
}, error });
|
|
831
|
+
this.addValidation({ fn: (input) => input.name.endsWith(ext), error });
|
|
767
832
|
return this;
|
|
768
833
|
}
|
|
769
834
|
maxSize(size, error) {
|
|
@@ -787,20 +852,20 @@ var SchemaTuple = class extends SchemaItem {
|
|
|
787
852
|
super(children);
|
|
788
853
|
this.children = children;
|
|
789
854
|
}
|
|
790
|
-
getSubInputs(input) {
|
|
791
|
-
return Array.isArray(input) ? this.children.map((e, i) => ({ value: input[i], item: e, path: i.toString() })) : [];
|
|
792
|
-
}
|
|
793
855
|
isOfType(input) {
|
|
794
856
|
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
795
857
|
return false;
|
|
796
858
|
}
|
|
797
|
-
for (const [
|
|
798
|
-
if (item instanceof SchemaItem && !item.isOfType(input[
|
|
859
|
+
for (const [index, item] of this.children.entries()) {
|
|
860
|
+
if (item instanceof SchemaItem && !item.isOfType(input[index])) {
|
|
799
861
|
return false;
|
|
800
862
|
}
|
|
801
863
|
}
|
|
802
864
|
return true;
|
|
803
865
|
}
|
|
866
|
+
getSubInputs(input) {
|
|
867
|
+
return Array.isArray(input) ? this.children.map((entry, index) => ({ value: input[index], item: entry, path: index.toString() })) : [];
|
|
868
|
+
}
|
|
804
869
|
};
|
|
805
870
|
|
|
806
871
|
// src/items/nullish.ts
|
|
@@ -831,6 +896,7 @@ var SchemaNever = class extends SchemaItem {
|
|
|
831
896
|
};
|
|
832
897
|
|
|
833
898
|
// src/Schema.ts
|
|
899
|
+
var import_object_util6 = require("@dzeio/object-util");
|
|
834
900
|
function parsable() {
|
|
835
901
|
return (target, propertyKey, descriptor) => {
|
|
836
902
|
if (!(propertyKey in target)) {
|
|
@@ -845,32 +911,16 @@ function parsable() {
|
|
|
845
911
|
return descriptor;
|
|
846
912
|
};
|
|
847
913
|
}
|
|
848
|
-
var Types = {
|
|
849
|
-
Any: SchemaAny,
|
|
850
|
-
Array: SchemaArray,
|
|
851
|
-
Tuple: SchemaTuple,
|
|
852
|
-
Boolean: SchemaBoolean,
|
|
853
|
-
Date: SchemaDate,
|
|
854
|
-
Enum: SchemaEnum,
|
|
855
|
-
Literal: SchemaLiteral,
|
|
856
|
-
Nullable: SchemaNullable,
|
|
857
|
-
Object: SchemaObject,
|
|
858
|
-
Record: SchemaRecord,
|
|
859
|
-
String: SchemaString,
|
|
860
|
-
Union: SchemaUnion,
|
|
861
|
-
Intersection: SchemaIntersection,
|
|
862
|
-
Default: SchemaDefault,
|
|
863
|
-
Undefined: SchemaUndefined,
|
|
864
|
-
Void: SchemaVoid,
|
|
865
|
-
Null: SchemaNull,
|
|
866
|
-
Nullish: SchemaNullish
|
|
867
|
-
};
|
|
868
914
|
var _Schema = class _Schema extends SchemaObject {
|
|
869
915
|
static register(module2) {
|
|
870
916
|
this.registeredModules.push(module2);
|
|
871
917
|
}
|
|
872
918
|
static getModule(name) {
|
|
873
|
-
|
|
919
|
+
const module2 = this.registeredModules.find((it) => it.name === name);
|
|
920
|
+
if (!module2) {
|
|
921
|
+
console.log("Couldn't find module", name);
|
|
922
|
+
}
|
|
923
|
+
return module2 != null ? module2 : SchemaAny;
|
|
874
924
|
}
|
|
875
925
|
static any() {
|
|
876
926
|
return new SchemaAny();
|
|
@@ -892,8 +942,8 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
892
942
|
* @param input the literal value (note: append `as const` else the typing won't work correctly)
|
|
893
943
|
* @returns
|
|
894
944
|
*/
|
|
895
|
-
static literal(
|
|
896
|
-
return new SchemaLiteral(
|
|
945
|
+
static literal(...inputs) {
|
|
946
|
+
return new SchemaLiteral(...inputs);
|
|
897
947
|
}
|
|
898
948
|
static nullable(...inputs) {
|
|
899
949
|
return new SchemaNullable(...inputs);
|
|
@@ -942,25 +992,35 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
942
992
|
}
|
|
943
993
|
static fromJSON(json) {
|
|
944
994
|
var _a, _b, _c, _d, _e, _f;
|
|
945
|
-
const fn = this.getModule(json.
|
|
995
|
+
const fn = this.getModule(json._i);
|
|
946
996
|
if (!fn) {
|
|
947
|
-
throw new Error(`Schema cannot parse ${json.
|
|
997
|
+
throw new Error(`Schema cannot parse ${json._i}`);
|
|
948
998
|
}
|
|
949
|
-
const item = new fn(...(_b = (_a = json.c) == null ? void 0 : _a.map((it) => this.
|
|
999
|
+
const item = new fn(...(_b = (_a = json.c) == null ? void 0 : _a.map((it) => this.deepParseSchemaJSON(it))) != null ? _b : []);
|
|
950
1000
|
for (const validation of (_c = json.f) != null ? _c : []) {
|
|
951
1001
|
if (!(validation.n in item)) {
|
|
952
1002
|
throw new Error("validation not available in Schema Item");
|
|
953
1003
|
}
|
|
954
|
-
item[validation.n](...(_e = (_d = validation.a) == null ? void 0 : _d.map((it) =>
|
|
1004
|
+
item[validation.n](...(_e = (_d = validation.a) == null ? void 0 : _d.map((it) => this.deepParseSchemaJSON(it))) != null ? _e : []);
|
|
955
1005
|
}
|
|
956
1006
|
item.attrs(...(_f = json.a) != null ? _f : []);
|
|
957
1007
|
return item;
|
|
958
1008
|
}
|
|
1009
|
+
static deepParseSchemaJSON(item) {
|
|
1010
|
+
if (this.isSchemaJSON(item)) {
|
|
1011
|
+
return _Schema.fromJSON(item);
|
|
1012
|
+
} else if (Array.isArray(item)) {
|
|
1013
|
+
return item.map((it) => this.deepParseSchemaJSON(it));
|
|
1014
|
+
} else if ((0, import_object_util6.isObject)(item)) {
|
|
1015
|
+
return (0, import_object_util6.objectRemap)(item, (value, key) => ({ value: this.deepParseSchemaJSON(value), key }));
|
|
1016
|
+
}
|
|
1017
|
+
return item;
|
|
1018
|
+
}
|
|
959
1019
|
static isSchemaJSON(data) {
|
|
960
1020
|
if (typeof data !== "object" || data === null) {
|
|
961
1021
|
return false;
|
|
962
1022
|
}
|
|
963
|
-
if (!("
|
|
1023
|
+
if (!("_i" in data)) {
|
|
964
1024
|
return false;
|
|
965
1025
|
}
|
|
966
1026
|
return true;
|
|
@@ -1007,7 +1067,10 @@ _Schema.registeredModules = [
|
|
|
1007
1067
|
SchemaUndefined,
|
|
1008
1068
|
SchemaVoid,
|
|
1009
1069
|
SchemaNull,
|
|
1010
|
-
SchemaNullish
|
|
1070
|
+
SchemaNullish,
|
|
1071
|
+
SchemaFile,
|
|
1072
|
+
SchemaAny,
|
|
1073
|
+
SchemaNumber
|
|
1011
1074
|
];
|
|
1012
1075
|
var Schema = _Schema;
|
|
1013
1076
|
var s = Schema;
|
|
@@ -1034,7 +1097,6 @@ var s = Schema;
|
|
|
1034
1097
|
SchemaUndefined,
|
|
1035
1098
|
SchemaUnion,
|
|
1036
1099
|
SchemaVoid,
|
|
1037
|
-
Types,
|
|
1038
1100
|
isNull,
|
|
1039
1101
|
parsable,
|
|
1040
1102
|
parseForm,
|