Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

439
Views
Cómo puedo validar el campo de cada objeto en una matriz usando el validador de clase en nest.js
import { ArrayMaxSize, ArrayMinSize, IsArray, IsBoolean, IsString, ValidateNested } from "class-validator"; import { Transform, Type } from "class-transformer"; class CreateAnswerDto { @IsBoolean() readonly isTrue: string; @IsString() title: string; @IsString() description: string; } const transformAnswers = answers => { return JSON.parse(answers.value); }; export class CreateQuestionDto { @Transform(transformAnswers, { toClassOnly: true }) @IsArray() @ArrayMinSize(4) @ArrayMaxSize(4) @ValidateNested({ each: true }) @Type(() => CreateAnswerDto) readonly answers: CreateAnswerDto[]; @IsString() readonly title: string; }

Así que tengo el mismo código y quiero validar cada campo de objeto en las respuestas, pero este código no funciona. Si envío los datos incorrectos, esta validación los omite. ¿Cómo puedo corregir validar mis respuestas?

¡Gracias por cualquier ayuda!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Entonces, resolví este problema.

 import { ArrayMaxSize, ArrayMinSize, IsArray, IsNotEmpty, IsString, IsUUID, MinLength, ValidateNested, } from "class-validator"; import { plainToClass, Transform, Type } from "class-transformer"; import { CreateAnswerDto } from "src/modules/answers/dto/create-answer.dto"; import { HasMimeType, IsFile, MaxFileSize, MemoryStoredFile } from "nestjs-form-data"; export class CreateQuestionDto { @ValidateNested({ each: true }) @Transform(({ value }) => plainToClass(CreateAnswerDto, JSON.parse(value))) @IsArray() @ArrayMinSize(4) @ArrayMaxSize(4) @Type(() => CreateAnswerDto) readonly answers: CreateAnswerDto[]; @IsUUID() @IsNotEmpty() readonly category_id: string; @IsFile() @MaxFileSize(220000) @HasMimeType(["image/jpeg", "image/png"]) readonly image: MemoryStoredFile; @IsString() @MinLength(5) readonly title: string; }

Y mis respuestas dto:

 import { Exclude } from "class-transformer"; import { IsBoolean, IsNotEmpty, IsString, MinLength } from "class-validator"; export class CreateAnswerDto { @IsBoolean() @IsNotEmpty() readonly isTrue: boolean; @IsString() @MinLength(5) readonly title: string; @IsString() @MinLength(5) readonly description: string; @IsString() @MinLength(5) readonly displayMessage: string; @Exclude() readonly id: string; constructor(partial: Partial<CreateAnswerDto>) { Object.assign(this, partial); } }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!