0

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() });
}
Anudeep GI
  • 931
  • 3
  • 14
  • 45
  • I assume what you want is to first check for e-mail and then check if the password matches like how we do Microsoft or Google logins? In that case, I recommend that you separate your endpoints – ionizer Nov 01 '19 at 07:05

1 Answers1

1

Your function createValidationFor should not receive req, res, next parameters because it is not a middleware, it just inserts (returns) the appropriate validation chain according to type value as the only needed parameter (in your example type = 'email'). Only function checkValidationResult is responsible for all the middleware stuff: sending error if validation errors exist or pass control with next().

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() });
};

router.post(
    '/login',
    createValidationFor('email'),
    checkValidationResult,
    (req, res, next) => {
        res.json({ allGood: true });
    }
);
Hlib Derbenov
  • 916
  • 1
  • 7
  • 15