Преглед на файлове

Add min/max constraints to bodyVerifier

production
Jack Foltz преди 5 години
родител
ревизия
cfcafdb993
Signed by: foltik <jack@foltz.io> GPG Key ID: D1F0331758D1F29A
променени са 2 файла, в които са добавени 19 реда и са изтрити 0 реда
  1. +6
    -0
      app/util/verifyBody.js
  2. +13
    -0
      test/middleware.js

+ 6
- 0
app/util/verifyBody.js Целия файл

@@ -16,6 +16,12 @@ const verifyProp = async (prop, expected) => {
throw {code: 400, message: `${expected.name} malformed.`};
}

if (expected.min && prop < expected.min)
throw {code: 400, message: `${expected.name} too small.`};

if (expected.max && prop > expected.max)
throw {code: 400, message: `${expected.name} too large.`};

if (expected.maxLength && prop.length > expected.maxLength)
throw {code: 400, message: `${expected.name} too long.`};



+ 13
- 0
test/middleware.js Целия файл

@@ -30,6 +30,9 @@ describe('Body Verification', () => {
}, {
expected: [{name: 'test', type: 'date'}],
body: {test: 1546368715}
}, {
expected: [{name: 'test', type: 'number', min: 12, max: 16}],
body: {test: 16}
}];

return Promise.all(tests.map(test => testVerifyBody(test.body, test.expected)));
@@ -60,6 +63,16 @@ describe('Body Verification', () => {
return testVerifyBody({test: 'test'}, expected, 400, 'test malformed.');
});

it('must error when smaller than the minimum', () => {
const expected = [{name: 'test', type: 'number', min: 10}];
return testVerifyBody({test: 3}, expected, 400, 'test too small.');
});

it('must error when larger than the maximum', () => {
const expected = [{name: 'test', type: 'number', max: 10}];
return testVerifyBody({test: 15}, expected, 400, 'test too large.');
});

it('must error with a length higher than the max', () => {
const expected = [{name: 'test', maxLength: 5}];
return testVerifyBody({test: '123456'}, expected, 400, 'test too long.');


Loading…
Отказ
Запис