【问题标题】:Validating with express Validator's .equals()使用 express Validator .equals() 进行验证
【发布时间】:2019-04-10 16:18:51
【问题描述】:

我正在尝试验证对端点的请求,使用 express 验证器的 .equals() 检查用户类型的输入是否等于“储蓄”或“当前”。但是 equals() 只选择第一个值。

我尝试使用 ||声明

 const creatAccount = [

    check('type').not().isEmpty().withMessage('Please specify account type'),

    check('type').equals('savings' || 'current').withMessage('Invalid Account Type: Account Type can be either "savings" or "current"'),

    check('initialDeposit').not().isEmpty().withMessage('specify a Initial Deposit'),

    (req, res, next) => {

      const errors = validationResult(req);

      const errMessages = [];

      if (!errors.isEmpty()) {

        errors.array().forEach((err) => {

          errMessages.push(err.msg);

        });
        return res.status(401).json({
          status: 401,
          error: errMessages,
        });
      }
      return next();
    },
  ]

【问题讨论】:

    标签: javascript express endpoint express-validator


    【解决方案1】:

    那是因为

    'savings' || 'current'
    

    首先被解析(作为'savings')并作为参数传递给.equals()。阅读更多关于如何解决的信息|| operator

    有几个选项:

    const message = 'Invalid Account Type: Account Type can be either "savings" or "current"';
    check('type').equals('savings').withMessage(message);
    check('type').equals('current').withMessage(message);
    

    此外,您可以将其移至函数

    const validateType = type => {
       const message = 'Invalid Account Type: Account Type can be either "savings" or "current"';
       check('type').equals(type).withMessage(message);
    }
    validateType('savings');
    validateType('current');
    

    推荐选项

    使用oneOf

    const { check, oneOf } = require('express-validator/check');
    const message = 'Invalid Account Type: Account Type can be either "savings" or "current"';
    const creatAccount = [
    
        check('type').not().isEmpty().withMessage('Please specify account type'),
    
        oneOf([
           check('type').equals('savings'),
           check('type').equals('current'),
         ], message),
    
        check('initialDeposit').not().isEmpty().withMessage('specify a Initial Deposit'),
    
        (req, res, next) => {
    
          const errors = validationResult(req);
    
          const errMessages = [];
    
          if (!errors.isEmpty()) {
    
            errors.array().forEach((err) => {
    
              errMessages.push(err.msg);
    
            });
            return res.status(401).json({
              status: 401,
              error: errMessages,
            });
          }
          return next();
        },
      ]
    

    【讨论】:

    • 使用两个语句不起作用,每个语句都会失败,这将返回错误。我会试试这个功能
    • 我得到 oneOf 没有定义
    • 请务必将其添加到您的需求语句中const { check, oneOf } = require('express-validator/check');
    • 非常感谢!!但是自定义错误消息不起作用。我会调查的。
    • @Pelumi 查看更新的答案。 oneOf 接受消息作为第二个参数,而不是在每次检查中都有消息。如果此答案对您有所帮助,请务必投票并将其正确设置为已接受的答案:)
    猜你喜欢
    • 2015-04-30
    • 2012-09-14
    • 2020-06-29
    • 2014-08-05
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多