title: How to handle TypeORM entity field unique validation error in NestJS?
body: <p>I've set a custom unique validator decorator on my TypeORM entity field email. NestJS has dependency injection, but the service is not injected.</p>
  <p>The error is:</p>
  <pre><code>TypeError: Cannot read property 'findByEmail' of undefined
  </code></pre>
  <p>Any help on implementing a custom email validator?</p>
  <p><code>user.entity.ts</code>:</p>
  <pre><code>@Column()
  @Validate(CustomEmail, {
      message: &quot;Title is too short or long!&quot;
  })
  @IsEmail()
  email: string;
  </code></pre>
  <p>My <code>CustomEmail</code> validator is</p>
  <pre><code>import {ValidatorConstraint, ValidatorConstraintInterface, 
  ValidationArguments} from &quot;class-validator&quot;;
  import {UserService} from &quot;./user.service&quot;;
  
  @ValidatorConstraint({ name: &quot;customText&quot;, async: true })
  export class CustomEmail implements ValidatorConstraintInterface {
  
    constructor(private userService: UserService) {}
    async validate(text: string, args: ValidationArguments) {
  
      const user = await this.userService.findByEmail(text);
      return !user; 
    }
  
    defaultMessage(args: ValidationArguments) { 
      return &quot;Text ($value) is too short or too long!&quot;;
    }
  }
  </code></pre>
  <p>I know I could set <code>unique</code> in the <code>Column</code> options</p>
  <pre><code>@Column({
    unique: true
  })
  </code></pre>
  <p>but this throws a mysql error and the <code>ExceptionsHandler</code> that crashes my app, so I can't handle it myself...</p>
  <p>Thankx!</p>