有条件地 运行 签入 express-validator

Conditionally run check in express-validator

我正在尝试有条件地 运行 签入快速验证器,验证器的功能部分很容易处理,因为我正在传递 req 但检查部分不会有条件地 运行。请帮助

我试过将检查部分变成一个函数,但它不起作用。这就是我想要实现的,但是 tenary 失败了

const onewayCheck = body('tripType').equals('one-way') ? [
  body('currentOfficeLocation')
    .exists({ checkFalsy: true })
    .withMessage('currentOfficeLocation Current Office Location is required')
    .isInt()
    .withMessage('currentOfficeLocation Current Office Location must be an integer'),
  body('destination')
    .exists({ checkFalsy: true })
    .withMessage('destination Destination is required')
    .isInt()
    .withMessage('destination Destination must be an integer'),
  body('departureDate')
    .exists({ checkFalsy: true })
    .withMessage('departureDate Departure date is required'),
  body('travelreasons')
    .exists({ checkFalsy: true })
    .withMessage('travelReasons Travel Reasons is required')
    .isString()
    .withMessage('travelReasons Travel Reasons should be strings'),
  body('accommodation')
    .exists({ checkFalsy: true })
    .withMessage('accommodation Accommodation is required')
    .isInt()
    .withMessage('accommodation Accommodation must be an integer'),
] : [];

我想确保检查炒锅

验证期间不需要运行检查。相反,您可以在控制器中检查它。
例如,假设您像这样检查控制器中的验证错误

const errors = validationResult(req);
if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
}

您可以运行通过这种方式return检查是否存在错误

if (req.body.tripType === 'one-way') {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }
}

这样验证逻辑在任何情况下都会 运行,但只有当 tripType 的值为 one-way 时,您才会 return 验证错误。

有人帮我解决了 /// route.js

const router = express.Router();

const validate = (validations, tripType) => {
  return async (req, res, next) => {
    if (req.body.tripType === tripType) {
      await Promise.all(validations.map(validation => validation.run(req)));
    }
    return next();
  };
};

router.post('/request', authenticate, validateRequestType, validate(onewayCheck(), 'one-way'),
  onewayValidateInput, multicityCheck, multicityValidateInput, tripRequest);

export default router;

///in the validation file
const onewayCheck = () => [
  body('currentOfficeLocation')
    .exists({ checkFalsy: true })
    .withMessage('currentOfficeLocation Current Office Location is required')
    .isInt()
    .withMessage('currentOfficeLocation Current Office Location must be an integer'),
  body('destination')
    .exists({ checkFalsy: true })
    .withMessage('destination Destination is required')
    .isInt()
    .withMessage('destination Destination must be an integer'),
  body('departureDate')
    .exists({ checkFalsy: true })
    .withMessage('departureDate Departure date is required'),
  body('travelreasons')
    .exists({ checkFalsy: true })
    .withMessage('travelReasons Travel Reasons is required')
    .isString()
    .withMessage('travelReasons Travel Reasons should be strings'),
  body('accommodation')
    .exists({ checkFalsy: true })
    .withMessage('accommodation Accommodation is required')
    .isInt()
    .withMessage('accommodation Accommodation must be an integer'),
];

我可以通过在路由中使用中间件来解决它,如下所示


router
  .post('/request', validate(onewayCheck(), 'one-way'),
    onewayValidateInput,
    validate(multicityCheck(), 'Multi-city'), multicityValidateInput, tripRequest)

验证函数检查请求的类型


const validate = (validations, tripType) => async (req, res, next) => {
  if (req.body.tripType === tripType) {
    await Promise.all(validations.map(validation => validation.run(req)));
  }
  return next();
};