I'm writing a registration endpoint for an api i'm working on and i'm using nestjs and class-validator to validate the input data. The user can register using either their phone number or email address or both. So to validate the input, I should make sure that at least one of them is provided. But I'm having a hard time figuring out how to do it without making a mess.
This is my dto:
export class register {
@ApiModelProperty()
@IsNotEmpty()
@IsAlpha()
firstName: string
@ApiModelProperty()
@IsNotEmpty()
@IsAlpha()
lastName: string
@ApiModelProperty()
@ValidateIf(o => o.email == undefined)
@IsNotEmpty()
@isIRMobile()
phone: string
@ApiModelProperty()
@ValidateIf(o => o.phone == undefined)
@IsNotEmpty()
@IsEmail()
email: string
@ApiModelProperty()
@IsNotEmpty()
password: string
}
As you can see, i've used conditional validation which works for the cases that only one of phone number or email address is provided. But the problem is that when both are provided, one of them won't be validated and an invalid value will be allowed.
Any suggestions?