@bedrockio/yada 1.4.1 → 1.4.2
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/CHANGELOG.md +4 -0
- package/dist/cjs/Schema.js +34 -25
- package/dist/cjs/array.js +3 -0
- package/dist/cjs/errors.js +0 -3
- package/dist/cjs/number.js +3 -0
- package/dist/cjs/object.js +3 -0
- package/dist/cjs/string.js +3 -0
- package/package.json +1 -1
- package/src/Schema.js +42 -26
- package/src/array.js +2 -0
- package/src/errors.js +0 -4
- package/src/number.js +2 -0
- package/src/object.js +2 -0
- package/src/string.js +2 -0
- package/types/Schema.d.ts.map +1 -1
- package/types/array.d.ts.map +1 -1
- package/types/errors.d.ts +0 -1
- package/types/errors.d.ts.map +1 -1
- package/types/number.d.ts.map +1 -1
- package/types/object.d.ts.map +1 -1
- package/types/string.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cjs/Schema.js
CHANGED
|
@@ -9,6 +9,9 @@ var _lodash = require("lodash");
|
|
|
9
9
|
var _errors = require("./errors");
|
|
10
10
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform', 'empty'];
|
|
11
11
|
const REQUIRED_TYPES = ['default', 'required', 'missing'];
|
|
12
|
+
function isSchema(arg) {
|
|
13
|
+
return arg instanceof Schema;
|
|
14
|
+
}
|
|
12
15
|
class Schema {
|
|
13
16
|
constructor(meta = {}) {
|
|
14
17
|
this.assertions = [];
|
|
@@ -289,18 +292,11 @@ class Schema {
|
|
|
289
292
|
if (set.length === 1 && Array.isArray(set[0])) {
|
|
290
293
|
set = set[0];
|
|
291
294
|
}
|
|
292
|
-
const types = set.map(el => {
|
|
293
|
-
if (!isSchema(el)) {
|
|
294
|
-
el = JSON.stringify(el);
|
|
295
|
-
}
|
|
296
|
-
return el;
|
|
297
|
-
});
|
|
298
|
-
const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
|
|
299
295
|
return this.clone({
|
|
300
296
|
enum: set
|
|
301
297
|
}).assert('enum', async (val, options) => {
|
|
302
298
|
if (val !== undefined) {
|
|
303
|
-
|
|
299
|
+
let captured = [];
|
|
304
300
|
for (let el of set) {
|
|
305
301
|
if (isSchema(el)) {
|
|
306
302
|
try {
|
|
@@ -310,28 +306,28 @@ class Schema {
|
|
|
310
306
|
options = (0, _lodash.omit)(options, 'cast');
|
|
311
307
|
return await el.validate(val, options);
|
|
312
308
|
} catch (err) {
|
|
313
|
-
|
|
314
|
-
if (first instanceof _errors.TypeError && first.isPrimitiveKind()) {
|
|
315
|
-
// If the error is a simple primitive type error then
|
|
316
|
-
// continue to show more meaningful messages for simple enums.
|
|
317
|
-
continue;
|
|
318
|
-
} else {
|
|
319
|
-
// Otherwise capture the error to surface messages on
|
|
320
|
-
// more complex schemas.
|
|
321
|
-
captured.push(err);
|
|
322
|
-
}
|
|
309
|
+
captured.push(err.details[0]);
|
|
323
310
|
}
|
|
324
311
|
} else if (el === val === allow) {
|
|
325
312
|
return;
|
|
326
313
|
}
|
|
327
314
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
315
|
+
captured = (0, _lodash.uniqBy)(captured, 'message');
|
|
316
|
+
const isTypeErrors = captured.every(error => {
|
|
317
|
+
return error instanceof _errors.TypeError;
|
|
318
|
+
});
|
|
319
|
+
if (captured.length === 1) {
|
|
320
|
+
throw captured[0];
|
|
321
|
+
}
|
|
322
|
+
if (captured.length === 0 || isTypeErrors) {
|
|
323
|
+
assertTypes({
|
|
324
|
+
set,
|
|
325
|
+
allow
|
|
333
326
|
});
|
|
334
327
|
}
|
|
328
|
+
throw new _errors.AllowedError(this.meta.message, captured.filter(error => {
|
|
329
|
+
return error instanceof _errors.TypeError ? false : true;
|
|
330
|
+
}));
|
|
335
331
|
}
|
|
336
332
|
});
|
|
337
333
|
}
|
|
@@ -440,6 +436,19 @@ class Schema {
|
|
|
440
436
|
}
|
|
441
437
|
}
|
|
442
438
|
exports.default = Schema;
|
|
443
|
-
function
|
|
444
|
-
|
|
439
|
+
function assertTypes(options) {
|
|
440
|
+
const {
|
|
441
|
+
set,
|
|
442
|
+
allow
|
|
443
|
+
} = options;
|
|
444
|
+
const types = set.map(el => {
|
|
445
|
+
if (!isSchema(el)) {
|
|
446
|
+
el = JSON.stringify(el);
|
|
447
|
+
}
|
|
448
|
+
return el;
|
|
449
|
+
});
|
|
450
|
+
const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
|
|
451
|
+
throw new _errors.LocalizedError(message, {
|
|
452
|
+
types: types.join(', ')
|
|
453
|
+
});
|
|
445
454
|
}
|
package/dist/cjs/array.js
CHANGED
package/dist/cjs/errors.js
CHANGED
package/dist/cjs/number.js
CHANGED
package/dist/cjs/object.js
CHANGED
package/dist/cjs/string.js
CHANGED
package/package.json
CHANGED
package/src/Schema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { omit } from 'lodash';
|
|
1
|
+
import { omit, uniqBy } from 'lodash';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
TypeError,
|
|
@@ -12,6 +12,10 @@ import {
|
|
|
12
12
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform', 'empty'];
|
|
13
13
|
const REQUIRED_TYPES = ['default', 'required', 'missing'];
|
|
14
14
|
|
|
15
|
+
export function isSchema(arg) {
|
|
16
|
+
return arg instanceof Schema;
|
|
17
|
+
}
|
|
18
|
+
|
|
15
19
|
export default class Schema {
|
|
16
20
|
constructor(meta = {}) {
|
|
17
21
|
this.assertions = [];
|
|
@@ -271,16 +275,10 @@ export default class Schema {
|
|
|
271
275
|
if (set.length === 1 && Array.isArray(set[0])) {
|
|
272
276
|
set = set[0];
|
|
273
277
|
}
|
|
274
|
-
const types = set.map((el) => {
|
|
275
|
-
if (!isSchema(el)) {
|
|
276
|
-
el = JSON.stringify(el);
|
|
277
|
-
}
|
|
278
|
-
return el;
|
|
279
|
-
});
|
|
280
|
-
const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
|
|
281
278
|
return this.clone({ enum: set }).assert('enum', async (val, options) => {
|
|
282
279
|
if (val !== undefined) {
|
|
283
|
-
|
|
280
|
+
let captured = [];
|
|
281
|
+
|
|
284
282
|
for (let el of set) {
|
|
285
283
|
if (isSchema(el)) {
|
|
286
284
|
try {
|
|
@@ -290,28 +288,36 @@ export default class Schema {
|
|
|
290
288
|
options = omit(options, 'cast');
|
|
291
289
|
return await el.validate(val, options);
|
|
292
290
|
} catch (err) {
|
|
293
|
-
|
|
294
|
-
if (first instanceof TypeError && first.isPrimitiveKind()) {
|
|
295
|
-
// If the error is a simple primitive type error then
|
|
296
|
-
// continue to show more meaningful messages for simple enums.
|
|
297
|
-
continue;
|
|
298
|
-
} else {
|
|
299
|
-
// Otherwise capture the error to surface messages on
|
|
300
|
-
// more complex schemas.
|
|
301
|
-
captured.push(err);
|
|
302
|
-
}
|
|
291
|
+
captured.push(err.details[0]);
|
|
303
292
|
}
|
|
304
293
|
} else if ((el === val) === allow) {
|
|
305
294
|
return;
|
|
306
295
|
}
|
|
307
296
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
297
|
+
|
|
298
|
+
captured = uniqBy(captured, 'message');
|
|
299
|
+
|
|
300
|
+
const isTypeErrors = captured.every((error) => {
|
|
301
|
+
return error instanceof TypeError;
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
if (captured.length === 1) {
|
|
305
|
+
throw captured[0];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (captured.length === 0 || isTypeErrors) {
|
|
309
|
+
assertTypes({
|
|
310
|
+
set,
|
|
311
|
+
allow,
|
|
313
312
|
});
|
|
314
313
|
}
|
|
314
|
+
|
|
315
|
+
throw new AllowedError(
|
|
316
|
+
this.meta.message,
|
|
317
|
+
captured.filter((error) => {
|
|
318
|
+
return error instanceof TypeError ? false : true;
|
|
319
|
+
}),
|
|
320
|
+
);
|
|
315
321
|
}
|
|
316
322
|
});
|
|
317
323
|
}
|
|
@@ -418,6 +424,16 @@ export default class Schema {
|
|
|
418
424
|
}
|
|
419
425
|
}
|
|
420
426
|
|
|
421
|
-
|
|
422
|
-
|
|
427
|
+
function assertTypes(options) {
|
|
428
|
+
const { set, allow } = options;
|
|
429
|
+
const types = set.map((el) => {
|
|
430
|
+
if (!isSchema(el)) {
|
|
431
|
+
el = JSON.stringify(el);
|
|
432
|
+
}
|
|
433
|
+
return el;
|
|
434
|
+
});
|
|
435
|
+
const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
|
|
436
|
+
throw new LocalizedError(message, {
|
|
437
|
+
types: types.join(', '),
|
|
438
|
+
});
|
|
423
439
|
}
|
package/src/array.js
CHANGED
package/src/errors.js
CHANGED
package/src/number.js
CHANGED
package/src/object.js
CHANGED
package/src/string.js
CHANGED
package/types/Schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAcA,kDAEC;AAED;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,uBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,YAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDAwCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,MAAM,CAMlB;IAED,2BAYC;IAED;;MAOC;IAED;;;;MASC;IAED;;MAOC;IAED,4BAMC;IAED,kBAEC;IAED,YAGC;IAID;;OAEG;IACH,kCAFa,IAAI,CAmDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED,gEAQC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAYC;IAED,qBAsCC;CACF"}
|
package/types/array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"AAuIA;;;;;;;GAOG;AACH,8CALc,MAAM,EAAA,eAUnB;mBAlJkB,UAAU;AAI7B;IACE,0BAGC;IAED,cAsCC;IAED,0BAUC;IAED,uBAUC;IAED,uBAaC;IAED,eAaC;IAID,mBAEC;CAuBF;uBAlIsB,cAAc"}
|
package/types/errors.d.ts
CHANGED
package/types/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AAqIA,gEAEC;AApID;IACE,uCAEC;CACF;AAED;IACE,+BAA8C;IAE9C,+CAIC;IAFC,aAAwB;IACxB,eAAsB;IAGxB;;;;MAWC;IAED,kBAOC;IAED,kCAKC;CACF;AAED;IACE,yCAGC;CACF;AAED;IACE,qCAIC;IADC,UAAgB;IAGlB;;;;;MAKC;CACF;AAED;IACE,uCAIC;IADC,YAAoB;IAGtB;;;;;MAKC;CACF;AAED;IAGE,uDAIC;IADC,WAAkB;IAGpB;;;;;MAKC;CACF;AAED;IAGE,uDAIC;IADC,WAAkB;IAGpB;;;;;MAKC;CACF;AAED;IACE,wCAGC;CACF;AAED;IACE,wCAGC;CACF"}
|
package/types/number.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.js"],"names":[],"mappings":"AA0FA;;GAEG;AACH,iDAEC;AA5FD;IACE,cAWC;IAED;;;OAGG;IACH,SAHW,MAAM,YACN,MAAM,QAUhB;IAED;;;OAGG;IACH,SAHW,MAAM,YACN,MAAM,QAUhB;IAED,iBAEC;IAED,iBAEC;IAED,gBAMC;IAED,8BAQC;CAmBF;uBAxFsB,cAAc"}
|
package/types/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AAmTA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAQnB;wBArTY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IACE,uBAGC;IAED,cA8EC;IAED;;;;;;OAMG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAsB9B;IAED;;;;;;OAMG;IACH,cAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAY9B;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;;;;OAOG;IACH,mBAHc,MAAM,EAAA,gBAkBnB;IAED;;;;;;OAMG;IACH,cAEC;IAED;;;;;;;;;OASG;IACH,YAFW,SAAS,GAAC,MAAM,gBA+B1B;CAgBF;mBArQgC,UAAU;uBAFpB,cAAc"}
|
package/types/string.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"AAmbA;;GAEG;AACH,iDAEC;AAjaD;IACE,cAoBC;IAED;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;OAEG;IACH,eAFW,MAAM,QAUhB;IAED;;OAEG;IACH,YAFW,MAAM,QAUhB;IAED;;OAEG;IACH,YAFW,MAAM,QAUhB;IAED,aAIC;IAED;;OAEG;IACH,mBAFW,OAAO,QAYjB;IAED;;OAEG;IACH,mBAFW,OAAO,QAYjB;IAED;;OAEG;IACH,WAFW,MAAM,QAahB;IAED,cAMC;IAED,uBASC;IAED,YAMC;IAED,YAMC;IAED,aAMC;IAED,cAMC;IAED;;;OAGG;IACH,iBAFG;QAA0B,OAAO,GAAzB,OAAO;KAAmB,QAQpC;IAED,mBAMC;IAED,WAMC;IAED,gBAMC;IAED,eAMC;IAED,YAMC;IAED,aAOC;IAED,eAMC;IAED;;OAEG;IACH,oBAFW,MAAM,QAQhB;IAED,gBAMC;IAED;;;;;;;OAOG;IACH,mBANG;QAAyB,SAAS,GAA1B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,YAAY,GAA7B,MAAM;QACW,YAAY,GAA7B,MAAM;KAAwB,QA+BxC;IAED;;;;;;;;;;;OAWG;IACH,cAVG;QAA0B,gBAAgB,GAAlC,OAAO;QACW,sBAAsB,GAAxC,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,4BAA4B,GAA9C,OAAO;QACW,eAAe,GAAjC,OAAO;QACW,sBAAsB,GAAxC,OAAO;QACW,eAAe,GAAjC,OAAO;QACY,SAAS,GAA5B,MAAM,EAAE;KAAqB,QAQvC;IAED;;;;;;;;OAQG;IACH,iBAPG;QAA0B,WAAW,GAA7B,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,kBAAkB,GAApC,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,cAAc,GAAhC,OAAO;QACW,iBAAiB,GAAnC,OAAO;KAAmC,QAQpD;IAED;;OAEG;IACH,eAFW,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,QAQ3B;IAED,YAMC;IAED,YAMC;IAED,cAMC;IAED,cAMC;IAED,iCAWC;CAgBF;uBA/asB,cAAc"}
|