@bedrockio/yada 1.1.0 → 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.
@@ -9,10 +9,6 @@ var _errors = require("./errors");
9
9
  var _utils = require("./utils");
10
10
  const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
11
11
  const REQUIRED_TYPES = ['default', 'required'];
12
-
13
- /**
14
- * @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
15
- */
16
12
  class Schema {
17
13
  constructor(meta = {}) {
18
14
  this.assertions = [];
@@ -51,24 +47,14 @@ class Schema {
51
47
 
52
48
  /**
53
49
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
54
- * @param {CustomSignature} args
50
+ * @param {Function} fn
55
51
  * @returns {this}
56
52
  */
57
- custom(...args) {
58
- let type, fn;
59
- if (typeof args[0] === 'function') {
60
- type = 'custom';
61
- fn = args[0];
62
- } else {
63
- type = args[0];
64
- fn = args[1];
65
- }
66
- if (!type) {
67
- throw new Error('Assertion type required.');
68
- } else if (!fn) {
53
+ custom(fn) {
54
+ if (!fn) {
69
55
  throw new Error('Assertion function required.');
70
56
  }
71
- return this.clone().assert(type, async (val, options) => {
57
+ return this.clone().assert('custom', async (val, options) => {
72
58
  return await fn(val, options);
73
59
  });
74
60
  }
@@ -163,7 +149,7 @@ class Schema {
163
149
  details.push(new _errors.TypeError(error, this.meta.type));
164
150
  } else if (type === 'format') {
165
151
  details.push(new _errors.FormatError(error, this.meta.format));
166
- } else if (!error.type) {
152
+ } else if (error instanceof _errors.LocalizedError) {
167
153
  details.push(new _errors.AssertionError(error, type));
168
154
  } else {
169
155
  details.push(error);
@@ -274,6 +260,7 @@ class Schema {
274
260
  enum: set
275
261
  }).assert('enum', async (val, options) => {
276
262
  if (val !== undefined) {
263
+ const captured = [];
277
264
  for (let el of set) {
278
265
  if (isSchema(el)) {
279
266
  try {
@@ -284,23 +271,27 @@ class Schema {
284
271
  return await el.validate(val, options);
285
272
  } catch (err) {
286
273
  const [first] = err.details;
287
- if (first instanceof _errors.TypeError) {
288
- // If the error is a simple type error then continue
289
- // 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.
290
277
  continue;
291
278
  } else {
292
- // Otherwise throw the error to surface messages on
279
+ // Otherwise capture the error to surface messages on
293
280
  // more complex schemas.
294
- throw err;
281
+ captured.push(err);
295
282
  }
296
283
  }
297
284
  } else if (el === val === allow) {
298
285
  return;
299
286
  }
300
287
  }
301
- throw new _errors.LocalizedError(message, {
302
- types: types.join(', ')
303
- });
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
+ }
304
295
  }
305
296
  });
306
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");
@@ -31,7 +31,7 @@ class ValidationError extends Error {
31
31
  }),
32
32
  ...(details.length && {
33
33
  details: details.map(error => {
34
- return error.toJSON();
34
+ return serializeError(error);
35
35
  })
36
36
  })
37
37
  };
@@ -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
  }
@@ -126,4 +135,14 @@ function getLocalizedMessage(arg) {
126
135
  } else {
127
136
  return (0, _localization.localize)(arg);
128
137
  }
138
+ }
139
+ function serializeError(error) {
140
+ if (error.toJSON) {
141
+ return error.toJSON();
142
+ } else {
143
+ return {
144
+ ...error,
145
+ message: error.message
146
+ };
147
+ }
129
148
  }
package/dist/cjs/index.js CHANGED
@@ -115,10 +115,10 @@ function reject(...args) {
115
115
 
116
116
  /**
117
117
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
118
- * @param {import("./Schema").CustomSignature} args
118
+ * @param {Function} fn
119
119
  */
120
- function custom(...args) {
121
- return new _Schema.default().custom(...args);
120
+ function custom(fn) {
121
+ return new _Schema.default().custom(fn);
122
122
  }
123
123
  var _default = {
124
124
  array: _array.default,
@@ -60,7 +60,7 @@ const DISALLOWED_TYPES = ['field', 'element', 'array', 'custom'];
60
60
  // names automatically. Instead the custom messages can include
61
61
  // the {field} token to allow it to be interpolated if required.
62
62
  function canAutoAddField(type, path) {
63
- return path.length && !DISALLOWED_TYPES.includes(type);
63
+ return type && path.length && !DISALLOWED_TYPES.includes(type);
64
64
  }
65
65
  function getFieldLabel(options) {
66
66
  const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.1.0",
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,
@@ -10,9 +11,6 @@ import { omit } from './utils';
10
11
  const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
11
12
  const REQUIRED_TYPES = ['default', 'required'];
12
13
 
13
- /**
14
- * @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
15
- */
16
14
  export default class Schema {
17
15
  constructor(meta = {}) {
18
16
  this.assertions = [];
@@ -47,24 +45,14 @@ export default class Schema {
47
45
 
48
46
  /**
49
47
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
50
- * @param {CustomSignature} args
48
+ * @param {Function} fn
51
49
  * @returns {this}
52
50
  */
53
- custom(...args) {
54
- let type, fn;
55
- if (typeof args[0] === 'function') {
56
- type = 'custom';
57
- fn = args[0];
58
- } else {
59
- type = args[0];
60
- fn = args[1];
61
- }
62
- if (!type) {
63
- throw new Error('Assertion type required.');
64
- } else if (!fn) {
51
+ custom(fn) {
52
+ if (!fn) {
65
53
  throw new Error('Assertion function required.');
66
54
  }
67
- return this.clone().assert(type, async (val, options) => {
55
+ return this.clone().assert('custom', async (val, options) => {
68
56
  return await fn(val, options);
69
57
  });
70
58
  }
@@ -155,7 +143,7 @@ export default class Schema {
155
143
  details.push(new TypeError(error, this.meta.type));
156
144
  } else if (type === 'format') {
157
145
  details.push(new FormatError(error, this.meta.format));
158
- } else if (!error.type) {
146
+ } else if (error instanceof LocalizedError) {
159
147
  details.push(new AssertionError(error, type));
160
148
  } else {
161
149
  details.push(error);
@@ -256,6 +244,7 @@ export default class Schema {
256
244
  const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
257
245
  return this.clone({ enum: set }).assert('enum', async (val, options) => {
258
246
  if (val !== undefined) {
247
+ const captured = [];
259
248
  for (let el of set) {
260
249
  if (isSchema(el)) {
261
250
  try {
@@ -266,23 +255,27 @@ export default class Schema {
266
255
  return await el.validate(val, options);
267
256
  } catch (err) {
268
257
  const [first] = err.details;
269
- if (first instanceof TypeError) {
270
- // If the error is a simple type error then continue
271
- // 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.
272
261
  continue;
273
262
  } else {
274
- // Otherwise throw the error to surface messages on
263
+ // Otherwise capture the error to surface messages on
275
264
  // more complex schemas.
276
- throw err;
265
+ captured.push(err);
277
266
  }
278
267
  }
279
268
  } else if ((el === val) === allow) {
280
269
  return;
281
270
  }
282
271
  }
283
- throw new LocalizedError(message, {
284
- types: types.join(', '),
285
- });
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
+ }
286
279
  }
287
280
  });
288
281
  }
package/src/errors.js CHANGED
@@ -23,7 +23,7 @@ export class ValidationError extends Error {
23
23
  }),
24
24
  ...(details.length && {
25
25
  details: details.map((error) => {
26
- return error.toJSON();
26
+ return serializeError(error);
27
27
  }),
28
28
  }),
29
29
  };
@@ -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
 
@@ -125,3 +135,14 @@ function getLocalizedMessage(arg) {
125
135
  return localize(arg);
126
136
  }
127
137
  }
138
+
139
+ function serializeError(error) {
140
+ if (error.toJSON) {
141
+ return error.toJSON();
142
+ } else {
143
+ return {
144
+ ...error,
145
+ message: error.message,
146
+ };
147
+ }
148
+ }
package/src/index.js CHANGED
@@ -34,10 +34,10 @@ function reject(...args) {
34
34
 
35
35
  /**
36
36
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
37
- * @param {import("./Schema").CustomSignature} args
37
+ * @param {Function} fn
38
38
  */
39
- function custom(...args) {
40
- return new Schema().custom(...args);
39
+ function custom(fn) {
40
+ return new Schema().custom(fn);
41
41
  }
42
42
 
43
43
  export {
package/src/messages.js CHANGED
@@ -54,7 +54,7 @@ const DISALLOWED_TYPES = ['field', 'element', 'array', 'custom'];
54
54
  // names automatically. Instead the custom messages can include
55
55
  // the {field} token to allow it to be interpolated if required.
56
56
  function canAutoAddField(type, path) {
57
- return path.length && !DISALLOWED_TYPES.includes(type);
57
+ return type && path.length && !DISALLOWED_TYPES.includes(type);
58
58
  }
59
59
 
60
60
  function getFieldLabel(options) {
package/types/Schema.d.ts CHANGED
@@ -1,7 +1,4 @@
1
1
  export function isSchema(arg: any): boolean;
2
- /**
3
- * @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
4
- */
5
2
  export default class Schema {
6
3
  constructor(meta?: {});
7
4
  assertions: any[];
@@ -17,10 +14,10 @@ export default class Schema {
17
14
  default(arg: any): this;
18
15
  /**
19
16
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
20
- * @param {CustomSignature} args
17
+ * @param {Function} fn
21
18
  * @returns {this}
22
19
  */
23
- custom(...args: CustomSignature): this;
20
+ custom(fn: Function): this;
24
21
  /**
25
22
  * Conditionally exclude fields inside an object schema.
26
23
  * [Link](https://github.com/bedrockio/yada#strip)
@@ -99,5 +96,4 @@ export default class Schema {
99
96
  enum?: undefined;
100
97
  };
101
98
  }
102
- export type CustomSignature = [fn: Function] | [type: string, fn: Function];
103
99
  //# sourceMappingURL=Schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAkXA,4CAEC;AAxWD;;GAEG;AACH;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,gBAHW,eAAe,GACb,IAAI,CAmBhB;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;8BAnWY,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,UAAU,CAAC"}
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"}
package/types/index.d.ts CHANGED
@@ -38,9 +38,9 @@ export function allow(...args: any[]): Schema;
38
38
  export function reject(...args: any[]): Schema;
39
39
  /**
40
40
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
41
- * @param {import("./Schema").CustomSignature} args
41
+ * @param {Function} fn
42
42
  */
43
- export function custom(...args: import("./Schema").CustomSignature): Schema;
43
+ export function custom(fn: Function): Schema;
44
44
  import { isSchema } from "./utils";
45
45
  import { isSchemaError } from "./utils";
46
46
  import { useLocalizer } from "./localization";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAaA;;GAEG;AACH,8BAEC;AAED;;GAEG;AACH,8CAEC;AAED;;GAEG;AACH,+CAEC;AAED;;;GAGG;AACH,gCAFW,OAAO,UAAU,EAAE,eAAe,UAI5C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAaA;;GAEG;AACH,8BAEC;AAED;;GAEG;AACH,8CAEC;AAED;;GAEG;AACH,+CAEC;AAED;;;GAGG;AACH,6CAEC"}