Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
Problems with ValidationPipe in NestJS when I need to validate the contents of an array

I have a situation where my client user can enter zero or multiple addresses. My problem is that if he enters an address, some fields need to be mandatory.

user.controller.ts

@Post()
@UsePipes(ValidationPipe)
async createUser(
    @Body() createUser: CreateUserDto,
) {
    return await this.service.saveUserAndAddress(createUser);
}

create-user.dto.ts

export class CreateUserDto {
    @IsNotEmpty({ message: 'ERROR_REQUIRED_FULL_NAME' })
    fullName?: string;

    @IsNotEmpty({ message: 'ERROR_REQUIRED_PASSWORD' })
    password?: string;

    @IsNotEmpty({ message: 'ERROR_REQUIRED_EMAIL' })
    @IsEmail({}, { message: 'ERROR_INVALID_EMAIL' })
    email?: string;

    ...

    addresses?: CreateUserAddressDto[];
}

create-user-address.dto.ts

export class CreateUserAddressDto {
    ...

    @IsNotEmpty()
    street: string;

    ...
}

CreateUserDto data is validated correctly and generates InternalServerErrorResponse, but CreateUserAddressDto data is not validated when there is some item in my array. Any idea how I can do this validation?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

What you are trying to do is - to basically add logic to primitive validators provided out of the box with nest - aka - defining a custom validator.

This can be done by using the two classes ValidatorConstraint and ValidatorConstraintInterface provided by the class validator.

In order to sort this, transform the incoming input / club whatever data you want to validate at once into an object - either using a pipe in nestjs or sent it as an object in the API call itself, then attach a validator on top of it.

To define a custom validator:

import { ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';


/**
 * declare your custom validator here
 */
@ValidatorConstraint({ name: 'MyValidator', async: false })
export class MyValidator implements ValidatorConstraintInterface {
  
   /** return true when tests pass **/
   validate(incomingObject: myIncomingDataInterface) {
    try {
     // your logic regarding what all is required in the object
     const output = someLogic(incomingObject);
     return output;
    } catch (e) {
      return false;
    }
  }

  defaultMessage() {
    return 'Address update needs ... xyz';
  }
}

Once you have defined this, keep this safe somewhere as per your project structure. Now you just need to call it whenever you want to put this validation.

In the data transfer object,

// import the validator
import { Validate } from 'class-validator';
import { MyValidator } from './../some/safe/place'

export class SomeDto{
    @ApiProperty({...})
    @Validate(MyValidator)
    thisBecomesIncomingObjectInFunction: string;
}

As simple as that.

over 4 years ago · Santiago Trujillo Relatório

0

Nest fw uses class-transformer to convert a json to a class object. You have to set the correct type for the sub-attribute if it is not a primitive value. And your attribute is an array, you have to config to tell class-validator that it is an array, and validate on each item.

Let's update CreateUserDto

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

export class CreateUserAddressDto {
    ...


    @ValidateNested({ each: true })
    @Type(() => CreateUserAddressDto)
    addresses?: CreateUserAddressDto[];

    ...
}

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda