@bedrockio/yada 1.0.9 → 1.0.11

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) {
@@ -57,7 +57,10 @@ class ObjectSchema extends _TypeSchema.default {
57
57
  return;
58
58
  }
59
59
  try {
60
- const result = await schema.validate(val, options);
60
+ const result = await schema.validate(val, {
61
+ ...options,
62
+ key
63
+ });
61
64
  if (result !== undefined) {
62
65
  return {
63
66
  ...obj,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
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/src/object.js CHANGED
@@ -47,7 +47,10 @@ class ObjectSchema extends TypeSchema {
47
47
  }
48
48
 
49
49
  try {
50
- const result = await schema.validate(val, options);
50
+ const result = await schema.validate(val, {
51
+ ...options,
52
+ key,
53
+ });
51
54
  if (result !== undefined) {
52
55
  return {
53
56
  ...obj,
package/test/all.test.js CHANGED
@@ -1640,7 +1640,7 @@ describe('other', () => {
1640
1640
  it('should have access to root object', async () => {
1641
1641
  const schema = yd.object({
1642
1642
  a: yd.array(yd.number()),
1643
- b: yd.number().custom((arr, { root }) => {
1643
+ b: yd.number().custom((num, { root }) => {
1644
1644
  if (!root.a.includes(root.b)) {
1645
1645
  throw new Error('"a" must include "b"');
1646
1646
  }
@@ -1650,6 +1650,15 @@ describe('other', () => {
1650
1650
  await assertFail(schema, { a: [1, 2, 3], b: 4 }, ['"a" must include "b"']);
1651
1651
  });
1652
1652
 
1653
+ it('should have access to current key', async () => {
1654
+ const schema = yd.object({
1655
+ a: yd.number().custom((num, { key }) => {
1656
+ return key;
1657
+ }),
1658
+ });
1659
+ expect(await schema.validate({ a: 12 })).toEqual({ a: 'a' });
1660
+ });
1661
+
1653
1662
  it('should correctly validate chained formats', async () => {
1654
1663
  let schema;
1655
1664
 
@@ -1802,6 +1811,19 @@ describe('getFullMessage', () => {
1802
1811
  'Auth code is required. Pass code is required. My token is required.'
1803
1812
  );
1804
1813
  });
1814
+
1815
+ it('should not interpolate tokens that do not exist', async () => {
1816
+ const schema = yd.custom(() => {
1817
+ throw new Error('Must {not} be.');
1818
+ });
1819
+ try {
1820
+ await schema.validate({
1821
+ foo: 'bar',
1822
+ });
1823
+ } catch (error) {
1824
+ expect(error.getFullMessage()).toBe('Must {not} be.');
1825
+ }
1826
+ });
1805
1827
  });
1806
1828
 
1807
1829
  describe('localization', () => {