@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.mjs
CHANGED
|
@@ -10,12 +10,104 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/helpers.ts
|
|
13
|
-
import {
|
|
13
|
+
import { objectGet, objectLoop, objectSet } from "@dzeio/object-util";
|
|
14
|
+
function isNull(value) {
|
|
15
|
+
return value === void 0 || value === null;
|
|
16
|
+
}
|
|
17
|
+
function parseQuery(schema, query, opts) {
|
|
18
|
+
const record = {};
|
|
19
|
+
for (const [key, value] of query) {
|
|
20
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
21
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
22
|
+
const allValues = query.getAll(key);
|
|
23
|
+
const finalValue = (schemaItem == null ? void 0 : schemaItem.parse(allValues).valid) ? allValues : value;
|
|
24
|
+
objectSet(record, keys, finalValue);
|
|
25
|
+
}
|
|
26
|
+
return schema.parse(record, opts);
|
|
27
|
+
}
|
|
28
|
+
function parseFormData(schema, data, opts) {
|
|
29
|
+
const record = {};
|
|
30
|
+
for (const [key, value] of data) {
|
|
31
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
32
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
33
|
+
const allValues = data.getAll(key);
|
|
34
|
+
const finalValue = (schemaItem == null ? void 0 : schemaItem.parse(allValues).valid) ? allValues : value;
|
|
35
|
+
objectSet(record, keys, finalValue);
|
|
36
|
+
}
|
|
37
|
+
const handleBoolean = (value, keys) => {
|
|
38
|
+
var _a;
|
|
39
|
+
if (value instanceof SchemaNullable) {
|
|
40
|
+
handleBoolean(value.unwrap(), keys);
|
|
41
|
+
}
|
|
42
|
+
if (value instanceof SchemaArray) {
|
|
43
|
+
const elements = objectGet(record, keys);
|
|
44
|
+
for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
|
|
45
|
+
handleBoolean(value.unwrap(), [...keys, it]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (value instanceof SchemaObject) {
|
|
49
|
+
handleSchemaForBoolean(value.model, keys);
|
|
50
|
+
}
|
|
51
|
+
if (value instanceof SchemaBoolean && typeof objectGet(record, keys) === "undefined") {
|
|
52
|
+
objectSet(record, keys, false);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const handleSchemaForBoolean = (model, keys = []) => {
|
|
56
|
+
objectLoop(model, (value, key) => {
|
|
57
|
+
handleBoolean(value, [...keys, key]);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
if (schema instanceof SchemaObject) {
|
|
61
|
+
handleSchemaForBoolean(schema.model);
|
|
62
|
+
}
|
|
63
|
+
return schema.parse(record, opts);
|
|
64
|
+
}
|
|
65
|
+
function parseForm(model, form, opts) {
|
|
66
|
+
return parseFormData(model, new FormData(form), opts);
|
|
67
|
+
}
|
|
68
|
+
function getSchemaItemAtPath(schema, path) {
|
|
69
|
+
while (schema instanceof SchemaNullable || schema instanceof SchemaDefault) {
|
|
70
|
+
schema = schema.unwrap();
|
|
71
|
+
}
|
|
72
|
+
if (path.length === 0 || !schema) {
|
|
73
|
+
return schema;
|
|
74
|
+
}
|
|
75
|
+
if (schema instanceof SchemaUnion || schema instanceof SchemaIntersection) {
|
|
76
|
+
for (const child of schema.unwrap()) {
|
|
77
|
+
try {
|
|
78
|
+
const result = getSchemaItemAtPath(child, path);
|
|
79
|
+
if (!result) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
} catch {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return void 0;
|
|
88
|
+
}
|
|
89
|
+
const key = path[0];
|
|
90
|
+
const next = path.slice(1);
|
|
91
|
+
if (schema instanceof SchemaArray && typeof key === "number") {
|
|
92
|
+
return getSchemaItemAtPath(schema.unwrap(), next);
|
|
93
|
+
}
|
|
94
|
+
if (schema instanceof SchemaTuple && typeof key === "number") {
|
|
95
|
+
return getSchemaItemAtPath(schema.unwrap()[key], next);
|
|
96
|
+
}
|
|
97
|
+
if (schema instanceof SchemaObject && typeof key === "string") {
|
|
98
|
+
return getSchemaItemAtPath(schema.model[key], next);
|
|
99
|
+
}
|
|
100
|
+
if (schema instanceof SchemaRecord && schema.isKeyOfType(key)) {
|
|
101
|
+
return getSchemaItemAtPath(schema.unwrap(), next);
|
|
102
|
+
}
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
14
105
|
|
|
15
106
|
// src/SchemaItem.ts
|
|
16
|
-
import { isObject, objectClean, objectRemap, objectSet } from "@dzeio/object-util";
|
|
107
|
+
import { isObject, objectClean, objectRemap, objectSet as objectSet2 } from "@dzeio/object-util";
|
|
17
108
|
var _SchemaItem = class _SchemaItem {
|
|
18
109
|
constructor(items) {
|
|
110
|
+
this.id = this.constructor.id;
|
|
19
111
|
// standard Schema V1 spec
|
|
20
112
|
this["~standard"] = {
|
|
21
113
|
vendor: "aptatio",
|
|
@@ -52,7 +144,6 @@ var _SchemaItem = class _SchemaItem {
|
|
|
52
144
|
*
|
|
53
145
|
* note: The type of the final Pre-Process MUST be valid
|
|
54
146
|
*/
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
56
147
|
this.preProcess = [];
|
|
57
148
|
/**
|
|
58
149
|
* post process the variable after it being validated
|
|
@@ -69,6 +160,10 @@ var _SchemaItem = class _SchemaItem {
|
|
|
69
160
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
70
161
|
}
|
|
71
162
|
}
|
|
163
|
+
setInvalidError(err) {
|
|
164
|
+
this.invalidError = err;
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
72
167
|
parseJSON() {
|
|
73
168
|
this.addPreProcess((input) => {
|
|
74
169
|
try {
|
|
@@ -90,10 +185,6 @@ var _SchemaItem = class _SchemaItem {
|
|
|
90
185
|
attr(...attributes) {
|
|
91
186
|
return this.attrs(...attributes);
|
|
92
187
|
}
|
|
93
|
-
setInvalidError(err) {
|
|
94
|
-
this.invalidError = err;
|
|
95
|
-
return this;
|
|
96
|
-
}
|
|
97
188
|
clone() {
|
|
98
189
|
return Schema.fromJSON(this.toJSON());
|
|
99
190
|
}
|
|
@@ -169,21 +260,10 @@ var _SchemaItem = class _SchemaItem {
|
|
|
169
260
|
objectClean(res, { deep: false });
|
|
170
261
|
return res;
|
|
171
262
|
}
|
|
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
|
-
}
|
|
182
263
|
addValidation(fn, error) {
|
|
183
264
|
this.validations.push(typeof fn === "function" ? { fn, error } : fn);
|
|
184
265
|
return this;
|
|
185
266
|
}
|
|
186
|
-
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
187
267
|
addPreProcess(fn) {
|
|
188
268
|
this.preProcess.push(fn);
|
|
189
269
|
return this;
|
|
@@ -199,7 +279,7 @@ var _SchemaItem = class _SchemaItem {
|
|
|
199
279
|
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
200
280
|
updateSubItemInput(path, input, value) {
|
|
201
281
|
if (path && isObject(input)) {
|
|
202
|
-
|
|
282
|
+
objectSet2(input, path.split("."), value);
|
|
203
283
|
return input;
|
|
204
284
|
} else {
|
|
205
285
|
return value;
|
|
@@ -223,220 +303,27 @@ var _SchemaItem = class _SchemaItem {
|
|
|
223
303
|
}
|
|
224
304
|
return result;
|
|
225
305
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
__decorateClass([
|
|
234
|
-
parsable()
|
|
235
|
-
], _SchemaItem.prototype, "setInvalidError", 1);
|
|
236
|
-
var SchemaItem = _SchemaItem;
|
|
237
|
-
|
|
238
|
-
// src/items/array.ts
|
|
239
|
-
var SchemaArray = class extends SchemaItem {
|
|
240
|
-
constructor(values) {
|
|
241
|
-
super([values]);
|
|
242
|
-
this.values = values;
|
|
243
|
-
}
|
|
244
|
-
unique() {
|
|
245
|
-
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
246
|
-
return this;
|
|
247
|
-
}
|
|
248
|
-
clean() {
|
|
249
|
-
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
250
|
-
return this;
|
|
251
|
-
}
|
|
252
|
-
split(separator = ",") {
|
|
253
|
-
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
254
|
-
return this;
|
|
255
|
-
}
|
|
256
|
-
unwrap() {
|
|
257
|
-
return this.values;
|
|
258
|
-
}
|
|
259
|
-
isOfType(input) {
|
|
260
|
-
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
261
|
-
}
|
|
262
|
-
getSubInputs(input) {
|
|
263
|
-
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
SchemaArray.id = "array";
|
|
267
|
-
__decorateClass([
|
|
268
|
-
parsable()
|
|
269
|
-
], SchemaArray.prototype, "unique", 1);
|
|
270
|
-
__decorateClass([
|
|
271
|
-
parsable()
|
|
272
|
-
], SchemaArray.prototype, "clean", 1);
|
|
273
|
-
__decorateClass([
|
|
274
|
-
parsable()
|
|
275
|
-
], SchemaArray.prototype, "split", 1);
|
|
276
|
-
|
|
277
|
-
// src/items/boolean.ts
|
|
278
|
-
var SchemaBoolean = class extends SchemaItem {
|
|
279
|
-
parseString(trueValue = "true", falseValue = "false") {
|
|
280
|
-
this.addPreProcess((input) => {
|
|
281
|
-
if (typeof input !== "string") {
|
|
282
|
-
return input;
|
|
283
|
-
}
|
|
284
|
-
if (input === trueValue) {
|
|
285
|
-
return true;
|
|
286
|
-
}
|
|
287
|
-
if (input === falseValue) {
|
|
288
|
-
return false;
|
|
289
|
-
}
|
|
290
|
-
return input;
|
|
291
|
-
});
|
|
292
|
-
return this;
|
|
293
|
-
}
|
|
294
|
-
isOfType(input) {
|
|
295
|
-
return typeof input === "boolean";
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
SchemaBoolean.id = "boolean";
|
|
299
|
-
__decorateClass([
|
|
300
|
-
parsable()
|
|
301
|
-
], SchemaBoolean.prototype, "parseString", 1);
|
|
302
|
-
|
|
303
|
-
// src/items/nullable.ts
|
|
304
|
-
var SchemaNullable = class extends SchemaItem {
|
|
305
|
-
constructor(child) {
|
|
306
|
-
super([child]);
|
|
307
|
-
this.child = child;
|
|
308
|
-
}
|
|
309
|
-
falsyAsNull() {
|
|
310
|
-
this.addPreProcess((it) => !it ? void 0 : it);
|
|
311
|
-
return this;
|
|
312
|
-
}
|
|
313
|
-
emptyAsNull() {
|
|
314
|
-
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
315
|
-
return this;
|
|
316
|
-
}
|
|
317
|
-
emptyFileAsNull() {
|
|
318
|
-
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
319
|
-
return this;
|
|
320
|
-
}
|
|
321
|
-
unwrap() {
|
|
322
|
-
return this.child;
|
|
323
|
-
}
|
|
324
|
-
isOfType(input) {
|
|
325
|
-
return typeof input === "undefined" || this.child.isOfType(input);
|
|
326
|
-
}
|
|
327
|
-
getSubInputs(input) {
|
|
328
|
-
return [{ value: input, item: this.child, path: void 0 }];
|
|
329
|
-
}
|
|
330
|
-
parseSubItems(input, options) {
|
|
331
|
-
if (isNull(input)) {
|
|
332
|
-
return { input: void 0 };
|
|
306
|
+
deepSerializeItem(item) {
|
|
307
|
+
if (item instanceof _SchemaItem) {
|
|
308
|
+
return item.toJSON();
|
|
309
|
+
} else if (Array.isArray(item)) {
|
|
310
|
+
return item.map((it) => this.deepSerializeItem(it));
|
|
311
|
+
} else if (isObject(item)) {
|
|
312
|
+
return objectRemap(item, (value, key) => ({ value: this.deepSerializeItem(value), key }));
|
|
333
313
|
}
|
|
334
|
-
return
|
|
314
|
+
return item;
|
|
335
315
|
}
|
|
336
316
|
};
|
|
337
|
-
SchemaNullable.id = "nullable";
|
|
338
317
|
__decorateClass([
|
|
339
318
|
parsable()
|
|
340
|
-
],
|
|
319
|
+
], _SchemaItem.prototype, "setInvalidError", 1);
|
|
341
320
|
__decorateClass([
|
|
342
321
|
parsable()
|
|
343
|
-
],
|
|
322
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
344
323
|
__decorateClass([
|
|
345
324
|
parsable()
|
|
346
|
-
],
|
|
347
|
-
|
|
348
|
-
// src/items/object.ts
|
|
349
|
-
import { isObject as isObject2, objectFind, objectMap } from "@dzeio/object-util";
|
|
350
|
-
var SchemaObject = class extends SchemaItem {
|
|
351
|
-
constructor(model) {
|
|
352
|
-
super([model]);
|
|
353
|
-
this.model = model;
|
|
354
|
-
}
|
|
355
|
-
isOfType(input) {
|
|
356
|
-
return isObject2(input) && !objectFind(this.model, (item, key) => !item.isOfType(input[key]));
|
|
357
|
-
}
|
|
358
|
-
getSubInputs(input) {
|
|
359
|
-
return isObject2(input) ? objectMap(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
SchemaObject.id = "object";
|
|
363
|
-
|
|
364
|
-
// src/helpers.ts
|
|
365
|
-
function isNull(value) {
|
|
366
|
-
return typeof value === "undefined" || value === null;
|
|
367
|
-
}
|
|
368
|
-
function parseQuery(schema, query, opts) {
|
|
369
|
-
const record = {};
|
|
370
|
-
for (const [key, value] of query) {
|
|
371
|
-
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
372
|
-
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
373
|
-
const allValues = query.getAll(key);
|
|
374
|
-
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
375
|
-
objectSet2(record, keys, finalValue);
|
|
376
|
-
}
|
|
377
|
-
return schema.parse(record, opts);
|
|
378
|
-
}
|
|
379
|
-
function parseFormData(schema, data, opts) {
|
|
380
|
-
const record = {};
|
|
381
|
-
for (const [key, value] of data) {
|
|
382
|
-
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
383
|
-
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
384
|
-
const allValues = data.getAll(key);
|
|
385
|
-
const finalValue = schemaItem && schemaItem.parse(allValues).valid ? allValues : value;
|
|
386
|
-
objectSet2(record, keys, finalValue);
|
|
387
|
-
}
|
|
388
|
-
const handleBoolean = (value, keys) => {
|
|
389
|
-
var _a;
|
|
390
|
-
if (value instanceof SchemaNullable) {
|
|
391
|
-
handleBoolean(value.unwrap(), keys);
|
|
392
|
-
}
|
|
393
|
-
if (value instanceof SchemaArray) {
|
|
394
|
-
const elements = objectGet(record, keys);
|
|
395
|
-
for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
|
|
396
|
-
handleBoolean(value.unwrap(), [...keys, it]);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
if (value instanceof SchemaObject) {
|
|
400
|
-
handleSchemaForBoolean(value.model, keys);
|
|
401
|
-
}
|
|
402
|
-
if (value instanceof SchemaBoolean && typeof objectGet(record, keys) === "undefined") {
|
|
403
|
-
objectSet2(record, keys, false);
|
|
404
|
-
}
|
|
405
|
-
};
|
|
406
|
-
const handleSchemaForBoolean = (model, keys = []) => {
|
|
407
|
-
objectLoop(model, (value, key) => {
|
|
408
|
-
handleBoolean(value, [...keys, key]);
|
|
409
|
-
});
|
|
410
|
-
};
|
|
411
|
-
if (schema instanceof SchemaObject) {
|
|
412
|
-
handleSchemaForBoolean(schema.model);
|
|
413
|
-
}
|
|
414
|
-
return schema.parse(record, opts);
|
|
415
|
-
}
|
|
416
|
-
function parseForm(model, form, opts) {
|
|
417
|
-
return parseFormData(model, new FormData(form), opts);
|
|
418
|
-
}
|
|
419
|
-
function getSchemaItemAtPath(schema, path) {
|
|
420
|
-
let current = schema;
|
|
421
|
-
const internalPath = objectClone(path);
|
|
422
|
-
for (const key of path) {
|
|
423
|
-
if (!current) {
|
|
424
|
-
return void 0;
|
|
425
|
-
}
|
|
426
|
-
internalPath.shift();
|
|
427
|
-
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
428
|
-
current = current.unwrap();
|
|
429
|
-
}
|
|
430
|
-
if (current instanceof SchemaObject) {
|
|
431
|
-
current = current.model[key];
|
|
432
|
-
current = getSchemaItemAtPath(current, internalPath);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
436
|
-
current = current.unwrap();
|
|
437
|
-
}
|
|
438
|
-
return current;
|
|
439
|
-
}
|
|
325
|
+
], _SchemaItem.prototype, "in", 1);
|
|
326
|
+
var SchemaItem = _SchemaItem;
|
|
440
327
|
|
|
441
328
|
// src/items/any.ts
|
|
442
329
|
var SchemaAny = class extends SchemaItem {
|
|
@@ -448,6 +335,12 @@ SchemaAny.id = "any";
|
|
|
448
335
|
|
|
449
336
|
// src/items/date.ts
|
|
450
337
|
var SchemaDate = class extends SchemaItem {
|
|
338
|
+
// overide the basic constructor to check that the date has a valid format
|
|
339
|
+
// purpose : reject incorrect date like '2020-30-45'
|
|
340
|
+
constructor() {
|
|
341
|
+
super();
|
|
342
|
+
this.addValidation((input) => !Number.isNaN(input.getDate()));
|
|
343
|
+
}
|
|
451
344
|
parseString(format = "default") {
|
|
452
345
|
this.addPreProcess((input) => {
|
|
453
346
|
if (typeof input !== "string") {
|
|
@@ -477,7 +370,7 @@ var SchemaDate = class extends SchemaItem {
|
|
|
477
370
|
break;
|
|
478
371
|
}
|
|
479
372
|
}
|
|
480
|
-
if (!date || isNaN(date.getDate())) {
|
|
373
|
+
if (!date || Number.isNaN(date.getDate())) {
|
|
481
374
|
return input;
|
|
482
375
|
}
|
|
483
376
|
return date;
|
|
@@ -494,19 +387,25 @@ __decorateClass([
|
|
|
494
387
|
], SchemaDate.prototype, "parseString", 1);
|
|
495
388
|
|
|
496
389
|
// src/items/record.ts
|
|
497
|
-
import { isObject as
|
|
390
|
+
import { isObject as isObject2, objectLoop as objectLoop2 } from "@dzeio/object-util";
|
|
498
391
|
var SchemaRecord = class extends SchemaItem {
|
|
499
392
|
constructor(keys, values) {
|
|
500
393
|
super([keys, values]);
|
|
501
394
|
this.keys = keys;
|
|
502
395
|
this.values = values;
|
|
503
396
|
}
|
|
397
|
+
unwrap() {
|
|
398
|
+
return this.values;
|
|
399
|
+
}
|
|
400
|
+
isKeyOfType(key) {
|
|
401
|
+
return this.keys.parse(key).valid;
|
|
402
|
+
}
|
|
504
403
|
isOfType(input) {
|
|
505
|
-
return
|
|
404
|
+
return isObject2(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
506
405
|
}
|
|
507
406
|
parseSubItems(input, options) {
|
|
508
407
|
const result = { input, errors: [] };
|
|
509
|
-
if (
|
|
408
|
+
if (isObject2(input)) {
|
|
510
409
|
const clone = {};
|
|
511
410
|
objectLoop2(input, (value, key) => {
|
|
512
411
|
var _a, _b;
|
|
@@ -529,6 +428,71 @@ var SchemaRecord = class extends SchemaItem {
|
|
|
529
428
|
};
|
|
530
429
|
SchemaRecord.id = "record";
|
|
531
430
|
|
|
431
|
+
// src/items/array.ts
|
|
432
|
+
var SchemaArray = class extends SchemaItem {
|
|
433
|
+
constructor(values) {
|
|
434
|
+
super([values]);
|
|
435
|
+
this.values = values;
|
|
436
|
+
}
|
|
437
|
+
unique() {
|
|
438
|
+
this.postProcess.push((input) => input.filter((it, idx) => input.indexOf(it) === idx));
|
|
439
|
+
return this;
|
|
440
|
+
}
|
|
441
|
+
clean() {
|
|
442
|
+
this.postProcess.push((input) => input.filter((it) => !isNull(it)));
|
|
443
|
+
return this;
|
|
444
|
+
}
|
|
445
|
+
split(separator = ",") {
|
|
446
|
+
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
447
|
+
return this;
|
|
448
|
+
}
|
|
449
|
+
unwrap() {
|
|
450
|
+
return this.values;
|
|
451
|
+
}
|
|
452
|
+
isOfType(input) {
|
|
453
|
+
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
454
|
+
}
|
|
455
|
+
getSubInputs(input) {
|
|
456
|
+
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
SchemaArray.id = "array";
|
|
460
|
+
__decorateClass([
|
|
461
|
+
parsable()
|
|
462
|
+
], SchemaArray.prototype, "unique", 1);
|
|
463
|
+
__decorateClass([
|
|
464
|
+
parsable()
|
|
465
|
+
], SchemaArray.prototype, "clean", 1);
|
|
466
|
+
__decorateClass([
|
|
467
|
+
parsable()
|
|
468
|
+
], SchemaArray.prototype, "split", 1);
|
|
469
|
+
|
|
470
|
+
// src/items/boolean.ts
|
|
471
|
+
var SchemaBoolean = class extends SchemaItem {
|
|
472
|
+
parseString(trueValue = "true", falseValue = "false") {
|
|
473
|
+
this.addPreProcess((input) => {
|
|
474
|
+
if (typeof input !== "string") {
|
|
475
|
+
return input;
|
|
476
|
+
}
|
|
477
|
+
if (input === trueValue) {
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
if (input === falseValue) {
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
return input;
|
|
484
|
+
});
|
|
485
|
+
return this;
|
|
486
|
+
}
|
|
487
|
+
isOfType(input) {
|
|
488
|
+
return typeof input === "boolean";
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
SchemaBoolean.id = "boolean";
|
|
492
|
+
__decorateClass([
|
|
493
|
+
parsable()
|
|
494
|
+
], SchemaBoolean.prototype, "parseString", 1);
|
|
495
|
+
|
|
532
496
|
// src/items/enum.ts
|
|
533
497
|
var SchemaEnum = class extends SchemaItem {
|
|
534
498
|
constructor(templateEnum) {
|
|
@@ -563,6 +527,51 @@ var SchemaLiteral = class extends SchemaItem {
|
|
|
563
527
|
};
|
|
564
528
|
SchemaLiteral.id = "literal";
|
|
565
529
|
|
|
530
|
+
// src/items/nullable.ts
|
|
531
|
+
var SchemaNullable = class extends SchemaItem {
|
|
532
|
+
constructor(child) {
|
|
533
|
+
super([child]);
|
|
534
|
+
this.child = child;
|
|
535
|
+
}
|
|
536
|
+
falsyAsNull() {
|
|
537
|
+
this.addPreProcess((it) => !it ? void 0 : it);
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
emptyAsNull() {
|
|
541
|
+
this.addPreProcess((it) => it === "" ? void 0 : it);
|
|
542
|
+
return this;
|
|
543
|
+
}
|
|
544
|
+
emptyFileAsNull() {
|
|
545
|
+
this.addPreProcess((it) => it instanceof File && it.size === 0 ? void 0 : it);
|
|
546
|
+
return this;
|
|
547
|
+
}
|
|
548
|
+
unwrap() {
|
|
549
|
+
return this.child;
|
|
550
|
+
}
|
|
551
|
+
isOfType(input) {
|
|
552
|
+
return input === void 0 || this.child.isOfType(input);
|
|
553
|
+
}
|
|
554
|
+
getSubInputs(input) {
|
|
555
|
+
return [{ value: input, item: this.child, path: void 0 }];
|
|
556
|
+
}
|
|
557
|
+
parseSubItems(input, options) {
|
|
558
|
+
if (isNull(input)) {
|
|
559
|
+
return { input: void 0 };
|
|
560
|
+
}
|
|
561
|
+
return super.parseSubItems(input, options);
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
SchemaNullable.id = "nullable";
|
|
565
|
+
__decorateClass([
|
|
566
|
+
parsable()
|
|
567
|
+
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
568
|
+
__decorateClass([
|
|
569
|
+
parsable()
|
|
570
|
+
], SchemaNullable.prototype, "emptyAsNull", 1);
|
|
571
|
+
__decorateClass([
|
|
572
|
+
parsable()
|
|
573
|
+
], SchemaNullable.prototype, "emptyFileAsNull", 1);
|
|
574
|
+
|
|
566
575
|
// src/items/number.ts
|
|
567
576
|
var SchemaNumber = class extends SchemaItem {
|
|
568
577
|
lte(value, message) {
|
|
@@ -632,6 +641,22 @@ __decorateClass([
|
|
|
632
641
|
parsable()
|
|
633
642
|
], SchemaNumber.prototype, "parseString", 1);
|
|
634
643
|
|
|
644
|
+
// src/items/object.ts
|
|
645
|
+
import { isObject as isObject3, objectFind, objectMap } from "@dzeio/object-util";
|
|
646
|
+
var SchemaObject = class extends SchemaItem {
|
|
647
|
+
constructor(model) {
|
|
648
|
+
super([model]);
|
|
649
|
+
this.model = model;
|
|
650
|
+
}
|
|
651
|
+
isOfType(input) {
|
|
652
|
+
return isObject3(input) && !objectFind(this.model, (item, key) => !item.isOfType(input[key]));
|
|
653
|
+
}
|
|
654
|
+
getSubInputs(input) {
|
|
655
|
+
return isObject3(input) ? objectMap(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
SchemaObject.id = "object";
|
|
659
|
+
|
|
635
660
|
// src/items/string.ts
|
|
636
661
|
var SchemaString = class extends SchemaItem {
|
|
637
662
|
min(value, message) {
|
|
@@ -657,6 +682,15 @@ var SchemaString = class extends SchemaItem {
|
|
|
657
682
|
});
|
|
658
683
|
return this;
|
|
659
684
|
}
|
|
685
|
+
trim() {
|
|
686
|
+
this.addPreProcess((input) => {
|
|
687
|
+
if (typeof input !== "string") {
|
|
688
|
+
return input;
|
|
689
|
+
}
|
|
690
|
+
return input.trim();
|
|
691
|
+
});
|
|
692
|
+
return this;
|
|
693
|
+
}
|
|
660
694
|
notEmpty(message) {
|
|
661
695
|
this.addValidation({
|
|
662
696
|
fn(input) {
|
|
@@ -704,6 +738,9 @@ __decorateClass([
|
|
|
704
738
|
__decorateClass([
|
|
705
739
|
parsable()
|
|
706
740
|
], SchemaString.prototype, "max", 1);
|
|
741
|
+
__decorateClass([
|
|
742
|
+
parsable()
|
|
743
|
+
], SchemaString.prototype, "trim", 1);
|
|
707
744
|
__decorateClass([
|
|
708
745
|
parsable()
|
|
709
746
|
], SchemaString.prototype, "notEmpty", 1);
|
|
@@ -720,6 +757,9 @@ var SchemaUnion = class extends SchemaItem {
|
|
|
720
757
|
super(schemas);
|
|
721
758
|
this.schemas = schemas;
|
|
722
759
|
}
|
|
760
|
+
unwrap() {
|
|
761
|
+
return this.schemas;
|
|
762
|
+
}
|
|
723
763
|
isOfType(input) {
|
|
724
764
|
return this.schemas.some((it) => it.isOfType(input));
|
|
725
765
|
}
|
|
@@ -745,6 +785,9 @@ var SchemaIntersection = class extends SchemaItem {
|
|
|
745
785
|
super(schemas);
|
|
746
786
|
this.schemas = schemas;
|
|
747
787
|
}
|
|
788
|
+
unwrap() {
|
|
789
|
+
return this.schemas;
|
|
790
|
+
}
|
|
748
791
|
isOfType(input) {
|
|
749
792
|
return this.schemas.every((it) => it.isOfType(input));
|
|
750
793
|
}
|
|
@@ -819,6 +862,9 @@ var SchemaTuple = class extends SchemaItem {
|
|
|
819
862
|
super(children);
|
|
820
863
|
this.children = children;
|
|
821
864
|
}
|
|
865
|
+
unwrap() {
|
|
866
|
+
return this.children;
|
|
867
|
+
}
|
|
822
868
|
isOfType(input) {
|
|
823
869
|
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
824
870
|
return false;
|
|
@@ -839,7 +885,7 @@ SchemaTuple.id = "tuple";
|
|
|
839
885
|
// src/items/nullish.ts
|
|
840
886
|
var SchemaUndefined = class extends SchemaItem {
|
|
841
887
|
isOfType(input) {
|
|
842
|
-
return
|
|
888
|
+
return input === void 0;
|
|
843
889
|
}
|
|
844
890
|
};
|
|
845
891
|
SchemaUndefined.id = "undefined";
|
|
@@ -851,7 +897,7 @@ var SchemaNull = class extends SchemaItem {
|
|
|
851
897
|
SchemaNull.id = "null";
|
|
852
898
|
var SchemaVoid = class extends SchemaItem {
|
|
853
899
|
isOfType(input) {
|
|
854
|
-
return
|
|
900
|
+
return input === void 0;
|
|
855
901
|
}
|
|
856
902
|
};
|
|
857
903
|
SchemaVoid.id = "void";
|
|
@@ -1066,10 +1112,12 @@ export {
|
|
|
1066
1112
|
SchemaObject,
|
|
1067
1113
|
SchemaRecord,
|
|
1068
1114
|
SchemaString,
|
|
1115
|
+
SchemaTuple,
|
|
1069
1116
|
SchemaUndefined,
|
|
1070
1117
|
SchemaUnion,
|
|
1071
1118
|
SchemaVoid,
|
|
1072
1119
|
Schema as default,
|
|
1120
|
+
getSchemaItemAtPath,
|
|
1073
1121
|
isNull,
|
|
1074
1122
|
parsable,
|
|
1075
1123
|
parseForm,
|