I am trying to validate an array of objects using DTO in nestjs. I have tried it but the data is not getting validated. I tried to search a lot but didn't get any answer. These are my files:
trivia.controller.ts file
import { Controller, Post, Body, ParseArrayPipe } from '@nestjs/common';
import { LoggerService } from '../logger/logger.service';
import { TriviaService } from './trivia.service';
import { PostTriviaScoreDto } from './dto/post-trivia-score.dto';
@Controller('trivia')
export class TriviaController {
constructor(private readonly logger: LoggerService, private readonly triviaService: TriviaService) {}
@Post('score')
postTriviaScore(@Body(new ParseArrayPipe({ items: PostTriviaScoreDto })) postTriviaScoreDto: PostTriviaScoreDto) {
this.logger.info('Trivia Controller : postTriviaScore : start');
return this.triviaService.postTriviaScore(postTriviaScoreParamsDto, postTriviaScoreDto);
}
}
trivia.service.ts file
import { LoggerService } from '../logger/logger.service';
import { PostTriviaScoreDto } from './dto/post-trivia-score.dto';
@Injectable()
export class TriviaService {
constructor(
private readonly logger: LoggerService,
) {}
postTriviaScore(allPlayersTriviaScore: PostTriviaScoreDto) {
this.logger.info('Trivia Service : postTriviaScore : start');
console.log('allPlayersScore ', allPlayersTriviaScore);
}
}
post-trivia-score.dto.ts file
import { Type } from 'class-transformer';
import { IsNotEmpty, IsUUID, ValidateNested } from 'class-validator';
class TriviaScore {
@IsNotEmpty()
@IsUUID()
question_id: string;
@IsNotEmpty()
@IsUUID()
selected_option_id: string;
@IsNotEmpty()
answered_in_time: number;
}
export class PostTriviaScoreDto {
@ValidateNested({ each: true })
@Type(() => TriviaScore)
items: TriviaScore[];
}
Structure of my JSON
[
{
"question_id": "088f1344-061e-4bcc-966f-512775f1f082",
"selected_option_id": "72305e08-fedd-49b1-adb9-1dd92c88f4db",
"answered_in_time": 2
}
]
The properties are not getting validated here. Even if I pass a string in answered_in_time field, it accepts the body and doesn't throw an error or if I pass empty string in any of the fields then also it accepts the request body.
Please help me if you guys know the solution as I am really stuck here.