Skip to content

Commit

Permalink
Auto cast boolean strings/numbers after validation
Browse files Browse the repository at this point in the history
  • Loading branch information
RemyJeancolas committed Feb 3, 2017
1 parent 5afea6a commit 5a4bc50
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/unit/RequestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ describe('RequestValidator', () => {
null, test
);

expected = 'Url: Param foo has invalid type (boolean)';
validator.validate(
{
route: {
validation: {
url: {
foo: {type: 'boolean'}
}
}
}, params: {
foo: 'bar'
}
},
null, test
);

expected = undefined;
validator.validate(
{
Expand Down
6 changes: 5 additions & 1 deletion ts/RequestValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ export class RequestValidator {
}
return isNumeric;
} else if (typeValidation.type === 'boolean') {
return ['0', '1', 'false', 'true', false, true, 0, 1].indexOf(typeValidation.value) !== -1;
const isBoolean = ['0', '1', 'false', 'true', false, true, 0, 1].indexOf(typeValidation.value) !== -1;
if (isBoolean === true) {
typeValidation.value = ['1', 'true', true, 1].indexOf(typeValidation.value) !== -1;
}
return isBoolean;
} else if (typeValidation.type === 'date') {
if (typeof typeValidation.value === 'object' && typeof typeValidation.value.getTime === 'function') {
return true;
Expand Down

0 comments on commit 5a4bc50

Please sign in to comment.