Validating the input data
In the Implementing the business logic recipe, we stored input data from the POST /recipes
endpoint in the database. However, we did not implement any validation logic, which means we could potentially insert a string into the price
field or a recipe without name
. Furthermore, it’s important to consider security concerns, as a malicious user could potentially insert a recipe with a description that’s excessively large, posing a risk to your application’s performance and storage.
In the backend world, there is a rule: never trust the user’s input. Fastify knows it well, so it integrates a powerful and feature-complete validation process. Let’s see it in action.
How to do it…
Follow these steps to integrate the validation process:
- Add the
schema
property to thePOST /recipes
route option:const jsonSchemaBody = { type: 'object', required...