My favorite NPM packages:
I decided to focus my attention on writing about how to come up with keep tracking of my favorite NPM packages in a way that can be used as a bookmark based on learning journeys. I want to start keeping it within tech stack that I am currently delve myself into. The first realm is
E) Express.js:
V:
Validation
Express-validator:
Wished you wanted to spend more time writing more important codes and abstract the process of validating the requests coming from your UI? Then look no further:
install:
npm i express-validator
documentation:
Snapshot:
Lets look at the starter code in index.js:
We are sending a post request to user route, passing a request user typed in username and password, and create an instance of the User, and finally saving the response in json format. BUT we want to check something before we save the values inside the database. What if the username and password already exists? Or, the password can’t be less than 5 characters long, contain characters: $,@,_, numbers, etc.
Now try to go over this gist:
on line 2, you de-structure the ‘body’ and ‘validationResults’ when you require from Express Validator npm library. body is a method that looks at the request body content such as password and username that the user entered and use express validator method such as .isEmail() and .isLength().
validationResults can assign all the common errors that comes from previous validation points and and to be able to return the template error message.
That’s it! you don’t have to figure out how to come up with a logic of how to validate email or password’s length! all you have to do is to keep moving onto the other meat of the code.
You are not limited! You can even user sanitizers to take validation an extra layer and custom validation for authentication enthusiasts by checking the documentations.
If you like this please gives it a clap so that I know and can add more content of this kind to this list.