'class-validator case-insensitive enum validation?
I have anum like this:
export enum UserRole {
USER,
ADMIN,
BLOGGER
}
and create.user.dto like this
import { IsEmail, IsEnum, IsNotEmpty, IsOptional } from 'class-validator';
import { UserRole } from './user.entity';
export class CreateUserDto {
@IsEmail()
email: string;
@IsNotEmpty()
firstName: string;
@IsNotEmpty()
lastName: string;
@IsOptional()
username: string;
@IsOptional()
@IsEnum(UserRole)
role: UserRole;
@IsNotEmpty()
password: string;
}
Now role validation does not fail if I only post the role uppercase('ADMIN','USER') or 'BLOGGER'.
How to make class-validator not case sensitive? I mean, validate true also for 'admin' 'aDmIn'.
Solution 1:[1]
your enum should like this
export enum UserRole {
USER = "USER",
ADMIN = "ADMIN",
BLOGGER = "BLOGGER"
}
is proven works in case-insensitive or vice versa
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DamarOwen |
