如何在单独的 mw 函数中提取快速验证器检查?

How to extract express-validator checks in separate mw-functions?

这工作正常,虽然它非常混乱:

//./user/router.js

const express = require('express')
const router = express.Router()
const controller = require('./controller')

// mw
const { check } = require('express-validator')

router.get('/register', controller.register)
router.post(
   '/register',
   [
      check('username')
         .not()
         .isEmpty()
         .withMessage('Username is required'),
      check('email')
         .isEmail()
         .withMessage('Incorrect email.'),
      check('password')
         .not()
         .isEmpty()
         .withMessage('Password is required.')
         .isLength({ min: 3 })
         .withMessage('Should be more then 3 characters.'),
   ],
   controller.register__post,
)
module.exports = router

目的是在单独的中间件函数中提取验证。所以路由器文件将是干净的和直截了当的

//./user/router.js

const express = require('express')
const router = express.Router()
const controller = require('./controller')

// mw
const { validateRegister } = require('./mw/validation')


router.get('/register', controller.register)
router.post('/register', validateRegister, controller.register__post)

module.exports = router


这不会引发任何错误,但验证错误不在 validationResult(req):

//./user/mw/validation.js

const { check } = require('express-validator')

exports.validateRegister = function(req, res, next) {

   check('username')
      .not()
      .isEmpty()
      .withMessage('Username is required')
   check('email')
      .isEmail()
      .withMessage('Incorrect email.')
   check('password')
      .not()
      .isEmpty()
      .withMessage('Password is required.')
      .isLength({ min: 3 })
      .withMessage('Should be more then 3 characters.')

   return next()

}

__

另外:check()函数在中间件功能方面是如何工作的?我的意思是,可以将 check-函数作为列表

传递
//./user/router.js

...
router.post('/register', [
      check('username').not().isEmpty(),
      check('email').isEmail(),
      check('password').isLength({ min: 3 }),
   ],
   controller.register__post)
...

但是 check-函数不调用 next(),所以这行不通:

//./user/router.js

const { validateRegister } = require('./mw/validation')

...

router.post('/register', validateRegister, controller.register__post)

//./user/mw/validation

const { check } = require('express-validator')

exports.validateRegister = function(req, res, next) {
   return check('email').isEmail()
}

在这种情况下使用 validator (on which express-validator 会很方便)。 验证方法相同。

中间件函数

//./user/mw.js

const { isEmail, isLength, isEmpty } = require('validator')

exports.validateRegister = function(req, res, next) {
   const { username, email, password } = req.body
   const errors = []

   if (isEmpty(username))
      errors.push({
         param: 'username',
         msg: 'Имя пользователя обязательно.',
      })

   if (!isEmail(email))
      errors.push({ param: 'email', msg: 'Некорректный email.' })

   if (!isLength(password, { min: 3 }))
      errors.push({
         param: 'password',
         msg: 'Пароль должен быть не менее 3 символов.',
      })

   req.errors = errors
   return next()
}

路由器

//./user/router.js

const express = require('express')
const router = express.Router()
const controller = require('./controller')
const { validateRegister } = require('./mw')

router.get('/register', controller.register)
router.post('/register', validateRegister, controller.register__post)


module.exports = router