【问题标题】:NestJS class-validator validate array of numbers in form dataNestJS 类验证器验证表单数据中的数字数组
【发布时间】:2021-11-17 05:17:58
【问题描述】:

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

这是我的 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;
}

【问题讨论】:

    标签: node.js express validation nestjs class-variables


    【解决方案1】:

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

    简答:

    @IsNumber({},{each: true})
    numbers: number[];
    

    【讨论】:

      猜你喜欢
      • 2021-02-03
      • 2021-03-09
      • 1970-01-01
      • 2019-08-29
      • 2020-05-07
      • 1970-01-01
      • 2022-08-02
      • 2022-01-05
      • 2018-08-21
      相关资源
      最近更新 更多