validateBodyController.js 852 B

123456789101112131415161718192021222324252627
  1. const { body } = require('express-validator/check');
  2. const { validationResult } = require('express-validator/check');
  3. exports.validationCriterias = [
  4. body('email')
  5. .exists()
  6. .withMessage('Email must be provided.')
  7. .isEmail()
  8. .withMessage('Email is not valid.')
  9. .trim()
  10. .normalizeEmail(),
  11. body('password', 'Password must be at least 8 chars long.')
  12. .exists()
  13. .withMessage('Password must be provided.')
  14. .isLength({ min: 8 }),
  15. ];
  16. exports.validateBody = (req, res, next) => {
  17. const errors = validationResult(req);
  18. if (!errors.isEmpty()) {
  19. const errorsObj = errors.mapped();
  20. const emailError = errorsObj.email && errorsObj.email.msg;
  21. const passwordError = errorsObj.password && errorsObj.password.msg;
  22. return res.status(400).json({ error: emailError || passwordError });
  23. }
  24. return next();
  25. };