6

I can't get the class-validator to work. It seems like I am not using it: everything works as if I didn't use class-validator. When sending a request with an incorrectly formatted body, I don't have any validation error, although I should.

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;
}

My controller:

@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);
}

My bootstrap:

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();

I can't find what's wrong. What did I miss?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
papillon
  • 1,807
  • 2
  • 19
  • 39

2 Answers2

4

In the current version of NestJS (7.6.14), validating a request body that is a JSON array is supported using the built in ParseArrayPipe.

@Post()
createBulk(
  @Body(new ParseArrayPipe({ items: CreateUserDto }))
  createUserDtos: CreateUserDto[],
) {
  return 'This action adds new users';
}

See the official docs or the source code for more info.

thomaux
  • 19,133
  • 10
  • 76
  • 103
3

NestJS actually does not support array validation out of the box. In order to validate an array, it must be wrapped in an object.

This way, I would not use a DTO corresponding to a list of items, but a DTO corresponding to an object that contains a list of items:

import { PatchForecastDTO } from './patch.forecast.dto';
import { IsArray, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

export class PatchForecastsDTO {
    @IsArray()
    @ValidateNested() // perform validation on children too
    @Type(() => PatchForecastDTO) // cast the payload to the correct DTO type
    forecasts: PatchForecastDTO[];
}

And I would use that DTO in my controller:

@Patch('/:encid/forecasts/updateAll')
async updateForecast(
    @Body() patchForecastsDTO: PatchForecastsDTO,
    @Param('encid') encid: string,
    @Query('userId') userId: string
): Promise<ForecastDTO[]> {
  return await this.instanceService.updateForecasts(userId, encid, patchForecastsDTO);
}
papillon
  • 1,807
  • 2
  • 19
  • 39