Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

local unknown setting should always take precedence of global allowUnknown setting #2882

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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']]
);
kamweti marked this conversation as resolved.
Show resolved Hide resolved

});


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

const config = {
Expand Down