@bedrockio/yada 1.1.0 → 1.1.1

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);
@@ -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
  };
@@ -126,4 +126,14 @@ function getLocalizedMessage(arg) {
126
126
  } else {
127
127
  return (0, _localization.localize)(arg);
128
128
  }
129
+ }
130
+ function serializeError(error) {
131
+ if (error.toJSON) {
132
+ return error.toJSON();
133
+ } else {
134
+ return {
135
+ ...error,
136
+ message: error.message
137
+ };
138
+ }
129
139
  }
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.1",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/Schema.js CHANGED
@@ -10,9 +10,6 @@ import { omit } from './utils';
10
10
  const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
11
11
  const REQUIRED_TYPES = ['default', 'required'];
12
12
 
13
- /**
14
- * @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
15
- */
16
13
  export default class Schema {
17
14
  constructor(meta = {}) {
18
15
  this.assertions = [];
@@ -47,24 +44,14 @@ export default class Schema {
47
44
 
48
45
  /**
49
46
  * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
50
- * @param {CustomSignature} args
47
+ * @param {Function} fn
51
48
  * @returns {this}
52
49
  */
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) {
50
+ custom(fn) {
51
+ if (!fn) {
65
52
  throw new Error('Assertion function required.');
66
53
  }
67
- return this.clone().assert(type, async (val, options) => {
54
+ return this.clone().assert('custom', async (val, options) => {
68
55
  return await fn(val, options);
69
56
  });
70
57
  }
@@ -155,7 +142,7 @@ export default class Schema {
155
142
  details.push(new TypeError(error, this.meta.type));
156
143
  } else if (type === 'format') {
157
144
  details.push(new FormatError(error, this.meta.format));
158
- } else if (!error.type) {
145
+ } else if (error instanceof LocalizedError) {
159
146
  details.push(new AssertionError(error, type));
160
147
  } else {
161
148
  details.push(error);
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
  };
@@ -125,3 +125,14 @@ function getLocalizedMessage(arg) {
125
125
  return localize(arg);
126
126
  }
127
127
  }
128
+
129
+ function serializeError(error) {
130
+ if (error.toJSON) {
131
+ return error.toJSON();
132
+ } else {
133
+ return {
134
+ ...error,
135
+ message: error.message,
136
+ };
137
+ }
138
+ }
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":"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"}
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"}