This my current code, it is working fine, But I need to access req.body.type in createValidationFor, if I try to access code req.body validation stops working I don't know why
router.post(
'/login',
createValidationFor('email'),
checkValidationResult,
(req, res, next) => {
res.json({ allGood: true });
} );
function createValidationFor(type) {
switch (type) {
case 'email':
return [
check('email').isEmail().withMessage('must be an email')
];
case 'password':
return [
check('password').isLength({ min: 5 })
];
default:
return [];
} }
function checkValidationResult(req, res, next) {
const result = validationResult(req);
if (result.isEmpty()) {
return next();
}
res.status(422).json({ errors: result.array() }); }
Modified code :- I am trying access req inside the createValidationFor function but validation stops working after that
router.post(
'/login',
createValidationFor,
checkValidationResult,
(req, res, next) => {
res.json({ allGood: true });
}
);
function createValidationFor(req, res) {
var type = req.body.type;
switch (type) {
case 'email':
return [
check('email').isEmail().withMessage('must be an email')
];
case 'password':
return [
check('password').isLength({ min: 5 })
];
default:
return [];
}
}
function checkValidationResult(req, res, next) {
const result = validationResult(req);
if (result.isEmpty()) {
return next();
}
res.status(422).json({ errors: result.array() });
}