cheke
Version:
Express request validator with object's style response body and inspired by Laravel's Validator
28 lines (24 loc) • 672 B
JavaScript
const hasErrors = require('./hasErrors');
const requests = ['params', 'body', 'query'];
const cheke = ({ errors = 'errors', ...args }) => async (req, res, next) => {
if (typeof errors !== 'string') throw Error('errors should be a string');
let failed;
await new Promise(resolve => {
requests.forEach(async value => {
if (failed) return;
if (args[value]) {
failed = await hasErrors({
data: req[value],
reqRules: args[value],
path: value,
});
resolve();
}
});
});
if (failed) {
return res.status(400).json({ [errors]: failed });
}
return next();
};
module.exports = cheke;