【发布时间】:2021-12-28 12:45:18
【问题描述】:
在 NestJS 中使用 class-validator,我有这个工作:
export class MatchDeclineReason {
@IsString()
@IsEnum(MatchDeclineReasonType)
@ApiProperty()
type: MatchDeclineReasonType;
@ValidateIf(reason => reason.type === MatchDeclineReasonType.Other)
@IsString()
@ApiProperty()
freeText: string;
}
所以如果delinceReason.type === Other,我希望得到一个freeText 字符串值。
但是,如果 declineReason.type 与 Other 有任何不同,我希望删除 freeText 属性。
有没有什么方法可以在不写CustomValidator的情况下实现这种行为?
我的ValidationPipe 配置:
app.useGlobalPipes(
new ValidationPipe({
disableErrorMessages: false,
whitelist: true,
transform: true,
}),
);
【问题讨论】:
标签: typescript validation nestjs class-validator