Validation in Express
Well, There are few library available for validation in nodejs. Today we will explore JOI library for validation and how we can cleanly integrate it with express. This approach allows us to make our main request handler very lean and clutter free from validation.
Steps to add request validation in Express
- Created JOI Validation object.
- Create Express Middleware, which will basically validate the request and updates the request body otherwise throws error.
- Added the validation middleware to express routes.
Creating JOI Validation object and creating express middleware
This approach allows us to combine JOI object creation and middleware definition in same file, and we can multiple middleware by adding new key to export object.
Adding Middleware to Express Routes
Now, in our login request handler we can write logic for login(we don’t have to worry about user request validation).
Thanks for reading…