如何在自定义验证程序中访问typeorm存储库?

时间:2019-07-07 17:03:11

标签: typeorm class-validator

我正在尝试创建一个有权访问userRepository的{​​{3}}。我也安装了typeorm-typedi-extensions

这不起作用:

@ValidatorConstraint({ async: true})
@Service()
export class IsUserAlreadyExistConstraint implements ValidatorConstraintInterface {
  constructor(
    @InjectRepository(User) private readonly userRepository: Repository<User>
  ) {}

    async validate(email: any, args: ValidationArguments) {
      console.log({ email })
      console.log(this.userRepository)
      const user = await this.userRepository.findOne({ email })
      console.log({user});
      if (user) return false;
      return true;
    }
}

export function IsUserAlreadyExist(validationOptions?: ValidationOptions) {
   return function (object: Object, propertyName: string) {
        registerDecorator({
            target: object.constructor,
            propertyName: propertyName,
            options: validationOptions,
            constraints: [],
            validator: IsUserAlreadyExistConstraint
        });
   };
}

0 个答案:

没有答案