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

176
Vistas
Is it possible to validate that one of 2 parameters are present using class-validator?

Using class-validator along with NestJS I want to validate that a user provides either propertyA or a propertyB but they don't need to provide both.

Currently, I'm doing something like:

export class TestDto {
  @ValidateIf(obj => !obj.propertyB)
  @IsString()
  @IsNotEmpty()
  propertyA

  @ValidateIf(obj => !obj.propertyA)
  @IsString()
  @IsNotEmpty()
  propertyB
}

If they provide none of the parameters there will be multiple errors saying that propertyA and propertyB are required and should be strings etc.

In the case that they provide neither property, I would want only a single error saying something like: "You must provide propertyA or propertyB."

Can this be accomplished using NestJS/class-validator?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do that by adding a new custom decorator to your class validator.

First, you must add a custom validation class (read document) and then define a custom decorator for that (read document).

You can see how to do that in this article.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Don't know whether this functionality exists in class-validator or not but you can simply achieve this scenario using Pipes

Just create a custom pipe and then define the logic over there

import { PipeTransform, Injectable, ArgumentMetadata, HttpException } from '@nestjs/common';

@Injectable()
export class CustomValidationPipe implements PipeTransform {
  transform(value: TestDto , metadata: ArgumentMetadata) {
    if(value.porpertyA && value.porpertyB) throw new HttpException("You must provide propertyA or propertyB" , 400)
    else return value;
  }
}

After That, you can use that Pipe over the controller

  @Post()
  create(@Body( new CustomValidationPipe()) testDto: TestDto) {
    return "create Resource"
  }

You can also use it in combination with a builtIn Validation Pipe just like this. In this case, your class validator logic will be executed first and then your custom pipe will execute

  @Post()
  create(@Body(ValidationPipe ,  new CustomValidationPipe()) testDto: TestDto) {
    return "create Resource"
  }


Hope This Helps

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