@bedrockio/yada 1.0.9 → 1.0.10

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.
@@ -33,7 +33,7 @@ function localize(template, values = {}) {
33
33
  }
34
34
  templates[template] = message;
35
35
  return message.replace(TOKEN_REG, (match, token) => {
36
- return values[token];
36
+ return token in values ? values[token] : match;
37
37
  });
38
38
  }
39
39
  function getLocalizerTemplates() {
@@ -33,12 +33,12 @@ function getLabeledMessage(error, options) {
33
33
  } = options;
34
34
  const base = getBase(error.message);
35
35
  if (field) {
36
- const msg = `{field} ${base}`;
36
+ const msg = `{field} ${downcase(base)}`;
37
37
  return (0, _localization.localize)(msg, {
38
38
  field: getFieldLabel(field, options)
39
39
  });
40
40
  } else if (index != null) {
41
- const msg = `Element at index "{index}" ${base}`;
41
+ const msg = `Element at index "{index}" ${downcase(base)}`;
42
42
  return (0, _localization.localize)(msg, {
43
43
  index
44
44
  });
@@ -60,7 +60,7 @@ function getBase(str) {
60
60
  if (str === 'Value is required.') {
61
61
  return 'is required.';
62
62
  } else {
63
- return downcase(str);
63
+ return str;
64
64
  }
65
65
  }
66
66
  function naturalize(str) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -29,7 +29,7 @@ export function localize(template, values = {}) {
29
29
  templates[template] = message;
30
30
 
31
31
  return message.replace(TOKEN_REG, (match, token) => {
32
- return values[token];
32
+ return token in values ? values[token] : match;
33
33
  });
34
34
  }
35
35
 
package/src/messages.js CHANGED
@@ -26,12 +26,12 @@ function getLabeledMessage(error, options) {
26
26
  const { field, index } = options;
27
27
  const base = getBase(error.message);
28
28
  if (field) {
29
- const msg = `{field} ${base}`;
29
+ const msg = `{field} ${downcase(base)}`;
30
30
  return localize(msg, {
31
31
  field: getFieldLabel(field, options),
32
32
  });
33
33
  } else if (index != null) {
34
- const msg = `Element at index "{index}" ${base}`;
34
+ const msg = `Element at index "{index}" ${downcase(base)}`;
35
35
  return localize(msg, {
36
36
  index,
37
37
  });
@@ -53,7 +53,7 @@ function getBase(str) {
53
53
  if (str === 'Value is required.') {
54
54
  return 'is required.';
55
55
  } else {
56
- return downcase(str);
56
+ return str;
57
57
  }
58
58
  }
59
59
 
package/test/all.test.js CHANGED
@@ -1802,6 +1802,19 @@ describe('getFullMessage', () => {
1802
1802
  'Auth code is required. Pass code is required. My token is required.'
1803
1803
  );
1804
1804
  });
1805
+
1806
+ it('should not interpolate tokens that do not exist', async () => {
1807
+ const schema = yd.custom(() => {
1808
+ throw new Error('Must {not} be.');
1809
+ });
1810
+ try {
1811
+ await schema.validate({
1812
+ foo: 'bar',
1813
+ });
1814
+ } catch (error) {
1815
+ expect(error.getFullMessage()).toBe('Must {not} be.');
1816
+ }
1817
+ });
1805
1818
  });
1806
1819
 
1807
1820
  describe('localization', () => {