Skip to content

Commit

Permalink
add label support for nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
BolajiOlajide committed Aug 5, 2018
1 parent d642ece commit e01a087
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/types/object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ internals.keysToLabels = function (schema, keys) {

const findLabel = function (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)) {
Expand Down
23 changes: 23 additions & 0 deletions test/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,29 @@ describe('object', () => {
]);
});

it('should apply labels with nested objects', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).with('a', ['b.c']);
const error = schema.validate({ a: 1 , b: { d: 2 } }).error;
expect(error).to.be.an.error('"first" missing required peer "second"');
expect(error.details).to.equal([{
message: '"first" missing required peer "second"',
path: ['a'],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'second',
label: 'a',
key: 'a'
}
}]);
});

describe('keys()', () => {

it('allows any key', async () => {
Expand Down

0 comments on commit e01a087

Please sign in to comment.