From 5a4bc50f06274ce3ea046dfd1560f37ae3ced6a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Jeancolas?= Date: Fri, 3 Feb 2017 17:00:44 -0500 Subject: [PATCH] Auto cast boolean strings/numbers after validation --- tests/unit/RequestValidator.ts | 16 ++++++++++++++++ ts/RequestValidator.ts | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/unit/RequestValidator.ts b/tests/unit/RequestValidator.ts index 3be1c31..2af21b0 100644 --- a/tests/unit/RequestValidator.ts +++ b/tests/unit/RequestValidator.ts @@ -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( { diff --git a/ts/RequestValidator.ts b/ts/RequestValidator.ts index 27b574d..ef6c37c 100644 --- a/ts/RequestValidator.ts +++ b/ts/RequestValidator.ts @@ -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;