enumsnestjs

Can a same enum be used for multiple properties in a dto in the nestjs?


In Nestjs I'm using a DTO whose properties have the same set of values, so decided to use the same enum for both. But I was informed that using same enum for multiple properties is incorrect.

I have been searching for this across internet, but no answer regarding this. Providing the sample DTO for details. Can an enum be used for multiple properties in a DTO?

enum Answer {
  YES = 'YES',
  NO = 'NO',
}

export class SampleDTO {
  @IsEnum(Answer)
  @IsOptional()
  isMultiColured: string = Answer.NO;

  @IsEnum(Answer)
  @IsOptional()
  isBranded: string = Answer.NO;
}

Solution

  • But I was informed that using same enum for multiple properties is incorrect.

    Not sure who you heard this from, but it's the first I've heard of it. The point of an enum is to have a known set of values in an enumerated format. Re-using that enum for variables that will hold the same set of possible values just makes sense