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

201
Visualizações
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 Respostas
Responde à pergunta

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 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