Skip to content

Commit

Permalink
Bugfix: when type is numeric value 0 should valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Cordinier committed Feb 23, 2017
1 parent 90eed83 commit 7a45cb9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ validation: {
##### Auto formatting
By default, every `numeric` and `date` inputs will be automatically transformed respectively to valid `number` and `Date` objects after validation. So you can directly use valid objects in your code.
By default, every `numeric`, `date` and `boolean` inputs will be automatically transformed respectively to valid `number`, `Date` and `boolean` objects after validation. So you can directly use valid objects in your code.
##### Extra formatting, data transformation
Expand Down
1 change: 1 addition & 0 deletions js/ParamValidation.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/ParamValidation.js.map

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

16 changes: 14 additions & 2 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.

1 change: 1 addition & 0 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.

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

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

it('RequestValidator::validate() null values', () => {
expected = undefined;
validator.validate(
Expand Down
10 changes: 8 additions & 2 deletions ts/RequestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,14 @@ export class RequestValidator {

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

0 comments on commit 7a45cb9

Please sign in to comment.