Skip to content

Commit

Permalink
Simplify nested findLabel logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug McCall authored and Doug McCall committed Aug 5, 2018
1 parent d804d25 commit d6c0aa9
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions lib/types/object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,27 +765,14 @@ internals.keysToLabels = function (schema, keys) {
return keys;
}

const findNestedLabel = function (key) {

const [parentKey, ...parts] = key.split('.');
const childKeys = parts.join('.');
const matchingChild = children.find((child) => child.key === parentKey);
const label = matchingChild ? internals.keysToLabels(matchingChild.schema, childKeys) : key;

return !key.endsWith(label) ? label : key;
};

const findLabel = function (key) {

if (key.includes('.')) {
return findNestedLabel(key);
}
const matchingChild = children.find((child) => child.key === key);
return matchingChild ? matchingChild.schema._getLabel(key) : key;
const matchingChild = schema._currentJoi.reach(schema, key);
return matchingChild ? matchingChild._getLabel(key) : key;
};

if (Array.isArray(keys)) {
return [].concat(keys.map(findLabel));
return keys.map(findLabel);
}

return findLabel(keys);
Expand Down

0 comments on commit d6c0aa9

Please sign in to comment.