Procházet zdrojové kódy

Add min/max constraints to bodyVerifier

production
Jack Foltz před 5 roky
rodič
revize
cfcafdb993
Podepsáno: foltik <jack@foltz.io> ID GPG klíče: D1F0331758D1F29A
2 změnil soubory, kde provedl 19 přidání a 0 odebrání
  1. +6
    -0
      app/util/verifyBody.js
  2. +13
    -0
      test/middleware.js

+ 6
- 0
app/util/verifyBody.js Zobrazit soubor

@@ -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 Zobrazit soubor

@@ -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.');


Načítá se…
Zrušit
Uložit