Kosmonaft
Kosmonaft

Reputation: 1353

NestJs Swagger mixed type with enum

I'm having small issues with NestJs Swagger ApiProperty Type generation. Initially, everything was fine as I had ENUM. However, the 3rd party which I was pulling information from, added a few new values and suddenly my API started failing the validation.

To prevent this, I wanted to mix the ENUM with a string to make a wild card.

Sadly I can't make it work. I tried with oneOf but once I do it this way the OpenApi is not generating anymore the ENUM :-(

export enum VehicleTypes {
  CAR = 'Car',
  BUS = 'Bus',
  TRUCK = 'Truck'
}

  @ApiProperty({
    oneOf: [
      { type: 'string' },
      {
        type: 'enum',
        enum: [...Object.values(VehicleTypes)],
      },
    ],
  })
  @Expose()
  vehicleType: VehicleTypes | string;

How I can make a Type which is expecting a value from the ENUM or eventually a string?

Upvotes: 1

Views: 301

Answers (1)

Nishan Raut
Nishan Raut

Reputation: 38

You can try this

@ApiProperty({
  required: true,
  example: VehicleTypes.CAR,
  enum: VehicleTypes,
 })
@IsEnum(VehicleTypes)
vehicleType: VehicleTypes | string;

Upvotes: 0

Related Questions