@bedrockio/yada 1.2.2 → 1.2.4

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.
@@ -44,9 +44,6 @@ function getInnerPath(error, options) {
44
44
  }
45
45
  }
46
46
  function getLabeledMessage(error, options) {
47
- const {
48
- type
49
- } = error;
50
47
  const {
51
48
  path = []
52
49
  } = options;
@@ -54,8 +51,10 @@ function getLabeledMessage(error, options) {
54
51
  let template;
55
52
  if (base.includes('{field}')) {
56
53
  template = base;
57
- } else if (canAutoAddField(type, path)) {
54
+ } else if (canAutoAddField(path, base)) {
58
55
  template = `{field} ${downcase(base)}`;
56
+ } else {
57
+ template = error.message;
59
58
  }
60
59
  if (template) {
61
60
  return (0, _localization.localize)(template, {
@@ -65,13 +64,14 @@ function getLabeledMessage(error, options) {
65
64
  return (0, _localization.localize)(base);
66
65
  }
67
66
  }
68
- const DISALLOWED_TYPES = ['field', 'element', 'array', 'custom'];
67
+ const GENERIC_MESSAGE_REG = /^Must|is required\.?$/;
69
68
 
70
- // Error types that have custom messages should not add the field
71
- // names automatically. Instead the custom messages can include
72
- // the {field} token to allow it to be interpolated if required.
73
- function canAutoAddField(type, path) {
74
- return type && path.length && !DISALLOWED_TYPES.includes(type);
69
+ // Only "generic" error messages should automatically add the field.
70
+ // A custom error message may be "Please verify you are human" which
71
+ // is intended to be understood in context and does not benefit from
72
+ // the inclusion of the field name.
73
+ function canAutoAddField(path, base) {
74
+ return path.length && GENERIC_MESSAGE_REG.test(base);
75
75
  }
76
76
  function getFieldLabel(options) {
77
77
  const {
@@ -11,7 +11,9 @@ var _password = require("./password");
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
  const SLUG_REG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
13
13
  const PHONE_REG = /^\+\d{1,3}\d{3,14}$/;
14
- const PHONE_DESCRIPTION = 'A phone number in [E.164](https://en.wikipedia.org/wiki/E.164) format.';
14
+ const NANP_REG = /^\+1[2-9]\d{2}[2-9]\d{6}$/;
15
+ const E164_DESCRIPTION = 'A phone number in [E.164](https://en.wikipedia.org/wiki/E.164) format.';
16
+ const NANP_DESCRIPTION = 'A phone number in [NANP](https://en.wikipedia.org/wiki/North_American_Numbering_Plan) format.';
15
17
  class StringSchema extends _TypeSchema.default {
16
18
  constructor() {
17
19
  super(String);
@@ -154,12 +156,15 @@ class StringSchema extends _TypeSchema.default {
154
156
  }
155
157
  });
156
158
  }
157
- phone() {
159
+ phone(code) {
160
+ const isNANP = code === 'US' || code === 'NANP';
161
+ const reg = isNANP ? NANP_REG : PHONE_REG;
162
+ const description = isNANP ? NANP_DESCRIPTION : E164_DESCRIPTION;
158
163
  return this.format('phone', str => {
159
- if (!PHONE_REG.test(str)) {
164
+ if (!reg.test(str)) {
160
165
  throw new _errors.LocalizedError('Must be a valid phone number.');
161
166
  }
162
- }).description(PHONE_DESCRIPTION);
167
+ }).description(description);
163
168
  }
164
169
  hex() {
165
170
  return this.format('hex', str => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/messages.js CHANGED
@@ -40,15 +40,16 @@ function getInnerPath(error, options) {
40
40
  }
41
41
 
42
42
  function getLabeledMessage(error, options) {
43
- const { type } = error;
44
43
  const { path = [] } = options;
45
44
  const base = getBase(error.message);
46
45
 
47
46
  let template;
48
47
  if (base.includes('{field}')) {
49
48
  template = base;
50
- } else if (canAutoAddField(type, path)) {
49
+ } else if (canAutoAddField(path, base)) {
51
50
  template = `{field} ${downcase(base)}`;
51
+ } else {
52
+ template = error.message;
52
53
  }
53
54
 
54
55
  if (template) {
@@ -60,13 +61,14 @@ function getLabeledMessage(error, options) {
60
61
  }
61
62
  }
62
63
 
63
- const DISALLOWED_TYPES = ['field', 'element', 'array', 'custom'];
64
+ const GENERIC_MESSAGE_REG = /^Must|is required\.?$/;
64
65
 
65
- // Error types that have custom messages should not add the field
66
- // names automatically. Instead the custom messages can include
67
- // the {field} token to allow it to be interpolated if required.
68
- function canAutoAddField(type, path) {
69
- return type && path.length && !DISALLOWED_TYPES.includes(type);
66
+ // Only "generic" error messages should automatically add the field.
67
+ // A custom error message may be "Please verify you are human" which
68
+ // is intended to be understood in context and does not benefit from
69
+ // the inclusion of the field name.
70
+ function canAutoAddField(path, base) {
71
+ return path.length && GENERIC_MESSAGE_REG.test(base);
70
72
  }
71
73
 
72
74
  function getFieldLabel(options) {
package/src/string.js CHANGED
@@ -12,10 +12,14 @@ import {
12
12
 
13
13
  const SLUG_REG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
14
14
  const PHONE_REG = /^\+\d{1,3}\d{3,14}$/;
15
+ const NANP_REG = /^\+1[2-9]\d{2}[2-9]\d{6}$/;
15
16
 
16
- const PHONE_DESCRIPTION =
17
+ const E164_DESCRIPTION =
17
18
  'A phone number in [E.164](https://en.wikipedia.org/wiki/E.164) format.';
18
19
 
20
+ const NANP_DESCRIPTION =
21
+ 'A phone number in [NANP](https://en.wikipedia.org/wiki/North_American_Numbering_Plan) format.';
22
+
19
23
  class StringSchema extends TypeSchema {
20
24
  constructor() {
21
25
  super(String);
@@ -149,12 +153,15 @@ class StringSchema extends TypeSchema {
149
153
  });
150
154
  }
151
155
 
152
- phone() {
156
+ phone(code) {
157
+ const isNANP = code === 'US' || code === 'NANP';
158
+ const reg = isNANP ? NANP_REG : PHONE_REG;
159
+ const description = isNANP ? NANP_DESCRIPTION : E164_DESCRIPTION;
153
160
  return this.format('phone', (str) => {
154
- if (!PHONE_REG.test(str)) {
161
+ if (!reg.test(str)) {
155
162
  throw new LocalizedError('Must be a valid phone number.');
156
163
  }
157
- }).description(PHONE_DESCRIPTION);
164
+ }).description(description);
158
165
  }
159
166
 
160
167
  hex() {
package/types/string.d.ts CHANGED
@@ -34,7 +34,7 @@ declare class StringSchema extends TypeSchema {
34
34
  */
35
35
  match(reg: RegExp): StringSchema;
36
36
  email(): StringSchema;
37
- phone(): StringSchema;
37
+ phone(code: any): StringSchema;
38
38
  hex(): StringSchema;
39
39
  md5(): StringSchema;
40
40
  sha1(): StringSchema;
@@ -1 +1 @@
1
- {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"AAyaA;;GAEG;AACH,iDAEC;AA5ZD;IACE,cAoBC;IAED;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;OAEG;IACH,eAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED,qBAIC;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,WAFW,MAAM,gBAahB;IAED,sBAMC;IAED,sBAMC;IAED,oBAMC;IAED,oBAMC;IAED,qBAMC;IAED,sBAMC;IAED;;;OAGG;IACH;QAF6B,OAAO,GAAzB,OAAO;qBAQjB;IAED,2BAMC;IAED,mBAMC;IAED,wBAMC;IAED,uBAMC;IAED,oBAMC;IAED,qBAOC;IAED,uBAMC;IAED;;OAEG;IACH,oBAFW,MAAM,gBAQhB;IAED,wBAMC;IAED;;;;;;;OAOG;IACH;QAN4B,SAAS,GAA1B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,YAAY,GAA7B,MAAM;QACW,YAAY,GAA7B,MAAM;qBA+BhB;IAED;;;;;;;;;;;OAWG;IACH;QAV6B,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;qBAQlB;IAED;;;;;;;;OAQG;IACH;QAP6B,WAAW,GAA7B,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,kBAAkB,GAApC,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,cAAc,GAAhC,OAAO;QACW,iBAAiB,GAAnC,OAAO;qBAQjB;IAED;;OAEG;IACH,eAFW,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,gBAQ3B;IAED,oBAMC;IAED,oBAMC;IAED,sBAMC;IAED,sBAMC;IAED,yCAWC;CAcF"}
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.js"],"names":[],"mappings":"AAgbA;;GAEG;AACH,iDAEC;AA/ZD;IACE,cAoBC;IAED;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;OAEG;IACH,eAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED;;OAEG;IACH,YAFW,MAAM,gBAUhB;IAED,qBAIC;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,mBAFW,OAAO,gBAYjB;IAED;;OAEG;IACH,WAFW,MAAM,gBAahB;IAED,sBAMC;IAED,+BASC;IAED,oBAMC;IAED,oBAMC;IAED,qBAMC;IAED,sBAMC;IAED;;;OAGG;IACH;QAF6B,OAAO,GAAzB,OAAO;qBAQjB;IAED,2BAMC;IAED,mBAMC;IAED,wBAMC;IAED,uBAMC;IAED,oBAMC;IAED,qBAOC;IAED,uBAMC;IAED;;OAEG;IACH,oBAFW,MAAM,gBAQhB;IAED,wBAMC;IAED;;;;;;;OAOG;IACH;QAN4B,SAAS,GAA1B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,UAAU,GAA3B,MAAM;QACW,YAAY,GAA7B,MAAM;QACW,YAAY,GAA7B,MAAM;qBA+BhB;IAED;;;;;;;;;;;OAWG;IACH;QAV6B,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;qBAQlB;IAED;;;;;;;;OAQG;IACH;QAP6B,WAAW,GAA7B,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,kBAAkB,GAApC,OAAO;QACW,iBAAiB,GAAnC,OAAO;QACW,cAAc,GAAhC,OAAO;QACW,iBAAiB,GAAnC,OAAO;qBAQjB;IAED;;OAEG;IACH,eAFW,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,gBAQ3B;IAED,oBAMC;IAED,oBAMC;IAED,sBAMC;IAED,sBAMC;IAED,yCAWC;CAcF"}