Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Express validator如何仅在存在另一个字段时使字段成为必需字段_Javascript_Node.js_Express_Express Validator - Fatal编程技术网

Javascript Express validator如何仅在存在另一个字段时使字段成为必需字段

Javascript Express validator如何仅在存在另一个字段时使字段成为必需字段,javascript,node.js,express,express-validator,Javascript,Node.js,Express,Express Validator,express validator,如何使一个字段仅在另一个字段存在时才成为必需字段 const validateUpdateStore = () => { return [ body('logo').optional().isURL().withMessage('invalid url'), body('email') .optional() .isEmail() .withMessage('email is invalid')

express validator,如何使一个字段仅在另一个字段存在时才成为必需字段

const validateUpdateStore = () => {
  return [
    body('logo').optional().isURL().withMessage('invalid url'),
    body('email')
      .optional()
      .isEmail()
      .withMessage('email is invalid')
      .trim()
      .escape(),
    body('phone').optional().isInt().withMessage('integers only!'),
    body('account_number').optional().isInt().withMessage('integers only!'),
    body('bank_code').optional().isInt().withMessage('integers only!'),
  ];
};
我希望仅当提供了
账号
时才需要
银行代码
字段,反之亦然

增加了对条件验证器的支持。我目前没有在文档中看到它,但是有一个包含更多信息的文档。看起来您应该能够如下定义您的验证:

const validateUpdateStore = () => {
  return [
    body('logo').optional().isURL().withMessage('invalid url'),
    body('email')
      .optional()
      .isEmail()
      .withMessage('email is invalid')
      .trim()
      .escape(),
    body('phone').optional().isInt().withMessage('integers only!'),
    body('account_number')
      .if(body('bank_code').exists()) // if bank code provided
      .not().empty() // then account number is also required
      .isInt() // along with the rest of the validation
      .withMessage('integers only!')
    ,
    body('bank_code')
      .if(body('account_number').exists()) // if account number provided
      .not().empty() // then bank code is also required
      .isInt() // along with the rest of the validation
      .withMessage('integers only!')
    ,
  ];
};