Why class validator throwing error in Nest.js DTO

109 Views Asked by At

I have Validation DTO as below in my nest.js project ->

import { IsArray, IsNotEmpty, IsNumber, IsString } from 'class-validator';

export class UpdateFruitColorDto {
  @IsArray()
  @IsNotEmpty()
  @IsString({ each: true })
  readonly color: string[];
  @IsNotEmpty()
  @IsNumber()
  readonly fruit_id: number;
}

but when i send json data as below from postman it is throwing me error ->

{
     "color":[
        "green",
        "yellow"
    ],
    "fruit_id": 1
   
}

like below ->

{
    "message": [
        "each value in color must be a string",
        "color should not be empty",
        "color must be an array",
        "fruit_id must be a number conforming to the specified constraints",
        "fruit_id should not be empty"
    ],
    "error": "Bad Request",
    "statusCode": 400
}

Mybe i am missing something here but i did not faced this kind of issue anytime before.

1

There are 1 best solutions below

0
On

Try to send data in a different format ("form-data" or "x-www-form-urlencoded" or "raw"). For raw also you may try different types (javascript, json, etc)