0

I have a dto which is called product and it has a field called units....which received array of strings and this strings are predefined.....

my valid strings are predefined in a array ...

let validItems = ['a', 'b', 'c', 'd', 'e']

the data I want to be accepted by my dto is ...

{
   product_id: 1,
   units: ['a', 'b', 'c']
}

{
  product_id: 2,
  units: ['c', 'e', 'd']
}

{
   product_id: 3,
   units: ['e', 'b', 'a']
}

my current dto(not requirement satisfied) is =>

export class Product {
  @IsString({ message: 'Product id must be a string' })
  product_id: string;


  @IsArray({ message: 'unit must be array' })
  @IsString({ each: true, message: 'must be a string' })
  units: string[];
}

what will be my DTO in nest.js. As I am new to nest.js so kindly provide me some good docs for this decorators

Riyad Zaigirdar
  • 691
  • 3
  • 8
  • 22

1 Answers1

0

I tried a lot and found a way to solve this problem.

I spent quite some time on the swagger docs for an array of strings.

import { ApiProperty } from "@nestjs/swagger";
import { IsArray, IsString } from "class-validator";

export class MenuOptionsByNames {
  @ApiProperty({ isArray: true, example: ["size_external_party"] })
  @IsArray()
  @IsString({ each: true, message: "Each item should be string" })
  menu_names: Array<string>;
}