Class Validator if one field is passed then the other one must not be passed

25 views Asked by At

export class CreateCouponSchema {
    @IsOptional()
    @IsString()
    name: string;

    @ValidateIf(obj => !obj.percent_off)
    @IsNotEmpty()
    @IsNumber()
    amount_off: number;

    @ValidateIf(obj => !obj.amount_off)
    @IsNotEmpty()
    @IsNumber()
    @Min(0)
    @Max(100)
    percent_off: number;
}

I would like to have a validation schema that if amount_off is passed/defined then percent_off must not be passed. And viceversa. Any advice on how to accomplish this?

0

There are 0 answers