diff --git a/app/util/verifyBody.js b/app/util/verifyBody.js index abdf8ec..f673de7 100644 --- a/app/util/verifyBody.js +++ b/app/util/verifyBody.js @@ -8,18 +8,26 @@ const verifyProp = async (prop, expected) => { return; if (expected.type) { - if (expected.type === 'date' && isNaN(new Date(prop))) - throw {code: 400, message: `${expected.name} malformed.`}; - else if (expected.type === 'array' && !(prop instanceof Array)) - throw {code: 400, message: `${expected.name} malformed.`}; - else if (typeof prop !== expected.type) - throw {code: 400, message: `${expected.name} malformed.`}; + if (expected.type === 'date') { + if (isNaN(new Date(prop))) + throw {code: 400, message: `${expected.name} malformed.`}; + } else if (expected.type === 'array') { + if (!(prop instanceof Array)) + throw {code: 400, message: `${expected.name} malformed.`}; + } else if (expected.type === 'number') { + if (isNaN(parseInt(prop))) + throw {code: 400, message: `${expected.name} malformed.`}; + } else { + if (typeof prop !== expected.type) + throw {code: 400, message: `${expected.name} malformed.`}; + } } - if (expected.min && prop < expected.min) + + if (expected.min && parseInt(prop) < expected.min) throw {code: 400, message: `${expected.name} too small.`}; - if (expected.max && prop > expected.max) + if (expected.max && parseInt(prop) > expected.max) throw {code: 400, message: `${expected.name} too large.`}; if (expected.maxLength && prop.length > expected.maxLength) diff --git a/test/middleware.js b/test/middleware.js index d5cf27d..c0c4d91 100644 --- a/test/middleware.js +++ b/test/middleware.js @@ -28,11 +28,11 @@ describe('Body Verification', () => { expected: [{name: 'test', type: 'date'}], body: {test: '11/12/2018'} }, { - expected: [{name: 'test', type: 'date'}], - body: {test: 1546368715} + expected: [{name: 'test', type: 'number'}], + body: {test: '1546368715'} }, { expected: [{name: 'test', type: 'number', min: 12, max: 16}], - body: {test: 16} + body: {test: '16'} }]; return Promise.all(tests.map(test => testVerifyBody(test.body, test.expected)));