Skip to content

Commit

Permalink
Fix nested paths on dependencies with keys. Fixes #1617.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Oct 22, 2018
1 parent e3402fe commit 59c8d23
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 56 deletions.
39 changes: 18 additions & 21 deletions lib/types/object/index.js
Expand Up @@ -305,8 +305,10 @@ internals.Object = class extends Any {

for (let i = 0; i < this._inner.dependencies.length; ++i) {
const dep = this._inner.dependencies[i];
const localState = new State(dep.key, dep.key === null ? state.path : [...state.path, dep.key]);
const err = internals[dep.type].call(this, dep.key !== null && target[dep.key], dep.peers, target, localState, options);
const hasKey = dep.key !== null;
const splitKey = hasKey && dep.key.split('.');
const localState = hasKey ? new State(splitKey[splitKey.length - 1], [...state.path, ...splitKey]) : new State(null, state.path);
const err = internals[dep.type].call(this, dep.key, hasKey && Hoek.reach(target, dep.key), dep.peers, target, localState, options);
if (err instanceof Errors.Err) {
errors.push(err);
if (options.abortEarly) {
Expand Down Expand Up @@ -777,10 +779,10 @@ internals.keysToLabels = function (schema, keys) {
};


internals.with = function (value, peers, parent, state, options) {
internals.with = function (key, value, peers, parent, state, options) {

if (value === undefined) {
return value;
return;
}

for (let i = 0; i < peers.length; ++i) {
Expand All @@ -790,22 +792,20 @@ internals.with = function (value, peers, parent, state, options) {
if (keysExist === undefined) {

return this.createError('object.with', {
main: state.key,
mainWithLabel: internals.keysToLabels(this, state.key),
main: key,
mainWithLabel: internals.keysToLabels(this, key),
peer,
peerWithLabel: internals.keysToLabels(this, peer)
}, state, options);
}
}

return value;
};


internals.without = function (value, peers, parent, state, options) {
internals.without = function (key, value, peers, parent, state, options) {

if (value === undefined) {
return value;
return;
}

for (let i = 0; i < peers.length; ++i) {
Expand All @@ -814,19 +814,17 @@ internals.without = function (value, peers, parent, state, options) {
if (keysExist !== undefined) {

return this.createError('object.without', {
main: state.key,
mainWithLabel: internals.keysToLabels(this, state.key),
main: key,
mainWithLabel: internals.keysToLabels(this, key),
peer,
peerWithLabel: internals.keysToLabels(this, peer)
}, state, options);
}
}

return value;
};


internals.xor = function (value, peers, parent, state, options) {
internals.xor = function (key, value, peers, parent, state, options) {

const present = [];
for (let i = 0; i < peers.length; ++i) {
Expand All @@ -839,7 +837,7 @@ internals.xor = function (value, peers, parent, state, options) {
}

if (present.length === 1) {
return value;
return;
}

const context = { peers, peersWithLabels: internals.keysToLabels(this, peers) };
Expand All @@ -855,14 +853,13 @@ internals.xor = function (value, peers, parent, state, options) {
};


internals.or = function (value, peers, parent, state, options) {
internals.or = function (key, value, peers, parent, state, options) {

for (let i = 0; i < peers.length; ++i) {
const peer = peers[i];
const keysExist = Hoek.reach(parent, peer);
if (keysExist !== undefined) {

return value;
return;
}
}

Expand All @@ -873,7 +870,7 @@ internals.or = function (value, peers, parent, state, options) {
};


internals.and = function (value, peers, parent, state, options) {
internals.and = function (key, value, peers, parent, state, options) {

const missing = [];
const present = [];
Expand Down Expand Up @@ -904,7 +901,7 @@ internals.and = function (value, peers, parent, state, options) {
};


internals.nand = function (value, peers, parent, state, options) {
internals.nand = function (key, value, peers, parent, state, options) {

const present = [];
for (let i = 0; i < peers.length; ++i) {
Expand Down

0 comments on commit 59c8d23

Please sign in to comment.