Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

467
Views
Validate an array of objects with class-validator and Nest.js

I am using class-validator with NestJS and trying to validate an array of objects, with this layout:

[
    {gameId: 1, numbers: [1, 2, 3, 5, 6]},
    {gameId: 2, numbers: [5, 6, 3, 5, 8]}
]

My resolver

createBet(@Args('createBetInput') createBetInput: CreateBetInput) {
    return this.betsService.create(createBetInput);
  }

My createBetInput DTO

import { InputType, Field, Int } from '@nestjs/graphql';
import { IsArray, IsNumber } from 'class-validator';

@InputType()
export class CreateBetInput {
  @IsNumber()
  @Field(() => Int)
  gameId: number;

  @Field(() => [Int])
  @IsArray()
  numbers: number[];
}

I've tried some solutions but I haven't been successful, honestly, I have no idea how to do this.

How can I modify the DTO to get the necessary validation?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are options of class-validator mixed with class-transformer to validating nested objects, your Array also is a nested object, so you can validate that like this:

import { Type } from 'class-transformer';
import { IsArray, ValidateNested } from 'class-validator';

class ItemsOfBet {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateBetInput)
  items: CreateBetInput[];
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!