@dzeio/schema 0.8.0 → 0.10.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 +42 -34
- package/dist/Schema.d.ts +42 -34
- package/dist/Schema.js +277 -230
- package/dist/Schema.mjs +272 -227
- 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,217 +355,27 @@ var _SchemaItem = class _SchemaItem {
|
|
|
273
355
|
}
|
|
274
356
|
return result;
|
|
275
357
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
var SchemaItem = _SchemaItem;
|
|
284
|
-
|
|
285
|
-
// src/items/array.ts
|
|
286
|
-
var SchemaArray = class extends SchemaItem {
|
|
287
|
-
constructor(values) {
|
|
288
|
-
super([values]);
|
|
289
|
-
this.values = values;
|
|
290
|
-
}
|
|
291
|
-
unique() {
|
|
292
|
-
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
293
|
-
return this;
|
|
294
|
-
}
|
|
295
|
-
clean() {
|
|
296
|
-
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
297
|
-
return this;
|
|
298
|
-
}
|
|
299
|
-
split(separator = ",") {
|
|
300
|
-
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
301
|
-
return this;
|
|
302
|
-
}
|
|
303
|
-
unwrap() {
|
|
304
|
-
return this.values;
|
|
305
|
-
}
|
|
306
|
-
isOfType(input) {
|
|
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() })) : [];
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
|
-
SchemaArray.id = "array";
|
|
314
|
-
__decorateClass([
|
|
315
|
-
parsable()
|
|
316
|
-
], SchemaArray.prototype, "unique", 1);
|
|
317
|
-
__decorateClass([
|
|
318
|
-
parsable()
|
|
319
|
-
], SchemaArray.prototype, "clean", 1);
|
|
320
|
-
__decorateClass([
|
|
321
|
-
parsable()
|
|
322
|
-
], SchemaArray.prototype, "split", 1);
|
|
323
|
-
|
|
324
|
-
// src/items/boolean.ts
|
|
325
|
-
var SchemaBoolean = class extends SchemaItem {
|
|
326
|
-
parseString(trueValue = "true", falseValue = "false") {
|
|
327
|
-
this.addPreProcess((input) => {
|
|
328
|
-
if (typeof input !== "string") {
|
|
329
|
-
return input;
|
|
330
|
-
}
|
|
331
|
-
if (input === trueValue) {
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
if (input === falseValue) {
|
|
335
|
-
return false;
|
|
336
|
-
}
|
|
337
|
-
return input;
|
|
338
|
-
});
|
|
339
|
-
return this;
|
|
340
|
-
}
|
|
341
|
-
isOfType(input) {
|
|
342
|
-
return typeof input === "boolean";
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
SchemaBoolean.id = "boolean";
|
|
346
|
-
__decorateClass([
|
|
347
|
-
parsable()
|
|
348
|
-
], SchemaBoolean.prototype, "parseString", 1);
|
|
349
|
-
|
|
350
|
-
// src/items/nullable.ts
|
|
351
|
-
var SchemaNullable = class extends SchemaItem {
|
|
352
|
-
constructor(child) {
|
|
353
|
-
super([child]);
|
|
354
|
-
this.child = child;
|
|
355
|
-
}
|
|
356
|
-
falsyAsNull() {
|
|
357
|
-
this.addPreProcess((it) => !it ? void 0 : it);
|
|
358
|
-
return this;
|
|
359
|
-
}
|
|
360
|
-
emptyAsNull() {
|
|
361
|
-
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
362
|
-
return this;
|
|
363
|
-
}
|
|
364
|
-
emptyFileAsNull() {
|
|
365
|
-
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
366
|
-
return this;
|
|
367
|
-
}
|
|
368
|
-
unwrap() {
|
|
369
|
-
return this.child;
|
|
370
|
-
}
|
|
371
|
-
isOfType(input) {
|
|
372
|
-
return typeof input === "undefined" || this.child.isOfType(input);
|
|
373
|
-
}
|
|
374
|
-
getSubInputs(input) {
|
|
375
|
-
return [{ value: input, item: this.child, path: void 0 }];
|
|
376
|
-
}
|
|
377
|
-
parseSubItems(input, options) {
|
|
378
|
-
if (isNull(input)) {
|
|
379
|
-
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 }));
|
|
380
365
|
}
|
|
381
|
-
return
|
|
366
|
+
return item;
|
|
382
367
|
}
|
|
383
368
|
};
|
|
384
|
-
SchemaNullable.id = "nullable";
|
|
385
369
|
__decorateClass([
|
|
386
370
|
parsable()
|
|
387
|
-
],
|
|
371
|
+
], _SchemaItem.prototype, "setInvalidError", 1);
|
|
388
372
|
__decorateClass([
|
|
389
373
|
parsable()
|
|
390
|
-
],
|
|
374
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
391
375
|
__decorateClass([
|
|
392
376
|
parsable()
|
|
393
|
-
],
|
|
394
|
-
|
|
395
|
-
// src/items/object.ts
|
|
396
|
-
var import_object_util2 = require("@dzeio/object-util");
|
|
397
|
-
var SchemaObject = class extends SchemaItem {
|
|
398
|
-
constructor(model) {
|
|
399
|
-
super([model]);
|
|
400
|
-
this.model = model;
|
|
401
|
-
}
|
|
402
|
-
isOfType(input) {
|
|
403
|
-
return (0, import_object_util2.isObject)(input) && !(0, import_object_util2.objectFind)(this.model, (item, key) => !item.isOfType(input[key]));
|
|
404
|
-
}
|
|
405
|
-
getSubInputs(input) {
|
|
406
|
-
return (0, import_object_util2.isObject)(input) ? (0, import_object_util2.objectMap)(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
SchemaObject.id = "object";
|
|
410
|
-
|
|
411
|
-
// src/helpers.ts
|
|
412
|
-
function isNull(value) {
|
|
413
|
-
return typeof value === "undefined" || value === null;
|
|
414
|
-
}
|
|
415
|
-
function parseQuery(schema, query, opts) {
|
|
416
|
-
const record = {};
|
|
417
|
-
for (const [key, value] of query) {
|
|
418
|
-
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
419
|
-
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
420
|
-
const allValues = query.getAll(key);
|
|
421
|
-
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
422
|
-
(0, import_object_util3.objectSet)(record, keys, finalValue);
|
|
423
|
-
}
|
|
424
|
-
return schema.parse(record, opts);
|
|
425
|
-
}
|
|
426
|
-
function parseFormData(schema, data, opts) {
|
|
427
|
-
const record = {};
|
|
428
|
-
for (const [key, value] of data) {
|
|
429
|
-
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
430
|
-
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
431
|
-
const allValues = data.getAll(key);
|
|
432
|
-
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
433
|
-
(0, import_object_util3.objectSet)(record, keys, finalValue);
|
|
434
|
-
}
|
|
435
|
-
const handleBoolean = (value, keys) => {
|
|
436
|
-
var _a;
|
|
437
|
-
if (value instanceof SchemaNullable) {
|
|
438
|
-
handleBoolean(value.unwrap(), keys);
|
|
439
|
-
}
|
|
440
|
-
if (value instanceof SchemaArray) {
|
|
441
|
-
const elements = (0, import_object_util3.objectGet)(record, keys);
|
|
442
|
-
for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
|
|
443
|
-
handleBoolean(value.unwrap(), [...keys, it]);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
if (value instanceof SchemaObject) {
|
|
447
|
-
handleSchemaForBoolean(value.model, keys);
|
|
448
|
-
}
|
|
449
|
-
if (value instanceof SchemaBoolean) {
|
|
450
|
-
(0, import_object_util3.objectSet)(record, keys, !!data.get(keys.join(".")));
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
const handleSchemaForBoolean = (model, keys = []) => {
|
|
454
|
-
(0, import_object_util3.objectLoop)(model, (value, key) => {
|
|
455
|
-
handleBoolean(value, [...keys, key]);
|
|
456
|
-
});
|
|
457
|
-
};
|
|
458
|
-
if (schema instanceof SchemaObject) {
|
|
459
|
-
handleSchemaForBoolean(schema.model);
|
|
460
|
-
}
|
|
461
|
-
return schema.parse(record, opts);
|
|
462
|
-
}
|
|
463
|
-
function parseForm(model, form, opts) {
|
|
464
|
-
return parseFormData(model, new FormData(form), opts);
|
|
465
|
-
}
|
|
466
|
-
function getSchemaItemAtPath(schema, path) {
|
|
467
|
-
let current = schema;
|
|
468
|
-
const internalPath = (0, import_object_util3.objectClone)(path);
|
|
469
|
-
for (const key of path) {
|
|
470
|
-
if (!current) {
|
|
471
|
-
return void 0;
|
|
472
|
-
}
|
|
473
|
-
internalPath.shift();
|
|
474
|
-
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
475
|
-
current = current.unwrap();
|
|
476
|
-
}
|
|
477
|
-
if (current instanceof SchemaObject) {
|
|
478
|
-
current = current.model[key];
|
|
479
|
-
current = getSchemaItemAtPath(current, internalPath);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
483
|
-
current = current.unwrap();
|
|
484
|
-
}
|
|
485
|
-
return current;
|
|
486
|
-
}
|
|
377
|
+
], _SchemaItem.prototype, "in", 1);
|
|
378
|
+
var SchemaItem = _SchemaItem;
|
|
487
379
|
|
|
488
380
|
// src/items/any.ts
|
|
489
381
|
var SchemaAny = class extends SchemaItem {
|
|
@@ -524,7 +416,7 @@ var SchemaDate = class extends SchemaItem {
|
|
|
524
416
|
break;
|
|
525
417
|
}
|
|
526
418
|
}
|
|
527
|
-
if (!date || isNaN(date.getDate())) {
|
|
419
|
+
if (!date || Number.isNaN(date.getDate())) {
|
|
528
420
|
return input;
|
|
529
421
|
}
|
|
530
422
|
return date;
|
|
@@ -541,21 +433,27 @@ __decorateClass([
|
|
|
541
433
|
], SchemaDate.prototype, "parseString", 1);
|
|
542
434
|
|
|
543
435
|
// src/items/record.ts
|
|
544
|
-
var
|
|
436
|
+
var import_object_util3 = require("@dzeio/object-util");
|
|
545
437
|
var SchemaRecord = class extends SchemaItem {
|
|
546
438
|
constructor(keys, values) {
|
|
547
439
|
super([keys, values]);
|
|
548
440
|
this.keys = keys;
|
|
549
441
|
this.values = values;
|
|
550
442
|
}
|
|
443
|
+
unwrap() {
|
|
444
|
+
return this.values;
|
|
445
|
+
}
|
|
446
|
+
isKeyOfType(key) {
|
|
447
|
+
return this.keys.parse(key).valid;
|
|
448
|
+
}
|
|
551
449
|
isOfType(input) {
|
|
552
|
-
return (0,
|
|
450
|
+
return (0, import_object_util3.isObject)(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
553
451
|
}
|
|
554
452
|
parseSubItems(input, options) {
|
|
555
453
|
const result = { input, errors: [] };
|
|
556
|
-
if ((0,
|
|
454
|
+
if ((0, import_object_util3.isObject)(input)) {
|
|
557
455
|
const clone = {};
|
|
558
|
-
(0,
|
|
456
|
+
(0, import_object_util3.objectLoop)(input, (value, key) => {
|
|
559
457
|
var _a, _b;
|
|
560
458
|
const res1 = this.keys.parse(key);
|
|
561
459
|
const res2 = this.values.parse(value);
|
|
@@ -576,6 +474,71 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
576
474
|
};
|
|
577
475
|
SchemaRecord.id = "record";
|
|
578
476
|
|
|
477
|
+
// src/items/array.ts
|
|
478
|
+
var SchemaArray = class extends SchemaItem {
|
|
479
|
+
constructor(values) {
|
|
480
|
+
super([values]);
|
|
481
|
+
this.values = values;
|
|
482
|
+
}
|
|
483
|
+
unique() {
|
|
484
|
+
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
485
|
+
return this;
|
|
486
|
+
}
|
|
487
|
+
clean() {
|
|
488
|
+
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
489
|
+
return this;
|
|
490
|
+
}
|
|
491
|
+
split(separator = ",") {
|
|
492
|
+
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
493
|
+
return this;
|
|
494
|
+
}
|
|
495
|
+
unwrap() {
|
|
496
|
+
return this.values;
|
|
497
|
+
}
|
|
498
|
+
isOfType(input) {
|
|
499
|
+
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
500
|
+
}
|
|
501
|
+
getSubInputs(input) {
|
|
502
|
+
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
SchemaArray.id = "array";
|
|
506
|
+
__decorateClass([
|
|
507
|
+
parsable()
|
|
508
|
+
], SchemaArray.prototype, "unique", 1);
|
|
509
|
+
__decorateClass([
|
|
510
|
+
parsable()
|
|
511
|
+
], SchemaArray.prototype, "clean", 1);
|
|
512
|
+
__decorateClass([
|
|
513
|
+
parsable()
|
|
514
|
+
], SchemaArray.prototype, "split", 1);
|
|
515
|
+
|
|
516
|
+
// src/items/boolean.ts
|
|
517
|
+
var SchemaBoolean = class extends SchemaItem {
|
|
518
|
+
parseString(trueValue = "true", falseValue = "false") {
|
|
519
|
+
this.addPreProcess((input) => {
|
|
520
|
+
if (typeof input !== "string") {
|
|
521
|
+
return input;
|
|
522
|
+
}
|
|
523
|
+
if (input === trueValue) {
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
if (input === falseValue) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
return input;
|
|
530
|
+
});
|
|
531
|
+
return this;
|
|
532
|
+
}
|
|
533
|
+
isOfType(input) {
|
|
534
|
+
return typeof input === "boolean";
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
SchemaBoolean.id = "boolean";
|
|
538
|
+
__decorateClass([
|
|
539
|
+
parsable()
|
|
540
|
+
], SchemaBoolean.prototype, "parseString", 1);
|
|
541
|
+
|
|
579
542
|
// src/items/enum.ts
|
|
580
543
|
var SchemaEnum = class extends SchemaItem {
|
|
581
544
|
constructor(templateEnum) {
|
|
@@ -610,6 +573,51 @@ var SchemaLiteral = class extends SchemaItem {
|
|
|
610
573
|
};
|
|
611
574
|
SchemaLiteral.id = "literal";
|
|
612
575
|
|
|
576
|
+
// src/items/nullable.ts
|
|
577
|
+
var SchemaNullable = class extends SchemaItem {
|
|
578
|
+
constructor(child) {
|
|
579
|
+
super([child]);
|
|
580
|
+
this.child = child;
|
|
581
|
+
}
|
|
582
|
+
falsyAsNull() {
|
|
583
|
+
this.addPreProcess((it) => !it ? void 0 : it);
|
|
584
|
+
return this;
|
|
585
|
+
}
|
|
586
|
+
emptyAsNull() {
|
|
587
|
+
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
588
|
+
return this;
|
|
589
|
+
}
|
|
590
|
+
emptyFileAsNull() {
|
|
591
|
+
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
592
|
+
return this;
|
|
593
|
+
}
|
|
594
|
+
unwrap() {
|
|
595
|
+
return this.child;
|
|
596
|
+
}
|
|
597
|
+
isOfType(input) {
|
|
598
|
+
return input === void 0 || this.child.isOfType(input);
|
|
599
|
+
}
|
|
600
|
+
getSubInputs(input) {
|
|
601
|
+
return [{ value: input, item: this.child, path: void 0 }];
|
|
602
|
+
}
|
|
603
|
+
parseSubItems(input, options) {
|
|
604
|
+
if (isNull(input)) {
|
|
605
|
+
return { input: void 0 };
|
|
606
|
+
}
|
|
607
|
+
return super.parseSubItems(input, options);
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
SchemaNullable.id = "nullable";
|
|
611
|
+
__decorateClass([
|
|
612
|
+
parsable()
|
|
613
|
+
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
614
|
+
__decorateClass([
|
|
615
|
+
parsable()
|
|
616
|
+
], SchemaNullable.prototype, "emptyAsNull", 1);
|
|
617
|
+
__decorateClass([
|
|
618
|
+
parsable()
|
|
619
|
+
], SchemaNullable.prototype, "emptyFileAsNull", 1);
|
|
620
|
+
|
|
613
621
|
// src/items/number.ts
|
|
614
622
|
var SchemaNumber = class extends SchemaItem {
|
|
615
623
|
lte(value, message) {
|
|
@@ -679,6 +687,22 @@ __decorateClass([
|
|
|
679
687
|
parsable()
|
|
680
688
|
], SchemaNumber.prototype, "parseString", 1);
|
|
681
689
|
|
|
690
|
+
// src/items/object.ts
|
|
691
|
+
var import_object_util4 = require("@dzeio/object-util");
|
|
692
|
+
var SchemaObject = class extends SchemaItem {
|
|
693
|
+
constructor(model) {
|
|
694
|
+
super([model]);
|
|
695
|
+
this.model = model;
|
|
696
|
+
}
|
|
697
|
+
isOfType(input) {
|
|
698
|
+
return (0, import_object_util4.isObject)(input) && !(0, import_object_util4.objectFind)(this.model, (item, key) => !item.isOfType(input[key]));
|
|
699
|
+
}
|
|
700
|
+
getSubInputs(input) {
|
|
701
|
+
return (0, import_object_util4.isObject)(input) ? (0, import_object_util4.objectMap)(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
SchemaObject.id = "object";
|
|
705
|
+
|
|
682
706
|
// src/items/string.ts
|
|
683
707
|
var SchemaString = class extends SchemaItem {
|
|
684
708
|
min(value, message) {
|
|
@@ -704,6 +728,15 @@ var SchemaString = class extends SchemaItem {
|
|
|
704
728
|
});
|
|
705
729
|
return this;
|
|
706
730
|
}
|
|
731
|
+
trim() {
|
|
732
|
+
this.addPreProcess((input) => {
|
|
733
|
+
if (typeof input !== "string") {
|
|
734
|
+
return input;
|
|
735
|
+
}
|
|
736
|
+
return input.trim();
|
|
737
|
+
});
|
|
738
|
+
return this;
|
|
739
|
+
}
|
|
707
740
|
notEmpty(message) {
|
|
708
741
|
this.addValidation({
|
|
709
742
|
fn(input) {
|
|
@@ -751,6 +784,9 @@ __decorateClass([
|
|
|
751
784
|
__decorateClass([
|
|
752
785
|
parsable()
|
|
753
786
|
], SchemaString.prototype, "max", 1);
|
|
787
|
+
__decorateClass([
|
|
788
|
+
parsable()
|
|
789
|
+
], SchemaString.prototype, "trim", 1);
|
|
754
790
|
__decorateClass([
|
|
755
791
|
parsable()
|
|
756
792
|
], SchemaString.prototype, "notEmpty", 1);
|
|
@@ -767,6 +803,9 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
767
803
|
super(schemas);
|
|
768
804
|
this.schemas = schemas;
|
|
769
805
|
}
|
|
806
|
+
unwrap() {
|
|
807
|
+
return this.schemas;
|
|
808
|
+
}
|
|
770
809
|
isOfType(input) {
|
|
771
810
|
return this.schemas.some((it) => it.isOfType(input));
|
|
772
811
|
}
|
|
@@ -792,6 +831,9 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
792
831
|
super(schemas);
|
|
793
832
|
this.schemas = schemas;
|
|
794
833
|
}
|
|
834
|
+
unwrap() {
|
|
835
|
+
return this.schemas;
|
|
836
|
+
}
|
|
795
837
|
isOfType(input) {
|
|
796
838
|
return this.schemas.every((it) => it.isOfType(input));
|
|
797
839
|
}
|
|
@@ -866,6 +908,9 @@ var SchemaTuple = class extends SchemaItem {
|
|
|
866
908
|
super(children);
|
|
867
909
|
this.children = children;
|
|
868
910
|
}
|
|
911
|
+
unwrap() {
|
|
912
|
+
return this.children;
|
|
913
|
+
}
|
|
869
914
|
isOfType(input) {
|
|
870
915
|
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
871
916
|
return false;
|
|
@@ -886,7 +931,7 @@ SchemaTuple.id = "tuple";
|
|
|
886
931
|
// src/items/nullish.ts
|
|
887
932
|
var SchemaUndefined = class extends SchemaItem {
|
|
888
933
|
isOfType(input) {
|
|
889
|
-
return
|
|
934
|
+
return input === void 0;
|
|
890
935
|
}
|
|
891
936
|
};
|
|
892
937
|
SchemaUndefined.id = "undefined";
|
|
@@ -898,7 +943,7 @@ var SchemaNull = class extends SchemaItem {
|
|
|
898
943
|
SchemaNull.id = "null";
|
|
899
944
|
var SchemaVoid = class extends SchemaItem {
|
|
900
945
|
isOfType(input) {
|
|
901
|
-
return
|
|
946
|
+
return input === void 0;
|
|
902
947
|
}
|
|
903
948
|
};
|
|
904
949
|
SchemaVoid.id = "void";
|
|
@@ -1114,9 +1159,11 @@ var s = Schema;
|
|
|
1114
1159
|
SchemaObject,
|
|
1115
1160
|
SchemaRecord,
|
|
1116
1161
|
SchemaString,
|
|
1162
|
+
SchemaTuple,
|
|
1117
1163
|
SchemaUndefined,
|
|
1118
1164
|
SchemaUnion,
|
|
1119
1165
|
SchemaVoid,
|
|
1166
|
+
getSchemaItemAtPath,
|
|
1120
1167
|
isNull,
|
|
1121
1168
|
parsable,
|
|
1122
1169
|
parseForm,
|