An easy way to overwrite Joi, Celebrate and Express error messages
What is Joi?
Joi in play
name is a required field, but
age isn’t. Joi differentiates two by calling
required() function on the specific field. It supports many types such as
binary among others. Their API reference is loaded with this and more information.
What’s more, it’s also possible to define request data that cannot appear together!
Imagine, you have a
/users/signup endpoint responsible for signing up a user and saving the data to a
users collection on MongoDB. With Joi, you can validate the data being sent as part of the request, either on the
request body, params or even
query ! Watch!
Joi for Koa
Express with Joi.
Express also has its own share of glory with celebrate being the Joi middleware of choice.
I believe that was a good introduction to Joi. Onto the business of the day!
Customizing error messages from Joi.
If you send the
/users/signup above using a REST API client, for instance, Postman, without passing an
“child \”email\” fails because [\”email\” is required]”.
This makes sense to you as the developer, but a typical user might not fathom.
Little tweak :)
I hope you are able to use knowledge obtained from this tutorial to make your REST API work the way you want it to.
Since GraphQL internally supports schema and its validation, I have purposefully omitted it from the above statement :D
See you next time!