Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

470
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda