npm i express-validator
const { check } = require('express-validator');
router.post(
'/signup',
[
check('name').not().isEmpty(),
check('email')
.normalizeEmail() // [email protected] => [email protected]
.isEmail(),
check('password').isLength({ min: 6 }),
],
usersController.signup
);
const signup = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
throw new HttpError('Invalid inputs passed, please check your data.', 422);
}
};