jay
jay

Reputation: 1

I'm trying to use express-validators in a function

Everyone says to use an array but I want to see how to would do it in a function.

I have something like this

const checkPasswordConfirm = () => {
    body('password-confirm').custom((value, { req }) => {
        if(value !== req.body.password) {
            throw new Error('Passwords confirmation does not match password')
        }
    })
}

Upvotes: 0

Views: 41

Answers (1)

NARGIS PARWEEN
NARGIS PARWEEN

Reputation: 1596

Here, you need to return true of validation passes

enter code heconst { body } = require('express-validator');

const checkPasswordConfirm = () => {
return body('password-confirm').custom((value, { req }) => {
    if (value !== req.body.password) {
        throw new Error('Password confirmation does not match password');
    }
    return true; // Return true if validation passes
  });
};

module.exports = checkPasswordConfirm;re

Upvotes: 0

Related Questions