Skip to content

Commit

Permalink
Merge pull request #11 from SSENSE/bug/number_null
Browse files Browse the repository at this point in the history
Fix bug where validation is failing when type=number and value=null
  • Loading branch information
RemyJeancolas authored Jan 17, 2017
2 parents 5832261 + 7ce9f14 commit 7939686
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion js/ParamValidation.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions js/RequestValidator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/RequestValidator.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions tests/unit/RequestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,48 @@ describe('RequestValidator', () => {
});
});

it('RequestValidator::validate() number', () => {
expected = undefined;
validator.validate(
{
route: {
validation: {
query: {
id: {type: 'number', required: false}
}
}
}, query: {}
},
null, test
);

expected = undefined;
const req: any = {
route: {
validation: {
body: {
id: {type: 'number', required: false, min: 0}
}
}
}, params: {
id: null
}
};
validator.validate(
{
route: {
validation: {
body: {
id: {type: 'number', required: false, min: 0}
}
}
}, params: {
id: null
}
}, null, test
);
});

it('RequestValidator::validate() min', () => {
expected = 'Query: Param id must have a minimum length of 2';
validator.validate(
Expand Down
13 changes: 8 additions & 5 deletions ts/RequestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ export class RequestValidator {
}

// Check min
if (RequestValidator.checkMin(input[key], paramValidation.min) !== true) {
errorMessages.push(
this.getErrorMessage(key, 'min', `Param ${key} must have a minimum length of ${paramValidation.min}`)
);
// Pass if min=0 and value==null
if (paramValidation.min !== 0 && input[key] !== null) {
if (RequestValidator.checkMin(input[key], paramValidation.min) !== true) {
errorMessages.push(
this.getErrorMessage(key, 'min', `Param ${key} must have a minimum length of ${paramValidation.min}`)
);
}
}

// Check max
Expand Down Expand Up @@ -302,7 +305,7 @@ export class RequestValidator {

if (inputType === 'undefined') {
return true;
} else if (typeValidation.type === 'numeric') {
} else if (typeValidation.type === 'numeric' || typeValidation.type === 'number') {
const isNumeric = !isNaN(typeValidation.value);
if (isNumeric === true) {
typeValidation.value = parseInt(typeValidation.value, 10);
Expand Down

0 comments on commit 7939686

Please sign in to comment.