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

193
Vistas
How to validate the direct function call in nestjs?

I am trying to send a direct function call, and I set the DTO, but not work. here is the code (Send is my DTO) in my controller:

 @Post('/Send')
  async SendE(body: Send) {
    const mail = await this.messageProducer.SendMessage(body);
    return mail;
  }

I make a direct call to SendE function here:

  @MessagePattern('Notification')
  async readMessage(@Payload() message: any, @Ctx() context: KafkaContext) {
    const messageString = JSON.stringify(context.getMessage().value);
    const toJson = JSON.parse(messageString);
    await this.SendE(toJson);
  }

I want the "Send" DTO can validate the "toJson", but it does not work. here is what my DTO looks like:

export class Send{
  @IsString()
  @ApiProperty({ required: true })
  MessageID: string;
  }

here is what the toJson looks like:

{
  MessageID: 123
}

If I send a non-string MessageID, it can pass the DTO. Please help

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

0

You have to enable validationPipe to enable DTO's, There are 2 approaches according to the docs of NestJS. The ValidationPipe is exported from @nestjs/common.

1. Globally in your main.ts:

// validate incoming requests
app.useGlobalPipes(
  new ValidationPipe({
    transform: true,
  })
);

2. Per controller

@Post()
@UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createCatDto: CreateCatDto) {
  this.catsService.create(createCatDto);
}
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