英文:
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: 'my-weird-exception',
RegularExpection: 'my-regular-expection'
... more
}
@ApiProperty({
description: 'Frontend can react on this property',
type: String, // <-----------WHAT HERE?
})
type: MyEnum.MyWeirdExpection;
Again, my goal is that in the swagger definition, says that MyWeirdExpection.type
is the constant 'my-weird-exception'
答案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: "Search by asset type",
required: false,
default: MyEnum.MyWeirdExpection,
type: MyEnum.MyWeirdExpection
})
type: string | undefined;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论