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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better unknown handling configuration #2743

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ declare namespace Joi {

type PresenceMode = 'optional' | 'required' | 'forbidden';

type AllowMode = boolean | 'allowed' | 'forbidden' | 'warn';

interface ErrorFormattingOptions {
/**
* when true, error message templates will escape special characters to HTML entities, for security purposes.
Expand Down Expand Up @@ -104,7 +106,10 @@ declare namespace Joi {
*
* @default false
*/
allowUnknown?: boolean;
allowUnknown?: AllowMode | {
mode: AllowMode,
type?: string,
};
/**
* when true, schema caching is enabled (for schemas with explicit caching rules).
*
Expand Down
18 changes: 15 additions & 3 deletions lib/types/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,18 +975,30 @@ internals.unknown = function (schema, value, unprocessed, errors, state, prefs)
}
}

const forbidUnknown = !Common.default(schema._flags.unknown, prefs.allowUnknown);
if (forbidUnknown) {
const allowUnknown = Common.default(schema._flags.unknown, prefs.allowUnknown);
const allowUnknownIsObject = typeof allowUnknown === 'object' && !Array.isArray(allowUnknown) && allowUnknown !== null;
const type = (allowUnknownIsObject ? allowUnknown.type : undefined) || 'object.unknown';

if (allowUnknown === false || allowUnknown === undefined || allowUnknown === 'forbidden' || (allowUnknownIsObject && allowUnknown.mode === 'forbidden')) {
for (const unprocessedKey of unprocessed) {
const localState = state.localize([...state.path, unprocessedKey], []);
const report = schema.$_createError('object.unknown', value[unprocessedKey], { child: unprocessedKey }, localState, prefs, { flags: false });
const report = schema.$_createError(type, value[unprocessedKey], { child: unprocessedKey }, localState, prefs, { flags: false });
if (prefs.abortEarly) {
return { value, errors: report };
}

errors.push(report);
}
}
else if (allowUnknown === 'warn' || (allowUnknownIsObject && allowUnknown.mode === 'warn')) {

for (const unprocessedKey of unprocessed) {
const localState = state.localize([...state.path, unprocessedKey], []);
const report = schema.$_createError(type, value[unprocessedKey], { child: unprocessedKey }, localState, prefs, { flags: false });

state.mainstay.warnings.push(report);
}
}
};


Expand Down
41 changes: 41 additions & 0 deletions test/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,47 @@ describe('Validator', () => {
}
});
});

it('reports warnings with nested options', () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll definitely want more tests here to cover root level unknown as well as allowUnknown: 'forbidden'


const schema = Joi.object({
nested: Joi.object({
validKey: Joi.string()
})
}).warning('custom.x').message('test');

const allowUnknown = {
mode: 'warn',
type: 'custom.x'
};

const input = {
nested: {
validKey: 'some string',
invalidKey: 'oh no!'
}
};

const { value, error, warning } = schema.validate(input, { allowUnknown });
expect(value).to.equal(input);
expect(error).to.not.exist();
expect(warning).to.equal({
message: 'test',
details: [
{
message: 'test',
path: ['nested', 'invalidKey'],
type: 'custom.x',
context: {
child: 'invalidKey',
key: 'invalidKey',
label: 'nested.invalidKey',
value: 'oh no!'
}
}
]
});
});
});

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