@bedrockio/yada 1.0.10 → 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,10 +48,17 @@ 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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.10",
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,14 +38,19 @@ 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
55
  const result = await schema.validate(val, options);
51
56
  if (result !== undefined) {
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,21 @@ 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 path', async () => {
1654
+ const schema = yd.object({
1655
+ a: yd.object({
1656
+ b: yd.number().custom((num, { path }) => {
1657
+ expect(path).toEqual(['a', 'b']);
1658
+ }),
1659
+ }),
1660
+ });
1661
+ await schema.validate({
1662
+ a: {
1663
+ b: 3,
1664
+ },
1665
+ });
1666
+ });
1667
+
1653
1668
  it('should correctly validate chained formats', async () => {
1654
1669
  let schema;
1655
1670