@dzeio/schema 0.4.3 → 0.6.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 +197 -69
- package/dist/Schema.d.ts +197 -69
- package/dist/Schema.js +394 -236
- package/dist/Schema.mjs +391 -241
- 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 { objectClone, 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
|
|
@@ -33,14 +33,18 @@ var _SchemaItem = class _SchemaItem {
|
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
-
/**
|
|
37
|
-
* keep public ?
|
|
38
|
-
*/
|
|
39
|
-
this.validations = [];
|
|
40
36
|
/**
|
|
41
37
|
* Function calls saved for serialization
|
|
42
38
|
*/
|
|
43
39
|
this.savedCalls = [];
|
|
40
|
+
/**
|
|
41
|
+
* list of attributes for custom works
|
|
42
|
+
*/
|
|
43
|
+
this.attributes = [];
|
|
44
|
+
/**
|
|
45
|
+
* keep public ?
|
|
46
|
+
*/
|
|
47
|
+
this.validations = [];
|
|
44
48
|
/**
|
|
45
49
|
* Pre process the variable for various reasons
|
|
46
50
|
*
|
|
@@ -58,21 +62,20 @@ var _SchemaItem = class _SchemaItem {
|
|
|
58
62
|
* note: the type of the final post-process MUST be valid
|
|
59
63
|
*/
|
|
60
64
|
this.postProcess = [];
|
|
61
|
-
/**
|
|
62
|
-
* list of attributes for custom works
|
|
63
|
-
*/
|
|
64
|
-
this.attributes = [];
|
|
65
65
|
this.invalidError = "the field is invalid";
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/member-ordering, @typescript-eslint/unbound-method
|
|
67
|
+
this.default = this.defaultValue;
|
|
66
68
|
if (items && items.length > 0) {
|
|
67
69
|
this.items = Array.isArray(items) ? items : Array.from(items);
|
|
68
70
|
}
|
|
69
71
|
}
|
|
70
|
-
|
|
72
|
+
parseJSON() {
|
|
71
73
|
this.addPreProcess((input) => {
|
|
72
|
-
|
|
73
|
-
return
|
|
74
|
+
try {
|
|
75
|
+
return typeof input === "string" ? JSON.parse(input) : input;
|
|
76
|
+
} catch {
|
|
77
|
+
return input;
|
|
74
78
|
}
|
|
75
|
-
return input;
|
|
76
79
|
});
|
|
77
80
|
return this;
|
|
78
81
|
}
|
|
@@ -94,40 +97,57 @@ var _SchemaItem = class _SchemaItem {
|
|
|
94
97
|
clone() {
|
|
95
98
|
return Schema.fromJSON(this.toJSON());
|
|
96
99
|
}
|
|
100
|
+
defaultValue(value, strict = true) {
|
|
101
|
+
return new SchemaDefault(this, value, strict);
|
|
102
|
+
}
|
|
97
103
|
nullable() {
|
|
98
104
|
return new SchemaNullable(this);
|
|
99
105
|
}
|
|
106
|
+
or(other) {
|
|
107
|
+
return new SchemaUnion(this, other);
|
|
108
|
+
}
|
|
109
|
+
and(other) {
|
|
110
|
+
return new SchemaIntersection(this, other);
|
|
111
|
+
}
|
|
112
|
+
array() {
|
|
113
|
+
return new SchemaArray(this);
|
|
114
|
+
}
|
|
115
|
+
// eslint-disable-next-line complexity
|
|
100
116
|
parse(input, options) {
|
|
101
|
-
var _a;
|
|
117
|
+
var _a, _b;
|
|
102
118
|
for (const preProcess of this.preProcess) {
|
|
103
119
|
input = preProcess(input);
|
|
104
120
|
}
|
|
121
|
+
const result = this.parseSubItems(input, options);
|
|
122
|
+
if ((_a = result.errors) == null ? void 0 : _a.length) {
|
|
123
|
+
return {
|
|
124
|
+
valid: false,
|
|
125
|
+
errors: result.errors
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
input = result.input;
|
|
105
129
|
if (!this.isOfType(input)) {
|
|
106
130
|
return {
|
|
107
131
|
valid: false,
|
|
108
132
|
errors: [{ message: "invalid type" }]
|
|
109
133
|
};
|
|
110
134
|
}
|
|
111
|
-
const
|
|
135
|
+
const validationErrors = [];
|
|
112
136
|
for (const validation of this.validations) {
|
|
113
137
|
if (!validation.fn(input)) {
|
|
114
|
-
|
|
115
|
-
message: (
|
|
138
|
+
validationErrors.push({
|
|
139
|
+
message: (_b = validation.error) != null ? _b : this.invalidError
|
|
116
140
|
});
|
|
117
141
|
if (options == null ? void 0 : options.fast) {
|
|
118
|
-
|
|
119
|
-
valid: false,
|
|
120
|
-
object: input,
|
|
121
|
-
errors
|
|
122
|
-
};
|
|
142
|
+
break;
|
|
123
143
|
}
|
|
124
144
|
}
|
|
125
145
|
}
|
|
126
|
-
if (
|
|
146
|
+
if (validationErrors.length > 0) {
|
|
127
147
|
return {
|
|
128
148
|
valid: false,
|
|
129
149
|
object: input,
|
|
130
|
-
errors
|
|
150
|
+
errors: validationErrors
|
|
131
151
|
};
|
|
132
152
|
}
|
|
133
153
|
for (const postProcess of this.postProcess) {
|
|
@@ -162,12 +182,43 @@ var _SchemaItem = class _SchemaItem {
|
|
|
162
182
|
this.postProcess.push(fn);
|
|
163
183
|
return this;
|
|
164
184
|
}
|
|
185
|
+
/** Returns the sub values of the input, the schema item to handle them, and the path to it */
|
|
186
|
+
getSubInputs(_input) {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
/** Sets the "part" of the input that this sub schema handles. Overrides the input with the result */
|
|
190
|
+
updateSubItemInput(path, input, value) {
|
|
191
|
+
if (path && isObject(input)) {
|
|
192
|
+
objectSet(input, path.split("."), value);
|
|
193
|
+
return input;
|
|
194
|
+
} else {
|
|
195
|
+
return value;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
parseSubItems(input, options) {
|
|
199
|
+
const result = { errors: [], input };
|
|
200
|
+
for (const { item, value, path } of this.getSubInputs(result.input)) {
|
|
201
|
+
const parsed = item.parse(value, options);
|
|
202
|
+
if (parsed.valid) {
|
|
203
|
+
result.input = this.updateSubItemInput(path, result.input, parsed.object);
|
|
204
|
+
} else {
|
|
205
|
+
for (const error of parsed.errors) {
|
|
206
|
+
error.field = error.field ? `${path}.${error.field}` : path;
|
|
207
|
+
result.errors.push(error);
|
|
208
|
+
}
|
|
209
|
+
if (options == null ? void 0 : options.fast) {
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
165
216
|
};
|
|
166
217
|
__decorateClass([
|
|
167
|
-
|
|
168
|
-
], _SchemaItem.prototype, "
|
|
218
|
+
parsable()
|
|
219
|
+
], _SchemaItem.prototype, "parseJSON", 1);
|
|
169
220
|
__decorateClass([
|
|
170
|
-
|
|
221
|
+
parsable()
|
|
171
222
|
], _SchemaItem.prototype, "in", 1);
|
|
172
223
|
var SchemaItem = _SchemaItem;
|
|
173
224
|
|
|
@@ -185,77 +236,37 @@ var SchemaArray = class extends SchemaItem {
|
|
|
185
236
|
this.addPreProcess((it) => typeof it === "string" ? it.split(separator) : it);
|
|
186
237
|
return this;
|
|
187
238
|
}
|
|
188
|
-
parse(input, options) {
|
|
189
|
-
const { valid, object, errors = [] } = super.parse(input, options);
|
|
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
|
-
};
|
|
230
|
-
}
|
|
231
239
|
unwrap() {
|
|
232
240
|
return this.values;
|
|
233
241
|
}
|
|
234
242
|
isOfType(input) {
|
|
235
|
-
return Array.isArray(input);
|
|
243
|
+
return Array.isArray(input) && input.every((entry) => this.values.isOfType(entry));
|
|
244
|
+
}
|
|
245
|
+
getSubInputs(input) {
|
|
246
|
+
return Array.isArray(input) ? input.map((entry, index) => ({ item: this.values, value: entry, path: index.toString() })) : [];
|
|
236
247
|
}
|
|
237
248
|
};
|
|
238
249
|
__decorateClass([
|
|
239
|
-
|
|
250
|
+
parsable()
|
|
240
251
|
], SchemaArray.prototype, "unique", 1);
|
|
241
252
|
__decorateClass([
|
|
242
|
-
|
|
253
|
+
parsable()
|
|
243
254
|
], SchemaArray.prototype, "split", 1);
|
|
244
255
|
|
|
245
256
|
// src/items/boolean.ts
|
|
246
257
|
var SchemaBoolean = class extends SchemaItem {
|
|
247
258
|
parseString(trueValue = "true", falseValue = "false") {
|
|
248
|
-
this.addPreProcess((
|
|
249
|
-
if (typeof
|
|
250
|
-
return
|
|
259
|
+
this.addPreProcess((input) => {
|
|
260
|
+
if (typeof input !== "string") {
|
|
261
|
+
return input;
|
|
251
262
|
}
|
|
252
|
-
if (
|
|
263
|
+
if (input === trueValue) {
|
|
253
264
|
return true;
|
|
254
265
|
}
|
|
255
|
-
if (
|
|
266
|
+
if (input === falseValue) {
|
|
256
267
|
return false;
|
|
257
268
|
}
|
|
258
|
-
return
|
|
269
|
+
return input;
|
|
259
270
|
});
|
|
260
271
|
return this;
|
|
261
272
|
}
|
|
@@ -264,7 +275,7 @@ var SchemaBoolean = class extends SchemaItem {
|
|
|
264
275
|
}
|
|
265
276
|
};
|
|
266
277
|
__decorateClass([
|
|
267
|
-
|
|
278
|
+
parsable()
|
|
268
279
|
], SchemaBoolean.prototype, "parseString", 1);
|
|
269
280
|
|
|
270
281
|
// src/items/nullable.ts
|
|
@@ -273,76 +284,43 @@ var SchemaNullable = class extends SchemaItem {
|
|
|
273
284
|
super([child]);
|
|
274
285
|
this.child = child;
|
|
275
286
|
}
|
|
276
|
-
|
|
287
|
+
falsyAsNull() {
|
|
277
288
|
this.addPreProcess((it) => !it ? void 0 : it);
|
|
278
289
|
return this;
|
|
279
290
|
}
|
|
280
291
|
unwrap() {
|
|
281
292
|
return this.child;
|
|
282
293
|
}
|
|
283
|
-
parse(input, options) {
|
|
284
|
-
const res = super.parse(input, options);
|
|
285
|
-
if (!res.valid) {
|
|
286
|
-
return this.child.parse(input, options);
|
|
287
|
-
}
|
|
288
|
-
if (this.isNull(res.object)) {
|
|
289
|
-
return {
|
|
290
|
-
valid: true,
|
|
291
|
-
object: void 0
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
return this.child.parse(input, options);
|
|
295
|
-
}
|
|
296
294
|
isOfType(input) {
|
|
297
|
-
return
|
|
295
|
+
return typeof input === "undefined" || this.child.isOfType(input);
|
|
296
|
+
}
|
|
297
|
+
getSubInputs(input) {
|
|
298
|
+
return [{ value: input, item: this.child, path: void 0 }];
|
|
298
299
|
}
|
|
299
|
-
|
|
300
|
-
|
|
300
|
+
parseSubItems(input, options) {
|
|
301
|
+
if (isNull(input)) {
|
|
302
|
+
return { input: void 0 };
|
|
303
|
+
}
|
|
304
|
+
return super.parseSubItems(input, options);
|
|
301
305
|
}
|
|
302
306
|
};
|
|
303
307
|
__decorateClass([
|
|
304
|
-
|
|
305
|
-
], SchemaNullable.prototype, "
|
|
308
|
+
parsable()
|
|
309
|
+
], SchemaNullable.prototype, "falsyAsNull", 1);
|
|
306
310
|
|
|
307
311
|
// src/items/object.ts
|
|
308
|
-
import { isObject,
|
|
312
|
+
import { isObject as isObject2, objectFind, objectMap } from "@dzeio/object-util";
|
|
309
313
|
var SchemaObject = class extends SchemaItem {
|
|
310
314
|
constructor(model) {
|
|
311
315
|
super([model]);
|
|
312
316
|
this.model = model;
|
|
313
317
|
this.id = "object";
|
|
314
318
|
}
|
|
315
|
-
parse(input, options) {
|
|
316
|
-
const { valid, object, errors = [] } = super.parse(input, options);
|
|
317
|
-
if (!valid) {
|
|
318
|
-
return {
|
|
319
|
-
valid,
|
|
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
|
-
};
|
|
343
|
-
}
|
|
344
319
|
isOfType(input) {
|
|
345
|
-
return
|
|
320
|
+
return isObject2(input) && !objectFind(this.model, (item, key) => !item.isOfType(input[key]));
|
|
321
|
+
}
|
|
322
|
+
getSubInputs(input) {
|
|
323
|
+
return isObject2(input) ? objectMap(this.model, (schema, key) => ({ item: schema, value: input[key], path: key.toString() })) : [];
|
|
346
324
|
}
|
|
347
325
|
};
|
|
348
326
|
|
|
@@ -350,18 +328,23 @@ var SchemaObject = class extends SchemaItem {
|
|
|
350
328
|
function isNull(value) {
|
|
351
329
|
return typeof value === "undefined" || value === null;
|
|
352
330
|
}
|
|
353
|
-
function parseQuery(
|
|
331
|
+
function parseQuery(schema, query, opts) {
|
|
354
332
|
const record = {};
|
|
355
333
|
for (const [key, value] of query) {
|
|
356
|
-
|
|
334
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
335
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
336
|
+
const finalValue = schemaItem instanceof SchemaArray ? query.getAll(key) : value;
|
|
337
|
+
objectSet2(record, keys, finalValue);
|
|
357
338
|
}
|
|
358
|
-
return
|
|
339
|
+
return schema.parse(record, opts);
|
|
359
340
|
}
|
|
360
|
-
function parseFormData(
|
|
341
|
+
function parseFormData(schema, data, opts) {
|
|
361
342
|
const record = {};
|
|
362
343
|
for (const [key, value] of data) {
|
|
363
|
-
const
|
|
364
|
-
|
|
344
|
+
const keys = key.split(".").map((it) => /^\d+$/g.test(it) ? Number.parseInt(it, 10) : it);
|
|
345
|
+
const schemaItem = getSchemaItemAtPath(schema, keys);
|
|
346
|
+
const finalValue = schemaItem instanceof SchemaArray ? data.getAll(key) : value;
|
|
347
|
+
objectSet2(record, keys, finalValue);
|
|
365
348
|
}
|
|
366
349
|
const handleBoolean = (value, keys) => {
|
|
367
350
|
var _a;
|
|
@@ -378,66 +361,87 @@ function parseFormData(model, data, opts) {
|
|
|
378
361
|
handleSchemaForBoolean(value.model, keys);
|
|
379
362
|
}
|
|
380
363
|
if (value instanceof SchemaBoolean) {
|
|
381
|
-
|
|
364
|
+
objectSet2(record, keys, !!data.get(keys.join(".")));
|
|
382
365
|
}
|
|
383
366
|
};
|
|
384
|
-
const handleSchemaForBoolean = (
|
|
385
|
-
|
|
367
|
+
const handleSchemaForBoolean = (model, keys = []) => {
|
|
368
|
+
objectLoop(model, (value, key) => {
|
|
386
369
|
handleBoolean(value, [...keys, key]);
|
|
387
370
|
});
|
|
388
371
|
};
|
|
389
|
-
if (
|
|
390
|
-
handleSchemaForBoolean(
|
|
372
|
+
if (schema instanceof SchemaObject) {
|
|
373
|
+
handleSchemaForBoolean(schema.model);
|
|
391
374
|
}
|
|
392
|
-
return
|
|
375
|
+
return schema.parse(record, opts);
|
|
393
376
|
}
|
|
394
377
|
function parseForm(model, form, opts) {
|
|
395
378
|
return parseFormData(model, new FormData(form), opts);
|
|
396
379
|
}
|
|
380
|
+
function getSchemaItemAtPath(schema, path) {
|
|
381
|
+
let current = schema;
|
|
382
|
+
const internalPath = objectClone(path);
|
|
383
|
+
for (const key of path) {
|
|
384
|
+
if (!current) {
|
|
385
|
+
return void 0;
|
|
386
|
+
}
|
|
387
|
+
internalPath.shift();
|
|
388
|
+
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
389
|
+
current = current.unwrap();
|
|
390
|
+
}
|
|
391
|
+
if (current instanceof SchemaObject) {
|
|
392
|
+
current = current.model[key];
|
|
393
|
+
current = getSchemaItemAtPath(current, internalPath);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
while (current instanceof SchemaNullable || current instanceof SchemaDefault) {
|
|
397
|
+
current = current.unwrap();
|
|
398
|
+
}
|
|
399
|
+
return current;
|
|
400
|
+
}
|
|
397
401
|
|
|
398
402
|
// src/items/any.ts
|
|
399
403
|
var SchemaAny = class extends SchemaItem {
|
|
400
|
-
|
|
401
|
-
isOfType(input) {
|
|
404
|
+
isOfType(_input) {
|
|
402
405
|
return true;
|
|
403
406
|
}
|
|
404
407
|
};
|
|
405
408
|
|
|
406
409
|
// src/items/date.ts
|
|
407
410
|
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;
|
|
411
|
+
parseString(format = "default") {
|
|
412
|
+
this.addPreProcess((input) => {
|
|
413
|
+
if (typeof input !== "string") {
|
|
414
|
+
return input;
|
|
423
415
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
416
|
+
let date;
|
|
417
|
+
switch (format) {
|
|
418
|
+
case "default": {
|
|
419
|
+
date = new Date(input);
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
case "yy-mm-dd":
|
|
423
|
+
case "iso8601": {
|
|
424
|
+
const splitted = input.split("-").map((it) => Number.parseInt(it, 10));
|
|
425
|
+
if (splitted.length !== 3) {
|
|
426
|
+
break;
|
|
428
427
|
}
|
|
428
|
+
date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
case "jj/mm/yy": {
|
|
429
432
|
const splitted = input.split("/").map((it) => Number.parseInt(it, 10));
|
|
430
433
|
if (splitted.length !== 3) {
|
|
431
|
-
|
|
434
|
+
break;
|
|
432
435
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
return date;
|
|
438
|
-
});
|
|
436
|
+
date = new Date(splitted[2], splitted[1] - 1, splitted[0]);
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
439
|
}
|
|
440
|
-
|
|
440
|
+
if (!date || isNaN(date.getDate())) {
|
|
441
|
+
return input;
|
|
442
|
+
}
|
|
443
|
+
return date;
|
|
444
|
+
});
|
|
441
445
|
return this;
|
|
442
446
|
}
|
|
443
447
|
isOfType(input) {
|
|
@@ -445,49 +449,41 @@ var SchemaDate = class extends SchemaItem {
|
|
|
445
449
|
}
|
|
446
450
|
};
|
|
447
451
|
__decorateClass([
|
|
448
|
-
|
|
452
|
+
parsable()
|
|
449
453
|
], SchemaDate.prototype, "parseString", 1);
|
|
450
454
|
|
|
451
455
|
// src/items/record.ts
|
|
452
|
-
import { isObject as
|
|
456
|
+
import { isObject as isObject3, objectLoop as objectLoop2 } from "@dzeio/object-util";
|
|
453
457
|
var SchemaRecord = class extends SchemaItem {
|
|
454
458
|
constructor(keys, values) {
|
|
455
459
|
super([keys, values]);
|
|
456
460
|
this.keys = keys;
|
|
457
461
|
this.values = values;
|
|
458
462
|
}
|
|
459
|
-
parse(input, options) {
|
|
460
|
-
const { valid, object, errors = [] } = super.parse(input, options);
|
|
461
|
-
if (!valid) {
|
|
462
|
-
return {
|
|
463
|
-
valid,
|
|
464
|
-
object,
|
|
465
|
-
errors
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
const clone = {};
|
|
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
|
-
};
|
|
488
|
-
}
|
|
489
463
|
isOfType(input) {
|
|
490
|
-
return
|
|
464
|
+
return isObject3(input) && Object.prototype.toString.call(input) === "[object Object]";
|
|
465
|
+
}
|
|
466
|
+
parseSubItems(input, options) {
|
|
467
|
+
const result = { input, errors: [] };
|
|
468
|
+
if (isObject3(input)) {
|
|
469
|
+
const clone = {};
|
|
470
|
+
objectLoop2(input, (value, key) => {
|
|
471
|
+
var _a, _b;
|
|
472
|
+
const res1 = this.keys.parse(key);
|
|
473
|
+
const res2 = this.values.parse(value);
|
|
474
|
+
if (!res1.valid || !res2.valid) {
|
|
475
|
+
result.errors.push(...((_a = res1.errors) != null ? _a : []).concat(...(_b = res2.errors) != null ? _b : []).map((it) => ({
|
|
476
|
+
message: it.message,
|
|
477
|
+
field: it.field ? `${key}.${it.field}` : key.toString()
|
|
478
|
+
})));
|
|
479
|
+
} else {
|
|
480
|
+
clone[res1.object] = res2.object;
|
|
481
|
+
}
|
|
482
|
+
return result.errors.length === 0 || !(options == null ? void 0 : options.fast);
|
|
483
|
+
});
|
|
484
|
+
result.input = clone;
|
|
485
|
+
}
|
|
486
|
+
return result;
|
|
491
487
|
}
|
|
492
488
|
};
|
|
493
489
|
|
|
@@ -562,9 +558,7 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
562
558
|
return this;
|
|
563
559
|
}
|
|
564
560
|
parseString() {
|
|
565
|
-
this.addPreProcess(
|
|
566
|
-
(input) => typeof input === "string" ? Number.parseFloat(input) : input
|
|
567
|
-
);
|
|
561
|
+
this.addPreProcess((input) => typeof input === "string" ? Number.parseFloat(input) : input);
|
|
568
562
|
return this;
|
|
569
563
|
}
|
|
570
564
|
isOfType(input) {
|
|
@@ -578,19 +572,19 @@ var SchemaNumber = class extends SchemaItem {
|
|
|
578
572
|
}
|
|
579
573
|
};
|
|
580
574
|
__decorateClass([
|
|
581
|
-
|
|
575
|
+
parsable()
|
|
582
576
|
], SchemaNumber.prototype, "lte", 1);
|
|
583
577
|
__decorateClass([
|
|
584
|
-
|
|
578
|
+
parsable()
|
|
585
579
|
], SchemaNumber.prototype, "gte", 1);
|
|
586
580
|
__decorateClass([
|
|
587
|
-
|
|
581
|
+
parsable()
|
|
588
582
|
], SchemaNumber.prototype, "lt", 1);
|
|
589
583
|
__decorateClass([
|
|
590
|
-
|
|
584
|
+
parsable()
|
|
591
585
|
], SchemaNumber.prototype, "gt", 1);
|
|
592
586
|
__decorateClass([
|
|
593
|
-
|
|
587
|
+
parsable()
|
|
594
588
|
], SchemaNumber.prototype, "parseString", 1);
|
|
595
589
|
|
|
596
590
|
// src/items/string.ts
|
|
@@ -647,57 +641,167 @@ var SchemaString = class extends SchemaItem {
|
|
|
647
641
|
}
|
|
648
642
|
};
|
|
649
643
|
__decorateClass([
|
|
650
|
-
|
|
644
|
+
parsable()
|
|
651
645
|
], SchemaString.prototype, "min", 1);
|
|
652
646
|
__decorateClass([
|
|
653
|
-
|
|
647
|
+
parsable()
|
|
654
648
|
], SchemaString.prototype, "toCasing", 1);
|
|
655
649
|
__decorateClass([
|
|
656
|
-
|
|
650
|
+
parsable()
|
|
657
651
|
], SchemaString.prototype, "max", 1);
|
|
658
652
|
__decorateClass([
|
|
659
|
-
|
|
653
|
+
parsable()
|
|
660
654
|
], SchemaString.prototype, "notEmpty", 1);
|
|
661
655
|
__decorateClass([
|
|
662
|
-
|
|
656
|
+
parsable()
|
|
663
657
|
], SchemaString.prototype, "regex", 1);
|
|
664
658
|
|
|
665
659
|
// src/items/union.ts
|
|
666
660
|
var SchemaUnion = class extends SchemaItem {
|
|
667
661
|
constructor(...schemas) {
|
|
668
|
-
super();
|
|
662
|
+
super(schemas);
|
|
669
663
|
this.schemas = schemas;
|
|
670
664
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
object,
|
|
677
|
-
errors
|
|
678
|
-
};
|
|
679
|
-
}
|
|
665
|
+
isOfType(input) {
|
|
666
|
+
return this.schemas.some((it) => it.isOfType(input));
|
|
667
|
+
}
|
|
668
|
+
parseSubItems(input, options) {
|
|
669
|
+
const result = { input, errors: [] };
|
|
680
670
|
for (const schema of this.schemas) {
|
|
681
|
-
const validation = schema.parse(input, options);
|
|
671
|
+
const validation = schema.parse(result.input, options);
|
|
682
672
|
if (validation.valid) {
|
|
683
|
-
return validation;
|
|
673
|
+
return { input: validation.object };
|
|
684
674
|
} else {
|
|
685
|
-
errors.push(...validation.errors);
|
|
675
|
+
result.errors.push(...validation.errors);
|
|
686
676
|
}
|
|
687
677
|
}
|
|
688
|
-
return
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// src/items/intersection.ts
|
|
683
|
+
import { isObject as isObject4 } from "@dzeio/object-util";
|
|
684
|
+
var SchemaIntersection = class extends SchemaItem {
|
|
685
|
+
constructor(...schemas) {
|
|
686
|
+
super(schemas);
|
|
687
|
+
this.schemas = schemas;
|
|
693
688
|
}
|
|
694
689
|
isOfType(input) {
|
|
695
|
-
return this.schemas.
|
|
690
|
+
return this.schemas.every((it) => it.isOfType(input));
|
|
691
|
+
}
|
|
692
|
+
parseSubItems(input, options) {
|
|
693
|
+
const result = { input, errors: [] };
|
|
694
|
+
for (const schema of this.schemas) {
|
|
695
|
+
const validation = schema.parse(result.input, options);
|
|
696
|
+
if (!validation.valid) {
|
|
697
|
+
result.errors.push(...validation.errors);
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (isObject4(result.input)) {
|
|
701
|
+
Object.assign(result.input, validation.object);
|
|
702
|
+
} else {
|
|
703
|
+
result.input = validation.object;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return result;
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
// src/items/default.ts
|
|
711
|
+
var SchemaDefault = class extends SchemaItem {
|
|
712
|
+
constructor(child, defaultVal, strict = true) {
|
|
713
|
+
super([child, defaultVal, strict]);
|
|
714
|
+
this.child = child;
|
|
715
|
+
this.defaultVal = defaultVal;
|
|
716
|
+
this.strict = strict;
|
|
717
|
+
}
|
|
718
|
+
unwrap() {
|
|
719
|
+
return this.child;
|
|
720
|
+
}
|
|
721
|
+
isOfType(input) {
|
|
722
|
+
return this.child.isOfType(input);
|
|
723
|
+
}
|
|
724
|
+
getSubInputs(input) {
|
|
725
|
+
return [{ item: this.child, value: isNull(input) || !this.strict && !input ? this.defaultVal : input, path: void 0 }];
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
// src/items/file.ts
|
|
730
|
+
var SchemaFile = class extends SchemaItem {
|
|
731
|
+
constructor() {
|
|
732
|
+
super();
|
|
733
|
+
this.addPreProcess((input) => this.isOfType(input) && input.size > 0 ? input : void 0);
|
|
734
|
+
}
|
|
735
|
+
extension(ext, error) {
|
|
736
|
+
this.addValidation({ fn: (input) => input.name.endsWith(ext), error });
|
|
737
|
+
return this;
|
|
738
|
+
}
|
|
739
|
+
maxSize(size, error) {
|
|
740
|
+
this.addValidation({ fn: (file) => file.size <= size, error });
|
|
741
|
+
return this;
|
|
742
|
+
}
|
|
743
|
+
isOfType(input) {
|
|
744
|
+
return input instanceof File;
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
__decorateClass([
|
|
748
|
+
parsable()
|
|
749
|
+
], SchemaFile.prototype, "extension", 1);
|
|
750
|
+
__decorateClass([
|
|
751
|
+
parsable()
|
|
752
|
+
], SchemaFile.prototype, "maxSize", 1);
|
|
753
|
+
|
|
754
|
+
// src/items/tuple.ts
|
|
755
|
+
var SchemaTuple = class extends SchemaItem {
|
|
756
|
+
constructor(...children) {
|
|
757
|
+
super(children);
|
|
758
|
+
this.children = children;
|
|
759
|
+
}
|
|
760
|
+
isOfType(input) {
|
|
761
|
+
if (!Array.isArray(input) || input.length > this.items.length) {
|
|
762
|
+
return false;
|
|
763
|
+
}
|
|
764
|
+
for (const [index, item] of this.children.entries()) {
|
|
765
|
+
if (item instanceof SchemaItem && !item.isOfType(input[index])) {
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
getSubInputs(input) {
|
|
772
|
+
return Array.isArray(input) ? this.children.map((entry, index) => ({ value: input[index], item: entry, path: index.toString() })) : [];
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
// src/items/nullish.ts
|
|
777
|
+
var SchemaUndefined = class extends SchemaItem {
|
|
778
|
+
isOfType(input) {
|
|
779
|
+
return typeof input === "undefined";
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
var SchemaNull = class extends SchemaItem {
|
|
783
|
+
isOfType(input) {
|
|
784
|
+
return input === null;
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
var SchemaVoid = class extends SchemaItem {
|
|
788
|
+
isOfType(input) {
|
|
789
|
+
return typeof input === "undefined";
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
var SchemaNullish = class extends SchemaItem {
|
|
793
|
+
isOfType(input) {
|
|
794
|
+
return isNull(input);
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
var SchemaNever = class extends SchemaItem {
|
|
798
|
+
isOfType(_input) {
|
|
799
|
+
return false;
|
|
696
800
|
}
|
|
697
801
|
};
|
|
698
802
|
|
|
699
803
|
// src/Schema.ts
|
|
700
|
-
function
|
|
804
|
+
function parsable() {
|
|
701
805
|
return (target, propertyKey, descriptor) => {
|
|
702
806
|
if (!(propertyKey in target)) {
|
|
703
807
|
throw new Error("property not set in object");
|
|
@@ -714,6 +818,7 @@ function parceable() {
|
|
|
714
818
|
var Types = {
|
|
715
819
|
Any: SchemaAny,
|
|
716
820
|
Array: SchemaArray,
|
|
821
|
+
Tuple: SchemaTuple,
|
|
717
822
|
Boolean: SchemaBoolean,
|
|
718
823
|
Date: SchemaDate,
|
|
719
824
|
Enum: SchemaEnum,
|
|
@@ -722,7 +827,13 @@ var Types = {
|
|
|
722
827
|
Object: SchemaObject,
|
|
723
828
|
Record: SchemaRecord,
|
|
724
829
|
String: SchemaString,
|
|
725
|
-
Union: SchemaUnion
|
|
830
|
+
Union: SchemaUnion,
|
|
831
|
+
Intersection: SchemaIntersection,
|
|
832
|
+
Default: SchemaDefault,
|
|
833
|
+
Undefined: SchemaUndefined,
|
|
834
|
+
Void: SchemaVoid,
|
|
835
|
+
Null: SchemaNull,
|
|
836
|
+
Nullish: SchemaNullish
|
|
726
837
|
};
|
|
727
838
|
var _Schema = class _Schema extends SchemaObject {
|
|
728
839
|
static register(module) {
|
|
@@ -772,9 +883,33 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
772
883
|
static string(...inputs) {
|
|
773
884
|
return new SchemaString(...inputs);
|
|
774
885
|
}
|
|
886
|
+
static file(...inputs) {
|
|
887
|
+
return new SchemaFile(...inputs);
|
|
888
|
+
}
|
|
775
889
|
static union(...inputs) {
|
|
776
890
|
return new SchemaUnion(...inputs);
|
|
777
891
|
}
|
|
892
|
+
static intersection(...inputs) {
|
|
893
|
+
return new SchemaIntersection(...inputs);
|
|
894
|
+
}
|
|
895
|
+
static tuple(...inputs) {
|
|
896
|
+
return new SchemaTuple(...inputs);
|
|
897
|
+
}
|
|
898
|
+
static undefined(...inputs) {
|
|
899
|
+
return new SchemaUndefined(...inputs);
|
|
900
|
+
}
|
|
901
|
+
static null(...inputs) {
|
|
902
|
+
return new SchemaNull(...inputs);
|
|
903
|
+
}
|
|
904
|
+
static nullish(...inputs) {
|
|
905
|
+
return new SchemaNullish(...inputs);
|
|
906
|
+
}
|
|
907
|
+
static void(...inputs) {
|
|
908
|
+
return new SchemaVoid(...inputs);
|
|
909
|
+
}
|
|
910
|
+
static never(...inputs) {
|
|
911
|
+
return new SchemaNever(...inputs);
|
|
912
|
+
}
|
|
778
913
|
static fromJSON(json) {
|
|
779
914
|
var _a, _b, _c, _d, _e, _f;
|
|
780
915
|
const fn = this.getModule(json.i);
|
|
@@ -827,6 +962,7 @@ var _Schema = class _Schema extends SchemaObject {
|
|
|
827
962
|
};
|
|
828
963
|
_Schema.registeredModules = [
|
|
829
964
|
SchemaArray,
|
|
965
|
+
SchemaTuple,
|
|
830
966
|
SchemaBoolean,
|
|
831
967
|
SchemaDate,
|
|
832
968
|
SchemaEnum,
|
|
@@ -835,7 +971,13 @@ _Schema.registeredModules = [
|
|
|
835
971
|
SchemaObject,
|
|
836
972
|
SchemaRecord,
|
|
837
973
|
SchemaString,
|
|
838
|
-
SchemaUnion
|
|
974
|
+
SchemaUnion,
|
|
975
|
+
SchemaIntersection,
|
|
976
|
+
SchemaDefault,
|
|
977
|
+
SchemaUndefined,
|
|
978
|
+
SchemaVoid,
|
|
979
|
+
SchemaNull,
|
|
980
|
+
SchemaNullish
|
|
839
981
|
];
|
|
840
982
|
var Schema = _Schema;
|
|
841
983
|
var s = Schema;
|
|
@@ -844,19 +986,27 @@ export {
|
|
|
844
986
|
SchemaArray,
|
|
845
987
|
SchemaBoolean,
|
|
846
988
|
SchemaDate,
|
|
989
|
+
SchemaDefault,
|
|
847
990
|
SchemaEnum,
|
|
991
|
+
SchemaFile,
|
|
992
|
+
SchemaIntersection,
|
|
848
993
|
SchemaItem,
|
|
849
994
|
SchemaLiteral,
|
|
995
|
+
SchemaNever,
|
|
996
|
+
SchemaNull,
|
|
850
997
|
SchemaNullable,
|
|
998
|
+
SchemaNullish,
|
|
851
999
|
SchemaNumber,
|
|
852
1000
|
SchemaObject,
|
|
853
1001
|
SchemaRecord,
|
|
854
1002
|
SchemaString,
|
|
1003
|
+
SchemaUndefined,
|
|
855
1004
|
SchemaUnion,
|
|
1005
|
+
SchemaVoid,
|
|
856
1006
|
Types,
|
|
857
1007
|
Schema as default,
|
|
858
1008
|
isNull,
|
|
859
|
-
|
|
1009
|
+
parsable,
|
|
860
1010
|
parseForm,
|
|
861
1011
|
parseFormData,
|
|
862
1012
|
parseQuery,
|