Skip to content

Commit

Permalink
Local object().unknown() should override stripUnknown option
Browse files Browse the repository at this point in the history
Fixes #983
  • Loading branch information
Marsup committed Mar 18, 2017
1 parent 2765e90 commit 374f52e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ internals.Object = class extends Any {
}

if ((this._inner.children || this._inner.patterns.length) && unprocessedKeys.length) {
if (options.stripUnknown ||
if ((options.stripUnknown && this._flags.allowUnknown !== true) ||
options.skipFunctions) {

const stripUnknown = options.stripUnknown
Expand Down
20 changes: 20 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,26 @@ describe('object', () => {
[{ a: { b: 5, c: 'ignore' } }, true]
], done);
});

it('overrides stripUnknown at a local level', (done) => {

const schema = Joi.object({
a: Joi.object({
b: Joi.number(),
c: Joi.object({
d: Joi.number()
})
}).unknown()
}).options({ allowUnknown: false, stripUnknown: true });

Helper.validate(schema, [
[{ a: { b: 5 } }, true, null, { a: { b: 5 } }],
[{ a: { b: 'x' } }, false, null, 'child "a" fails because [child "b" fails because ["b" must be a number]]'],
[{ a: { b: 5 }, d: 'ignore' }, true, null, { a: { b: 5 } }],
[{ a: { b: 5, d: 'ignore' } }, true, null, { a: { b: 5, d: 'ignore' } }],
[{ a: { b: 5, c: { e: 'ignore' } } }, true, null, { a: { b: 5, c: {} } }]
], done);
});
});

describe('rename()', () => {
Expand Down

0 comments on commit 374f52e

Please sign in to comment.