Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/types/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ internals.unknown = function (schema, value, unprocessed, errors, state, prefs)
}
}

const forbidUnknown = !Common.default(schema._flags.unknown, prefs.allowUnknown);
const forbidUnknown = !Common.default(schema._flags.unknown, (prefs.allowUnknown && !state.ancestors.length));
if (forbidUnknown) {
for (const unprocessedKey of unprocessed) {
const localState = state.localize([...state.path, unprocessedKey], []);
Expand Down
42 changes: 42 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,48 @@ describe('Joi', () => {
Helper.validate(Joi.compile({ other: Joi.number() }), [[{ foo: 'bar' }, false, '"foo" is not allowed']]);
});

it('local unknown setting always overrides global allowUnknown setting', () => {
const input = {
a: {
b: "h",
c: "f"
},
d: "h"
};

Helper.validate(
Joi.object({
a: Joi.object().keys({
b: Joi.string()
}).unknown(true)
}),
{ allowUnknown: false },
[[input, false, '"d" is not allowed']]
);

Helper.validate(
Joi.object({
a: Joi.object().keys({
b: Joi.string()
}).unknown(false)
}),
{ allowUnknown: true },
[[input, false, '"a.c" is not allowed']]
);

Helper.validate(
Joi.object({
a: Joi.object().keys({
b: Joi.string()
})
}),
{ allowUnknown: true },
[[input, false, '"a.c" is not allowed']]
);
Comment thread
kamweti marked this conversation as resolved.

});


it('validates required key with multiple options', () => {

const config = {
Expand Down