我正在尝试使用class-validator和NestJS验证嵌套对象。我已经尝试通过使用class-transform 的装饰器来跟踪此线程@Type,但没有任何运气。这是我所拥有的:
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[];
}
Run Code Online (Sandbox Code Playgroud)
我也在使用内置的nestjs验证管道,这是我的引导程序:
async function bootstrap() {
const app = await NestFactory.create(ServerModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(config.PORT);
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)
对于其他属性,它工作正常,对象数组是唯一不起作用的数组。