Reputation: 81
First of all Hello,
I work on an API with Node, Express & Mongoose. I also use Joi param validation to validate my param, but I have a problem with the error message when a required param inside an object is missing.
See exemple below :
my param-validation.js
body: {
nameBusiness: Joi.string().required(),
street: Joi.string().required(),
cityName: Joi.string().required(),
zipCode: Joi.number().required(),
account: Joi.object({
birthday: Joi.number().required(),
firstName: Joi.string().required(),
lastName: Joi.string().required(),
email: Joi.string().regex(/^[^\s@]+@[^\s@]+\.[^\s@]+$/).required(),
}).required()
}
My problem is when I make a request without the field "firstName" the error is :
"\"firstName\" is required
but I think it's not clear for me the error should be : "\"account.firstName\" is required
.
Someone know how Joi can display this error ?
Upvotes: 3
Views: 1441
Reputation: 261
In your case, default message will be the same as you are mentioning. For complete path such as "account.firstName" you have look for path key in error object.
You can find his property in "validationErrorObject" like:-
if(validationResult.error){
validationResult.error.details.forEach(function(error){
if(error.path.length > 1){
console.log(error.path.join("."));
}else{
console.log(error.path[0]);
}
});
}
Upvotes: 2