@dzeio/schema 0.4.2 → 0.5.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 +184 -59
- package/dist/Schema.d.ts +184 -59
- package/dist/Schema.js +362 -225
- package/dist/Schema.mjs +360 -231
- package/package.json +3 -3
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 as
|
|
13
|
+
import { objectGet, objectLoop, objectSet as objectSet2 } from "@dzeio/object-util";
|
|
14
14
|
|
|
15
15
|
// src/SchemaItem.ts
|
|
16
|
-
import { objectClean } from "@dzeio/object-util";
|
|
16
|
+
import { isObject, objectClean, objectSet } from "@dzeio/object-util";
|
|
17
17
|
var _SchemaItem = class _SchemaItem {
|
|
18
18
|
constructor(items) {
|
|
19
19
|
// standard Schema V1 spec
|
|
@@ -63,25 +63,17 @@ var _SchemaItem = class _SchemaItem {
|
|
|
63
63
|
*/
|
|
64
64
|
this.attributes = [];
|
|
65
65
|
this.invalidError = "the field is invalid";
|
|
66
|
+
this.default = this.defaultValue;
|
|
66
67
|
if (items && items.length > 0) {
|
|
67
68
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
|
-
defaultValue(value, strict = true) {
|
|
71
|
-
this.addPreProcess((input) => {
|
|
72
|
-
if (strict && isNull(input) || !strict && !input) {
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
return input;
|
|
76
|
-
});
|
|
77
|
-
return this;
|
|
78
|
-
}
|
|
79
71
|
in(...values) {
|
|
80
72
|
this.addValidation((input) => values.includes(input));
|
|
81
73
|
return this;
|
|
82
74
|
}
|
|
83
75
|
attrs(...attributes) {
|
|
84
|
-
this.attributes.
|
|
76
|
+
this.attributes.push(...attributes);
|
|
85
77
|
return this;
|
|
86
78
|
}
|
|
87
79
|
attr(...attributes) {
|
|
@@ -94,40 +86,87 @@ var _SchemaItem = class _SchemaItem {
|
|
|
94
86
|
clone() {
|
|
95
87
|
return Schema.fromJSON(this.toJSON());
|
|
96
88
|
}
|
|
89
|
+
defaultValue(value, strict = true) {
|
|
90
|
+
return new SchemaDefault(this, value, strict);
|
|
91
|
+
}
|
|
97
92
|
nullable() {
|
|
98
93
|
return new SchemaNullable(this);
|
|
99
94
|
}
|
|
95
|
+
or(other) {
|
|
96
|
+
return new SchemaUnion(this, other);
|
|
97
|
+
}
|
|
98
|
+
and(other) {
|
|
99
|
+
return new SchemaIntersection(this, other);
|
|
100
|
+
}
|
|
101
|
+
array() {
|
|
102
|
+
return new SchemaArray(this);
|
|
103
|
+
}
|
|
104
|
+
/** Returns the sub values of the input, the schema item to handle them, and the path to it */
|
|
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
|
+
}
|
|
100
135
|
parse(input, options) {
|
|
101
|
-
var _a;
|
|
136
|
+
var _a, _b;
|
|
102
137
|
for (const preProcess of this.preProcess) {
|
|
103
138
|
input = preProcess(input);
|
|
104
139
|
}
|
|
140
|
+
const result = this.parseSubItems(input, options);
|
|
141
|
+
if ((_a = result.errors) == null ? void 0 : _a.length) {
|
|
142
|
+
return {
|
|
143
|
+
valid: false,
|
|
144
|
+
errors: result.errors
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
input = result.input;
|
|
105
148
|
if (!this.isOfType(input)) {
|
|
106
149
|
return {
|
|
107
150
|
valid: false,
|
|
108
151
|
errors: [{ message: "invalid type" }]
|
|
109
152
|
};
|
|
110
153
|
}
|
|
111
|
-
const
|
|
154
|
+
const validationErrors = [];
|
|
112
155
|
for (const validation of this.validations) {
|
|
113
156
|
if (!validation.fn(input)) {
|
|
114
|
-
|
|
115
|
-
message: (
|
|
157
|
+
validationErrors.push({
|
|
158
|
+
message: (_b = validation.error) != null ? _b : this.invalidError
|
|
116
159
|
});
|
|
117
160
|
if (options == null ? void 0 : options.fast) {
|
|
118
|
-
|
|
119
|
-
valid: false,
|
|
120
|
-
object: input,
|
|
121
|
-
errors
|
|
122
|
-
};
|
|
161
|
+
break;
|
|
123
162
|
}
|
|
124
163
|
}
|
|
125
164
|
}
|
|
126
|
-
if (
|
|
165
|
+
if (validationErrors.length > 0) {
|
|
127
166
|
return {
|
|
128
167
|
valid: false,
|
|
129
168
|
object: input,
|
|
130
|
-
errors
|
|
169
|
+
errors: validationErrors
|
|
131
170
|
};
|
|
132
171
|
}
|
|
133
172
|
for (const postProcess of this.postProcess) {
|
|
@@ -162,13 +201,23 @@ var _SchemaItem = class _SchemaItem {
|
|
|
162
201
|
this.postProcess.push(fn);
|
|
163
202
|
return this;
|
|
164
203
|
}
|
|
204
|
+
parseJSON() {
|
|
205
|
+
this.addPreProcess((input) => {
|
|
206
|
+
try {
|
|
207
|
+
return typeof input === "string" ? JSON.parse(input) : input;
|
|
208
|
+
} catch {
|
|
209
|
+
return input;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
165
214
|
};
|
|
166
215
|
__decorateClass([
|
|
167
|
-
|
|
168
|
-
], _SchemaItem.prototype, "defaultValue", 1);
|
|
169
|
-
__decorateClass([
|
|
170
|
-
parceable()
|
|
216
|
+
parsable()
|
|
171
217
|
], _SchemaItem.prototype, "in", 1);
|
|
218
|
+
__decorateClass([
|
|
219
|
+
parsable()
|
|
220
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
172
221
|
var SchemaItem = _SchemaItem;
|
|
173
222
|
|
|
174
223
|
// src/items/array.ts
|
|
@@ -185,77 +234,37 @@ var SchemaArray = class extends SchemaItem {
|
|
|
185
234
|
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
186
235
|
return this;
|
|
187
236
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (!valid) {
|
|
191
|
-
return {
|
|
192
|
-
valid,
|
|
193
|
-
object,
|
|
194
|
-
errors
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
const clone = [];
|
|
198
|
-
const errs = [];
|
|
199
|
-
for (let idx = 0; idx < object.length; idx++) {
|
|
200
|
-
const item = object[idx];
|
|
201
|
-
const res = this.values.parse(item);
|
|
202
|
-
if (res.valid) {
|
|
203
|
-
clone.push(res.object);
|
|
204
|
-
continue;
|
|
205
|
-
}
|
|
206
|
-
const errss = res.errors.map((it) => ({
|
|
207
|
-
...it,
|
|
208
|
-
field: it.field ? `${idx}.${it.field}` : idx.toString()
|
|
209
|
-
}));
|
|
210
|
-
if (options == null ? void 0 : options.fast) {
|
|
211
|
-
return {
|
|
212
|
-
valid: false,
|
|
213
|
-
object: clone,
|
|
214
|
-
errors: errss
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
errs.push(...errss);
|
|
218
|
-
}
|
|
219
|
-
if (errs.length > 0) {
|
|
220
|
-
return {
|
|
221
|
-
valid: false,
|
|
222
|
-
object: clone,
|
|
223
|
-
errors: errs
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
valid: true,
|
|
228
|
-
object: clone
|
|
229
|
-
};
|
|
237
|
+
getSubInputs(input) {
|
|
238
|
+
return Array.isArray(input) ? input.map((e, i) => ({ item: this.values, value: e, path: i.toString() })) : [];
|
|
230
239
|
}
|
|
231
240
|
unwrap() {
|
|
232
241
|
return this.values;
|
|
233
242
|
}
|
|
234
243
|
isOfType(input) {
|
|
235
|
-
return Array.isArray(input);
|
|
244
|
+
return Array.isArray(input) && input.every((e) => this.values.isOfType(e));
|
|
236
245
|
}
|
|
237
246
|
};
|
|
238
247
|
__decorateClass([
|
|
239
|
-
|
|
248
|
+
parsable()
|
|
240
249
|
], SchemaArray.prototype, "unique", 1);
|
|
241
250
|
__decorateClass([
|
|
242
|
-
|
|
251
|
+
parsable()
|
|
243
252
|
], SchemaArray.prototype, "split", 1);
|
|
244
253
|
|
|
245
254
|
// src/items/boolean.ts
|
|
246
255
|
var SchemaBoolean = class extends SchemaItem {
|
|
247
256
|
parseString(trueValue = "true", falseValue = "false") {
|
|
248
|
-
this.addPreProcess((
|
|
249
|
-
if (typeof
|
|
250
|
-
return
|
|
257
|
+
this.addPreProcess((input) => {
|
|
258
|
+
if (typeof input !== "string") {
|
|
259
|
+
return input;
|
|
251
260
|
}
|
|
252
|
-
if (
|
|
261
|
+
if (input === trueValue) {
|
|
253
262
|
return true;
|
|
254
263
|
}
|
|
255
|
-
if (
|
|
264
|
+
if (input === falseValue) {
|
|
256
265
|
return false;
|
|
257
266
|
}
|
|
258
|
-
return
|
|
267
|
+
return input;
|
|
259
268
|
});
|
|
260
269
|
return this;
|
|
261
270
|
}
|
|
@@ -264,7 +273,7 @@ var SchemaBoolean = class extends SchemaItem {
|
|
|
264
273
|
}
|
|
265
274
|
};
|
|
266
275
|
__decorateClass([
|
|
267
|
-
|
|
276
|
+
parsable()
|
|
268
277
|
], SchemaBoolean.prototype, "parseString", 1);
|
|
269
278
|
|
|
270
279
|
// src/items/nullable.ts
|
|
@@ -273,76 +282,46 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
273
282
|
super([child]);
|
|
274
283
|
this.child = child;
|
|
275
284
|
}
|
|
276
|
-
|
|
285
|
+
falsyAsNull() {
|
|
277
286
|
this.addPreProcess((it) => !it ? void 0 : it);
|
|
278
287
|
return this;
|
|
279
288
|
}
|
|
280
289
|
unwrap() {
|
|
281
290
|
return this.child;
|
|
282
291
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
return {
|
|
290
|
-
valid: true,
|
|
291
|
-
object: void 0
|
|
292
|
-
};
|
|
292
|
+
getSubInputs(input) {
|
|
293
|
+
return [{ value: input, item: this.child, path: void 0 }];
|
|
294
|
+
}
|
|
295
|
+
parseSubItems(input, options) {
|
|
296
|
+
if (isNull(input)) {
|
|
297
|
+
return { input: void 0 };
|
|
293
298
|
}
|
|
294
|
-
return
|
|
299
|
+
return super.parseSubItems(input, options);
|
|
295
300
|
}
|
|
296
301
|
isOfType(input) {
|
|
297
|
-
return
|
|
298
|
-
}
|
|
299
|
-
isNull(value) {
|
|
300
|
-
return typeof value === "undefined" || value === null;
|
|
302
|
+
return typeof input === "undefined" || this.child.isOfType(input);
|
|
301
303
|
}
|
|
302
304
|
};
|
|
303
305
|
__decorateClass([
|
|
304
|
-
|
|
305
|
-
], SchemaNullable.prototype, "
|
|
306
|
+
parsable()
|
|
307
|
+
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
306
308
|
|
|
307
309
|
// src/items/object.ts
|
|
308
|
-
import { isObject,
|
|
310
|
+
import { isObject as isObject2, objectFind, objectMap } from "@dzeio/object-util";
|
|
309
311
|
var SchemaObject = class extends SchemaItem {
|
|
310
312
|
constructor(model) {
|
|
311
313
|
super([model]);
|
|
312
314
|
this.model = model;
|
|
313
315
|
this.id = "object";
|
|
314
316
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
object,
|
|
321
|
-
errors
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
const clone = objectClone(object);
|
|
325
|
-
const res = {};
|
|
326
|
-
objectLoop(this.model, (childSchema, key) => {
|
|
327
|
-
const childValue = clone[key];
|
|
328
|
-
const child = childSchema.parse(childValue);
|
|
329
|
-
if (!child.valid) {
|
|
330
|
-
errors.push(...child.errors.map((it) => ({
|
|
331
|
-
...it,
|
|
332
|
-
field: it.field ? `${key}.${it.field}` : key
|
|
333
|
-
})));
|
|
334
|
-
}
|
|
335
|
-
res[key] = child.object;
|
|
336
|
-
return child.valid || !(options == null ? void 0 : options.fast);
|
|
337
|
-
});
|
|
338
|
-
return {
|
|
339
|
-
valid: errors.length === 0,
|
|
340
|
-
errors,
|
|
341
|
-
object: res
|
|
342
|
-
};
|
|
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)) : [];
|
|
343
322
|
}
|
|
344
323
|
isOfType(input) {
|
|
345
|
-
return
|
|
324
|
+
return isObject2(input) && !objectFind(this.model, (item, k) => !item.isOfType(input[k]));
|
|
346
325
|
}
|
|
347
326
|
};
|
|
348
327
|
|
|
@@ -353,15 +332,16 @@ function isNull(value) {
|
|
|
353
332
|
function parseQuery(model, query, opts) {
|
|
354
333
|
const record = {};
|
|
355
334
|
for (const [key, value] of query) {
|
|
356
|
-
|
|
335
|
+
const hasMultipleValues = query.getAll(key).length > 1;
|
|
336
|
+
objectSet2(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? query.getAll(key) : value);
|
|
357
337
|
}
|
|
358
338
|
return model.parse(record, opts);
|
|
359
339
|
}
|
|
360
|
-
function parseFormData(
|
|
340
|
+
function parseFormData(schema, data, opts) {
|
|
361
341
|
const record = {};
|
|
362
342
|
for (const [key, value] of data) {
|
|
363
343
|
const hasMultipleValues = data.getAll(key).length > 1;
|
|
364
|
-
|
|
344
|
+
objectSet2(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
|
|
365
345
|
}
|
|
366
346
|
const handleBoolean = (value, keys) => {
|
|
367
347
|
var _a;
|
|
@@ -378,18 +358,18 @@ function parseFormData(model, data, opts) {
|
|
|
378
358
|
handleSchemaForBoolean(value.model, keys);
|
|
379
359
|
}
|
|
380
360
|
if (value instanceof SchemaBoolean) {
|
|
381
|
-
|
|
361
|
+
objectSet2(record, keys, !!data.get(keys.join(".")));
|
|
382
362
|
}
|
|
383
363
|
};
|
|
384
|
-
const handleSchemaForBoolean = (
|
|
385
|
-
|
|
364
|
+
const handleSchemaForBoolean = (model, keys = []) => {
|
|
365
|
+
objectLoop(model, (value, key) => {
|
|
386
366
|
handleBoolean(value, [...keys, key]);
|
|
387
367
|
});
|
|
388
368
|
};
|
|
389
|
-
if (
|
|
390
|
-
handleSchemaForBoolean(
|
|
369
|
+
if (schema instanceof SchemaObject) {
|
|
370
|
+
handleSchemaForBoolean(schema.model);
|
|
391
371
|
}
|
|
392
|
-
return
|
|
372
|
+
return schema.parse(record, opts);
|
|
393
373
|
}
|
|
394
374
|
function parseForm(model, form, opts) {
|
|
395
375
|
return parseFormData(model, new FormData(form), opts);
|
|
@@ -397,47 +377,47 @@ function parseForm(model, form, opts) {
|
|
|
397
377
|
|
|
398
378
|
// src/items/any.ts
|
|
399
379
|
var SchemaAny = class extends SchemaItem {
|
|
400
|
-
|
|
401
|
-
isOfType(input) {
|
|
380
|
+
isOfType(_input) {
|
|
402
381
|
return true;
|
|
403
382
|
}
|
|
404
383
|
};
|
|
405
384
|
|
|
406
385
|
// src/items/date.ts
|
|
407
386
|
var SchemaDate = class extends SchemaItem {
|
|
408
|
-
parseString(format = "
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
this.addPreProcess((it) => {
|
|
413
|
-
if (typeof it !== "string") {
|
|
414
|
-
return it;
|
|
415
|
-
}
|
|
416
|
-
const date = new Date(it);
|
|
417
|
-
if (isNaN(date.getDate())) {
|
|
418
|
-
return it;
|
|
419
|
-
}
|
|
420
|
-
return date;
|
|
421
|
-
});
|
|
422
|
-
break;
|
|
387
|
+
parseString(format = "default") {
|
|
388
|
+
this.addPreProcess((input) => {
|
|
389
|
+
if (typeof input !== "string") {
|
|
390
|
+
return input;
|
|
423
391
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
392
|
+
let date = void 0;
|
|
393
|
+
switch (format) {
|
|
394
|
+
case "default": {
|
|
395
|
+
date = new Date(input);
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
case "yy-mm-dd":
|
|
399
|
+
case "iso8601": {
|
|
400
|
+
const splitted = input.split("-").map((it) => Number.parseInt(it, 10));
|
|
401
|
+
if (splitted.length !== 3) {
|
|
402
|
+
break;
|
|
428
403
|
}
|
|
404
|
+
date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
case "jj/mm/yy": {
|
|
429
408
|
const splitted = input.split("/").map((it) => Number.parseInt(it, 10));
|
|
430
409
|
if (splitted.length !== 3) {
|
|
431
|
-
|
|
432
|
-
}
|
|
433
|
-
const date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
434
|
-
if (isNaN(date.getDate())) {
|
|
435
|
-
return input;
|
|
410
|
+
break;
|
|
436
411
|
}
|
|
437
|
-
|
|
438
|
-
|
|
412
|
+
date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
439
415
|
}
|
|
440
|
-
|
|
416
|
+
if (!date || isNaN(date.getDate())) {
|
|
417
|
+
return input;
|
|
418
|
+
}
|
|
419
|
+
return date;
|
|
420
|
+
});
|
|
441
421
|
return this;
|
|
442
422
|
}
|
|
443
423
|
isOfType(input) {
|
|
@@ -445,49 +425,41 @@ var SchemaDate = class extends SchemaItem {
|
|
|
445
425
|
}
|
|
446
426
|
};
|
|
447
427
|
__decorateClass([
|
|
448
|
-
|
|
428
|
+
parsable()
|
|
449
429
|
], SchemaDate.prototype, "parseString", 1);
|
|
450
430
|
|
|
451
431
|
// src/items/record.ts
|
|
452
|
-
import { isObject as
|
|
432
|
+
import { isObject as isObject3, objectLoop as objectLoop2 } from "@dzeio/object-util";
|
|
453
433
|
var SchemaRecord = class extends SchemaItem {
|
|
454
434
|
constructor(keys, values) {
|
|
455
435
|
super([keys, values]);
|
|
456
436
|
this.keys = keys;
|
|
457
437
|
this.values = values;
|
|
458
438
|
}
|
|
459
|
-
|
|
460
|
-
const {
|
|
461
|
-
if (
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
439
|
+
parseSubItems(input, options) {
|
|
440
|
+
const result = { input, errors: [] };
|
|
441
|
+
if (isObject3(input)) {
|
|
442
|
+
const clone = {};
|
|
443
|
+
objectLoop2(input, (value, key) => {
|
|
444
|
+
var _a, _b;
|
|
445
|
+
const res1 = this.keys.parse(key);
|
|
446
|
+
const res2 = this.values.parse(value);
|
|
447
|
+
if (!res1.valid || !res2.valid) {
|
|
448
|
+
result.errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
|
|
449
|
+
message: it.message,
|
|
450
|
+
field: it.field ? `${key}.${it.field}` : key.toString()
|
|
451
|
+
})));
|
|
452
|
+
} else {
|
|
453
|
+
clone[res1.object] = res2.object;
|
|
454
|
+
}
|
|
455
|
+
return result.errors.length === 0 || !(options == null ? void 0 : options.fast);
|
|
456
|
+
});
|
|
457
|
+
result.input = clone;
|
|
467
458
|
}
|
|
468
|
-
|
|
469
|
-
objectLoop3(object, (value, key) => {
|
|
470
|
-
var _a, _b;
|
|
471
|
-
const res1 = this.keys.parse(key);
|
|
472
|
-
const res2 = this.values.parse(value);
|
|
473
|
-
if (!res1.valid || !res2.valid) {
|
|
474
|
-
errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
|
|
475
|
-
message: it.message,
|
|
476
|
-
field: it.field ? `${key}.${it.field}` : key.toString()
|
|
477
|
-
})));
|
|
478
|
-
} else {
|
|
479
|
-
clone[res1.object] = res2.object;
|
|
480
|
-
}
|
|
481
|
-
return errors.length === 0 || !(options == null ? void 0 : options.fast);
|
|
482
|
-
});
|
|
483
|
-
return {
|
|
484
|
-
valid: errors.length === 0,
|
|
485
|
-
errors,
|
|
486
|
-
object: clone
|
|
487
|
-
};
|
|
459
|
+
return result;
|
|
488
460
|
}
|
|
489
461
|
isOfType(input) {
|
|
490
|
-
return
|
|
462
|
+
return isObject3(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
491
463
|
}
|
|
492
464
|
};
|
|
493
465
|
|
|
@@ -562,9 +534,7 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
562
534
|
return this;
|
|
563
535
|
}
|
|
564
536
|
parseString() {
|
|
565
|
-
this.addPreProcess(
|
|
566
|
-
(input) => typeof input === "string" ? Number.parseFloat(input) : input
|
|
567
|
-
);
|
|
537
|
+
this.addPreProcess((input) => typeof input === "string" ? parseFloat(input) : input);
|
|
568
538
|
return this;
|
|
569
539
|
}
|
|
570
540
|
isOfType(input) {
|
|
@@ -578,19 +548,19 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
578
548
|
}
|
|
579
549
|
};
|
|
580
550
|
__decorateClass([
|
|
581
|
-
|
|
551
|
+
parsable()
|
|
582
552
|
], SchemaNumber.prototype, "lte", 1);
|
|
583
553
|
__decorateClass([
|
|
584
|
-
|
|
554
|
+
parsable()
|
|
585
555
|
], SchemaNumber.prototype, "gte", 1);
|
|
586
556
|
__decorateClass([
|
|
587
|
-
|
|
557
|
+
parsable()
|
|
588
558
|
], SchemaNumber.prototype, "lt", 1);
|
|
589
559
|
__decorateClass([
|
|
590
|
-
|
|
560
|
+
parsable()
|
|
591
561
|
], SchemaNumber.prototype, "gt", 1);
|
|
592
562
|
__decorateClass([
|
|
593
|
-
|
|
563
|
+
parsable()
|
|
594
564
|
], SchemaNumber.prototype, "parseString", 1);
|
|
595
565
|
|
|
596
566
|
// src/items/string.ts
|
|
@@ -647,57 +617,170 @@ var SchemaString = class extends SchemaItem {
|
|
|
647
617
|
}
|
|
648
618
|
};
|
|
649
619
|
__decorateClass([
|
|
650
|
-
|
|
620
|
+
parsable()
|
|
651
621
|
], SchemaString.prototype, "min", 1);
|
|
652
622
|
__decorateClass([
|
|
653
|
-
|
|
623
|
+
parsable()
|
|
654
624
|
], SchemaString.prototype, "toCasing", 1);
|
|
655
625
|
__decorateClass([
|
|
656
|
-
|
|
626
|
+
parsable()
|
|
657
627
|
], SchemaString.prototype, "max", 1);
|
|
658
628
|
__decorateClass([
|
|
659
|
-
|
|
629
|
+
parsable()
|
|
660
630
|
], SchemaString.prototype, "notEmpty", 1);
|
|
661
631
|
__decorateClass([
|
|
662
|
-
|
|
632
|
+
parsable()
|
|
663
633
|
], SchemaString.prototype, "regex", 1);
|
|
664
634
|
|
|
665
635
|
// src/items/union.ts
|
|
666
636
|
var SchemaUnion = class extends SchemaItem {
|
|
667
637
|
constructor(...schemas) {
|
|
668
|
-
super();
|
|
638
|
+
super(schemas);
|
|
669
639
|
this.schemas = schemas;
|
|
670
640
|
}
|
|
671
|
-
|
|
672
|
-
const {
|
|
673
|
-
if (!valid) {
|
|
674
|
-
return {
|
|
675
|
-
valid,
|
|
676
|
-
object,
|
|
677
|
-
errors
|
|
678
|
-
};
|
|
679
|
-
}
|
|
641
|
+
parseSubItems(input, options) {
|
|
642
|
+
const result = { input, errors: [] };
|
|
680
643
|
for (const schema of this.schemas) {
|
|
681
|
-
const validation = schema.parse(input, options);
|
|
644
|
+
const validation = schema.parse(result.input, options);
|
|
682
645
|
if (validation.valid) {
|
|
683
|
-
return validation;
|
|
646
|
+
return { input: validation.object };
|
|
684
647
|
} else {
|
|
685
|
-
errors.push(...validation.errors);
|
|
648
|
+
result.errors.push(...validation.errors);
|
|
686
649
|
}
|
|
687
650
|
}
|
|
688
|
-
return
|
|
689
|
-
valid: false,
|
|
690
|
-
errors,
|
|
691
|
-
object
|
|
692
|
-
};
|
|
651
|
+
return result;
|
|
693
652
|
}
|
|
694
653
|
isOfType(input) {
|
|
695
654
|
return this.schemas.some((it) => it.isOfType(input));
|
|
696
655
|
}
|
|
697
656
|
};
|
|
698
657
|
|
|
658
|
+
// src/items/intersection.ts
|
|
659
|
+
import { isObject as isObject4 } from "@dzeio/object-util";
|
|
660
|
+
var SchemaIntersection = class extends SchemaItem {
|
|
661
|
+
constructor(...schemas) {
|
|
662
|
+
super(schemas);
|
|
663
|
+
this.schemas = schemas;
|
|
664
|
+
}
|
|
665
|
+
parseSubItems(input, options) {
|
|
666
|
+
const result = { input, errors: [] };
|
|
667
|
+
for (const schema of this.schemas) {
|
|
668
|
+
const validation = schema.parse(result.input, options);
|
|
669
|
+
if (!validation.valid) {
|
|
670
|
+
result.errors.push(...validation.errors);
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
if (isObject4(result.input)) {
|
|
674
|
+
Object.assign(result.input, validation.object);
|
|
675
|
+
} else {
|
|
676
|
+
result.input = validation.object;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return result;
|
|
680
|
+
}
|
|
681
|
+
isOfType(input) {
|
|
682
|
+
return this.schemas.every((it) => it.isOfType(input));
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
// src/items/default.ts
|
|
687
|
+
var SchemaDefault = class extends SchemaItem {
|
|
688
|
+
constructor(child, defaultVal, strict = true) {
|
|
689
|
+
super([child, defaultVal, strict]);
|
|
690
|
+
this.child = child;
|
|
691
|
+
this.defaultVal = defaultVal;
|
|
692
|
+
this.strict = strict;
|
|
693
|
+
}
|
|
694
|
+
unwrap() {
|
|
695
|
+
return this.child;
|
|
696
|
+
}
|
|
697
|
+
getSubInputs(input) {
|
|
698
|
+
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
699
|
+
}
|
|
700
|
+
isOfType(input) {
|
|
701
|
+
return this.child.isOfType(input);
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// src/items/file.ts
|
|
706
|
+
var SchemaFile = class extends SchemaItem {
|
|
707
|
+
constructor() {
|
|
708
|
+
super();
|
|
709
|
+
this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
|
|
710
|
+
}
|
|
711
|
+
extension(ext, error) {
|
|
712
|
+
this.addValidation({ fn: (input) => {
|
|
713
|
+
var _a;
|
|
714
|
+
return (_a = input.name) == null ? void 0 : _a.endsWith(ext);
|
|
715
|
+
}, error });
|
|
716
|
+
return this;
|
|
717
|
+
}
|
|
718
|
+
maxSize(size, error) {
|
|
719
|
+
this.addValidation({ fn: (file) => file.size <= size, error });
|
|
720
|
+
return this;
|
|
721
|
+
}
|
|
722
|
+
isOfType(input) {
|
|
723
|
+
return input instanceof File;
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
__decorateClass([
|
|
727
|
+
parsable()
|
|
728
|
+
], SchemaFile.prototype, "extension", 1);
|
|
729
|
+
__decorateClass([
|
|
730
|
+
parsable()
|
|
731
|
+
], SchemaFile.prototype, "maxSize", 1);
|
|
732
|
+
|
|
733
|
+
// src/items/tuple.ts
|
|
734
|
+
var SchemaTuple = class extends SchemaItem {
|
|
735
|
+
constructor(...children) {
|
|
736
|
+
super(children);
|
|
737
|
+
this.children = children;
|
|
738
|
+
}
|
|
739
|
+
getSubInputs(input) {
|
|
740
|
+
return Array.isArray(input) ? this.children.map((e, i) => ({ value: input[i], item: e, path: i.toString() })) : [];
|
|
741
|
+
}
|
|
742
|
+
isOfType(input) {
|
|
743
|
+
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
for (const [i, item] of this.children.entries()) {
|
|
747
|
+
if (item instanceof SchemaItem && !item.isOfType(input[i])) {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
// src/items/nullish.ts
|
|
756
|
+
var SchemaUndefined = class extends SchemaItem {
|
|
757
|
+
isOfType(input) {
|
|
758
|
+
return typeof input === "undefined";
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
var SchemaNull = class extends SchemaItem {
|
|
762
|
+
isOfType(input) {
|
|
763
|
+
return input === null;
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
var SchemaVoid = class extends SchemaItem {
|
|
767
|
+
isOfType(input) {
|
|
768
|
+
return typeof input === "undefined";
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
var SchemaNullish = class extends SchemaItem {
|
|
772
|
+
isOfType(input) {
|
|
773
|
+
return isNull(input);
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
var SchemaNever = class extends SchemaItem {
|
|
777
|
+
isOfType(_input) {
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
|
|
699
782
|
// src/Schema.ts
|
|
700
|
-
function
|
|
783
|
+
function parsable() {
|
|
701
784
|
return (target, propertyKey, descriptor) => {
|
|
702
785
|
if (!(propertyKey in target)) {
|
|
703
786
|
throw new Error("property not set in object");
|
|
@@ -714,6 +797,7 @@ function parceable() {
|
|
|
714
797
|
var Types = {
|
|
715
798
|
Any: SchemaAny,
|
|
716
799
|
Array: SchemaArray,
|
|
800
|
+
Tuple: SchemaTuple,
|
|
717
801
|
Boolean: SchemaBoolean,
|
|
718
802
|
Date: SchemaDate,
|
|
719
803
|
Enum: SchemaEnum,
|
|
@@ -722,7 +806,13 @@ var Types = {
|
|
|
722
806
|
Object: SchemaObject,
|
|
723
807
|
Record: SchemaRecord,
|
|
724
808
|
String: SchemaString,
|
|
725
|
-
Union: SchemaUnion
|
|
809
|
+
Union: SchemaUnion,
|
|
810
|
+
Intersection: SchemaIntersection,
|
|
811
|
+
Default: SchemaDefault,
|
|
812
|
+
Undefined: SchemaUndefined,
|
|
813
|
+
Void: SchemaVoid,
|
|
814
|
+
Null: SchemaNull,
|
|
815
|
+
Nullish: SchemaNullish
|
|
726
816
|
};
|
|
727
817
|
var _Schema = class _Schema extends SchemaObject {
|
|
728
818
|
static register(module) {
|
|
@@ -772,9 +862,33 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
772
862
|
static string(...inputs) {
|
|
773
863
|
return new SchemaString(...inputs);
|
|
774
864
|
}
|
|
865
|
+
static file(...inputs) {
|
|
866
|
+
return new SchemaFile(...inputs);
|
|
867
|
+
}
|
|
775
868
|
static union(...inputs) {
|
|
776
869
|
return new SchemaUnion(...inputs);
|
|
777
870
|
}
|
|
871
|
+
static intersection(...inputs) {
|
|
872
|
+
return new SchemaIntersection(...inputs);
|
|
873
|
+
}
|
|
874
|
+
static tuple(...inputs) {
|
|
875
|
+
return new SchemaTuple(...inputs);
|
|
876
|
+
}
|
|
877
|
+
static undefined(...inputs) {
|
|
878
|
+
return new SchemaUndefined(...inputs);
|
|
879
|
+
}
|
|
880
|
+
static null(...inputs) {
|
|
881
|
+
return new SchemaNull(...inputs);
|
|
882
|
+
}
|
|
883
|
+
static nullish(...inputs) {
|
|
884
|
+
return new SchemaNullish(...inputs);
|
|
885
|
+
}
|
|
886
|
+
static void(...inputs) {
|
|
887
|
+
return new SchemaVoid(...inputs);
|
|
888
|
+
}
|
|
889
|
+
static never(...inputs) {
|
|
890
|
+
return new SchemaNever(...inputs);
|
|
891
|
+
}
|
|
778
892
|
static fromJSON(json) {
|
|
779
893
|
var _a, _b, _c, _d, _e, _f;
|
|
780
894
|
const fn = this.getModule(json.i);
|
|
@@ -827,6 +941,7 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
827
941
|
};
|
|
828
942
|
_Schema.registeredModules = [
|
|
829
943
|
SchemaArray,
|
|
944
|
+
SchemaTuple,
|
|
830
945
|
SchemaBoolean,
|
|
831
946
|
SchemaDate,
|
|
832
947
|
SchemaEnum,
|
|
@@ -835,7 +950,13 @@ _Schema.registeredModules = [
|
|
|
835
950
|
SchemaObject,
|
|
836
951
|
SchemaRecord,
|
|
837
952
|
SchemaString,
|
|
838
|
-
SchemaUnion
|
|
953
|
+
SchemaUnion,
|
|
954
|
+
SchemaIntersection,
|
|
955
|
+
SchemaDefault,
|
|
956
|
+
SchemaUndefined,
|
|
957
|
+
SchemaVoid,
|
|
958
|
+
SchemaNull,
|
|
959
|
+
SchemaNullish
|
|
839
960
|
];
|
|
840
961
|
var Schema = _Schema;
|
|
841
962
|
var s = Schema;
|
|
@@ -844,19 +965,27 @@ export {
|
|
|
844
965
|
SchemaArray,
|
|
845
966
|
SchemaBoolean,
|
|
846
967
|
SchemaDate,
|
|
968
|
+
SchemaDefault,
|
|
847
969
|
SchemaEnum,
|
|
970
|
+
SchemaFile,
|
|
971
|
+
SchemaIntersection,
|
|
848
972
|
SchemaItem,
|
|
849
973
|
SchemaLiteral,
|
|
974
|
+
SchemaNever,
|
|
975
|
+
SchemaNull,
|
|
850
976
|
SchemaNullable,
|
|
977
|
+
SchemaNullish,
|
|
851
978
|
SchemaNumber,
|
|
852
979
|
SchemaObject,
|
|
853
980
|
SchemaRecord,
|
|
854
981
|
SchemaString,
|
|
982
|
+
SchemaUndefined,
|
|
855
983
|
SchemaUnion,
|
|
984
|
+
SchemaVoid,
|
|
856
985
|
Types,
|
|
857
986
|
Schema as default,
|
|
858
987
|
isNull,
|
|
859
|
-
|
|
988
|
+
parsable,
|
|
860
989
|
parseForm,
|
|
861
990
|
parseFormData,
|
|
862
991
|
parseQuery,
|