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
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.
main.ts:// validate incoming requests
app.useGlobalPipes(
new ValidationPipe({
transform: true,
})
);
@Post()
@UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
}