Having trouble implementing express-validator's imperative validations in TypeScript because the type for validations
cannot be found.
// reusable function for multiple 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) => {
// no validation errors in the request.
const user = await User.create({ ... });
});