@bedrockio/yada 1.0.34 → 1.0.35

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/dist/cjs/array.js CHANGED
@@ -47,11 +47,7 @@ class ArraySchema extends _Schema.default {
47
47
  options = (0, _utils.omit)(options, 'message');
48
48
  result.push(await schema.validate(el, options));
49
49
  } catch (error) {
50
- if (error.details?.length === 1) {
51
- errors.push(new _errors.ElementError(error.details[0].message, i));
52
- } else {
53
- errors.push(new _errors.ElementError('Element failed validation.', i, error.details));
54
- }
50
+ errors.push(new _errors.ElementError('Element failed validation.', i, error.original, error.details));
55
51
  }
56
52
  }
57
53
  if (errors.length) {
@@ -24,11 +24,34 @@ class ValidationError extends Error {
24
24
  this.type = type;
25
25
  }
26
26
  toJSON() {
27
- return {
28
- type: this.type,
29
- message: this.message,
30
- details: this.details
31
- };
27
+ if (this.canRollup()) {
28
+ const [first] = this.details;
29
+ return {
30
+ type: this.type,
31
+ message: first.message
32
+ };
33
+ } else {
34
+ return {
35
+ type: this.type,
36
+ message: this.message,
37
+ details: this.details.map(error => {
38
+ return error.toJSON();
39
+ })
40
+ };
41
+ }
42
+ }
43
+ canRollup() {
44
+ const {
45
+ details
46
+ } = this;
47
+ if (this.isFieldType() && details.length === 1) {
48
+ return !details[0].isFieldType?.();
49
+ } else {
50
+ return false;
51
+ }
52
+ }
53
+ isFieldType() {
54
+ return this.type === 'field' || this.type === 'element';
32
55
  }
33
56
  getFullMessage(options) {
34
57
  return (0, _messages.getFullMessage)(this, {
@@ -47,22 +70,23 @@ class FieldError extends ValidationError {
47
70
  }
48
71
  toJSON() {
49
72
  return {
50
- field: this.field,
51
- ...super.toJSON()
73
+ ...super.toJSON(),
74
+ field: this.field
52
75
  };
53
76
  }
54
77
  }
55
78
  exports.FieldError = FieldError;
56
79
  class ElementError extends ValidationError {
57
- constructor(message, index, details) {
80
+ constructor(message, index, original, details) {
58
81
  super(message, details, 'element');
59
82
  this.index = index;
83
+ this.original = original;
60
84
  this.details = details;
61
85
  }
62
86
  toJSON() {
63
87
  return {
64
- index: this.index,
65
- ...super.toJSON()
88
+ ...super.toJSON(),
89
+ index: this.index
66
90
  };
67
91
  }
68
92
  }
@@ -14,9 +14,8 @@ function getFullMessage(error, options) {
14
14
  return error.details.map(error => {
15
15
  if ((0, _errors.isSchemaError)(error)) {
16
16
  return getFullMessage(error, {
17
- field: error.field,
18
- index: error.index,
19
- ...options
17
+ ...options,
18
+ path: getInnerPath(error, options)
20
19
  });
21
20
  } else {
22
21
  return error.message;
@@ -26,34 +25,44 @@ function getFullMessage(error, options) {
26
25
  return getLabeledMessage(error, options);
27
26
  }
28
27
  }
28
+ function getInnerPath(error, options) {
29
+ const {
30
+ type
31
+ } = error;
32
+ const {
33
+ path = []
34
+ } = options;
35
+ if (type === 'field') {
36
+ return [...path, error.field];
37
+ } else if (type === 'element') {
38
+ return [...path, error.index];
39
+ } else {
40
+ return path;
41
+ }
42
+ }
29
43
  function getLabeledMessage(error, options) {
30
44
  const {
31
- field,
32
- index
45
+ path = []
33
46
  } = options;
34
47
  const base = getBase(error.message);
35
- if (field) {
48
+ if (path.length) {
36
49
  const msg = `{field} ${downcase(base)}`;
37
50
  return (0, _localization.localize)(msg, {
38
- field: getFieldLabel(field, options)
39
- });
40
- } else if (index != null) {
41
- const msg = `Element at index "{index}" ${downcase(base)}`;
42
- return (0, _localization.localize)(msg, {
43
- index
51
+ field: getFieldLabel(options)
44
52
  });
45
53
  } else {
46
54
  return (0, _localization.localize)(base);
47
55
  }
48
56
  }
49
- function getFieldLabel(field, options) {
57
+ function getFieldLabel(options) {
50
58
  const {
59
+ path = [],
51
60
  natural
52
61
  } = options;
53
62
  if (natural) {
54
- return naturalize(field);
63
+ return naturalize(path[path.length - 1]);
55
64
  } else {
56
- return `"${field}"`;
65
+ return `"${path.join('.')}"`;
57
66
  }
58
67
  }
59
68
  function getBase(str) {
@@ -90,15 +90,7 @@ class ObjectSchema extends _TypeSchema.default {
90
90
  };
91
91
  }
92
92
  } catch (error) {
93
- if (error.details?.length === 1) {
94
- const {
95
- message,
96
- original
97
- } = error.details[0];
98
- throw new _errors.FieldError(message, key, original);
99
- } else {
100
- throw new _errors.FieldError('Field failed validation.', key, error.original, error.details);
101
- }
93
+ throw new _errors.FieldError('Field failed validation.', key, error.original, error.details);
102
94
  }
103
95
  }
104
96
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.34",
3
+ "version": "1.0.35",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/array.js CHANGED
@@ -38,13 +38,14 @@ class ArraySchema extends Schema {
38
38
  options = omit(options, 'message');
39
39
  result.push(await schema.validate(el, options));
40
40
  } catch (error) {
41
- if (error.details?.length === 1) {
42
- errors.push(new ElementError(error.details[0].message, i));
43
- } else {
44
- errors.push(
45
- new ElementError('Element failed validation.', i, error.details)
46
- );
47
- }
41
+ errors.push(
42
+ new ElementError(
43
+ 'Element failed validation.',
44
+ i,
45
+ error.original,
46
+ error.details
47
+ )
48
+ );
48
49
  }
49
50
  }
50
51
  if (errors.length) {
package/src/errors.js CHANGED
@@ -20,11 +20,34 @@ export class ValidationError extends Error {
20
20
  }
21
21
 
22
22
  toJSON() {
23
- return {
24
- type: this.type,
25
- message: this.message,
26
- details: this.details,
27
- };
23
+ if (this.canRollup()) {
24
+ const [first] = this.details;
25
+ return {
26
+ type: this.type,
27
+ message: first.message,
28
+ };
29
+ } else {
30
+ return {
31
+ type: this.type,
32
+ message: this.message,
33
+ details: this.details.map((error) => {
34
+ return error.toJSON();
35
+ }),
36
+ };
37
+ }
38
+ }
39
+
40
+ canRollup() {
41
+ const { details } = this;
42
+ if (this.isFieldType() && details.length === 1) {
43
+ return !details[0].isFieldType?.();
44
+ } else {
45
+ return false;
46
+ }
47
+ }
48
+
49
+ isFieldType() {
50
+ return this.type === 'field' || this.type === 'element';
28
51
  }
29
52
 
30
53
  getFullMessage(options) {
@@ -45,23 +68,24 @@ export class FieldError extends ValidationError {
45
68
 
46
69
  toJSON() {
47
70
  return {
48
- field: this.field,
49
71
  ...super.toJSON(),
72
+ field: this.field,
50
73
  };
51
74
  }
52
75
  }
53
76
 
54
77
  export class ElementError extends ValidationError {
55
- constructor(message, index, details) {
78
+ constructor(message, index, original, details) {
56
79
  super(message, details, 'element');
57
80
  this.index = index;
81
+ this.original = original;
58
82
  this.details = details;
59
83
  }
60
84
 
61
85
  toJSON() {
62
86
  return {
63
- index: this.index,
64
87
  ...super.toJSON(),
88
+ index: this.index,
65
89
  };
66
90
  }
67
91
  }
package/src/messages.js CHANGED
@@ -8,9 +8,8 @@ export function getFullMessage(error, options) {
8
8
  .map((error) => {
9
9
  if (isSchemaError(error)) {
10
10
  return getFullMessage(error, {
11
- field: error.field,
12
- index: error.index,
13
11
  ...options,
12
+ path: getInnerPath(error, options),
14
13
  });
15
14
  } else {
16
15
  return error.message;
@@ -22,30 +21,37 @@ export function getFullMessage(error, options) {
22
21
  }
23
22
  }
24
23
 
24
+ function getInnerPath(error, options) {
25
+ const { type } = error;
26
+ const { path = [] } = options;
27
+ if (type === 'field') {
28
+ return [...path, error.field];
29
+ } else if (type === 'element') {
30
+ return [...path, error.index];
31
+ } else {
32
+ return path;
33
+ }
34
+ }
35
+
25
36
  function getLabeledMessage(error, options) {
26
- const { field, index } = options;
37
+ const { path = [] } = options;
27
38
  const base = getBase(error.message);
28
- if (field) {
39
+ if (path.length) {
29
40
  const msg = `{field} ${downcase(base)}`;
30
41
  return localize(msg, {
31
- field: getFieldLabel(field, options),
32
- });
33
- } else if (index != null) {
34
- const msg = `Element at index "{index}" ${downcase(base)}`;
35
- return localize(msg, {
36
- index,
42
+ field: getFieldLabel(options),
37
43
  });
38
44
  } else {
39
45
  return localize(base);
40
46
  }
41
47
  }
42
48
 
43
- function getFieldLabel(field, options) {
44
- const { natural } = options;
49
+ function getFieldLabel(options) {
50
+ const { path = [], natural } = options;
45
51
  if (natural) {
46
- return naturalize(field);
52
+ return naturalize(path[path.length - 1]);
47
53
  } else {
48
- return `"${field}"`;
54
+ return `"${path.join('.')}"`;
49
55
  }
50
56
  }
51
57
 
package/src/object.js CHANGED
@@ -76,17 +76,12 @@ class ObjectSchema extends TypeSchema {
76
76
  };
77
77
  }
78
78
  } catch (error) {
79
- if (error.details?.length === 1) {
80
- const { message, original } = error.details[0];
81
- throw new FieldError(message, key, original);
82
- } else {
83
- throw new FieldError(
84
- 'Field failed validation.',
85
- key,
86
- error.original,
87
- error.details
88
- );
89
- }
79
+ throw new FieldError(
80
+ 'Field failed validation.',
81
+ key,
82
+ error.original,
83
+ error.details
84
+ );
90
85
  }
91
86
  }
92
87
  });
@@ -1 +1 @@
1
- {"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"AA2IA;;;;;;;GAOG;AACH,8CALc,MAAM,iBAUnB;;AApJD;IACE,0BAGC;IAED;;OAEG;IACH,cA2CC;IAED,iCAUC;IAED,8BAUC;IAED,8BAaC;IAED,sBAaC;CA2BF"}
1
+ {"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"AA4IA;;;;;;;GAOG;AACH,8CALc,MAAM,iBAUnB;;AArJD;IACE,0BAGC;IAED;;OAEG;IACH,cA4CC;IAED,iCAUC;IAED,8BAUC;IAED,8BAaC;IAED,sBAaC;CA2BF"}
package/types/errors.d.ts CHANGED
@@ -9,10 +9,16 @@ export class ValidationError extends Error {
9
9
  details: any[];
10
10
  type: string;
11
11
  toJSON(): {
12
+ type: string;
13
+ message: any;
14
+ details?: undefined;
15
+ } | {
12
16
  type: string;
13
17
  message: string;
14
18
  details: any[];
15
19
  };
20
+ canRollup(): boolean;
21
+ isFieldType(): boolean;
16
22
  getFullMessage(options: any): any;
17
23
  }
18
24
  export class FieldError extends ValidationError {
@@ -21,21 +27,32 @@ export class FieldError extends ValidationError {
21
27
  original: any;
22
28
  details: any;
23
29
  toJSON(): {
30
+ field: any;
31
+ type: string;
32
+ message: any;
33
+ details?: undefined;
34
+ } | {
35
+ field: any;
24
36
  type: string;
25
37
  message: string;
26
38
  details: any[];
27
- field: any;
28
39
  };
29
40
  }
30
41
  export class ElementError extends ValidationError {
31
- constructor(message: any, index: any, details: any);
42
+ constructor(message: any, index: any, original: any, details: any);
32
43
  index: any;
44
+ original: any;
33
45
  details: any;
34
46
  toJSON(): {
47
+ index: any;
48
+ type: string;
49
+ message: any;
50
+ details?: undefined;
51
+ } | {
52
+ index: any;
35
53
  type: string;
36
54
  message: string;
37
55
  details: any[];
38
- index: any;
39
56
  };
40
57
  }
41
58
  export class AssertionError extends Error {
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AA0FA,iDAMC;AA7FD;IACE,uCAGC;IADC,YAAoB;IAGtB,gBAEC;CACF;AAED;IACE,0DAIC;IAFC,eAAsB;IACtB,aAAgB;IAGlB;;;;MAMC;IAED,kCAKC;CACF;AAED;IACE,mEAKC;IAHC,WAAkB;IAClB,cAAwB;IACxB,aAAsB;IAGxB;;;;;MAKC;CACF;AAED;IACE,oDAIC;IAFC,WAAkB;IAClB,aAAsB;IAGxB;;;;;MAKC;CACF;AAED;IACE,oDAIC;IAFC,UAAgB;IAChB,cAAwB;IAG1B;;;MAKC;CACF;AAED;IACE,wCAGC;IADC,aAAsB;CAEzB"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.js"],"names":[],"mappings":"AAkHA,iDAMC;AArHD;IACE,uCAGC;IADC,YAAoB;IAGtB,gBAEC;CACF;AAED;IACE,0DAIC;IAFC,eAAsB;IACtB,aAAgB;IAGlB;;;;;;;;MAgBC;IAED,qBAOC;IAED,uBAEC;IAED,kCAKC;CACF;AAED;IACE,mEAKC;IAHC,WAAkB;IAClB,cAAwB;IACxB,aAAsB;IAGxB;;;;;;;;;;MAKC;CACF;AAED;IACE,mEAKC;IAHC,WAAkB;IAClB,cAAwB;IACxB,aAAsB;IAGxB;;;;;;;;;;MAKC;CACF;AAED;IACE,oDAIC;IAFC,UAAgB;IAChB,cAAwB;IAG1B;;;MAKC;CACF;AAED;IACE,wCAGC;IADC,aAAsB;CAEzB"}
@@ -1 +1 @@
1
- {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.js"],"names":[],"mappings":"AAGA,8DAmBC"}
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.js"],"names":[],"mappings":"AAGA,8DAkBC"}
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AA2MA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAMnB;;;;AA7MD;;GAEG;AAEH;IAUE;;OAEG;IACH,cAqEC;IAED;;OAEG;IACH,kBAEC;IAED;;OAEG;IAEH,YAHW,SAAS,GAAC,MAAM,gBAkC1B;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;CAcF"}
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AAsMA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAMnB;;;;AAxMD;;GAEG;AAEH;IAUE;;OAEG;IACH,cAgEC;IAED;;OAEG;IACH,kBAEC;IAED;;OAEG;IAEH,YAHW,SAAS,GAAC,MAAM,gBAkC1B;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;CAcF"}