I have this type:
export type BranchOperatorRole = 'none' | 'seller' | 'operator' | 'administrator';
With which class-validator decorator can I validate that a property has one of those values?
import { IsEmail, IsString, Contains } from "class-validator";
export type BranchOperatorRole = 'none' | 'seller' | 'operator' | 'administrator';
export class AddBranchOperatorRequest extends User {
@IsEmail()
email: string;
@Contains(BranchOperatorRole )
role: BranchOperatorRole;
}
You can't validate by type since Types disappears in runtime. You can create Enum and use IsEnum
decorator for validation. Example
In your case try something like this:
export enum BranchOperatorRoleEnum = {
none=1,
seller=2,
// other
}
class AddBranchOperatorRequest {
@IsEnum(BranchOperatorRoleEnum)
role: BranchOperatorRole;
}
Or even with array instead of enum
export const BranchOperatorRoles: BranchOperatorRole[] = [
'none',
'seller',
// other
]
class AddBranchOperatorRequest {
@IsEnum(BranchOperatorRoles)
role: typeof BranchOperatorRoles;
}