@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.mjs
CHANGED
|
@@ -10,10 +10,10 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/helpers.ts
|
|
13
|
-
import { objectGet, objectLoop, objectSet as objectSet2 } from "@dzeio/object-util";
|
|
13
|
+
import { objectClone, objectGet, objectLoop, objectSet as objectSet2 } from "@dzeio/object-util";
|
|
14
14
|
|
|
15
15
|
// src/SchemaItem.ts
|
|
16
|
-
import { isObject, objectClean, objectSet } from "@dzeio/object-util";
|
|
16
|
+
import { isObject, objectClean, objectRemap, objectSet } from "@dzeio/object-util";
|
|
17
17
|
var _SchemaItem = class _SchemaItem {
|
|
18
18
|
constructor(items) {
|
|
19
19
|
// standard Schema V1 spec
|
|
@@ -33,14 +33,18 @@ var _SchemaItem = class _SchemaItem {
|
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
-
/**
|
|
37
|
-
* keep public ?
|
|
38
|
-
*/
|
|
39
|
-
this.validations = [];
|
|
40
36
|
/**
|
|
41
37
|
* Function calls saved for serialization
|
|
42
38
|
*/
|
|
43
39
|
this.savedCalls = [];
|
|
40
|
+
/**
|
|
41
|
+
* list of attributes for custom works
|
|
42
|
+
*/
|
|
43
|
+
this.attributes = [];
|
|
44
|
+
/**
|
|
45
|
+
* keep public ?
|
|
46
|
+
*/
|
|
47
|
+
this.validations = [];
|
|
44
48
|
/**
|
|
45
49
|
* Pre process the variable for various reasons
|
|
46
50
|
*
|
|
@@ -58,16 +62,23 @@ var _SchemaItem = class _SchemaItem {
|
|
|
58
62
|
* note: the type of the final post-process MUST be valid
|
|
59
63
|
*/
|
|
60
64
|
this.postProcess = [];
|
|
61
|
-
/**
|
|
62
|
-
* list of attributes for custom works
|
|
63
|
-
*/
|
|
64
|
-
this.attributes = [];
|
|
65
65
|
this.invalidError = "the field is invalid";
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering, @typescript-eslint/unbound-method
|
|
66
67
|
this.default = this.defaultValue;
|
|
67
68
|
if (items && items.length > 0) {
|
|
68
69
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
69
70
|
}
|
|
70
71
|
}
|
|
72
|
+
parseJSON() {
|
|
73
|
+
this.addPreProcess((input) => {
|
|
74
|
+
try {
|
|
75
|
+
return typeof input === "string" ? JSON.parse(input) : input;
|
|
76
|
+
} catch {
|
|
77
|
+
return input;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
71
82
|
in(...values) {
|
|
72
83
|
this.addValidation((input) => values.includes(input));
|
|
73
84
|
return this;
|
|
@@ -101,37 +112,7 @@ var _SchemaItem = class _SchemaItem {
|
|
|
101
112
|
array() {
|
|
102
113
|
return new SchemaArray(this);
|
|
103
114
|
}
|
|
104
|
-
|
|
105
|
-
getSubInputs(_input) {
|
|
106
|
-
return [];
|
|
107
|
-
}
|
|
108
|
-
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
109
|
-
updateSubItemInput(path, input, value) {
|
|
110
|
-
if (path && isObject(input)) {
|
|
111
|
-
objectSet(input, path.split("."), value);
|
|
112
|
-
return input;
|
|
113
|
-
} else {
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
parseSubItems(input, options) {
|
|
118
|
-
const result = { errors: [], input };
|
|
119
|
-
for (const { item, value, path } of this.getSubInputs(result.input)) {
|
|
120
|
-
const parsed = item.parse(value, options);
|
|
121
|
-
if (parsed.valid) {
|
|
122
|
-
result.input = this.updateSubItemInput(path, result.input, parsed.object);
|
|
123
|
-
} else {
|
|
124
|
-
for (const error of parsed.errors) {
|
|
125
|
-
error.field = error.field ? `${path}.${error.field}` : path;
|
|
126
|
-
result.errors.push(error);
|
|
127
|
-
}
|
|
128
|
-
if (options == null ? void 0 : options.fast) {
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
115
|
+
// eslint-disable-next-line complexity
|
|
135
116
|
parse(input, options) {
|
|
136
117
|
var _a, _b;
|
|
137
118
|
for (const preProcess of this.preProcess) {
|
|
@@ -148,7 +129,7 @@ var _SchemaItem = class _SchemaItem {
|
|
|
148
129
|
if (!this.isOfType(input)) {
|
|
149
130
|
return {
|
|
150
131
|
valid: false,
|
|
151
|
-
errors: [{ message:
|
|
132
|
+
errors: [{ message: this.invalidError }]
|
|
152
133
|
};
|
|
153
134
|
}
|
|
154
135
|
const validationErrors = [];
|
|
@@ -180,14 +161,24 @@ var _SchemaItem = class _SchemaItem {
|
|
|
180
161
|
toJSON() {
|
|
181
162
|
var _a;
|
|
182
163
|
const res = {
|
|
183
|
-
|
|
164
|
+
_i: this.constructor.name,
|
|
184
165
|
a: this.attributes.length > 0 ? this.attributes : void 0,
|
|
185
|
-
c: (_a = this.items) == null ? void 0 : _a.map((it) =>
|
|
166
|
+
c: (_a = this.items) == null ? void 0 : _a.map((it) => this.deepSerializeItem(it)),
|
|
186
167
|
f: this.savedCalls.map((it) => it.args ? { n: it.name, a: Array.from(it.args) } : { n: it.name })
|
|
187
168
|
};
|
|
188
169
|
objectClean(res, { deep: false });
|
|
189
170
|
return res;
|
|
190
171
|
}
|
|
172
|
+
deepSerializeItem(item) {
|
|
173
|
+
if (item instanceof _SchemaItem) {
|
|
174
|
+
return item.toJSON();
|
|
175
|
+
} else if (Array.isArray(item)) {
|
|
176
|
+
return item.map((it) => this.deepSerializeItem(it));
|
|
177
|
+
} else if (isObject(item)) {
|
|
178
|
+
return objectRemap(item, (value, key) => ({ value: this.deepSerializeItem(value), key }));
|
|
179
|
+
}
|
|
180
|
+
return item;
|
|
181
|
+
}
|
|
191
182
|
addValidation(fn, error) {
|
|
192
183
|
this.validations.push(typeof fn === "function" ? { fn, error } : fn);
|
|
193
184
|
return this;
|
|
@@ -201,23 +192,44 @@ var _SchemaItem = class _SchemaItem {
|
|
|
201
192
|
this.postProcess.push(fn);
|
|
202
193
|
return this;
|
|
203
194
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
195
|
+
/** Returns the sub values of the input, the schema item to handle them, and the path to it */
|
|
196
|
+
getSubInputs(_input) {
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
200
|
+
updateSubItemInput(path, input, value) {
|
|
201
|
+
if (path && isObject(input)) {
|
|
202
|
+
objectSet(input, path.split("."), value);
|
|
203
|
+
return input;
|
|
204
|
+
} else {
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
parseSubItems(input, options) {
|
|
209
|
+
const result = { errors: [], input };
|
|
210
|
+
for (const { item, value, path } of this.getSubInputs(result.input)) {
|
|
211
|
+
const parsed = item.parse(value, options);
|
|
212
|
+
if (parsed.valid) {
|
|
213
|
+
result.input = this.updateSubItemInput(path, result.input, parsed.object);
|
|
214
|
+
} else {
|
|
215
|
+
for (const error of parsed.errors) {
|
|
216
|
+
error.field = error.field ? `${path}.${error.field}` : path;
|
|
217
|
+
result.errors.push(error);
|
|
218
|
+
}
|
|
219
|
+
if (options == null ? void 0 : options.fast) {
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
210
222
|
}
|
|
211
|
-
}
|
|
212
|
-
return
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
213
225
|
}
|
|
214
226
|
};
|
|
215
227
|
__decorateClass([
|
|
216
228
|
parsable()
|
|
217
|
-
], _SchemaItem.prototype, "
|
|
229
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
218
230
|
__decorateClass([
|
|
219
231
|
parsable()
|
|
220
|
-
], _SchemaItem.prototype, "
|
|
232
|
+
], _SchemaItem.prototype, "in", 1);
|
|
221
233
|
var SchemaItem = _SchemaItem;
|
|
222
234
|
|
|
223
235
|
// src/items/array.ts
|
|
@@ -230,23 +242,30 @@ var SchemaArray = class extends SchemaItem {
|
|
|
230
242
|
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
231
243
|
return this;
|
|
232
244
|
}
|
|
245
|
+
clean() {
|
|
246
|
+
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
233
249
|
split(separator = ",") {
|
|
234
250
|
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
235
251
|
return this;
|
|
236
252
|
}
|
|
237
|
-
getSubInputs(input) {
|
|
238
|
-
return Array.isArray(input) ? input.map((e, i) => ({ item: this.values, value: e, path: i.toString() })) : [];
|
|
239
|
-
}
|
|
240
253
|
unwrap() {
|
|
241
254
|
return this.values;
|
|
242
255
|
}
|
|
243
256
|
isOfType(input) {
|
|
244
|
-
return Array.isArray(input) && input.every((
|
|
257
|
+
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
258
|
+
}
|
|
259
|
+
getSubInputs(input) {
|
|
260
|
+
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
245
261
|
}
|
|
246
262
|
};
|
|
247
263
|
__decorateClass([
|
|
248
264
|
parsable()
|
|
249
265
|
], SchemaArray.prototype, "unique", 1);
|
|
266
|
+
__decorateClass([
|
|
267
|
+
parsable()
|
|
268
|
+
], SchemaArray.prototype, "clean", 1);
|
|
250
269
|
__decorateClass([
|
|
251
270
|
parsable()
|
|
252
271
|
], SchemaArray.prototype, "split", 1);
|
|
@@ -286,9 +305,20 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
286
305
|
this.addPreProcess((it) => !it ? void 0 : it);
|
|
287
306
|
return this;
|
|
288
307
|
}
|
|
308
|
+
emptyAsNull() {
|
|
309
|
+
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
310
|
+
return this;
|
|
311
|
+
}
|
|
312
|
+
emptyFileAsNull() {
|
|
313
|
+
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
314
|
+
return this;
|
|
315
|
+
}
|
|
289
316
|
unwrap() {
|
|
290
317
|
return this.child;
|
|
291
318
|
}
|
|
319
|
+
isOfType(input) {
|
|
320
|
+
return typeof input === "undefined" || this.child.isOfType(input);
|
|
321
|
+
}
|
|
292
322
|
getSubInputs(input) {
|
|
293
323
|
return [{ value: input, item: this.child, path: void 0 }];
|
|
294
324
|
}
|
|
@@ -298,13 +328,16 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
298
328
|
}
|
|
299
329
|
return super.parseSubItems(input, options);
|
|
300
330
|
}
|
|
301
|
-
isOfType(input) {
|
|
302
|
-
return typeof input === "undefined" || this.child.isOfType(input);
|
|
303
|
-
}
|
|
304
331
|
};
|
|
305
332
|
__decorateClass([
|
|
306
333
|
parsable()
|
|
307
334
|
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
335
|
+
__decorateClass([
|
|
336
|
+
parsable()
|
|
337
|
+
], SchemaNullable.prototype, "emptyAsNull", 1);
|
|
338
|
+
__decorateClass([
|
|
339
|
+
parsable()
|
|
340
|
+
], SchemaNullable.prototype, "emptyFileAsNull", 1);
|
|
308
341
|
|
|
309
342
|
// src/items/object.ts
|
|
310
343
|
import { isObject as isObject2, objectFind, objectMap } from "@dzeio/object-util";
|
|
@@ -314,14 +347,11 @@ var SchemaObject = class extends SchemaItem {
|
|
|
314
347
|
this.model = model;
|
|
315
348
|
this.id = "object";
|
|
316
349
|
}
|
|
317
|
-
getSubInputs(input) {
|
|
318
|
-
return isObject2(input) ? objectMap(
|
|
319
|
-
input,
|
|
320
|
-
(v, k) => this.model[k] ? { item: this.model[k], value: v, path: k.toString() } : void 0
|
|
321
|
-
).filter((e) => !isNull(e)) : [];
|
|
322
|
-
}
|
|
323
350
|
isOfType(input) {
|
|
324
|
-
return isObject2(input) && !objectFind(this.model, (item,
|
|
351
|
+
return isObject2(input) && !objectFind(this.model, (item, key) => !item.isOfType(input[key]));
|
|
352
|
+
}
|
|
353
|
+
getSubInputs(input) {
|
|
354
|
+
return isObject2(input) ? objectMap(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
325
355
|
}
|
|
326
356
|
};
|
|
327
357
|
|
|
@@ -329,19 +359,25 @@ var SchemaObject = class extends SchemaItem {
|
|
|
329
359
|
function isNull(value) {
|
|
330
360
|
return typeof value === "undefined" || value === null;
|
|
331
361
|
}
|
|
332
|
-
function parseQuery(
|
|
362
|
+
function parseQuery(schema, query, opts) {
|
|
333
363
|
const record = {};
|
|
334
364
|
for (const [key, value] of query) {
|
|
335
|
-
const
|
|
336
|
-
|
|
365
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
366
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
367
|
+
const allValues = query.getAll(key);
|
|
368
|
+
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
369
|
+
objectSet2(record, keys, finalValue);
|
|
337
370
|
}
|
|
338
|
-
return
|
|
371
|
+
return schema.parse(record, opts);
|
|
339
372
|
}
|
|
340
373
|
function parseFormData(schema, data, opts) {
|
|
341
374
|
const record = {};
|
|
342
375
|
for (const [key, value] of data) {
|
|
343
|
-
const
|
|
344
|
-
|
|
376
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
377
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
378
|
+
const allValues = data.getAll(key);
|
|
379
|
+
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
380
|
+
objectSet2(record, keys, finalValue);
|
|
345
381
|
}
|
|
346
382
|
const handleBoolean = (value, keys) => {
|
|
347
383
|
var _a;
|
|
@@ -374,6 +410,27 @@ function parseFormData(schema, data, opts) {
|
|
|
374
410
|
function parseForm(model, form, opts) {
|
|
375
411
|
return parseFormData(model, new FormData(form), opts);
|
|
376
412
|
}
|
|
413
|
+
function getSchemaItemAtPath(schema, path) {
|
|
414
|
+
let current = schema;
|
|
415
|
+
const internalPath = objectClone(path);
|
|
416
|
+
for (const key of path) {
|
|
417
|
+
if (!current) {
|
|
418
|
+
return void 0;
|
|
419
|
+
}
|
|
420
|
+
internalPath.shift();
|
|
421
|
+
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
422
|
+
current = current.unwrap();
|
|
423
|
+
}
|
|
424
|
+
if (current instanceof SchemaObject) {
|
|
425
|
+
current = current.model[key];
|
|
426
|
+
current = getSchemaItemAtPath(current, internalPath);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
430
|
+
current = current.unwrap();
|
|
431
|
+
}
|
|
432
|
+
return current;
|
|
433
|
+
}
|
|
377
434
|
|
|
378
435
|
// src/items/any.ts
|
|
379
436
|
var SchemaAny = class extends SchemaItem {
|
|
@@ -389,7 +446,7 @@ var SchemaDate = class extends SchemaItem {
|
|
|
389
446
|
if (typeof input !== "string") {
|
|
390
447
|
return input;
|
|
391
448
|
}
|
|
392
|
-
let date
|
|
449
|
+
let date;
|
|
393
450
|
switch (format) {
|
|
394
451
|
case "default": {
|
|
395
452
|
date = new Date(input);
|
|
@@ -436,6 +493,9 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
436
493
|
this.keys = keys;
|
|
437
494
|
this.values = values;
|
|
438
495
|
}
|
|
496
|
+
isOfType(input) {
|
|
497
|
+
return isObject3(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
498
|
+
}
|
|
439
499
|
parseSubItems(input, options) {
|
|
440
500
|
const result = { input, errors: [] };
|
|
441
501
|
if (isObject3(input)) {
|
|
@@ -458,9 +518,6 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
458
518
|
}
|
|
459
519
|
return result;
|
|
460
520
|
}
|
|
461
|
-
isOfType(input) {
|
|
462
|
-
return isObject3(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
463
|
-
}
|
|
464
521
|
};
|
|
465
522
|
|
|
466
523
|
// src/items/enum.ts
|
|
@@ -485,13 +542,13 @@ var SchemaEnum = class extends SchemaItem {
|
|
|
485
542
|
|
|
486
543
|
// src/items/literal.ts
|
|
487
544
|
var SchemaLiteral = class extends SchemaItem {
|
|
488
|
-
constructor(
|
|
489
|
-
super(
|
|
490
|
-
this.
|
|
491
|
-
this.validations.push({ fn: (it) =>
|
|
545
|
+
constructor(...values) {
|
|
546
|
+
super(values);
|
|
547
|
+
this.values = values;
|
|
548
|
+
this.validations.push({ fn: (it) => this.values.includes(it) });
|
|
492
549
|
}
|
|
493
550
|
isOfType(input) {
|
|
494
|
-
return typeof input === typeof
|
|
551
|
+
return this.values.some((it) => typeof input === typeof it);
|
|
495
552
|
}
|
|
496
553
|
};
|
|
497
554
|
|
|
@@ -534,7 +591,7 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
534
591
|
return this;
|
|
535
592
|
}
|
|
536
593
|
parseString() {
|
|
537
|
-
this.addPreProcess((input) => typeof input === "string" ? parseFloat(input) : input);
|
|
594
|
+
this.addPreProcess((input) => typeof input === "string" ? Number.parseFloat(input) : input);
|
|
538
595
|
return this;
|
|
539
596
|
}
|
|
540
597
|
isOfType(input) {
|
|
@@ -606,6 +663,15 @@ var SchemaString = class extends SchemaItem {
|
|
|
606
663
|
});
|
|
607
664
|
return this;
|
|
608
665
|
}
|
|
666
|
+
email(message) {
|
|
667
|
+
this.addValidation({
|
|
668
|
+
fn(input) {
|
|
669
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(input);
|
|
670
|
+
},
|
|
671
|
+
error: message
|
|
672
|
+
});
|
|
673
|
+
return this;
|
|
674
|
+
}
|
|
609
675
|
minLength(value, message) {
|
|
610
676
|
return this.min(value, message);
|
|
611
677
|
}
|
|
@@ -631,6 +697,9 @@ __decorateClass([
|
|
|
631
697
|
__decorateClass([
|
|
632
698
|
parsable()
|
|
633
699
|
], SchemaString.prototype, "regex", 1);
|
|
700
|
+
__decorateClass([
|
|
701
|
+
parsable()
|
|
702
|
+
], SchemaString.prototype, "email", 1);
|
|
634
703
|
|
|
635
704
|
// src/items/union.ts
|
|
636
705
|
var SchemaUnion = class extends SchemaItem {
|
|
@@ -638,6 +707,9 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
638
707
|
super(schemas);
|
|
639
708
|
this.schemas = schemas;
|
|
640
709
|
}
|
|
710
|
+
isOfType(input) {
|
|
711
|
+
return this.schemas.some((it) => it.isOfType(input));
|
|
712
|
+
}
|
|
641
713
|
parseSubItems(input, options) {
|
|
642
714
|
const result = { input, errors: [] };
|
|
643
715
|
for (const schema of this.schemas) {
|
|
@@ -650,9 +722,6 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
650
722
|
}
|
|
651
723
|
return result;
|
|
652
724
|
}
|
|
653
|
-
isOfType(input) {
|
|
654
|
-
return this.schemas.some((it) => it.isOfType(input));
|
|
655
|
-
}
|
|
656
725
|
};
|
|
657
726
|
|
|
658
727
|
// src/items/intersection.ts
|
|
@@ -662,6 +731,9 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
662
731
|
super(schemas);
|
|
663
732
|
this.schemas = schemas;
|
|
664
733
|
}
|
|
734
|
+
isOfType(input) {
|
|
735
|
+
return this.schemas.every((it) => it.isOfType(input));
|
|
736
|
+
}
|
|
665
737
|
parseSubItems(input, options) {
|
|
666
738
|
const result = { input, errors: [] };
|
|
667
739
|
for (const schema of this.schemas) {
|
|
@@ -678,9 +750,6 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
678
750
|
}
|
|
679
751
|
return result;
|
|
680
752
|
}
|
|
681
|
-
isOfType(input) {
|
|
682
|
-
return this.schemas.every((it) => it.isOfType(input));
|
|
683
|
-
}
|
|
684
753
|
};
|
|
685
754
|
|
|
686
755
|
// src/items/default.ts
|
|
@@ -694,12 +763,12 @@ var SchemaDefault = class extends SchemaItem {
|
|
|
694
763
|
unwrap() {
|
|
695
764
|
return this.child;
|
|
696
765
|
}
|
|
697
|
-
getSubInputs(input) {
|
|
698
|
-
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
699
|
-
}
|
|
700
766
|
isOfType(input) {
|
|
701
767
|
return this.child.isOfType(input);
|
|
702
768
|
}
|
|
769
|
+
getSubInputs(input) {
|
|
770
|
+
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
771
|
+
}
|
|
703
772
|
};
|
|
704
773
|
|
|
705
774
|
// src/items/file.ts
|
|
@@ -709,10 +778,7 @@ var SchemaFile = class extends SchemaItem {
|
|
|
709
778
|
this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
|
|
710
779
|
}
|
|
711
780
|
extension(ext, error) {
|
|
712
|
-
this.addValidation({ fn: (input) =>
|
|
713
|
-
var _a;
|
|
714
|
-
return (_a = input.name) == null ? void 0 : _a.endsWith(ext);
|
|
715
|
-
}, error });
|
|
781
|
+
this.addValidation({ fn: (input) => input.name.endsWith(ext), error });
|
|
716
782
|
return this;
|
|
717
783
|
}
|
|
718
784
|
maxSize(size, error) {
|
|
@@ -736,20 +802,20 @@ var SchemaTuple = class extends SchemaItem {
|
|
|
736
802
|
super(children);
|
|
737
803
|
this.children = children;
|
|
738
804
|
}
|
|
739
|
-
getSubInputs(input) {
|
|
740
|
-
return Array.isArray(input) ? this.children.map((e, i) => ({ value: input[i], item: e, path: i.toString() })) : [];
|
|
741
|
-
}
|
|
742
805
|
isOfType(input) {
|
|
743
806
|
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
744
807
|
return false;
|
|
745
808
|
}
|
|
746
|
-
for (const [
|
|
747
|
-
if (item instanceof SchemaItem && !item.isOfType(input[
|
|
809
|
+
for (const [index, item] of this.children.entries()) {
|
|
810
|
+
if (item instanceof SchemaItem && !item.isOfType(input[index])) {
|
|
748
811
|
return false;
|
|
749
812
|
}
|
|
750
813
|
}
|
|
751
814
|
return true;
|
|
752
815
|
}
|
|
816
|
+
getSubInputs(input) {
|
|
817
|
+
return Array.isArray(input) ? this.children.map((entry, index) => ({ value: input[index], item: entry, path: index.toString() })) : [];
|
|
818
|
+
}
|
|
753
819
|
};
|
|
754
820
|
|
|
755
821
|
// src/items/nullish.ts
|
|
@@ -780,6 +846,7 @@ var SchemaNever = class extends SchemaItem {
|
|
|
780
846
|
};
|
|
781
847
|
|
|
782
848
|
// src/Schema.ts
|
|
849
|
+
import { isObject as isObject5, objectRemap as objectRemap2 } from "@dzeio/object-util";
|
|
783
850
|
function parsable() {
|
|
784
851
|
return (target, propertyKey, descriptor) => {
|
|
785
852
|
if (!(propertyKey in target)) {
|
|
@@ -794,32 +861,16 @@ function parsable() {
|
|
|
794
861
|
return descriptor;
|
|
795
862
|
};
|
|
796
863
|
}
|
|
797
|
-
var Types = {
|
|
798
|
-
Any: SchemaAny,
|
|
799
|
-
Array: SchemaArray,
|
|
800
|
-
Tuple: SchemaTuple,
|
|
801
|
-
Boolean: SchemaBoolean,
|
|
802
|
-
Date: SchemaDate,
|
|
803
|
-
Enum: SchemaEnum,
|
|
804
|
-
Literal: SchemaLiteral,
|
|
805
|
-
Nullable: SchemaNullable,
|
|
806
|
-
Object: SchemaObject,
|
|
807
|
-
Record: SchemaRecord,
|
|
808
|
-
String: SchemaString,
|
|
809
|
-
Union: SchemaUnion,
|
|
810
|
-
Intersection: SchemaIntersection,
|
|
811
|
-
Default: SchemaDefault,
|
|
812
|
-
Undefined: SchemaUndefined,
|
|
813
|
-
Void: SchemaVoid,
|
|
814
|
-
Null: SchemaNull,
|
|
815
|
-
Nullish: SchemaNullish
|
|
816
|
-
};
|
|
817
864
|
var _Schema = class _Schema extends SchemaObject {
|
|
818
865
|
static register(module) {
|
|
819
866
|
this.registeredModules.push(module);
|
|
820
867
|
}
|
|
821
868
|
static getModule(name) {
|
|
822
|
-
|
|
869
|
+
const module = this.registeredModules.find((it) => it.name === name);
|
|
870
|
+
if (!module) {
|
|
871
|
+
console.log("Couldn't find module", name);
|
|
872
|
+
}
|
|
873
|
+
return module != null ? module : SchemaAny;
|
|
823
874
|
}
|
|
824
875
|
static any() {
|
|
825
876
|
return new SchemaAny();
|
|
@@ -841,8 +892,8 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
841
892
|
* @param input the literal value (note: append `as const` else the typing won't work correctly)
|
|
842
893
|
* @returns
|
|
843
894
|
*/
|
|
844
|
-
static literal(
|
|
845
|
-
return new SchemaLiteral(
|
|
895
|
+
static literal(...inputs) {
|
|
896
|
+
return new SchemaLiteral(...inputs);
|
|
846
897
|
}
|
|
847
898
|
static nullable(...inputs) {
|
|
848
899
|
return new SchemaNullable(...inputs);
|
|
@@ -891,25 +942,35 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
891
942
|
}
|
|
892
943
|
static fromJSON(json) {
|
|
893
944
|
var _a, _b, _c, _d, _e, _f;
|
|
894
|
-
const fn = this.getModule(json.
|
|
945
|
+
const fn = this.getModule(json._i);
|
|
895
946
|
if (!fn) {
|
|
896
|
-
throw new Error(`Schema cannot parse ${json.
|
|
947
|
+
throw new Error(`Schema cannot parse ${json._i}`);
|
|
897
948
|
}
|
|
898
|
-
const item = new fn(...(_b = (_a = json.c) == null ? void 0 : _a.map((it) => this.
|
|
949
|
+
const item = new fn(...(_b = (_a = json.c) == null ? void 0 : _a.map((it) => this.deepParseSchemaJSON(it))) != null ? _b : []);
|
|
899
950
|
for (const validation of (_c = json.f) != null ? _c : []) {
|
|
900
951
|
if (!(validation.n in item)) {
|
|
901
952
|
throw new Error("validation not available in Schema Item");
|
|
902
953
|
}
|
|
903
|
-
item[validation.n](...(_e = (_d = validation.a) == null ? void 0 : _d.map((it) =>
|
|
954
|
+
item[validation.n](...(_e = (_d = validation.a) == null ? void 0 : _d.map((it) => this.deepParseSchemaJSON(it))) != null ? _e : []);
|
|
904
955
|
}
|
|
905
956
|
item.attrs(...(_f = json.a) != null ? _f : []);
|
|
906
957
|
return item;
|
|
907
958
|
}
|
|
959
|
+
static deepParseSchemaJSON(item) {
|
|
960
|
+
if (this.isSchemaJSON(item)) {
|
|
961
|
+
return _Schema.fromJSON(item);
|
|
962
|
+
} else if (Array.isArray(item)) {
|
|
963
|
+
return item.map((it) => this.deepParseSchemaJSON(it));
|
|
964
|
+
} else if (isObject5(item)) {
|
|
965
|
+
return objectRemap2(item, (value, key) => ({ value: this.deepParseSchemaJSON(value), key }));
|
|
966
|
+
}
|
|
967
|
+
return item;
|
|
968
|
+
}
|
|
908
969
|
static isSchemaJSON(data) {
|
|
909
970
|
if (typeof data !== "object" || data === null) {
|
|
910
971
|
return false;
|
|
911
972
|
}
|
|
912
|
-
if (!("
|
|
973
|
+
if (!("_i" in data)) {
|
|
913
974
|
return false;
|
|
914
975
|
}
|
|
915
976
|
return true;
|
|
@@ -956,7 +1017,10 @@ _Schema.registeredModules = [
|
|
|
956
1017
|
SchemaUndefined,
|
|
957
1018
|
SchemaVoid,
|
|
958
1019
|
SchemaNull,
|
|
959
|
-
SchemaNullish
|
|
1020
|
+
SchemaNullish,
|
|
1021
|
+
SchemaFile,
|
|
1022
|
+
SchemaAny,
|
|
1023
|
+
SchemaNumber
|
|
960
1024
|
];
|
|
961
1025
|
var Schema = _Schema;
|
|
962
1026
|
var s = Schema;
|
|
@@ -982,7 +1046,6 @@ export {
|
|
|
982
1046
|
SchemaUndefined,
|
|
983
1047
|
SchemaUnion,
|
|
984
1048
|
SchemaVoid,
|
|
985
|
-
Types,
|
|
986
1049
|
Schema as default,
|
|
987
1050
|
isNull,
|
|
988
1051
|
parsable,
|