mirror of
https://github.com/20kaushik02/express-sequelize-backend-template.git
synced 2026-01-25 07:14:06 +00:00
Boilerplates, base config, folder structure, utils, etc.
This commit is contained in:
28
validators/index.js
Normal file
28
validators/index.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const { validationResult } = require("express-validator");
|
||||
|
||||
const typedefs = require("../typedefs");
|
||||
|
||||
/**
|
||||
* Middleware to call after running validators to finalize result
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Next} next
|
||||
*/
|
||||
const validate = (req, res, next) => {
|
||||
const errors = validationResult(req);
|
||||
if (errors.isEmpty()) {
|
||||
return next();
|
||||
}
|
||||
const extractedErrors = []
|
||||
errors.array().map(err => extractedErrors.push({
|
||||
[err.param]: err.msg
|
||||
}));
|
||||
|
||||
return res.status(400).json({
|
||||
message: extractedErrors,
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validate,
|
||||
}
|
||||
Reference in New Issue
Block a user