import { InjectRepository } from "@nestjs/typeorm";
import { ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";
import { Customer } from "../entities/customer.entity";
import { Repository } from "typeorm";
@ValidatorConstraint({ async: true })
export class CustomerPhoneExistsRule implements ValidatorConstraintInterface {
constructor(@InjectRepository(Customer) private customerRepo: Repository<Customer>) {}
async validate(customerPhone: string) {
console.log(customerPhone);
console.log(await this.customerRepo.find({ where: { isActive: true } }));
return this.customerRepo
.findOneBy({ phone: customerPhone, isActive: false, deletedAt: null })
.then(customer => {
return customer !== undefined;
});
}
defaultMessage(): string {
return "Customer phone number already exists";
}
}
I was tryna add a custom validation to my project, but i keep getting an error saying
TypeError: Cannot read properties of undefined (reading 'find').
when i googled i saw many people using the repository pattern with separate repository file and injecting that to the validation. is that the only way it'll work ?