#1477 Add Support for nested path with Joi.object().or(), .nand(), .and(), .with(), .without(), .xor()#1554
#1477 Add Support for nested path with Joi.object().or(), .nand(), .and(), .with(), .without(), .xor()#1554Marsup merged 12 commits intohapijs:v14from BolajiOlajide:ft-add-support-for-nested-objects
Conversation
lib/types/object/index.js
Outdated
| if (Object.prototype.hasOwnProperty.call(parent, peer) && | ||
| parent[peer] !== undefined) { | ||
| const splitPeers = peer.split('.'); | ||
| const keysExist = Utils.objHasKeys(parent, splitPeers); |
There was a problem hiding this comment.
You can certainly use Hoek.reach to achieve the same thing instead of creating your own.
There was a problem hiding this comment.
Oh! I'll check it out and update the PR. Thanks @Marsup
|
The logic if (Object.prototype.hasOwnProperty.call(parent, peer) &&
parent[peer] !== undefined) {is repeated 4 times in if (!Object.prototype.hasOwnProperty.call(parent, peer) ||
parent[peer] === undefined) {being repeated twice. Does it really only make sense to support the nested syntax only for I am eagerly awaiting this feature for |
|
Don't worry I won't merge it until all are supported, baby steps… 🙂 |
|
I'm working on it. I'll implement the change for other methods. |
|
This looks great - exactly what I was hoping for! The only concern I would have, which comes from my own fiddling, would be the incompatibilities that arise around scenarios regarding the usage of labels, like in the I believe with the current implementation, if you duplicate the |
|
True @dhm116 ... Just tried it out now and the tests failed. I haven't used labels before but I'll look into its usage and see how to fix this. |
|
It's tricky with labels for nested objects. I'd appreciate some help here. |
|
This probably needs to be cleaned up a bit, but this seems to pass the tests now - BolajiOlajide/joi@ft-add-support-for-nested-objects...dhm116:ft-add-support-for-nested-objects internals.keysToLabels = function (schema, keys) {
const children = schema._inner.children;
if (!children) {
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;
};
if (Array.isArray(keys)) {
return [].concat(keys.map(findLabel));
}
return findLabel(keys);
};You could either clean this up (after @Marsup weighs in) and update your PR, or I can submit a PR to your branch. |
|
Let's wait for @Marsup to weigh in. Once he's approved, A PR submitted to my branch would be great. |
|
Try that see if it works: |
|
Thanks @Marsup . That works. I'll refactor and add more tests and push in a few minutes. |
|
I've submitted https://github.com/BolajiOlajide/joi/pull/1 based upon @Marsup's beautiful simplification, which produces the following test results: |
Updates for handling labels for nested peers
|
It looks alright, just missing the docs. I changed the base branch as it's going to be a breaking change. |
|
What do I need to do @Marsup ? |
|
Actually let me think about that one, it's looking more and more like we could refs instead of plain strings. |
dd7c4ec to
11afe35
Compare
|
Hey @Marsup , |
6c672b8 to
6aedbb2
Compare
|
Thanks a lot! |
|
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
What Does This PR Do?
Add Support for nested path with Joi.object().or()
Description Of Task To Be Completed
Add Support for nested path with Joi.object().or()
Any Background Context You Want To Provide?
Check this out
How can this be manually tested?
N/A
What are the relevant github issues?
#1477
Screenshot(s)
N/A