@zhin.js/schema 1.0.71 → 1.0.72
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/lib/schema.js +14 -3
- package/lib/validate.js +10 -0
- package/package.json +1 -1
package/lib/schema.js
CHANGED
|
@@ -188,7 +188,7 @@ export class Schema {
|
|
|
188
188
|
Schema.formatters = new Map();
|
|
189
189
|
function checkDefault(schema, key, value, fallback = value) {
|
|
190
190
|
if (isEmpty(value)) {
|
|
191
|
-
value = schema.meta.default
|
|
191
|
+
value = schema.meta.default ?? fallback;
|
|
192
192
|
}
|
|
193
193
|
const validateType = (schema, key, value) => {
|
|
194
194
|
switch (schema.meta.type) {
|
|
@@ -319,6 +319,8 @@ Schema.extend('regexp', function (key, value) {
|
|
|
319
319
|
});
|
|
320
320
|
Schema.extend('date', function (key, value) {
|
|
321
321
|
value = Schema.checkDefault(this, key, value);
|
|
322
|
+
if (typeof value === 'undefined')
|
|
323
|
+
return value;
|
|
322
324
|
return new Date(value);
|
|
323
325
|
});
|
|
324
326
|
Schema.extend('const', function (key, value) {
|
|
@@ -330,17 +332,26 @@ Schema.extend('const', function (key, value) {
|
|
|
330
332
|
});
|
|
331
333
|
Schema.extend('tuple', function (key, value) {
|
|
332
334
|
value = Schema.checkDefault(this, key, value, []);
|
|
335
|
+
if (value.length > 0 && value.length !== this.options.list.length)
|
|
336
|
+
throw new TypeError(`${key} tuple length mismatch`);
|
|
333
337
|
return value.map((item, index) => this.options.list[index](item, `${key}[${index}]`));
|
|
334
338
|
});
|
|
335
339
|
Schema.extend('union', function (key, value) {
|
|
336
340
|
value = Schema.checkDefault(this, key, value);
|
|
341
|
+
let lastError;
|
|
342
|
+
const issues = [];
|
|
337
343
|
for (const schema of this.options.list) {
|
|
338
344
|
try {
|
|
339
345
|
return schema(value, key);
|
|
340
346
|
}
|
|
341
|
-
catch {
|
|
347
|
+
catch (e) {
|
|
348
|
+
lastError = e;
|
|
349
|
+
issues.push(e instanceof Error ? e.message : String(e));
|
|
350
|
+
}
|
|
342
351
|
}
|
|
343
|
-
|
|
352
|
+
const error = new Error(`${key} union type not match: ${issues.join('; ')}`);
|
|
353
|
+
error.lastError = lastError;
|
|
354
|
+
throw error;
|
|
344
355
|
});
|
|
345
356
|
Schema.extend('intersect', function (key, value) {
|
|
346
357
|
value = Schema.checkDefault(this, key, value);
|
package/lib/validate.js
CHANGED
|
@@ -127,7 +127,17 @@ function validateOnly(schema, path, value) {
|
|
|
127
127
|
return current;
|
|
128
128
|
}
|
|
129
129
|
case 'regexp':
|
|
130
|
+
if (value !== undefined && typeof value !== 'string' && !(value instanceof RegExp)) {
|
|
131
|
+
throw issue(path, `${path} is not a RegExp|string`);
|
|
132
|
+
}
|
|
133
|
+
return value;
|
|
130
134
|
case 'date':
|
|
135
|
+
if (value !== undefined && typeof value !== 'number' && !(value instanceof Date)) {
|
|
136
|
+
throw issue(path, `${path} is not a Date|number`);
|
|
137
|
+
}
|
|
138
|
+
if (value instanceof Date && isNaN(value.getTime())) {
|
|
139
|
+
throw issue(path, `${path} is not a valid Date`);
|
|
140
|
+
}
|
|
131
141
|
return value;
|
|
132
142
|
default:
|
|
133
143
|
throw issue(path, `${path} has unsupported schema type ${schema.meta.type}`);
|