ValidateIf() 未按预期工作。不验证 (NestJS)

问题描述 投票:0回答:1

我使用 validateIf() 来检查 paymentService 是否等于我的代码中定义的某个枚举值。但它不会验证并抛出错误。


    @ApiProperty({ required: true })
    @IsNotEmpty()
    @IsEnum(PAYMENT_SERVICE,{
        message:`Payment Service must be one of the valid values ${Object.values(PAYMENT_SERVICE)}`
    })
    paymentService: PAYMENT_SERVICE
    
    @ApiProperty({ required: true })
    @IsNotEmpty()
    @IsInt()
    @Transform(({ value }) => parseInt(value))
    amount: number

    @ApiProperty({ required: false })
    @ValidateIf((object) => object.paymentService == PAYMENT_SERVICE.STRIPE)
    @IsString()
    @IsOptional()
    currency: string

    @ApiProperty({ required: true })
    @IsNotEmpty()
    @IsInt()
    @Transform(({ value }) => parseInt(value))
    qurandaziId: number
} 



export enum PAYMENT_SERVICE {
    STRIPE = 'STRIPE',
    JAZZCASH = 'JAZZCASH',
}


 
validation nestjs dto class-validator
1个回答
0
投票
in currency field replace @IsOptional() to @IsNotEmpty({ message: 'currency_not_empty' })
© www.soinside.com 2019 - 2024. All rights reserved.