使用expressjs内部(res,req)函数而不是中间件内部进行验证

使用expressjs内部(res,req)函数而不是中间件内部进行验证,express,express-validator,Express,Express Validator,我正在使用ExpressValidator库在后端进行验证。这是图书馆: 我有这个密码 // ...rest of the initial code omitted for simplicity. const { check, validationResult } = require('express-validator'); app.post('/user', [ check('username').isEmail(), check('password').isLength({ m

我正在使用ExpressValidator库在后端进行验证。这是图书馆:

我有这个密码

// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');

app.post('/user', [
  check('username').isEmail(),
  check('password').isLength({ min: 5 })
], (req, res) => {
  // Finds the validation errors in this request and wraps them in an object with handy functions
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }

  User.create({
    username: req.body.username,
    password: req.body.password
  }).then(user => res.json(user));
});
是否可以在(req,res)=>{}内调用验证函数

我有一些需求,在构建验证数组之前,我需要检查通过请求到达的内容

基本上,我希望能够做到这一点:

app.post('/user', (req, res) => {

  const { importantParam, email, password, firstname, lastname } = request.body
  let validateThis = []

  if(importantParam == true)
     validateThis = [
        check('username').isEmail(),
        check('password').isLength({ min: 5 })
     ]
  else 
     validateThis = [
        check('username').isEmail(),
        check('password').isLength({ min: 5 })
        check('firstname').isLength({ min: 5 })
        check('lastname').isLength({ min: 5 })
     ]

  runValidationFunction(validateThis)

  //now below code can check for validation errors
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }

  User.create({
    username: req.body.username,
    password: req.body.password
  }).then(user => res.json(user));
});
这就是我需要做的,根据其中一个参数是否具有特定值来构造验证数组。在第一个示例中,我不知道如何实现这一点,因为在采用这种方法时,似乎无法访问请求

app.post('/user', validationArray, (req, res) => {}
你知道我如何直接在里面调用expressvalidate函数吗

(req, res) => {}

您可以做的是在自定义验证中运行检查。您还需要
验证器

const { check, body, param } = require('express-validator/check');
const { validator } = require('express-validator');

const checkValidation = () => {
  return [
    check('importantParam')
    .custom((value, {req}) => {
      const data = req.body;
      if (!validator.isEmail(data.username)) throw new Error("Not valid Email");
      if (!validator.isLength(data.password, {min:5})) throw new Error("Not valid password");
      // if importantParam is false, add the following properties to validation 
      if (!value) {
        if (!validator.isLength(data.firstname, {min:5})) throw new Error("Not valid firstname");
        if (!validator.isLength(data.lastname, {min:5})) throw new Error("Not valid lastname");
      } 
      return true;
  })
  ];
};

app.post('/user', checkValidation(), (req, res) => {
    //now below code can check for validation errors
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }
    // do stuff here
});
希望有帮助