Reputation: 1547
In a post request of my app, email is considered valid if it's either an empty string or a valid email string, how do I do this with express validator?
Upvotes: 0
Views: 914
Reputation: 119
Taken from the express validator documentation.
req.check('email').optional().isEmail();
//if there is no error, email is either undefined or a valid mail.
Upvotes: 1
Reputation: 53626
You... read the instructions for node-validator? =)
var check = require('validator').check;
...
app.post("...", function(req, res, next) {
try {
check(res.body.thatvar).isEmail();
next();
}
catch (e) { next("TOTALLY NOT EMAIL! O_O"); }
}, ...);
Now go read https://github.com/chriso/node-validator
Upvotes: 0