如何在NestJS类的Swagger定义中定义常量?

huangapple go评论56阅读模式
英文:

How to define a constant in the swagger definition of a NestJS Class?

问题

我有一堆使用相同接口的异常,这样前端就更容易区分它们来自同一个端点。

我想在 Swagger 中记录,`MyWeirdExpection` 有一个名为 `type` 的属性,其取值为 `my-weird-exception`(它来自一个字符串枚举,如果相关的话)。

这样做是否可能?

类似这样的代码:

```typescript

enum MyEnum {
   MyWeirdExpection: 'my-weird-exception',
   RegularExpection: 'my-regular-expection'
... 更多
}

  @ApiProperty({
    description: '前端可以根据此属性做出反应',
    type: String, // <-----------这里怎么写?
  })
  type: MyEnum.MyWeirdExpection;

再次强调,我的目标是在 Swagger 定义中,说明 MyWeirdExpection.type 是常量 'my-weird-exception'


<details>
<summary>英文:</summary>

I have a bunch of Exceptions that use the same interface, so it is easier for the frontend to distinct between them from the same endpoint.

I want to be able to document in swagger that `MyWeirdExpection` has a property `type` that has the value `my-weird-exception` (it comes from an string ENUM if that is relevant).

Is this possible to do?

Something like:

```typescript

enum MyEnum {
   MyWeirdExpection: &#39;my-weird-exception&#39;,
   RegularExpection: &#39;my-regular-expection&#39;
... more
}



  @ApiProperty({
    description: &#39;Frontend can react on this property&#39;,
    type: String, // &lt;-----------WHAT HERE?
  })
  type: MyEnum.MyWeirdExpection;

Again, my goal is that in the swagger definition, says that MyWeirdExpection.type is the constant &#39;my-weird-exception&#39;

答案1

得分: 0

以下将提供一个名为 MyEnum.MyWeirdExpection 的类型,其值将填充为 'my-weird-exception':

@ApiProperty({
    description: "按资产类型搜索",
    required: false,
    default: MyEnum.MyWeirdExpection,
    type: MyEnum.MyWeirdExpection
})
type: string | undefined;
英文:

The following will provide a type of MyEnum.MyWeirdExpection with the value populated as 'my-weird-exception':

@ApiProperty({
    description: &quot;Search by asset type&quot;,
    required: false,
    default: MyEnum.MyWeirdExpection,
    type: MyEnum.MyWeirdExpection
})
type: string | undefined;

huangapple
  • 本文由 发表于 2023年2月16日 15:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定