快速验证器错误检查

时间:2019-04-04 02:28:30

标签: express-validator

我正在使用Express-validator 5.3.1。我定义了这样的密码检查:

check('password')
  .not().isEmpty().withMessage('Password can\'t be null')
  .isString()
  .isLength({ min: 8 }),
check('retypePassword')
  .not().isEmpty().withMessage('Password can\'t be null')
  .isString().withMessage('Password must be a string')
  .custom((value, { req }) => {
    if(value.trim() !== req.body.password.trim()) {
      throw new Error ('Password confirmation does not match password');
    }
  }),

为了进行测试,我发送了相同的密码并输入retypePassword('password')并收到错误消息:

  

[       {           “ location”:“ body”,           “ param”:“ retypePassword”,           “ value”:“ password”,           “ msg”:“无效值”       }   ]

我定义了带有retypePassword的所有错误的消息,所以怎么回事?

1 个答案:

答案 0 :(得分:1)

实施autocomplete-text-view验证时,如果验证成功,则必须返回autocomplete-text-view,否则将收到“无效值”错误消息。因此,您只需添加一个custom语句

true