Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidationPipe only works in Gateways if validateCustomDecorators=true #3127

Closed
da-mkay opened this issue Oct 8, 2019 · 4 comments
Closed

Comments

@da-mkay
Copy link
Contributor

da-mkay commented Oct 8, 2019

Bug Report

Current behavior

Using below code I try to use the ValidationPipe to validate against my AuthenticateDto class.

  1. Without using the @MessageBody decorator, the transform-method of ValdiationPipe is never called. Is that correct? It is not mentioned in the docs.
  2. But even if @MessageBody is used, I have to add {validateCustomDecorators: true} to the pipe's options. Otherwise the ValidationPipe skips validation.

Input Code

@UsePipes(new ValidationPipe())
@SubscribeMessage('authenticate')
authenticate(
    @ConnectedSocket() client: Socket,
    @MessageBody() data: AuthenticateDto
) {
    // ...
}

Expected behavior

It should not be neccessary to pass validateCustomDecorators: true to get validation working, because we also don't have to do it in HTTP controller context.
Maybe it should also not be neccessary to use the @MessageBody decorator or the docs are wrong :)

Possible Solution

Inside the ValidationPipe (here) it is checked whether the (data-)argument is of type "custom" and if so whether validateCustomDecorators is true. The question here should be: why is the type "custom" and not, e.g., "body"? I used the @MessageBody decorator, so it should be some kind of body/payload-type right?
The "custom"-string is created here inside ParamsTokenFactory, based on the type-parameter passed in which is of type RouteParamtypes.
Well, in this case, we use websockets, so the type-parameter is actually of type WsParamtype. Okay, but how could it get there?
Check out this line of PipesConsumer. The type-castings allow any value to be passed there.
To sum up, WsContextCreator uses that PipesConsumer which uses ParamsTokenFactory which, as described earlier, considers only RouteParamtypes, not WsParamtype to determine the type-string. Thus, for websockets the type is always "custom".
This looks kinda wrong to me. Shouldn't the PipesConsumer work with different XyzTokenFactory's, depending on the context? ParamsTokenFactory for HTTP and some new WsTokenFactoryfor websockets?

Environment


Nest version: 6.8.2
 
For Tooling issues:
- Node version: 10.16.3  
- Platform: Mac 

Maybe this issue is related: #3114

@baflo
Copy link

baflo commented Oct 10, 2019

I had a similar problem. Without digging too deep, I'd say it's the same. In my case it was introduced with 6.8.0.

In @nestjs/websockets there's a newly defined constant PARAM_ARGS_METADATA which seems to never being used to set metadata.

However, it's tried to be used at several points, e.g. in ws-context-creator.ts#L152. The metadata that should be defined for PARAM_ARGS_METADATA is stored to metadata, which is in turn used by getParamsMetadata, that always returns an empty array [] as metadata is always empty (as it's never written).

Consequently, paramsOptions (ws-context-creator.ts#L82) is always an empty array, and eventually pipesFn is never returned (ws-context-creator.ts#L235).

This causes the ValidationPipe never being invoked.

@kamilmysliwiec
Copy link
Member

Fixed in 6.8.3. Please, let me know if you face any issue.

@da-mkay
Copy link
Contributor Author

da-mkay commented Oct 11, 2019

Awesome, 6.8.3 works (no validateCustomDecorators or MessageBody needed anymore). Thank you!

@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants