NestJS 类验证器验证表单数据中的数字数组

Sha*_*Sha 6 validation class-variables node.js express nestjs

如何处理表单数据中数字数组的验证?因为我以表单数据格式传递数据,所以我无法验证数字数组实际上,我不知道该怎么做。

在此输入图像描述

这是我的CreatePostDto

export class CreatePostDto {
  @IsNotEmpty()
  @IsString()
  @MinLength(3)
  title: string;

  @ApiProperty({ type: 'string', format: 'binary' })
  thumbnail: any;

  @IsNotEmpty()
  @IsString()
  @MinLength(20)
  @MaxLength(300)
  description: string;

  @IsNotEmpty()
  @IsString()
  @MinLength(20)
  body: string;

  @IsOptional()
  @Type(() => Number)
  @IsNumberString({}, { each: true })
  tags: number[];

  @IsOptional()
  @Type(() => Number)
  @IsNumberString({}, { each: true })
  categories: number[];

  @IsBooleanString()
  published: boolean;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 19

决定: https: //github.com/typestack/class-validator/issues/454

简短回答:

@IsNumber({},{each: true})
numbers: number[];
Run Code Online (Sandbox Code Playgroud)