[ ] 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.
class-validator not working
class-validator should work
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:
Removing the dependency from @nestjs/common would make it so that the project (following the docs) would work after installing the class-validator dependency.
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:
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.
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!
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
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';