Reputation: 13
I got this form with optional email and I'm using express-validator to make a server side validation.
When I submit, it's suposed to validate and then process the data to save it.
routes/users.js
router.post('/save', users.validationRules(), users.save)
controllers/usersController.js
usersController.validationRules = () => {
return [
// ...
// Some other validation rules that works
// ...
check('email', 'You need a valid Email')
.optional()
.isEmail(),
// ... More validation rules that also works ...
]
}
usersController.save = (req, res) => {
let errors = validationResult(req)
console.log(errors);
}
It shows me the error message like if it were ignoring the optional()
method. Do you guys have any idea about what is happening or what am I doing wrong?
Upvotes: 0
Views: 4161
Reputation: 971
You can try use .optional({ checkFalsy: true })
to check against falsy values (eg "", 0, false, null).
More about it from the docs
Upvotes: 2