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?

3
  • You need to be much more specific about what is going wrong! Error Messages, behavior etc. Commented Jan 21, 2020 at 13:35
  • @FuzzyTemper done Commented Jan 21, 2020 at 14:08
  • This was helpful for me stackoverflow.com/questions/58343262/… Commented Dec 21, 2020 at 17:08

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.