Skip to content

Commit 7a45cb9

Browse files
author
Yannick Cordinier
committed
Bugfix: when type is numeric value 0 should valid
1 parent 90eed83 commit 7a45cb9

9 files changed

+57
-8
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ validation: {
139139
140140
##### Auto formatting
141141
142-
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.
142+
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.
143143
144144
##### Extra formatting, data transformation
145145

Diff for: js/ParamValidation.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: js/ParamValidation.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: js/RequestValidator.js

+14-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: js/RequestValidator.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: js/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: js/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/unit/RequestValidator.ts

+29
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,35 @@ describe('RequestValidator', () => {
222222
);
223223
});
224224

225+
it('RequestValidator::validate() numeric', () => {
226+
expected = undefined;
227+
validator.validate(
228+
{
229+
route: {
230+
validation: {
231+
query: {
232+
id: {type: 'numeric', required: false}
233+
}
234+
}
235+
}, query: {id: '1'}
236+
},
237+
null, test
238+
);
239+
expected = undefined;
240+
validator.validate(
241+
{
242+
route: {
243+
validation: {
244+
query: {
245+
id: {type: 'numeric', required: false, min: 0}
246+
}
247+
}
248+
}, query: {id: '0'}
249+
},
250+
null, test
251+
);
252+
});
253+
225254
it('RequestValidator::validate() null values', () => {
226255
expected = undefined;
227256
validator.validate(

Diff for: ts/RequestValidator.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,14 @@ export class RequestValidator {
308308

309309
if (inputType === 'undefined' || typeValidation.value === null) {
310310
return true;
311-
} else if (typeValidation.type === 'numeric' || typeValidation.type === 'number') {
312-
const isNumeric = !isNaN(typeValidation.value);
311+
} else if (typeValidation.type === 'numeric') {
312+
const isNumeric = !(typeValidation.value.length === 0) && !isNaN(typeValidation.value) ;
313+
if (isNumeric === true) {
314+
typeValidation.value = parseInt(typeValidation.value, 10);
315+
}
316+
return isNumeric;
317+
} else if (typeValidation.type === 'number') {
318+
const isNumeric = !isNaN(typeValidation.value) ;
313319
if (isNumeric === true) {
314320
typeValidation.value = parseInt(typeValidation.value, 10);
315321
}

0 commit comments

Comments
 (0)