I'm trying to validate nested objects using class-validator and NestJS. I've already tried following this thread by using the @Type
decorator from class-transform and didn't have any luck. This what I have:
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[]; }
I'm also using built-in nestjs validation pipe, this is my bootstrap:
async function bootstrap() { const app = await NestFactory.create(ServerModule); app.useGlobalPipes(new ValidationPipe()); await app.listen(config.PORT); } bootstrap();
It's working fine for other properties, the array of objects is the only one not working.
for me, I would able to validate nested object with 'class-transformer'
import { Type } from 'class-transformer';
full example:
import { MinLength, MaxLength, IsNotEmpty, ValidateNested, IsDefined, IsNotEmptyObject, IsObject, IsString, } from 'class-validator'; import { Type } from 'class-transformer'; class MultiLanguageDTO { @IsString() @IsNotEmpty() @MinLength(4) @MaxLength(40) en: string; @IsString() @IsNotEmpty() @MinLength(4) @MaxLength(40) ar: string; } export class VideoDTO { @IsDefined() @IsNotEmptyObject() @IsObject() @ValidateNested() @Type(() => MultiLanguageDTO) name!: MultiLanguageDTO; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With