Me pregunto si hay una manera de crear un dto para validar una matriz de objetos.
Matriz de ejemplo:
[ { "name": "Tag 1", "description": "This is the first tag" }, { "name": "Tag 2", "description": "This is the second tag" } ]Por el momento tengo esto, mientras funciona, no es lo que busco.
export class Tags { @ApiProperty({ description: 'The name of the tag', example: 'Tag 1', required: true }) @IsString() @MaxLength(30) @MinLength(1) name: string; @ApiProperty({ description: 'The description of the tag', example: 'This is the first tag', required: true }) @IsString() @MinLength(3) description: string; } export class CreateTagDto { @ApiProperty({ type: [Tags] }) @Type(() => Tags) @ArrayMinSize(1) @ValidateNested({ each: true }) tags: Tags[]; }Solo usa ParseArrayPipe :
Actualice su controlador :
@Post() createExample(@Body(new ParseArrayPipe({ items: Tags, whitelist: true })) body: Tags[]) { ... } Asegúrese de tener items y la whitelist configurados.
Actualice su DTO :
import { IsString, Length } from "class-validator"; export class Tags { @IsString() @Length(1, 30) name: string; @IsString() @Length(3) description: string; }