Skip to content

Latest commit

 

History

History

class-transformers

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Nestjs class transformers

Extends class-transformers package with additional features.

Installation

npm install --save @algoan/nestjs-class-transformers

EnumFallback

Usage

import { EnumFallback } from '@algoan/nestjs-class-transformers';

export enum UserRole {
  ADMIN = 'ADMIN',
  READER = 'READER',
}

class User {
  @EnumFallback({
    type: UserRole,
    fallback: (value: UserRole) => UserRole.READER // if the role is not "ADMIN" or "READER", then the role will be "READER".
  })
  public role?: UserRole;
}

It works with array too:

import { EnumFallback } from '@algoan/nestjs-class-transformers';

export enum UserRole {
  ADMIN = 'ADMIN',
  READER = 'READER',
}

class User {
  @EnumFallback({
    type: UserRole,
    fallback: (value: UserRole) => UserRole.READER // if an array element is not "ADMIN" or "READER", then the role will be "READER".
  })
  public roles: UserRole[];
}