@dzeio/schema 0.9.0 → 0.11.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 +43 -34
- package/dist/Schema.d.ts +43 -34
- package/dist/Schema.js +283 -233
- package/dist/Schema.mjs +278 -230
- package/package.json +1 -1
package/dist/Schema.js
CHANGED
|
@@ -46,10 +46,12 @@ __export(Schema_exports, {
|
|
|
46
46
|
SchemaObject: () => SchemaObject,
|
|
47
47
|
SchemaRecord: () => SchemaRecord,
|
|
48
48
|
SchemaString: () => SchemaString,
|
|
49
|
+
SchemaTuple: () => SchemaTuple,
|
|
49
50
|
SchemaUndefined: () => SchemaUndefined,
|
|
50
51
|
SchemaUnion: () => SchemaUnion,
|
|
51
52
|
SchemaVoid: () => SchemaVoid,
|
|
52
53
|
default: () => Schema,
|
|
54
|
+
getSchemaItemAtPath: () => getSchemaItemAtPath,
|
|
53
55
|
isNull: () => isNull,
|
|
54
56
|
parsable: () => parsable,
|
|
55
57
|
parseForm: () => parseForm,
|
|
@@ -60,12 +62,104 @@ __export(Schema_exports, {
|
|
|
60
62
|
module.exports = __toCommonJS(Schema_exports);
|
|
61
63
|
|
|
62
64
|
// src/helpers.ts
|
|
63
|
-
var
|
|
65
|
+
var import_object_util = require("@dzeio/object-util");
|
|
66
|
+
function isNull(value) {
|
|
67
|
+
return value === void 0 || value === null;
|
|
68
|
+
}
|
|
69
|
+
function parseQuery(schema, query, opts) {
|
|
70
|
+
const record = {};
|
|
71
|
+
for (const [key, value] of query) {
|
|
72
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
73
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
74
|
+
const allValues = query.getAll(key);
|
|
75
|
+
const finalValue = (schemaItem == null ? void 0 : schemaItem.parse(allValues).valid) ? allValues : value;
|
|
76
|
+
(0, import_object_util.objectSet)(record, keys, finalValue);
|
|
77
|
+
}
|
|
78
|
+
return schema.parse(record, opts);
|
|
79
|
+
}
|
|
80
|
+
function parseFormData(schema, data, opts) {
|
|
81
|
+
const record = {};
|
|
82
|
+
for (const [key, value] of data) {
|
|
83
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
84
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
85
|
+
const allValues = data.getAll(key);
|
|
86
|
+
const finalValue = (schemaItem == null ? void 0 : schemaItem.parse(allValues).valid) ? allValues : value;
|
|
87
|
+
(0, import_object_util.objectSet)(record, keys, finalValue);
|
|
88
|
+
}
|
|
89
|
+
const handleBoolean = (value, keys) => {
|
|
90
|
+
var _a;
|
|
91
|
+
if (value instanceof SchemaNullable) {
|
|
92
|
+
handleBoolean(value.unwrap(), keys);
|
|
93
|
+
}
|
|
94
|
+
if (value instanceof SchemaArray) {
|
|
95
|
+
const elements = (0, import_object_util.objectGet)(record, keys);
|
|
96
|
+
for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
|
|
97
|
+
handleBoolean(value.unwrap(), [...keys, it]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (value instanceof SchemaObject) {
|
|
101
|
+
handleSchemaForBoolean(value.model, keys);
|
|
102
|
+
}
|
|
103
|
+
if (value instanceof SchemaBoolean && typeof (0, import_object_util.objectGet)(record, keys) === "undefined") {
|
|
104
|
+
(0, import_object_util.objectSet)(record, keys, false);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const handleSchemaForBoolean = (model, keys = []) => {
|
|
108
|
+
(0, import_object_util.objectLoop)(model, (value, key) => {
|
|
109
|
+
handleBoolean(value, [...keys, key]);
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
if (schema instanceof SchemaObject) {
|
|
113
|
+
handleSchemaForBoolean(schema.model);
|
|
114
|
+
}
|
|
115
|
+
return schema.parse(record, opts);
|
|
116
|
+
}
|
|
117
|
+
function parseForm(model, form, opts) {
|
|
118
|
+
return parseFormData(model, new FormData(form), opts);
|
|
119
|
+
}
|
|
120
|
+
function getSchemaItemAtPath(schema, path) {
|
|
121
|
+
while (schema instanceof SchemaNullable || schema instanceof SchemaDefault) {
|
|
122
|
+
schema = schema.unwrap();
|
|
123
|
+
}
|
|
124
|
+
if (path.length === 0 || !schema) {
|
|
125
|
+
return schema;
|
|
126
|
+
}
|
|
127
|
+
if (schema instanceof SchemaUnion || schema instanceof SchemaIntersection) {
|
|
128
|
+
for (const child of schema.unwrap()) {
|
|
129
|
+
try {
|
|
130
|
+
const result = getSchemaItemAtPath(child, path);
|
|
131
|
+
if (!result) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
} catch {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
const key = path[0];
|
|
142
|
+
const next = path.slice(1);
|
|
143
|
+
if (schema instanceof SchemaArray && typeof key === "number") {
|
|
144
|
+
return getSchemaItemAtPath(schema.unwrap(), next);
|
|
145
|
+
}
|
|
146
|
+
if (schema instanceof SchemaTuple && typeof key === "number") {
|
|
147
|
+
return getSchemaItemAtPath(schema.unwrap()[key], next);
|
|
148
|
+
}
|
|
149
|
+
if (schema instanceof SchemaObject && typeof key === "string") {
|
|
150
|
+
return getSchemaItemAtPath(schema.model[key], next);
|
|
151
|
+
}
|
|
152
|
+
if (schema instanceof SchemaRecord && schema.isKeyOfType(key)) {
|
|
153
|
+
return getSchemaItemAtPath(schema.unwrap(), next);
|
|
154
|
+
}
|
|
155
|
+
return void 0;
|
|
156
|
+
}
|
|
64
157
|
|
|
65
158
|
// src/SchemaItem.ts
|
|
66
|
-
var
|
|
159
|
+
var import_object_util2 = require("@dzeio/object-util");
|
|
67
160
|
var _SchemaItem = class _SchemaItem {
|
|
68
161
|
constructor(items) {
|
|
162
|
+
this.id = this.constructor.id;
|
|
69
163
|
// standard Schema V1 spec
|
|
70
164
|
this["~standard"] = {
|
|
71
165
|
vendor: "aptatio",
|
|
@@ -102,7 +196,6 @@ var _SchemaItem = class _SchemaItem {
|
|
|
102
196
|
*
|
|
103
197
|
* note: The type of the final Pre-Process MUST be valid
|
|
104
198
|
*/
|
|
105
|
-
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
106
199
|
this.preProcess = [];
|
|
107
200
|
/**
|
|
108
201
|
* post process the variable after it being validated
|
|
@@ -119,6 +212,10 @@ var _SchemaItem = class _SchemaItem {
|
|
|
119
212
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
120
213
|
}
|
|
121
214
|
}
|
|
215
|
+
setInvalidError(err) {
|
|
216
|
+
this.invalidError = err;
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
122
219
|
parseJSON() {
|
|
123
220
|
this.addPreProcess((input) => {
|
|
124
221
|
try {
|
|
@@ -140,10 +237,6 @@ var _SchemaItem = class _SchemaItem {
|
|
|
140
237
|
attr(...attributes) {
|
|
141
238
|
return this.attrs(...attributes);
|
|
142
239
|
}
|
|
143
|
-
setInvalidError(err) {
|
|
144
|
-
this.invalidError = err;
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
240
|
clone() {
|
|
148
241
|
return Schema.fromJSON(this.toJSON());
|
|
149
242
|
}
|
|
@@ -216,24 +309,13 @@ var _SchemaItem = class _SchemaItem {
|
|
|
216
309
|
c: (_a = this.items) == null ? void 0 : _a.map((it) => this.deepSerializeItem(it)),
|
|
217
310
|
f: this.savedCalls.map((it) => it.args ? { n: it.name, a: Array.from(it.args) } : { n: it.name })
|
|
218
311
|
};
|
|
219
|
-
(0,
|
|
312
|
+
(0, import_object_util2.objectClean)(res, { deep: false });
|
|
220
313
|
return res;
|
|
221
314
|
}
|
|
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
|
-
}
|
|
232
315
|
addValidation(fn, error) {
|
|
233
316
|
this.validations.push(typeof fn === "function" ? { fn, error } : fn);
|
|
234
317
|
return this;
|
|
235
318
|
}
|
|
236
|
-
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
237
319
|
addPreProcess(fn) {
|
|
238
320
|
this.preProcess.push(fn);
|
|
239
321
|
return this;
|
|
@@ -248,8 +330,8 @@ var _SchemaItem = class _SchemaItem {
|
|
|
248
330
|
}
|
|
249
331
|
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
250
332
|
updateSubItemInput(path, input, value) {
|
|
251
|
-
if (path && (0,
|
|
252
|
-
(0,
|
|
333
|
+
if (path && (0, import_object_util2.isObject)(input)) {
|
|
334
|
+
(0, import_object_util2.objectSet)(input, path.split("."), value);
|
|
253
335
|
return input;
|
|
254
336
|
} else {
|
|
255
337
|
return value;
|
|
@@ -273,220 +355,27 @@ var _SchemaItem = class _SchemaItem {
|
|
|
273
355
|
}
|
|
274
356
|
return result;
|
|
275
357
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
__decorateClass([
|
|
284
|
-
parsable()
|
|
285
|
-
], _SchemaItem.prototype, "setInvalidError", 1);
|
|
286
|
-
var SchemaItem = _SchemaItem;
|
|
287
|
-
|
|
288
|
-
// src/items/array.ts
|
|
289
|
-
var SchemaArray = class extends SchemaItem {
|
|
290
|
-
constructor(values) {
|
|
291
|
-
super([values]);
|
|
292
|
-
this.values = values;
|
|
293
|
-
}
|
|
294
|
-
unique() {
|
|
295
|
-
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
296
|
-
return this;
|
|
297
|
-
}
|
|
298
|
-
clean() {
|
|
299
|
-
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
300
|
-
return this;
|
|
301
|
-
}
|
|
302
|
-
split(separator = ",") {
|
|
303
|
-
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
304
|
-
return this;
|
|
305
|
-
}
|
|
306
|
-
unwrap() {
|
|
307
|
-
return this.values;
|
|
308
|
-
}
|
|
309
|
-
isOfType(input) {
|
|
310
|
-
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
311
|
-
}
|
|
312
|
-
getSubInputs(input) {
|
|
313
|
-
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
SchemaArray.id = "array";
|
|
317
|
-
__decorateClass([
|
|
318
|
-
parsable()
|
|
319
|
-
], SchemaArray.prototype, "unique", 1);
|
|
320
|
-
__decorateClass([
|
|
321
|
-
parsable()
|
|
322
|
-
], SchemaArray.prototype, "clean", 1);
|
|
323
|
-
__decorateClass([
|
|
324
|
-
parsable()
|
|
325
|
-
], SchemaArray.prototype, "split", 1);
|
|
326
|
-
|
|
327
|
-
// src/items/boolean.ts
|
|
328
|
-
var SchemaBoolean = class extends SchemaItem {
|
|
329
|
-
parseString(trueValue = "true", falseValue = "false") {
|
|
330
|
-
this.addPreProcess((input) => {
|
|
331
|
-
if (typeof input !== "string") {
|
|
332
|
-
return input;
|
|
333
|
-
}
|
|
334
|
-
if (input === trueValue) {
|
|
335
|
-
return true;
|
|
336
|
-
}
|
|
337
|
-
if (input === falseValue) {
|
|
338
|
-
return false;
|
|
339
|
-
}
|
|
340
|
-
return input;
|
|
341
|
-
});
|
|
342
|
-
return this;
|
|
343
|
-
}
|
|
344
|
-
isOfType(input) {
|
|
345
|
-
return typeof input === "boolean";
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
SchemaBoolean.id = "boolean";
|
|
349
|
-
__decorateClass([
|
|
350
|
-
parsable()
|
|
351
|
-
], SchemaBoolean.prototype, "parseString", 1);
|
|
352
|
-
|
|
353
|
-
// src/items/nullable.ts
|
|
354
|
-
var SchemaNullable = class extends SchemaItem {
|
|
355
|
-
constructor(child) {
|
|
356
|
-
super([child]);
|
|
357
|
-
this.child = child;
|
|
358
|
-
}
|
|
359
|
-
falsyAsNull() {
|
|
360
|
-
this.addPreProcess((it) => !it ? void 0 : it);
|
|
361
|
-
return this;
|
|
362
|
-
}
|
|
363
|
-
emptyAsNull() {
|
|
364
|
-
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
365
|
-
return this;
|
|
366
|
-
}
|
|
367
|
-
emptyFileAsNull() {
|
|
368
|
-
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
369
|
-
return this;
|
|
370
|
-
}
|
|
371
|
-
unwrap() {
|
|
372
|
-
return this.child;
|
|
373
|
-
}
|
|
374
|
-
isOfType(input) {
|
|
375
|
-
return typeof input === "undefined" || this.child.isOfType(input);
|
|
376
|
-
}
|
|
377
|
-
getSubInputs(input) {
|
|
378
|
-
return [{ value: input, item: this.child, path: void 0 }];
|
|
379
|
-
}
|
|
380
|
-
parseSubItems(input, options) {
|
|
381
|
-
if (isNull(input)) {
|
|
382
|
-
return { input: void 0 };
|
|
358
|
+
deepSerializeItem(item) {
|
|
359
|
+
if (item instanceof _SchemaItem) {
|
|
360
|
+
return item.toJSON();
|
|
361
|
+
} else if (Array.isArray(item)) {
|
|
362
|
+
return item.map((it) => this.deepSerializeItem(it));
|
|
363
|
+
} else if ((0, import_object_util2.isObject)(item)) {
|
|
364
|
+
return (0, import_object_util2.objectRemap)(item, (value, key) => ({ value: this.deepSerializeItem(value), key }));
|
|
383
365
|
}
|
|
384
|
-
return
|
|
366
|
+
return item;
|
|
385
367
|
}
|
|
386
368
|
};
|
|
387
|
-
SchemaNullable.id = "nullable";
|
|
388
369
|
__decorateClass([
|
|
389
370
|
parsable()
|
|
390
|
-
],
|
|
371
|
+
], _SchemaItem.prototype, "setInvalidError", 1);
|
|
391
372
|
__decorateClass([
|
|
392
373
|
parsable()
|
|
393
|
-
],
|
|
374
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
394
375
|
__decorateClass([
|
|
395
376
|
parsable()
|
|
396
|
-
],
|
|
397
|
-
|
|
398
|
-
// src/items/object.ts
|
|
399
|
-
var import_object_util2 = require("@dzeio/object-util");
|
|
400
|
-
var SchemaObject = class extends SchemaItem {
|
|
401
|
-
constructor(model) {
|
|
402
|
-
super([model]);
|
|
403
|
-
this.model = model;
|
|
404
|
-
}
|
|
405
|
-
isOfType(input) {
|
|
406
|
-
return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item, key) => !item.isOfType(input[key]));
|
|
407
|
-
}
|
|
408
|
-
getSubInputs(input) {
|
|
409
|
-
return (0, import_object_util2.isObject)(input) ? (0, import_object_util2.objectMap)(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
410
|
-
}
|
|
411
|
-
};
|
|
412
|
-
SchemaObject.id = "object";
|
|
413
|
-
|
|
414
|
-
// src/helpers.ts
|
|
415
|
-
function isNull(value) {
|
|
416
|
-
return typeof value === "undefined" || value === null;
|
|
417
|
-
}
|
|
418
|
-
function parseQuery(schema, query, opts) {
|
|
419
|
-
const record = {};
|
|
420
|
-
for (const [key, value] of query) {
|
|
421
|
-
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
422
|
-
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
423
|
-
const allValues = query.getAll(key);
|
|
424
|
-
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
425
|
-
(0, import_object_util3.objectSet)(record, keys, finalValue);
|
|
426
|
-
}
|
|
427
|
-
return schema.parse(record, opts);
|
|
428
|
-
}
|
|
429
|
-
function parseFormData(schema, data, opts) {
|
|
430
|
-
const record = {};
|
|
431
|
-
for (const [key, value] of data) {
|
|
432
|
-
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
433
|
-
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
434
|
-
const allValues = data.getAll(key);
|
|
435
|
-
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
436
|
-
(0, import_object_util3.objectSet)(record, keys, finalValue);
|
|
437
|
-
}
|
|
438
|
-
const handleBoolean = (value, keys) => {
|
|
439
|
-
var _a;
|
|
440
|
-
if (value instanceof SchemaNullable) {
|
|
441
|
-
handleBoolean(value.unwrap(), keys);
|
|
442
|
-
}
|
|
443
|
-
if (value instanceof SchemaArray) {
|
|
444
|
-
const elements = (0, import_object_util3.objectGet)(record, keys);
|
|
445
|
-
for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
|
|
446
|
-
handleBoolean(value.unwrap(), [...keys, it]);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
if (value instanceof SchemaObject) {
|
|
450
|
-
handleSchemaForBoolean(value.model, keys);
|
|
451
|
-
}
|
|
452
|
-
if (value instanceof SchemaBoolean && typeof (0, import_object_util3.objectGet)(record, keys) === "undefined") {
|
|
453
|
-
(0, import_object_util3.objectSet)(record, keys, false);
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
const handleSchemaForBoolean = (model, keys = []) => {
|
|
457
|
-
(0, import_object_util3.objectLoop)(model, (value, key) => {
|
|
458
|
-
handleBoolean(value, [...keys, key]);
|
|
459
|
-
});
|
|
460
|
-
};
|
|
461
|
-
if (schema instanceof SchemaObject) {
|
|
462
|
-
handleSchemaForBoolean(schema.model);
|
|
463
|
-
}
|
|
464
|
-
return schema.parse(record, opts);
|
|
465
|
-
}
|
|
466
|
-
function parseForm(model, form, opts) {
|
|
467
|
-
return parseFormData(model, new FormData(form), opts);
|
|
468
|
-
}
|
|
469
|
-
function getSchemaItemAtPath(schema, path) {
|
|
470
|
-
let current = schema;
|
|
471
|
-
const internalPath = (0, import_object_util3.objectClone)(path);
|
|
472
|
-
for (const key of path) {
|
|
473
|
-
if (!current) {
|
|
474
|
-
return void 0;
|
|
475
|
-
}
|
|
476
|
-
internalPath.shift();
|
|
477
|
-
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
478
|
-
current = current.unwrap();
|
|
479
|
-
}
|
|
480
|
-
if (current instanceof SchemaObject) {
|
|
481
|
-
current = current.model[key];
|
|
482
|
-
current = getSchemaItemAtPath(current, internalPath);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
486
|
-
current = current.unwrap();
|
|
487
|
-
}
|
|
488
|
-
return current;
|
|
489
|
-
}
|
|
377
|
+
], _SchemaItem.prototype, "in", 1);
|
|
378
|
+
var SchemaItem = _SchemaItem;
|
|
490
379
|
|
|
491
380
|
// src/items/any.ts
|
|
492
381
|
var SchemaAny = class extends SchemaItem {
|
|
@@ -498,6 +387,12 @@ SchemaAny.id = "any";
|
|
|
498
387
|
|
|
499
388
|
// src/items/date.ts
|
|
500
389
|
var SchemaDate = class extends SchemaItem {
|
|
390
|
+
// overide the basic constructor to check that the date has a valid format
|
|
391
|
+
// purpose : reject incorrect date like '2020-30-45'
|
|
392
|
+
constructor() {
|
|
393
|
+
super();
|
|
394
|
+
this.addValidation((input) => !Number.isNaN(input.getDate()));
|
|
395
|
+
}
|
|
501
396
|
parseString(format = "default") {
|
|
502
397
|
this.addPreProcess((input) => {
|
|
503
398
|
if (typeof input !== "string") {
|
|
@@ -527,7 +422,7 @@ var SchemaDate = class extends SchemaItem {
|
|
|
527
422
|
break;
|
|
528
423
|
}
|
|
529
424
|
}
|
|
530
|
-
if (!date || isNaN(date.getDate())) {
|
|
425
|
+
if (!date || Number.isNaN(date.getDate())) {
|
|
531
426
|
return input;
|
|
532
427
|
}
|
|
533
428
|
return date;
|
|
@@ -544,21 +439,27 @@ __decorateClass([
|
|
|
544
439
|
], SchemaDate.prototype, "parseString", 1);
|
|
545
440
|
|
|
546
441
|
// src/items/record.ts
|
|
547
|
-
var
|
|
442
|
+
var import_object_util3 = require("@dzeio/object-util");
|
|
548
443
|
var SchemaRecord = class extends SchemaItem {
|
|
549
444
|
constructor(keys, values) {
|
|
550
445
|
super([keys, values]);
|
|
551
446
|
this.keys = keys;
|
|
552
447
|
this.values = values;
|
|
553
448
|
}
|
|
449
|
+
unwrap() {
|
|
450
|
+
return this.values;
|
|
451
|
+
}
|
|
452
|
+
isKeyOfType(key) {
|
|
453
|
+
return this.keys.parse(key).valid;
|
|
454
|
+
}
|
|
554
455
|
isOfType(input) {
|
|
555
|
-
return (0,
|
|
456
|
+
return (0, import_object_util3.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
556
457
|
}
|
|
557
458
|
parseSubItems(input, options) {
|
|
558
459
|
const result = { input, errors: [] };
|
|
559
|
-
if ((0,
|
|
460
|
+
if ((0, import_object_util3.isObject)(input)) {
|
|
560
461
|
const clone = {};
|
|
561
|
-
(0,
|
|
462
|
+
(0, import_object_util3.objectLoop)(input, (value, key) => {
|
|
562
463
|
var _a, _b;
|
|
563
464
|
const res1 = this.keys.parse(key);
|
|
564
465
|
const res2 = this.values.parse(value);
|
|
@@ -579,6 +480,71 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
579
480
|
};
|
|
580
481
|
SchemaRecord.id = "record";
|
|
581
482
|
|
|
483
|
+
// src/items/array.ts
|
|
484
|
+
var SchemaArray = class extends SchemaItem {
|
|
485
|
+
constructor(values) {
|
|
486
|
+
super([values]);
|
|
487
|
+
this.values = values;
|
|
488
|
+
}
|
|
489
|
+
unique() {
|
|
490
|
+
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
491
|
+
return this;
|
|
492
|
+
}
|
|
493
|
+
clean() {
|
|
494
|
+
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
495
|
+
return this;
|
|
496
|
+
}
|
|
497
|
+
split(separator = ",") {
|
|
498
|
+
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
499
|
+
return this;
|
|
500
|
+
}
|
|
501
|
+
unwrap() {
|
|
502
|
+
return this.values;
|
|
503
|
+
}
|
|
504
|
+
isOfType(input) {
|
|
505
|
+
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
506
|
+
}
|
|
507
|
+
getSubInputs(input) {
|
|
508
|
+
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
SchemaArray.id = "array";
|
|
512
|
+
__decorateClass([
|
|
513
|
+
parsable()
|
|
514
|
+
], SchemaArray.prototype, "unique", 1);
|
|
515
|
+
__decorateClass([
|
|
516
|
+
parsable()
|
|
517
|
+
], SchemaArray.prototype, "clean", 1);
|
|
518
|
+
__decorateClass([
|
|
519
|
+
parsable()
|
|
520
|
+
], SchemaArray.prototype, "split", 1);
|
|
521
|
+
|
|
522
|
+
// src/items/boolean.ts
|
|
523
|
+
var SchemaBoolean = class extends SchemaItem {
|
|
524
|
+
parseString(trueValue = "true", falseValue = "false") {
|
|
525
|
+
this.addPreProcess((input) => {
|
|
526
|
+
if (typeof input !== "string") {
|
|
527
|
+
return input;
|
|
528
|
+
}
|
|
529
|
+
if (input === trueValue) {
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
if (input === falseValue) {
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
return input;
|
|
536
|
+
});
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
isOfType(input) {
|
|
540
|
+
return typeof input === "boolean";
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
SchemaBoolean.id = "boolean";
|
|
544
|
+
__decorateClass([
|
|
545
|
+
parsable()
|
|
546
|
+
], SchemaBoolean.prototype, "parseString", 1);
|
|
547
|
+
|
|
582
548
|
// src/items/enum.ts
|
|
583
549
|
var SchemaEnum = class extends SchemaItem {
|
|
584
550
|
constructor(templateEnum) {
|
|
@@ -613,6 +579,51 @@ var SchemaLiteral = class extends SchemaItem {
|
|
|
613
579
|
};
|
|
614
580
|
SchemaLiteral.id = "literal";
|
|
615
581
|
|
|
582
|
+
// src/items/nullable.ts
|
|
583
|
+
var SchemaNullable = class extends SchemaItem {
|
|
584
|
+
constructor(child) {
|
|
585
|
+
super([child]);
|
|
586
|
+
this.child = child;
|
|
587
|
+
}
|
|
588
|
+
falsyAsNull() {
|
|
589
|
+
this.addPreProcess((it) => !it ? void 0 : it);
|
|
590
|
+
return this;
|
|
591
|
+
}
|
|
592
|
+
emptyAsNull() {
|
|
593
|
+
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
594
|
+
return this;
|
|
595
|
+
}
|
|
596
|
+
emptyFileAsNull() {
|
|
597
|
+
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
600
|
+
unwrap() {
|
|
601
|
+
return this.child;
|
|
602
|
+
}
|
|
603
|
+
isOfType(input) {
|
|
604
|
+
return input === void 0 || this.child.isOfType(input);
|
|
605
|
+
}
|
|
606
|
+
getSubInputs(input) {
|
|
607
|
+
return [{ value: input, item: this.child, path: void 0 }];
|
|
608
|
+
}
|
|
609
|
+
parseSubItems(input, options) {
|
|
610
|
+
if (isNull(input)) {
|
|
611
|
+
return { input: void 0 };
|
|
612
|
+
}
|
|
613
|
+
return super.parseSubItems(input, options);
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
SchemaNullable.id = "nullable";
|
|
617
|
+
__decorateClass([
|
|
618
|
+
parsable()
|
|
619
|
+
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
620
|
+
__decorateClass([
|
|
621
|
+
parsable()
|
|
622
|
+
], SchemaNullable.prototype, "emptyAsNull", 1);
|
|
623
|
+
__decorateClass([
|
|
624
|
+
parsable()
|
|
625
|
+
], SchemaNullable.prototype, "emptyFileAsNull", 1);
|
|
626
|
+
|
|
616
627
|
// src/items/number.ts
|
|
617
628
|
var SchemaNumber = class extends SchemaItem {
|
|
618
629
|
lte(value, message) {
|
|
@@ -682,6 +693,22 @@ __decorateClass([
|
|
|
682
693
|
parsable()
|
|
683
694
|
], SchemaNumber.prototype, "parseString", 1);
|
|
684
695
|
|
|
696
|
+
// src/items/object.ts
|
|
697
|
+
var import_object_util4 = require("@dzeio/object-util");
|
|
698
|
+
var SchemaObject = class extends SchemaItem {
|
|
699
|
+
constructor(model) {
|
|
700
|
+
super([model]);
|
|
701
|
+
this.model = model;
|
|
702
|
+
}
|
|
703
|
+
isOfType(input) {
|
|
704
|
+
return (0, import_object_util4.isObject)(input) && !(0, import_object_util4.objectFind)(this.model, (item, key) => !item.isOfType(input[key]));
|
|
705
|
+
}
|
|
706
|
+
getSubInputs(input) {
|
|
707
|
+
return (0, import_object_util4.isObject)(input) ? (0, import_object_util4.objectMap)(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
SchemaObject.id = "object";
|
|
711
|
+
|
|
685
712
|
// src/items/string.ts
|
|
686
713
|
var SchemaString = class extends SchemaItem {
|
|
687
714
|
min(value, message) {
|
|
@@ -707,6 +734,15 @@ var SchemaString = class extends SchemaItem {
|
|
|
707
734
|
});
|
|
708
735
|
return this;
|
|
709
736
|
}
|
|
737
|
+
trim() {
|
|
738
|
+
this.addPreProcess((input) => {
|
|
739
|
+
if (typeof input !== "string") {
|
|
740
|
+
return input;
|
|
741
|
+
}
|
|
742
|
+
return input.trim();
|
|
743
|
+
});
|
|
744
|
+
return this;
|
|
745
|
+
}
|
|
710
746
|
notEmpty(message) {
|
|
711
747
|
this.addValidation({
|
|
712
748
|
fn(input) {
|
|
@@ -754,6 +790,9 @@ __decorateClass([
|
|
|
754
790
|
__decorateClass([
|
|
755
791
|
parsable()
|
|
756
792
|
], SchemaString.prototype, "max", 1);
|
|
793
|
+
__decorateClass([
|
|
794
|
+
parsable()
|
|
795
|
+
], SchemaString.prototype, "trim", 1);
|
|
757
796
|
__decorateClass([
|
|
758
797
|
parsable()
|
|
759
798
|
], SchemaString.prototype, "notEmpty", 1);
|
|
@@ -770,6 +809,9 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
770
809
|
super(schemas);
|
|
771
810
|
this.schemas = schemas;
|
|
772
811
|
}
|
|
812
|
+
unwrap() {
|
|
813
|
+
return this.schemas;
|
|
814
|
+
}
|
|
773
815
|
isOfType(input) {
|
|
774
816
|
return this.schemas.some((it) => it.isOfType(input));
|
|
775
817
|
}
|
|
@@ -795,6 +837,9 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
795
837
|
super(schemas);
|
|
796
838
|
this.schemas = schemas;
|
|
797
839
|
}
|
|
840
|
+
unwrap() {
|
|
841
|
+
return this.schemas;
|
|
842
|
+
}
|
|
798
843
|
isOfType(input) {
|
|
799
844
|
return this.schemas.every((it) => it.isOfType(input));
|
|
800
845
|
}
|
|
@@ -869,6 +914,9 @@ var SchemaTuple = class extends SchemaItem {
|
|
|
869
914
|
super(children);
|
|
870
915
|
this.children = children;
|
|
871
916
|
}
|
|
917
|
+
unwrap() {
|
|
918
|
+
return this.children;
|
|
919
|
+
}
|
|
872
920
|
isOfType(input) {
|
|
873
921
|
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
874
922
|
return false;
|
|
@@ -889,7 +937,7 @@ SchemaTuple.id = "tuple";
|
|
|
889
937
|
// src/items/nullish.ts
|
|
890
938
|
var SchemaUndefined = class extends SchemaItem {
|
|
891
939
|
isOfType(input) {
|
|
892
|
-
return
|
|
940
|
+
return input === void 0;
|
|
893
941
|
}
|
|
894
942
|
};
|
|
895
943
|
SchemaUndefined.id = "undefined";
|
|
@@ -901,7 +949,7 @@ var SchemaNull = class extends SchemaItem {
|
|
|
901
949
|
SchemaNull.id = "null";
|
|
902
950
|
var SchemaVoid = class extends SchemaItem {
|
|
903
951
|
isOfType(input) {
|
|
904
|
-
return
|
|
952
|
+
return input === void 0;
|
|
905
953
|
}
|
|
906
954
|
};
|
|
907
955
|
SchemaVoid.id = "void";
|
|
@@ -1117,9 +1165,11 @@ var s = Schema;
|
|
|
1117
1165
|
SchemaObject,
|
|
1118
1166
|
SchemaRecord,
|
|
1119
1167
|
SchemaString,
|
|
1168
|
+
SchemaTuple,
|
|
1120
1169
|
SchemaUndefined,
|
|
1121
1170
|
SchemaUnion,
|
|
1122
1171
|
SchemaVoid,
|
|
1172
|
+
getSchemaItemAtPath,
|
|
1123
1173
|
isNull,
|
|
1124
1174
|
parsable,
|
|
1125
1175
|
parseForm,
|