Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!