nestjs-components icon indicating copy to clipboard operation
nestjs-components copied to clipboard

How to validate EmittedMessage payload with DTO? Validation is not working for me.

Open gitSambhal opened this issue 3 years ago • 11 comments

Hi I want to validate the EmittedMessage using the DTO but the validation is not working. I am using Google Pubsub microservice.

export class OrderDTO {
  @IsString()
  @IsNotEmpty()
  public id: string;

   ...
}
@EventPattern(PS_SUBSCRIPTION_EVENTS.CREATE_ORDER_SUBSCRIBE)
  public async handleOrderReceivedFromCustomer(
    @Payload(ValidationPipe)
    data: EmittedMessage<OrderDTO>,
  ): Promise<void> {
    const order = <OrderDTO>data.payload;
    // Do something
}

I have added global validation pipe, tried with controller/param level validation pipes using @UsePipes but still it goes inside the consoller function without throwing the error if I send the empty object in Google Pubsub event data.

gitSambhal avatar Aug 31 '22 14:08 gitSambhal

Hey @gitSambhal I've transferred your issue to the correct repository since it is related to the nestjs-components repo. I have a look at it as soon as I can!

ccoeurderoy avatar Sep 01 '22 07:09 ccoeurderoy

Thank you @ccoeurderoy

gitSambhal avatar Sep 01 '22 11:09 gitSambhal

@ccoeurderoy Can you plz share the issue link, I am unable to find it in the nestjs issues.

gitSambhal avatar Sep 05 '22 05:09 gitSambhal

Hey @gitSambhal, sorry I've transferred it to the @algoan/nestjs-components repository!

ccoeurderoy avatar Sep 05 '22 08:09 ccoeurderoy

@ccoeurderoy kindly share the link.

gitSambhal avatar Sep 05 '22 09:09 gitSambhal

@gitSambhal this is the transferred issue 😅 You've opened it in algoan/pubsub repository and now, it is opened in nestjs-components repo.

I've moved into this repo because it is related to nestjs-google-pubsub-microservice lib, not algoan/pubsub.

ccoeurderoy avatar Sep 05 '22 09:09 ccoeurderoy

Oh ok, now I understood. I forgot that it was in algoan/pubsub. Thanks.

gitSambhal avatar Sep 05 '22 09:09 gitSambhal

Any update on this issue?

samuelsweet avatar Sep 17 '22 10:09 samuelsweet

@samuelsweet I am now using Joi schema validation for validation the schema. No updates on this issue.

gitSambhal avatar Sep 17 '22 11:09 gitSambhal

Hey guys, since the EmittedMessage is not a DTO class, the native NestJS validation pipe cannot work (I could be wrong).

But it could be great to add an option in order to validate the emitted payload property. However, I cannot tell you when it will be done 😕

ccoeurderoy avatar Oct 03 '22 10:10 ccoeurderoy

Hi @ccoeurderoy I am using Joi library for validation instead of DTOs.

gitSambhal avatar Jul 21 '23 14:07 gitSambhal