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

391
Vistas
Why NestJS validation pipe fail in unit test when field is missing but skipMissingProperties is true?

I have a custom validation pipe in NestJS where I set skipMissingProperties to true, so when I don't send a field that is decorated with IsDefined the validation will not throw exception. For example, if the DTO is:

class ModelDTO {
  @IsDefined()
  @IsNumber()
  requiredNumber1: number;

  @IsDefined()
  @IsNumber()
  requiredNumber2: number;
}

and the actual object the validation pipe accepts is:

{
  requiredNumber2: 1
}

it doesn't fail (I save some log in this case).

When I do e2e tests it works fine - I can see the validation pipe continues to the controller and that I get the desired log.

For some reason when I do unit-tests for some reason the same case failes with Bad Request (400) exception, the error message is requiredNumber2 should not be null or undefined.

Why the unit-test fails while the e2e works well? (By the way - in manual tests the pipe also works well).

EDIT: the pipe is:

class CustomValidationPipe extends ValidationPipe {
 const OPTIONS = {
  transform: true,
  skipMissingProperties: true,
  whitelist: true,
  forbidNonWhitelisted: false,
  forbidUnknownValues: true,
  transformOptions: {
   enableImplicitConversion: true,
  }
 }
  
 constructor() {
  super(OPTIONS);
 }

 async transform(value, metadata) {
  const objectToValidate = plainToClass(
   metadata.metatype, 
   cloneDeep(value)
  );

  const errors = await validate(
   objectToValidate,
   {
    ...OPTIONS,
    skipMissingProperties: false,
    forbidNonWhitelisted: true,
   }
  )

  if (errors.length > 0) {
   // log
  }

  return super.transform(value, metadata);
 }
}
about 4 years ago · Juan Pablo Isaza
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