Skip to content

fix: null typecheck in parameter validation #849

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

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 src/ActionParameterHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ActionParameterHandler<T extends BaseDriver> {
// check cases when parameter is required but its empty and throw errors in this case
if (param.required) {
const isValueEmpty = value === null || value === undefined || value === '';
const isValueEmptyObject = typeof value === 'object' && Object.keys(value).length === 0;
const isValueEmptyObject = typeof value === 'object' && value !== null && Object.keys(value).length === 0;

if (param.type === 'body' && !param.name && (isValueEmpty || isValueEmptyObject)) {
// body has a special check and error message
Expand Down
20 changes: 20 additions & 0 deletions test/ActionParameterHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ describe('ActionParameterHandler', () => {

expect(processedValue).to.be.eq(undefined);
});

it('handle - null provided', async () => {
try {
const param = buildParamMetadata('id', 'param', true);
const action = {
request: {
params: {
id: null,
},
},
response: {},
};

await actionParameterHandler.handle(action, param);
} catch (error) {
expect(error.toString()).to.be.eq(
'ParamRequiredError: Parameter "id" is required for request on undefined undefined'
);
}
});
});
describe('negative', () => {
it('handle - throws error if the parameter is required', async () => {
Expand Down