check.js 514 B

123456789101112131415161718
  1. const { validationResult } = require('express-validator');
  2. // 字段验证中间件
  3. module.exports = validations => {
  4. return async (req, res, next) => {
  5. await Promise.all(validations.map(validation => validation.run(req)));
  6. const errors = validationResult(req);
  7. if (errors.isEmpty()) {
  8. return next();
  9. }
  10. res.status(400).json({
  11. code: -1,
  12. msg: errors
  13. .array()
  14. .reduce((pre, item) => `${pre}${item.location}.${item.param} is ${item.msg};`, ''),
  15. });
  16. };
  17. };