2

这工作正常:

    import { IsIn } from 'class-validator';
    import { ApiProperty } from '@nestjs/swagger';

    export class createEventDto {
      @IsIn([0, 1, 2, 3, 4, 5])
      @ApiProperty({
        description: 'description of the severity property',
      })
      severity: number;
    }

大摇大摆地看起来像这样: 招摇号

我试图了解如何将严重性类型更改为枚举,我尝试过什么:

export enum Severity {
  Critical = 1,
  Major = 2,
  Minor = 3,
  Warning = 2,
  Info = 1,
  Clear = 0,
}
    import { IsEnum } from 'class-validator';
    import { ApiProperty } from '@nestjs/swagger';
    import { Severity} from '../enums/severities';

    export class createEventDto {
      @IsEnum(Severity)
      @ApiProperty({
        description: 'description of the severity property',
      })
      severity: Severity;
    }

虽然它正在工作,但招摇看起来有点不对劲(示例不正确,并且模式中严重性的描述嵌套在括号中: 招摇枚举

4

1 回答 1

7

如果你想在 SwaggerUI 上表达一个枚举,你需要提供enum属性给ApiProperty装饰器

 import { IsEnum } from 'class-validator';
 import { ApiProperty } from '@nestjs/swagger';
 import { Severity} from '../enums/severities';

 export class createEventDto {
   @IsEnum(Severity)
   @ApiProperty({
     description: 'description of the severity property',
     enum: Severity
   })
   severity: Severity;
 }

文档:https ://docs.nestjs.com/recipes/swagger#enums

于 2020-06-10T15:12:03.423 回答