9

How can I handle the validation of an array of numbers in form data? Because I am passing data in a form-data format, I can't validate an array of numbers Actually, I don't know how to do it.

enter image description here

This is my CreatePostDto:

export class CreatePostDto {
  @IsNotEmpty()
  @IsString()
  @MinLength(3)
  title: string;

  @ApiProperty({ type: 'string', format: 'binary' })
  thumbnail: any;

  @IsNotEmpty()
  @IsString()
  @MinLength(20)
  @MaxLength(300)
  description: string;

  @IsNotEmpty()
  @IsString()
  @MinLength(20)
  body: string;

  @IsOptional()
  @Type(() => Number)
  @IsNumberString({}, { each: true })
  tags: number[];

  @IsOptional()
  @Type(() => Number)
  @IsNumberString({}, { each: true })
  categories: number[];

  @IsBooleanString()
  published: boolean;
}

enter image description here

1 Answer 1

25

A decision: https://github.com/typestack/class-validator/issues/454

Short answer:

@IsNumber({},{each: true})
numbers: number[];
Sign up to request clarification or add additional context in comments.

1 Comment

This solves the issue by returning a 400 bad request. But it sends a generic message saying: "each value in must be a number conforming to the specified constraints". Is there a way so that it says that each number must be an number?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.