Search code examples
nestjsnestjs-swagger

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


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:


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'


Solution

  • 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;