#Can we use Enums on DTOs?

6 messages · Page 1 of 1 (latest)

granite fable
#

I remember reading on the docs to use Class instead of interfaces, but I was wondering, can we use Enum? for example if I needed to create a "role" property in my DTO class and use the enum "Roles" as the type, would that work? What kind of decorators should I use to validate it?

I have the app.useGlobalPipes(new ValidationPipe()) enabled on the bootstrap()

import { IsEnum, IsOptional, IsString, MaxLength } from 'class-validator'
import { UserRoles } from '../enum/UserRoles'

export class UserDetailsDTO {
  @IsOptional()
  @IsString()
  @MaxLength(128)
  readonly bio: string | null

  @IsEnum(UserRoles)
  readonly role: UserRoles
}

and then on the enum/UserRoles

export enum UserRoles {
  USER,
  MODERATOR,
  ADMIN,
  OWNER
}
#

pls @ me when replying

storm vault
#

I've been using IsEnum with ValidationPipe just fine

granite fable
#

sorry 😅

#

but thanks for the tips!