2018-07-28 16:49:54 -04:00
|
|
|
|
|
|
|
// Verifies the request body is well formed
|
|
|
|
// expectedProps follows the format:
|
|
|
|
// [{name: 'myList', instance: 'Array'}, {name: 'myVar', type: 'string', optional: true}, etc.]
|
|
|
|
const verifyBody = expectedProps =>
|
|
|
|
(req, res, next) => {
|
|
|
|
for (let i = 0; i < expectedProps.length; i++) {
|
|
|
|
const expected = expectedProps[i];
|
|
|
|
const prop = req.body[expected.name];
|
|
|
|
|
|
|
|
if (!expected.optional && !prop)
|
|
|
|
return res.status(400).json({message: expected.name + ' not specified.'});
|
|
|
|
|
2018-07-28 16:53:02 -04:00
|
|
|
if (prop && expected.type && typeof prop !== expected.type)
|
2018-07-28 16:49:54 -04:00
|
|
|
return res.status(400).json({message: expected.name + ' malformed.'});
|
|
|
|
|
2018-07-28 16:53:02 -04:00
|
|
|
if (prop && expected.instance && !(prop instanceof expected.instance))
|
2018-07-28 16:49:54 -04:00
|
|
|
return res.status(400).json({message: expected.name + ' malformed.'});
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = verifyBody;
|