Nestjs IsUnique custom validation (with database query) for email, username, etc

Sdílet
Vložit
  • čas přidán 25. 07. 2023
  • Let's implement IsUnique custom validation in Nestjs to check if a value (e.g. email, username, etc.) is unique or not, by querying the database.
    Blog post: zestmade.com/blog/nestjs-uniq...
    Git repository for the project: github.com/ZestArinze/nestjs-...
  • Věda a technologie

Komentáře • 13

  • @mr-koi
    @mr-koi Před 7 měsíci +1

    You are awesome

  • @carlosjhardelorellano2477
    @carlosjhardelorellano2477 Před 5 měsíci

    thank you man

  • @donggdiep
    @donggdiep Před 3 měsíci

    it give me this error : "Nest can't resolve dependencies of the IsUniqueConstraint (?). Please make sure that the argument EntityManager at index [0] is available in the AppModule context." please help me

  • @user-bw5ix7kb4p
    @user-bw5ix7kb4p Před 4 měsíci

    i am getting user table already exists error? What is the solution to resolve this problem?

  • @MarcoFeller
    @MarcoFeller Před 5 měsíci

    Thanks, I was just looking for this and it works perfectly. One thing i am struggeling with now is how can i put this unique constarint test into the unit test?

  • @Fenaco96
    @Fenaco96 Před 9 měsíci

    Thanks! It's a very practical solution. Is there no risk of sql injection if we use this decorator?

    • @ZestMade
      @ZestMade  Před 9 měsíci +1

      The developer specifies (in the code) the table and column for the database query. Do not use user-supplied data as the table and/or column names.
      Disclaimer: I suggest you kindly consult your security expert for this question you've asked here and all your other security-related concerns.
      Happy coding.

  • @arshitkalkani9192
    @arshitkalkani9192 Před 5 měsíci

    I have a question if user want to update their name using the update endpoint that time is how to manage the uniqueness of the name.
    if we use this validation then it will not allow to update the name because it will check the name is already exists or not.
    so how to manage this situation.

    • @trankkat2031
      @trankkat2031 Před měsícem

      In the DTO file there is always a pair of create and update files. Now you can choose to inherit the createDto file or rewrite it in updateDto

    • @paulmacguire
      @paulmacguire Před 28 dny

      You figure it out? Im having the same issue

  • @hagerkhaled2299
    @hagerkhaled2299 Před 8 měsíci

    why it give me this error ?
    ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'getRepository')
    TypeError: Cannot read properties of undefined (reading 'getRepository')

    • @guilhermealbino8737
      @guilhermealbino8737 Před 8 měsíci

      Please check if in your main.ts file there is the line:
      useContainer(app.select(AppModule), { fallbackOnErrors: true });
      Also, ensure that the module containing your service is resolving the validator within the providers array.

    • @user-bw5ix7kb4p
      @user-bw5ix7kb4p Před 4 měsíci

      @@guilhermealbino8737 I am getting user table already exists error? WHat is the solution to resolve this issue?