guys. I am trying to validate email format by using Joi and really new to coding.
By using the following syntax, it works for me to display custom error messages.
However, the result only showed the "invalid email format" and never showed "cannot be an empty field" even I left the input empty.
schema = Joi.object().keys({
email: Joi.string()
.empty()
.email()
.error(errors => {
errors.forEach(err => {
switch (err.type) {
case "any.empty":
err.message = `cannot be an empty field`;
break;
case "string.email":
err.message = `invalid email format`;
break;
default:
break;
}
});
return errors;
})
});
Thanks in advance. Cheers!