Express Validator:参数如何传递给验证器函数

时间:2018-04-19 07:33:56

标签: node.js validation express express-validator

我很好奇参数如何传递给验证器中间件,摘录取自express-validator,例如 programming_language 传递给check()函数。

const { check, oneOf, validationResult } = require('express-validator/check');
app.post('/start-freelancing', oneOf([
  check('programming_language').isIn(['javascript', 'java', 'php']),
  check('design_tools').isIn(['photoshop', 'gimp'])
]), (req, res, next) => {
  try {
    validationResult(req).throw();

    // yay! we're good to start selling our skilled services :)))
    res.json(...);
  } catch (err) {
    // Oh noes. This user doesn't have enough skills for this...
    res.status(422).json(...);
  }
});

1 个答案:

答案 0 :(得分:1)

check('programming_language')

'programming_language'是一个请求参数。无需单独传递任何内容。它来自

req.body.programming_language
req.cookies.programming_language
req.headers.programming_language
req.params.programming_language
req.query.programming_language

'design_tools'

也是如此
req.body.design_tools
req.cookies.design_tools
req.headers.design_tools
req.params.design_tools
req.query.design_tools