Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 46

Nest.JS DTO Validation

My DTO is

 @Expose()
 @IsNotEmpty()
 @IsJSON({ each: true })
 filesRole: string

filesRole is something like that: [{"file": "14125.png", "role": "bg"}, {"file": "x12.png", "role": "cover"}]

I want to validate role to be bg or cover.

Upvotes: 0

Views: 1779

Answers (2)

prashant isotiya
prashant isotiya

Reputation: 199

update your main DTO:

@Expose()
@IsNotEmpty()
@IsArray()
@ValidateNested({ each: true })
filesRole: Data[]; 

Data DTO:

export class Data {
    @IsNotEmpty()
    @IsString()
    file: string;

    @IsNotEmpty()
    @IsString()
    @IsIn(Object.values(roleEnum))
    role: roleEnum;
}

roleEnum :

export enum roleEnum {
    bg = 'bg',
    cover = 'cover',
}

Upvotes: 0

water_ak47
water_ak47

Reputation: 1306

You can try it with enum:

export enum Role {
  bg = 'bg',
  cover = 'cover',
}
@IsEnum(Role)
@Expose()
@IsNotEmpty()
@IsJSON({ each: true })
filesRole: Role

Upvotes: 2

Related Questions