I am trying to conditionally validate nested DTO with class validator, but @ValidateIf seems to be not applying correctly and always validating the nested class.
I have the following DTO:
export class SuperAdminStoreDto extends StoreDto {
@IsOptional()
@IsBoolean()
readonly payments: boolean;
@ValidateIf(object => object.payments)
@ValidateNested({ each: true })
@Type(() => PaymentDetails)
readonly payment_details: PaymentDetails[];
}
class PaymentDetails {
@IsNumberString()
@IsNotEmpty()
readonly min: string;
@IsNumberString()
@IsNotEmpty()
readonly max: string;
@IsNumberString()
@IsNotEmpty()
readonly percentage: string;
}
Let's say the object being validated is:
{
"payments": false,
"payment_details": {
"min": "1"
}
}
I get no errors although I should because I have set the validator with following flags:
{
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: {
target: false,
}
}
I am expecting the validator to not allow the child object to be present and throw an error.