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

КОМЕНТАРІ • 48

  • @maxralph01
    @maxralph01 Рік тому +3

    Thank you so much for this. Now, I will replace the "express-validator" with "joi".

  • @exrocker47
    @exrocker47 Рік тому +4

    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!

  • @onigbindedavid6630
    @onigbindedavid6630 2 місяці тому

    My first time watching ds n d explanation is top notch

  • @emekatimothyiloba699
    @emekatimothyiloba699 Місяць тому

    You deserve a million subscribers. Thank you

  • @Sir_Pickle
    @Sir_Pickle Рік тому +5

    Just want to say thank you. You deserve way more subscribers! Really helping me with my software development course

  • @FirozAhamed
    @FirozAhamed 6 місяців тому

    Nice work. That is lots of informative video

  • @matthewakahomen1964
    @matthewakahomen1964 2 роки тому +1

    Thanks for this, it was so helpful. I wish I could ask you some questions privately.

  • @frogfood24
    @frogfood24 2 роки тому +1

    Extremely concise, thanks!

  • @JoshuaMusau
    @JoshuaMusau Рік тому

    Thank you so much for this. You're super concise and eloquent. +1 subscriber

  • @kakhatezelashvili3368
    @kakhatezelashvili3368 Рік тому +1

    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 ?

    • @nikita-dev
      @nikita-dev  Рік тому +1

      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

  • @sultanmatthews-cy8uz
    @sultanmatthews-cy8uz Рік тому

    Loved this!!
    Keep it up

  • @nourkamal1412
    @nourkamal1412 Рік тому

    thank u so much for that amazing tutorial.

  • @sneezsloth
    @sneezsloth 2 роки тому

    Very simple and helpful! 😊

  • @zaxpeedy2852
    @zaxpeedy2852 Рік тому

    very professional and helpful.

  • @osifojohn1513
    @osifojohn1513 Рік тому

    Very helpful bro, keep it up

  • @KelechiNwaji-fg3vn
    @KelechiNwaji-fg3vn Рік тому

    This was really helpful, thanks

  • @danielgabay8816
    @danielgabay8816 Рік тому

    Thanks! You helped me a lot!

  • @medAmineRg
    @medAmineRg Рік тому

    awesome tut thankyou so much

  • @brayancantor4109
    @brayancantor4109 2 роки тому

    Excellent video!

  • @AbaijanAlimbekov
    @AbaijanAlimbekov 2 місяці тому

    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"

  • @runyalen
    @runyalen Рік тому

    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 ?

    • @nikita-dev
      @nikita-dev  Рік тому +1

      yes, you can add custom error messages using Joi: stackoverflow.com/questions/48720942/node-js-joi-how-to-display-a-custom-error-messages

  • @vitaly-
    @vitaly- 2 роки тому

    Thanks! It's very useful)

  • @mehmetaltinsoy525
    @mehmetaltinsoy525 2 роки тому

    Great video. Thanks

  • @anothergamerbyjaman
    @anothergamerbyjaman Рік тому

    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?

    • @nikita-dev
      @nikita-dev  Рік тому

      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)

  • @lannguyen-xr1xl
    @lannguyen-xr1xl Рік тому

    thank you so much

  • @newtechnology452
    @newtechnology452 Рік тому

    Thanks!

  • @edu.paixao
    @edu.paixao Рік тому

    Thank you! 🎉

  • @ABUTAHER-wg7gz
    @ABUTAHER-wg7gz Рік тому

    we can use async direct inside of Joi schema? mean user fetch form db

    • @nikita-dev
      @nikita-dev  Рік тому

      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.

  • @ehSamurai3483
    @ehSamurai3483 4 місяці тому

    How can I validate files?

  • @CaptainSlowbeard
    @CaptainSlowbeard 2 роки тому

    Nice job!

  • @eurisko7119
    @eurisko7119 Рік тому

    which is better express validator or joi

    • @nikita-dev
      @nikita-dev  Рік тому

      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

  • @namikaze5374
    @namikaze5374 2 роки тому

    How to implement custom message for each property?

    • @nikita-dev
      @nikita-dev  2 роки тому +1

      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

  • @yazanali1454
    @yazanali1454 Рік тому

    Can you share your font and theme and terminal theme please 🙁💚

    • @nikita-dev
      @nikita-dev  Рік тому +1

      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

    • @yazanali1454
      @yazanali1454 Рік тому +1

      @@nikita-dev 💚

  • @alefali542
    @alefali542 9 місяців тому

    Hi🎉

  • @2A_American
    @2A_American Рік тому +1

    Do some Udemy courses

    • @nikita-dev
      @nikita-dev  Рік тому

      I plan to make some courses in the upcoming months

  • @WellingtonRogatiOficial
    @WellingtonRogatiOficial Рік тому

    Thanks! You helped me a lot!