question: validate on insert and on update
I i have to distinguish from insert validation and update validation. For example the IsEmailAlreadyExist custom validator i have to use only if i have to insert a new record and not updating the same. I heve tried to use different groups but i have to decorate every field on my entities.
I have put a ValidateIf with the presence of the ID but is not a clean way.
I'd like to create a custom costraint and a decorator to accomplish that.
TIA Andrea Del Brocco
Groups like insert and update is the only solution I know 😕
Hi @andreanta
Can you please detail your question a little more with a minimal example of what do you want to achieve and why you cannot achieve it.
Hi, i try to better explain: Usually when you insert an entity you have to do some different validation, for example i have to check if the user exist. I use the example IsEmailAlreadyExist as decorator that check if the email is already used.
When i update the same entity i have to skip this validation or access some other fields inside it to better make a find query (es where id <> xx).
I'll try to use different groups (one for insert one for update) and try to pass different parameter on validation inside the save logic but i have to put group inside every validation property and is too much customization.
Now i'll try to use a validateif to exclude the validation but it does not validate all the filed not only the check .
@ValidatorConstraint({ name: "isEmailAlreadyExist", async: true })
@Injectable()
export class IsEmailAlreadyExist implements ValidatorConstraintInterface {
constructor(protected readonly anagraficaService: AnagraficaService) {}
async validate(text: string) {
const user = await this.anagraficaService.findOne({
Email: text,
});
return !user;
}
}
@andreanta Did you resolve this issue?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.