我无法让类验证器工作。看起来我没有使用它:一切都像我没有使用类验证器一样工作。当发送正文格式不正确的请求时,我没有任何验证错误,尽管我应该这样做。
My DTO:
import { IsInt, Min, Max } from 'class-validator';
export class PatchForecastDTO {
@IsInt()
@Min(0)
@Max(9)
score1: number;
@IsInt()
@Min(0)
@Max(9)
score2: number;
gameId: string;
}
我的控制器:
@Patch('/:encid/forecasts/updateAll')
async updateForecast(
@Body() patchForecastDTO: PatchForecastDTO[],
@Param('encid') encid: string,
@Query('userId') userId: string
): Promise<ForecastDTO[]> {
return await this.instanceService.updateForecasts(userId, encid, patchForecastDTO);
}
我的引导程序:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(PORT);
Logger.log(`Application is running on http://localhost:${PORT}`, 'Bootstrap');
}
bootstrap();
我找不到问题所在。我错过了什么?
在当前版本的 NestJS (7.6.14) 中,支持使用内置函数验证 JSON 数组形式的请求正文ParseArrayPipe
.
@Post()
createBulk(
@Body(new ParseArrayPipe({ items: CreateUserDto }))
createUserDtos: CreateUserDto[],
) {
return 'This action adds new users';
}
See the 官方文档 https://docs.nestjs.com/techniques/validation#parsing-and-validating-arrays or the 源代码 https://github.com/nestjs/nest/blob/master/packages/common/pipes/parse-array.pipe.ts了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)