Nest: class-validator not working nest-typescript-starter

Created on 22 Apr 2018  路  9Comments  路  Source: nestjs/nest

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

class-validator not working

Expected behavior

class-validator should work

Minimal reproduction of the problem with instructions

Following the docs (https://docs.nestjs.com) untill the 'pipes' section (https://docs.nestjs.com/pipes). The pipes section creates a ValidationPipe. If you do not use this one, but try to use the ValidationPipe from the class-validator (npm install class-validator --save). The validation will not work.

@nestjs/common has a node_modules with the same dependency (class-validator). However if you do not install class-validator through npm, starting the application (npm start) will throw an error 'Cannot find module 'class-validator' '.

Workarounds:

  • npm install class-validator --save to install the dependency, remove node_modules in @nestjs/common
    or
  • npm install class-validator --save to install the dependency, do not remove node_modules in @nestjs/common but instead copy paste the installed class-validator from node_modules to your project files and reference to that directory as import

What is the motivation / use case for changing the behavior?


Removing the dependency from @nestjs/common would make it so that the project (following the docs) would work after installing the class-validator dependency.

Environment


Nest version:
  "name": "nest-typescript-starter",
  "version": "1.0.0"

    "@nestjs/common": "^4.5.9",
    "@nestjs/core": "^4.5.10",
    "@nestjs/microservices": "^4.5.8",
    "@nestjs/testing": "^4.5.5",
    "@nestjs/websockets": "^4.5.8",



For Tooling issues:
- Node version: v9.11.1 
- Platform:  Linux


Others:

common potential issue 馃挃 question 馃檶

Most helpful comment

There is a ticket open for this on the class-validator site.

link

my workaround (for now) is to import the same library

import { IsInt, IsString } from '@nestjs/common/node_modules/class-validator';

All 9 comments

Honestly, I cannot reproduce your issue 馃檨

@kamilmysliwiec @bartduisters
I think you have the same problem as https://github.com/nestjs/nest/issues/379

It seems like you're using the latest class-validator version while nest uses an older version as explained by https://github.com/nestjs/nest/issues/379#issuecomment-362764944

I have this same issue. Even with the same version of class-validator (0.8.1) installed in both root and @nestjs/[email protected]. Removing class-validator from the @nestjs/[email protected]/node_modules tree allows validation errors to be thrown correctly.

There is a ticket open for this on the class-validator site.

link

my workaround (for now) is to import the same library

import { IsInt, IsString } from '@nestjs/common/node_modules/class-validator';

In the incoming v5 release, class-validator has been pulled out from internal dependencies. You should no longer face any issue because ValidationPipe will always use the same version of the package.

That will solve the issue, thanks!

1115

import { IsEmail, IsNotEmpty } from 'class-validator';

I'm also facing this issue. Here is my package.json :

"dependencies": {
"@nestjs/common": "^6.7.2",
"@nestjs/config": "^0.0.7",
"@nestjs/core": "^6.7.2",
"@nestjs/jwt": "^6.1.1",
"@nestjs/mongoose": "^7.0.1",
"@nestjs/passport": "^6.2.0",
"@nestjs/platform-express": "^6.7.2",
"@nestjs/swagger": "^4.5.12",
"@nestjs/typeorm": "^6.3.4",
"bcryptjs": "^2.4.3",
"dotenv": "^8.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.21",
"nest-winston": "^1.3.1",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"pg": "^7.18.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.0",
"rxjs": "^6.5.3",
"swagger-ui-express": "^4.1.4",
"winston": "^3.2.1"
},
"devDependencies": {
"@nestjs/cli": "^6.9.0",
"@nestjs/schematics": "^6.7.0",
"@nestjs/testing": "^6.7.1",
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.1",
"@types/jest": "^24.0.18",
"@types/jsonwebtoken": "^8.3.5",
"@types/mongoose": "^5.7.29",
"@types/node": "^12.7.5",
"@types/passport": "^1.0.2",
"@types/passport-jwt": "^3.0.3",
"@types/passport-local": "^1.0.33",
"@types/supertest": "^2.0.8",
"jest": "^24.9.0",
"nodemon": "^2.0.2",
"prettier": "^1.18.2",
"supertest": "^4.0.2",
"ts-jest": "^24.1.0",
"ts-loader": "^6.1.1",
"ts-node": "^8.6.2",
"tsconfig-paths": "^3.9.0",
"tslint": "^5.20.0",
"typescript": "^3.6.3"
}

I thought its part of nestjs core but its not. Installing package separately solved this issue
npm i class-validator --save

Was this page helpful?
0 / 5 - 0 ratings