@bedrockio/yada 1.1.1 → 1.1.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.
@@ -260,6 +260,7 @@ class Schema {
260
260
  enum: set
261
261
  }).assert('enum', async (val, options) => {
262
262
  if (val !== undefined) {
263
+ const captured = [];
263
264
  for (let el of set) {
264
265
  if (isSchema(el)) {
265
266
  try {
@@ -270,23 +271,27 @@ class Schema {
270
271
  return await el.validate(val, options);
271
272
  } catch (err) {
272
273
  const [first] = err.details;
273
- if (first instanceof _errors.TypeError) {
274
- // If the error is a simple type error then continue
275
- // to show more meaningful messages for simple enums.
274
+ if (first instanceof _errors.TypeError && first.isPrimitiveKind()) {
275
+ // If the error is a simple primitive type error then
276
+ // continue to show more meaningful messages for simple enums.
276
277
  continue;
277
278
  } else {
278
- // Otherwise throw the error to surface messages on
279
+ // Otherwise capture the error to surface messages on
279
280
  // more complex schemas.
280
- throw err;
281
+ captured.push(err);
281
282
  }
282
283
  }
283
284
  } else if (el === val === allow) {
284
285
  return;
285
286
  }
286
287
  }
287
- throw new _errors.LocalizedError(message, {
288
- types: types.join(', ')
289
- });
288
+ if (captured.length) {
289
+ throw new _errors.AllowedError(this.meta.message, captured);
290
+ } else {
291
+ throw new _errors.LocalizedError(message, {
292
+ types: types.join(', ')
293
+ });
294
+ }
290
295
  }
291
296
  });
292
297
  }
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.ValidationError = exports.TypeError = exports.LocalizedError = exports.FormatError = exports.FieldError = exports.ElementError = exports.AssertionError = exports.ArrayError = void 0;
6
+ exports.ValidationError = exports.TypeError = exports.LocalizedError = exports.FormatError = exports.FieldError = exports.ElementError = exports.AssertionError = exports.ArrayError = exports.AllowedError = void 0;
7
7
  exports.isSchemaError = isSchemaError;
8
8
  var _messages = require("./messages");
9
9
  var _localization = require("./localization");
@@ -57,6 +57,9 @@ class TypeError extends ValidationError {
57
57
  this.type = 'type';
58
58
  this.kind = kind;
59
59
  }
60
+ isPrimitiveKind() {
61
+ return this.kind !== 'array' && this.kind !== 'object';
62
+ }
60
63
  toJSON() {
61
64
  return {
62
65
  ...super.toJSON(),
@@ -109,12 +112,18 @@ class ElementError extends ValidationError {
109
112
  exports.ElementError = ElementError;
110
113
  class ArrayError extends ValidationError {
111
114
  constructor(message, details) {
112
- super(message);
115
+ super(message, details);
113
116
  this.type = 'array';
114
- this.details = details;
115
117
  }
116
118
  }
117
119
  exports.ArrayError = ArrayError;
120
+ class AllowedError extends ValidationError {
121
+ constructor(message, details) {
122
+ super(message, details);
123
+ this.type = 'allowed';
124
+ }
125
+ }
126
+ exports.AllowedError = AllowedError;
118
127
  function isSchemaError(arg) {
119
128
  return arg instanceof ValidationError;
120
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/Schema.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  TypeError,
3
3
  FormatError,
4
+ AllowedError,
4
5
  AssertionError,
5
6
  LocalizedError,
6
7
  ValidationError,
@@ -243,6 +244,7 @@ export default class Schema {
243
244
  const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
244
245
  return this.clone({ enum: set }).assert('enum', async (val, options) => {
245
246
  if (val !== undefined) {
247
+ const captured = [];
246
248
  for (let el of set) {
247
249
  if (isSchema(el)) {
248
250
  try {
@@ -253,23 +255,27 @@ export default class Schema {
253
255
  return await el.validate(val, options);
254
256
  } catch (err) {
255
257
  const [first] = err.details;
256
- if (first instanceof TypeError) {
257
- // If the error is a simple type error then continue
258
- // to show more meaningful messages for simple enums.
258
+ if (first instanceof TypeError && first.isPrimitiveKind()) {
259
+ // If the error is a simple primitive type error then
260
+ // continue to show more meaningful messages for simple enums.
259
261
  continue;
260
262
  } else {
261
- // Otherwise throw the error to surface messages on
263
+ // Otherwise capture the error to surface messages on
262
264
  // more complex schemas.
263
- throw err;
265
+ captured.push(err);
264
266
  }
265
267
  }
266
268
  } else if ((el === val) === allow) {
267
269
  return;
268
270
  }
269
271
  }
270
- throw new LocalizedError(message, {
271
- types: types.join(', '),
272
- });
272
+ if (captured.length) {
273
+ throw new AllowedError(this.meta.message, captured);
274
+ } else {
275
+ throw new LocalizedError(message, {
276
+ types: types.join(', '),
277
+ });
278
+ }
273
279
  }
274
280
  });
275
281
  }
package/src/errors.js CHANGED
@@ -51,6 +51,10 @@ export class TypeError extends ValidationError {
51
51
  this.kind = kind;
52
52
  }
53
53
 
54
+ isPrimitiveKind() {
55
+ return this.kind !== 'array' && this.kind !== 'object';
56
+ }
57
+
54
58
  toJSON() {
55
59
  return {
56
60
  ...super.toJSON(),
@@ -106,9 +110,15 @@ export class ElementError extends ValidationError {
106
110
 
107
111
  export class ArrayError extends ValidationError {
108
112
  constructor(message, details) {
109
- super(message);
113
+ super(message, details);
110
114
  this.type = 'array';
111
- this.details = details;
115
+ }
116
+ }
117
+
118
+ export class AllowedError extends ValidationError {
119
+ constructor(message, details) {
120
+ super(message, details);
121
+ this.type = 'allowed';
112
122
  }
113
123
  }
114
124
 
@@ -1 +1 @@
1
- {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAqWA,4CAEC;AA3VD;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,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,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,iDA0CC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAWC;IAED;;MAOC;IAED;;;;MASC;IAED,kBAEC;IAED,4BAMC;IAID;;OAEG;IACH,kCAFa,IAAI,CA4ChB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAGC;IAED;;;;;;;;MAoCC;CACF"}
1
+ {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AA2WA,4CAEC;AAhWD;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,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,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,iDA0CC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAWC;IAED;;MAOC;IAED;;;;MASC;IAED,kBAEC;IAED,4BAMC;IAID;;OAEG;IACH,kCAFa,IAAI,CAiDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAGC;IAED;;;;;;;;MAoCC;CACF"}
package/types/errors.d.ts CHANGED
@@ -19,6 +19,7 @@ export class AssertionError extends ValidationError {
19
19
  export class TypeError extends ValidationError {
20
20
  constructor(message: any, kind: any);
21
21
  kind: any;
22
+ isPrimitiveKind(): boolean;
22
23
  toJSON(): {
23
24
  kind: any;
24
25
  details: any[];
@@ -58,6 +59,8 @@ export class ElementError extends ValidationError {
58
59
  }
59
60
  export class ArrayError extends ValidationError {
60
61
  constructor(message: any, details: any);
61
- details: any;
62
+ }
63
+ export class AllowedError extends ValidationError {
64
+ constructor(message: any, details: any);
62
65
  }
63
66
  //# sourceMappingURL=errors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AAkHA,iDAEC;AAjHD;IACE,uCAEC;CACF;AAED;IACE,uCAIC;IAFC,aAAwB;IACxB,eAAsB;IAGxB;;;;MAaC;IAED,kCAKC;CACF;AAED;IACE,yCAGC;CACF;AAED;IACE,qCAIC;IADC,UAAgB;IAGlB;;;;;MAKC;CACF;AAED;IACE,uCAIC;IADC,YAAoB;IAGtB;;;;;MAKC;CACF;AAED;IACE,oDAIC;IADC,WAAkB;IAGpB;;;;;MAKC;CACF;AAED;IACE,oDAIC;IADC,WAAkB;IAGpB;;;;;MAKC;CACF;AAED;IACE,wCAIC;IADC,aAAsB;CAEzB"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AA4HA,iDAEC;AA3HD;IACE,uCAEC;CACF;AAED;IACE,uCAIC;IAFC,aAAwB;IACxB,eAAsB;IAGxB;;;;MAaC;IAED,kCAKC;CACF;AAED;IACE,yCAGC;CACF;AAED;IACE,qCAIC;IADC,UAAgB;IAGlB,2BAEC;IAED;;;;;MAKC;CACF;AAED;IACE,uCAIC;IADC,YAAoB;IAGtB;;;;;MAKC;CACF;AAED;IACE,oDAIC;IADC,WAAkB;IAGpB;;;;;MAKC;CACF;AAED;IACE,oDAIC;IADC,WAAkB;IAGpB;;;;;MAKC;CACF;AAED;IACE,wCAGC;CACF;AAED;IACE,wCAGC;CACF"}