实现快速验证器命令式验证时,哪种验证类型合适?

时间:2020-06-26 15:12:28

标签: typescript express express-validator

试图在TypeScript中实现express-validatorimperative validations,但找不到validations的类型。

// can be reused by many routes
const validate = validations => {
  return async (req, res, next) => {
    await Promise.all(validations.map(validation => validation.run(req)));

    const errors = validationResult(req);
    if (errors.isEmpty()) {
      return next();
    }

    res.status(422).json({ errors: errors.array() });
  };
};

app.post('/api/create-user', validate([
  body('email').isEmail(),
  body('password').isLength({ min: 6 })
]), async (req, res, next) => {
  // request is guaranteed to not have any validation errors.
  const user = await User.create({ ... });
});

1 个答案:

答案 0 :(得分:0)

找到了!类型为ValidationChain[]