admin管理员组

文章数量:1415111

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

So I have the same code, and I want to validate each object field in answers, but this code doesn't work. If I send the wrong data this validation skips it. How can I correct validate my answers?

Thanks for any help!

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

So I have the same code, and I want to validate each object field in answers, but this code doesn't work. If I send the wrong data this validation skips it. How can I correct validate my answers?

Thanks for any help!

Share Improve this question asked Dec 22, 2021 at 21:47 IhorIhor 911 silver badge5 bronze badges 4
  • Did you try this? stackoverflow./questions/58343262/… The first answer worked for me – Reut Schremer Commented Dec 23, 2021 at 8:26
  • Yes, I did. It works for me without @Transform(transformAnswers, { toClassOnly: true }) and when I send answers as an array. But I need to send it using formData and need to stringify answers. Then I need to transform these stringified answers to an array and validate it. But after transform validation doesn't work – Ihor Commented Dec 23, 2021 at 15:04
  • @Ihor Can you share the data which is not working? – Lars Flieger Commented Dec 25, 2021 at 0:23
  • Does this answer your question? Convert stringified JSON to Object using class-transformer – Lars Flieger Commented Dec 25, 2021 at 0:41
Add a ment  | 

1 Answer 1

Reset to default 6

So, I resolved this problem.

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

And my Answers 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);
  }
}

本文标签: javascriptHow I can validate each object39s field in array using classvalidator in nestjsStack Overflow