簡體   English   中英

有條件地在快速驗證器中運行檢查

[英]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() });
}

您可以檢查是否以這種方式返回錯誤

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

這樣,驗證邏輯將在任何情況下運行,但僅當tripType具有值one-way才會返回驗證錯誤。

有人幫我解決了 /// 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)

validate 函數檢查請求的類型


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM