Просмотр исходного кода

Fix string coercion of number types in body verifier

production
Jack Foltz 5 лет назад
Родитель
Сommit
d1610db1fe
Подписано: foltik <jack@foltz.io> Идентификатор GPG ключа: D1F0331758D1F29A
2 измененных файлов: 19 добавлений и 11 удалений
  1. +16
    -8
      app/util/verifyBody.js
  2. +3
    -3
      test/middleware.js

+ 16
- 8
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)


+ 3
- 3
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)));


Загрузка…
Отмена
Сохранить