I am using express-validator for the first time and I can't find a way to assert if two fields are equal (if that can be done at all).
Example: A form containing 2 times an email address (one as standard confirmation) is submitted. I want to check that the fields match.
I found a workaround by myself which works but I wonder if I'm not just doing something unnecessary. Here is the code (the data is coming through an ajax call):
//routes.js
function validator(req, res, next) {
req.checkBody('name', 'cannot be empty').notEmpty();
req.checkBody('email', 'not valid email').isEmail();
var errors = req.validationErrors(); // up to here standard express-validator
// Custom check to see if confirmation email matches.
if (!errors) errors = [];
if (email !== email_confirm){
errors.push({param: 'email_confirm', msg: 'mail does not match!', value: email_confirm})
}
if (errors.length > 0) {
res.json({msg: 'validation', errors:errors}); // send back the errors
}
else {
// I don't want to insert the email twice in the DB
delete req.body.email_confirm
next(); // this will proceed to the post request that inserts data in the db
}
};
So my question is: is there a native method in express-validator to check if (email===email_confirm)? If not is there a better/more standard way to do what I have done above? I'm quite new to node/express in general. Thank you.