Search code examples
typescriptexpressexpress-validator

What is the right type for validations when implementing express-validator imperative validations?


Trying to implement express-validator’s imperative validations in TypeScript, but can’t find the type for 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({ ... });
});

Solution

  • Found it! The type is ValidationChain[].