@bedrockio/yada 1.0.11 → 1.0.12

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.
@@ -48,19 +48,23 @@ class ObjectSchema extends _TypeSchema.default {
48
48
  for (let [key, schema] of Object.entries(this.meta.fields)) {
49
49
  this.assert('field', async (obj, options) => {
50
50
  if (obj) {
51
- const val = obj[key];
51
+ const {
52
+ path = []
53
+ } = options;
52
54
  const {
53
55
  strip
54
56
  } = schema.meta;
57
+ const val = obj[key];
58
+ options = {
59
+ ...options,
60
+ path: [...path, key]
61
+ };
55
62
  if (strip && strip(val, options)) {
56
63
  delete obj[key];
57
64
  return;
58
65
  }
59
66
  try {
60
- const result = await schema.validate(val, {
61
- ...options,
62
- key
63
- });
67
+ const result = await schema.validate(val, options);
64
68
  if (result !== undefined) {
65
69
  return {
66
70
  ...obj,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/object.js CHANGED
@@ -38,19 +38,21 @@ class ObjectSchema extends TypeSchema {
38
38
  for (let [key, schema] of Object.entries(this.meta.fields)) {
39
39
  this.assert('field', async (obj, options) => {
40
40
  if (obj) {
41
- const val = obj[key];
41
+ const { path = [] } = options;
42
42
  const { strip } = schema.meta;
43
+ const val = obj[key];
44
+
45
+ options = {
46
+ ...options,
47
+ path: [...path, key],
48
+ };
43
49
 
44
50
  if (strip && strip(val, options)) {
45
51
  delete obj[key];
46
52
  return;
47
53
  }
48
-
49
54
  try {
50
- const result = await schema.validate(val, {
51
- ...options,
52
- key,
53
- });
55
+ const result = await schema.validate(val, options);
54
56
  if (result !== undefined) {
55
57
  return {
56
58
  ...obj,
package/test/all.test.js CHANGED
@@ -1650,13 +1650,19 @@ 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 () => {
1653
+ it('should have access to current path', async () => {
1654
1654
  const schema = yd.object({
1655
- a: yd.number().custom((num, { key }) => {
1656
- return key;
1655
+ a: yd.object({
1656
+ b: yd.number().custom((num, { path }) => {
1657
+ expect(path).toEqual(['a', 'b']);
1658
+ }),
1657
1659
  }),
1658
1660
  });
1659
- expect(await schema.validate({ a: 12 })).toEqual({ a: 'a' });
1661
+ await schema.validate({
1662
+ a: {
1663
+ b: 3,
1664
+ },
1665
+ });
1660
1666
  });
1661
1667
 
1662
1668
  it('should correctly validate chained formats', async () => {