mirror of
https://github.com/Foltik/Shimapan
synced 2025-02-25 16:50:35 -05:00
Add min/max constraints to bodyVerifier
This commit is contained in:
parent
122fee21c6
commit
cfcafdb993
@ -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.`};
|
||||
|
||||
|
@ -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…
Reference in New Issue
Block a user