class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

feature: ValidateNested Array is not Working

Open edutucci opened this issue 1 year ago • 1 comments

Description

The decorator IsArray() does not check if the value is array. In my example I need check the arrays allergen and additives in class NutritionalInfoDto.

import {
  IsArray,
  IsBoolean,
  IsIn,
  IsNotEmpty,
  IsNumber,
  IsObject,
  IsOptional,
  IsString,
  Min,
  ValidateNested,
} from 'class-validator'

class NutritionalInfoDto {
  @IsArray({ message: 'availabilityId precisa ser um array' })
  @IsString({
    message: 'todos os valores de availabilityId precisam ser uma string!',
    each: true,
  })
  @IsOptional()
  allergen: string[];

  @IsArray({ message: 'availabilityId precisa ser um array' })
  @IsString({
    message: 'todos os valores de availabilityId precisam ser uma string!',
    each: true,
  })
  @IsOptional()
  additives: string[];
}

export class ProductDto {
  @IsObject({ each: true })
  @ValidateNested({
    each: true,
    message: 'wrong values',
  })
  @Type(() => NutritionalInfoDto)
  nutritionalInfo: NutritionalInfoDto;
}

edutucci avatar May 27 '24 14:05 edutucci

I'm facing the same issue. @IsArray() is not working. @edutucci were you able to solve your problem?

fe-sak avatar Feb 13 '25 17:02 fe-sak