How to Validate Requests in Node JS (with Joi validator) | Node JS Tutorial
ฝัง
- เผยแพร่เมื่อ 6 ก.ย. 2024
- In this video you'll learn how to validate API requests using the Joi validator library!
UPDATED 2024 - I would strongly recommend using Zod instead of Joi, because of it's built-in type-safety (zod.dev)
Joi: joi.dev/api/?v...
Code: github.com/nik...
Github: github.com/nik...
Support Me: www.buymeacoff...
Contact Me: nikitadev292@gmail.com
#nodejs #joivalidator #nodevalidation
Thank you so much for this. Now, I will replace the "express-validator" with "joi".
Hey man! I just wanted to say THANK YOU! Your content quality is seriously awesome. I have noticed you dont share too many vids. Dont let the algorithm get you and keep up the good work!
thanks so much!
My first time watching ds n d explanation is top notch
You deserve a million subscribers. Thank you
Just want to say thank you. You deserve way more subscribers! Really helping me with my software development course
thank you!
Nice work. That is lots of informative video
Thanks for this, it was so helpful. I wish I could ask you some questions privately.
Extremely concise, thanks!
Thank you so much for this. You're super concise and eloquent. +1 subscriber
Great video. I just wonder why could not we just export a validator(payload) function that would validate the payload against predefined schema. What's the point of returning a function instead of validation result itself ?
you could definitely export a function that takes in a schema and payload, for example:
const validate = (schema, payload) => schema.validate(payload);
but then you would have to pass in the same schema every time you want to use it, as opposed to defining it once, and then using the function that is returned. Hope that makes sense
Loved this!!
Keep it up
thank u so much for that amazing tutorial.
Very simple and helpful! 😊
very professional and helpful.
Very helpful bro, keep it up
This was really helpful, thanks
Thanks! You helped me a lot!
awesome tut thankyou so much
Excellent video!
Nikita Hi , Is this methot working with chenging data on server too . If not how can I make that? Pls can you make new video about that : " how to patch data on next js with Joi libruary"
thanks, could you do the same video but creating a middleware validator these time programmed in OOP.
Also can we create custom message responses with joi ?
yes, you can add custom error messages using Joi: stackoverflow.com/questions/48720942/node-js-joi-how-to-display-a-custom-error-messages
Thanks! It's very useful)
Great video. Thanks
What should I use if I want an unique value? if I signup with an email and I don't want you to have to create an account with the same email what should I use?
First you would validate that the email from the request is actually a valid email. Then in your controller make sure to add some business logic that verifies that the email is not in use (ie: fetching a user for that email, verifying that no user exists with that email)
thank you so much
Thanks!
Thank you! 🎉
we can use async direct inside of Joi schema? mean user fetch form db
A Joi schema is not the right place to make a database call. You should first validate the incoming request (make sure they passed a valid userId, email, etc.), and once the Joi schema validates it, then you should make the database call. You only want to make database calls after you've verified that it was a proper request.
How can I validate files?
Nice job!
thank you!
which is better express validator or joi
Joi is a bit more popular and can be used for general object schema validation, while express-validator is used specifically as express middleware and is tightly coupled to express JS. I would recommend using Joi in most cases
How to implement custom message for each property?
you can call the messages() method at the end of the Joi field, and pass in an object with the different error types as the keys, and the custom error message as the value.
Here is an example: runkit.com/embed/fnfaq3j0z9l2
Here is the list of error types: github.com/sideway/joi/blob/master/API.md#list-of-errors
Can you share your font and theme and terminal theme please 🙁💚
VSC theme: "Atom One Dark" with the "Material Font Icons" extension.
Font: "MonoLisa, Menlo, Monaco, 'Courier New', monospace"
The terminal theme is a custom one I made using iTerm2
@@nikita-dev 💚
Hi🎉
Do some Udemy courses
I plan to make some courses in the upcoming months
Thanks! You helped me a lot!