question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidationPipe not working

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ShadowManucommented, Feb 3, 2018

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 (as 0.8.1) and use a recent @nest/common version (like 4.5.9, that will pull itself a class-validator version of 0.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?

  1. Use latest version and wait for PR #383 to land. Slowly but readily the version matches.
  2. Downgrade your version in package.json
  3. Copy and Paste the 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 😃

2reactions
marcjcommented, Mar 19, 2018

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 of app/node_modules/class-validator. Maybe you should put ‘class-validator’ in peerDependencies or tell people in the docs that they have to remove nestjs/common’s class-validator package when you add class-validator manually after installing nestjs/common.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing NestJS Validation Pipe not working - Stack Overflow
I found the mistake in my tests. Once I added the ValidationPipe() into the app definition: app = moduleFixture.createNestApplication(); app ...
Read more >
How to use the in-built NestJS ValidationPipe?
We look at how to use in-built NestJS ValidationPipe and setup declarative validations using the powerful class-validator package.
Read more >
Validation | NestJS - A progressive Node.js framework
The ValidationPipe provides a convenient approach to enforce validation rules for ... Our ValidationPipe can also filter out properties that should not be ......
Read more >
Fixing validation error in NestJS when ... - Darragh ORiordan
Parsing and validating these the wrong way can cause issues. ... setting in the validation pipe configuration as expected now.
Read more >
API payloads validation and transformation in NestJS - Medium
Output procedures ensure that the data we expose as output does not ... With NestJS ValidationPipe , handling several validations becomes much easier....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found