【发布时间】:2019-05-16 02:49:21
【问题描述】:
我正在尝试使用类验证器和 NestJS 验证嵌套对象。我已经尝试通过使用 class-transform 中的 @Type 装饰器来关注这个 thread 并且没有任何运气。这是我所拥有的:
DTO:
class PositionDto {
@IsNumber()
cost: number;
@IsNumber()
quantity: number;
}
export class FreeAgentsCreateEventDto {
@IsNumber()
eventId: number;
@IsEnum(FinderGamesSkillLevel)
skillLevel: FinderGamesSkillLevel;
@ValidateNested({ each: true })
@Type(() => PositionDto)
positions: PositionDto[];
}
我也在使用内置的 nestjs 验证管道,这是我的引导程序:
async function bootstrap() {
const app = await NestFactory.create(ServerModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(config.PORT);
}
bootstrap();
它对其他属性工作正常,对象数组是唯一不工作的。
【问题讨论】:
-
我刚刚将您的代码放在一个空的示例项目中,它似乎对我有用。 “不工作”的具体价值是什么?你的期望是什么?例如,如果您将
"positions": [{"other": true}]放入您的正文中,它会拒绝 400。positions: []是一个有效值。 -
我希望如果你尝试
positions: [1],它会抛出一个错误 -
@ArrayNotEmpty()?
标签: javascript node.js typescript nestjs class-validator