NodeJS Swagger docs在nest js中不使用类验证器

holgip5t  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(138)

我特灵着在一个nest js应用程序中输入以下类防御。

import { ApiProperty } from '@nestjs/swagger';
import {
  IsArray,
  ValidateNested,
  IsString,
  IsNotEmpty,
  IsNumber,
} from 'class-validator';

export default class CreateDatasetDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  internal_id: bigint;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  name: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  part_int_id: bigint;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  part_parent: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  foc_allow: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  complaint_category: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  payable: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  call_center_allow: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  rtc_allowed: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  no_message: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  desc_int_id: bigint;

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  title_int_id: bigint;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  title_name: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  device_category: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  dev_cat_id: bigint;
}

export class BulkCreateDatasets {
  @ApiProperty({
    type: [CreateDatasetDto],
  })
  @IsArray()
  @ValidateNested({ each: true })
  @IsNotEmpty()
  datasets: CreateDatasetDto[];
}

但是当我用下面的curl请求测试它时

curl -X 'POST' \
  'http://localhost:3000/hul/create_bulk_dataset' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer somejwt' \
  -H 'Content-Type: application/json' \
  -d '{
  "datasets": [
    {
      "internal_id": 223,
      "name": "string",
      "part_int_id": 0,
      "part_parent": false,
      "foc_allow": "string",
      "complaint_category": "string",
      "payable": "string",
      "call_center_allow": "string",
      "rtc_allowed": "string",
      "no_message": "string",
      "desc_int_id": 0,
      "title_int_id": 0,
      "title_name": "string",
      "device_category": "string",
      "dev_cat_id": 0
    }
  ]
}'

我明白了即使我已经定义了类中的所有属性。

{
  "statusCode": 400,
  "message": [
    "datasets.0.property internal_id should not exist",
    "datasets.0.property name should not exist",
    "datasets.0.property part_int_id should not exist",
    "datasets.0.property part_parent should not exist",
    "datasets.0.property foc_allow should not exist",
    "datasets.0.property complaint_category should not exist",
    "datasets.0.property payable should not exist",
    "datasets.0.property call_center_allow should not exist",
    "datasets.0.property rtc_allowed should not exist",
    "datasets.0.property no_message should not exist",
    "datasets.0.property desc_int_id should not exist",
    "datasets.0.property title_int_id should not exist",
    "datasets.0.property title_name should not exist",
    "datasets.0.property device_category should not exist",
    "datasets.0.property dev_cat_id should not exist"
  ],
  "error": "Bad Request"
}

请告诉我,我错在哪里。
我试着用curl和swagger来运行它。我在两个地方都得到了验证错误。

8zzbczxx

8zzbczxx1#

将这个装饰器添加到datasetprops对我来说似乎很好

@Type(() => CreateDatasetDto)

但我还是不明白为什么它没有自动地那样做

相关问题