nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.73k stars 7.63k forks source link

Cannot use `UsePipes` with `ConnectedSocket` #4233

Closed baflo closed 4 years ago

baflo commented 4 years ago

Bug Report

Current behavior

Hi, I ran into an issue when I was updating nestjs from 6.7.2 to 6.11.11: my validation pipe for websocket event wouldn't work as expected anymore. I found that all parameters of an event handler are put to the validation pipe and only if all of them succeeded the validation, the handler would be called. Now, I still had the "old" pattern instead of using ConnectedSocket and MessageBody:

@SubscribeMessage('events')
handleEvent(client: Socket, data: string): string {
  return data;
}

When I changed to only use MessageBody, everything works as usual. However, if I add ConnectedSocket, it won't.

Input Code

https://github.com/baflo/nest-1/blob/connectedsocket-usepipes-bug/sample/02-gateways/src/events/events.gateway.ts

Expected behavior

I expected at least the old pattern to not change the behaviour, i.e. to not check every parameter, but only the data.

Possible Solution

Maybe I can put the ValidationPipe to just one parameter?

Environment


Nest version: 6.11.11


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

kamilmysliwiec commented 4 years ago

When I changed to only use MessageBody, everything works as usual. However, if I add ConnectedSocket, it won't.

You must use either the decorators approach or classic approach (without ANY parameter decorator). For example, the following code:

handleEvent(client: Socket, @MessageBody() data: string): string {
  return data;
}

will not work properly.

In your case, you can simply change your method signature to this:

handleEvent(@MessageBody() data: string): string {
  return data;
}

If you want to bind a pipe to a single parameter, you can do it in this way:

handleEvent(@MessageBody(ValidationPipe) data: string): string {
  return data;
}