ValidationPipe not working
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
To be quite concise: the ValidationPipe
doesn’t work at all. Maybe I’m not using the way it is intended to be used but that seems unlikely.
It looks like no validation is performed at all.
Expected behavior
I expect to get a validation error thrown at my face.
Minimal reproduction of the problem with instructions
Here is a repository which provides all information to reproduce my issue: https://github.com/fwoelffel/nestjs-validation-bug
Clone, install and start the NestJS application:
git clone https://github.com/fwoelffel/nestjs-validation-bug.git
cd nestjs-validation-bug
npm install
npm start
Now, the http://localhost:3000
endpoint expects a POST
request containing a payload of type MyDto
.
Here is the DTO definition:
import { IsNumber } from 'class-validator';
export class MyDto {
@IsNumber()
readonly test: number;
}
And here is the controller:
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { MyDto } from "./my.dto";
@Controller()
export class AppController {
@Post()
root(@Body(new ValidationPipe()) data: MyDto) {
return data.test;
}
}
Finally, here is the cURL
command to perform a request with an invalid payload (according to my DTO definition):
curl -v \
http://localhost:3000/ \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"test": "I AM NOT A NUMBER! PLEASE TRIGGER A VALIDATION ERROR"
}'
As you can see, here is what the controller responds:
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
> cache-control: no-cache
> content-type: application/json
> Content-Length: 66
>
* upload completely sent off: 66 out of 66 bytes
< HTTP/1.1 201 Created
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 52
< ETag: W/"34-dEFIJF4e8ZgnTeWs0topCHUb1WY"
< Date: Thu, 25 Jan 2018 17:51:12 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
I AM NOT A NUMBER! PLEASE TRIGGER A VALIDATION ERROR%
The expected output was obviously a validation error response.
Environment
Nest version: 4.5.10
For Tooling issues:
- Node version: v8.9.4
- Platform: Linux
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:14 (3 by maintainers)
I’ve been hitting my head for some hours for this issue. I’ve found out it is about the version mismatch of
class-validator
versions between the project and nest.For example: If you have a recent
class-validator
version (as0.8.1
) and use a recent@nest/common
version (like4.5.9
, that will pull itself aclass-validator
version of0.7.3
), your Pipe won’t work: your dtos are in version 8.x and the pipe will use version 7.x and there is a failure on that.I don’t believe its an API error from
class-validator
, but something internally changed to make the versions non-compatible. If you leave the SAME code but solve the version mismatch, everything works.Now, solution time: what can be done?
TransformPipe
inside your project. You only have to fix the imports, but without changing the implementation, it will work since its using also your project version.I’ve tested myself the options 2 and 3 (so I’m not just theorizing), and will actually proceed with option 3: I’ll get latest versions and will leave all working even after the PR lands (and can then update). Option 1 doesnt work before PR and Option 2 will make this issue reappear after the PR.
My 2 cents 😃
Ok, got the validation working, I had in
app/node_modules/@nestjs/common/node_modules
a class-validator, although I use in mine Dtos the class-validator ofapp/node_modules/class-validator
. Maybe you should put ‘class-validator’ in peerDependencies or tell people in the docs that they have to removenestjs/common
’s class-validator package when you add class-validator manually after installing nestjs/common.