darraghoriordan / eslint-plugin-nestjs-typed

Some eslint rules for working with NestJs projects
http://www.darraghoriordan.com
171 stars 34 forks source link

nestjs-typed/validated-non-primitive-property-needs-type-decorator when using Union types #17

Closed alvipeo closed 2 years ago

alvipeo commented 2 years ago

Here's a property of some union type with a validation decorator:

  @ApiProperty({
      enum: ["planned", "failed", "blocked", "in-progress"]
   })
   @IsIn(RELEASE_STATUSES)
   status!: ReleaseStatus;

and the ReleaseStatus defined as follows:

export const RELEASE_STATUSES = ["planned", "failed", "blocked", "in-progress", "released"];
export type ReleaseStatus = typeof RELEASE_STATUSES[number];

When I run lint I get:

A non-primitve property with validation should probably use a @Type decorator @darraghor/nestjs-typed/validated-non-primitive-property-needs-type-decorator

I'm not sure if I'm wrong somewhere or the rule doesn't work here...

Another variant is with a simple type aliasing:

   export type ReleaseManager = string;

   @IsNotEmpty()
   releaseManager!: ReleaseManager;

This produces same error.

darraghoriordan commented 2 years ago

Hey alvipeo!

Yea there are limits to what I try to detect. I don't check if a type is aliased to a primitive type. It isn't a common use case in the apps i've written at least. anyone doing that can just disable the rule for that property.

For your releases status list - I DO require a type property for arrays. The only non-primitive type that I don't require a @Type() for is enums.

So you could fix your issue there a couple of ways

method 1: Disable the rule

// eslint-disable-next-line @darraghor/nestjs-typed/validated-non-primitive-property-needs-type-decorator
  @ApiProperty({
      enum: ["planned", "failed", "blocked", "in-progress"]
   })
   @IsIn(RELEASE_STATUSES)
   status!: ReleaseStatus;

Method 2: indicate the array element type

  @ApiProperty({
      enum: ["planned", "failed", "blocked", "in-progress"]
   })
   @IsIn(RELEASE_STATUSES)
@Type(()=> String)
   status!: ReleaseStatus;

Method 3: Use an enum (This is what I usually do for these)

export enum ReleaseStatus {
PLANNED="planned";
FAILED= "failed";
BLOCKED= "blocked"
IN_PROGRESS = "in-progress"
}

  @ApiProperty({
      enum: ReleaseStatus, 
      enumName "ReleaseStatus"
   })
   @IsEnum(ReleaseStatus)
   status!: ReleaseStatus;