Validate the Fastify Input with Joi
Editor’s note: This is a cross-post written by Senior Software Developer, Manuel Spigolon. Manuel has his own blog at backend.cafe where you can subscribe for updates and find more great posts. Some of the links in the article point to Manuel’s personal GitHub account.
You can use joi
with fastify
without any problem!
If you are an hapi
developer, you may know the joi
library. It is a powerful validation library that allows you to define programmatic schemas for your data and validate them with ease.
One of the hardest things about migrating from hapi
to fastify
is the complexity that’s involved for you to be able to keep using joi
for the route's input validation.
But now, thanks to the joi-compiler
library, you can use joi
with fastify
without any problem!
“ℹ️ Note If you have been afraid to use it because it has very few downloads, don't worry — it is a new library and I'm the author. And, if you don't know me, I'm a Fastify core maintainer and I'm the co-author of the Fastify book too!
”
The joi-compiler
module replaces the default ajv
schema compiler with a joi
-based one and allows you to use joi
schemas to validate your data. Let's see how to use it!
Integrate joi
with fastify
After you have installed the latest fastify@4
and joi-compiler
module:
If you run this code, you will be able to test the joi
validation with the following command:
As you can see, the joi
schema is working as expected! In a few lines of code, we have integrated joi
with fastify
and we are able to use it to validate the input of your application routes.
How it works
The joi-compiler
module is a Fastify schema compiler, it is used by Fastify to build the components during the startup to guarantee the encapsulated feature .
For this reason, the joi-compiler
can be configured in the schemaController
option during the Fastify application creation.
“ℹ️ Note How theFastify Schema Controllerworks is out of the scope of this article. But if you want to know more about it, you can read theFastify book!”
How to configure the joi-compiler
The JoiCompiler
accepts an optional configuration object to customize the joi
instance.
The default configuration is:
When you instantiate the joiCompilerInstance
, the returned object has the following properties:
buildValidator
: The function to pass to theschemaController
option of Fastify.bucket
: Thejoi
bucket that contains the schemas when you call theapp.addSchema(joiSchema)
method. You can omit this option if you don't useapp.addSchema
.joi
: A customizedjoi
instance that contains the installedextensions
, if there are any. It is a good practice to use this instance to build your schemas.
How to use ajv
and joi
together!
The power of Fastify is that you can use different schema compilers at the same time! Here is an example of how you can learn from the Fastify book :
If you run this code, you will be able to use the ajv
and joi
validation in the same application! The ajv
validation will be used for the /ajv
route and the joi
validation will be used for the /joi
route.
Pretty cool, right?
Summary
The joi-compiler
is a powerful tool that allows you to build and manage joi
instances for Fastify out of the box in a few lines of code. If you're a joi
user, joi-compiler
is definitely worth checking out!
If you enjoyed this article comment, please share and follow @ManuEomm on Twitter!
Insight, imagination and expertly engineered solutions to accelerate and sustain progress.
Contact