ValidationPipe still uses class-validator package

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When changing the class-validator package to instead use @nestjs\class-validator, I found that ValidationPipe still requires the class-validator package. This is also verified by this page: https://docs.nestjs.com/techniques/validationUsePipes(new ValidationPipe

[Nest] 150  - 11/11/2021, 11:00:13 AM   ERROR [PackageLoader] The "class-validator" package is missing. Please, make sure to install this library ($ npm install class-validator) to take advantage of ValidationPipe.
#14 77.71   ●  process.exit called with "1"
#14 77.71 
#14 77.71       59 | 
#14 77.71       60 |   @Post()
#14 77.71     > 61 |   @UsePipes(new ValidationPipe({ transform: true }))

Minimum reproduction code

none yet

Steps to reproduce

npm ci npm test npm run build

Expected behavior

I was hoping the class-validator package would have been detected after installing @nestjs\class-validator

Package

Other package

@nestjs/class-validator

NestJS version

8.2.0

Packages versions

"@azure/service-bus": "^7.0.2",
"@nestjs/azure-database": "^2.1.0",
"@nestjs/class-validator": "^0.13.3",
"@nestjs/common": "^8.2.0",
"@nestjs/config": "^1.1.0",
"@nestjs/core": "^8.2.0",
"@nestjs/platform-express": "^8.2.0",

Node.js version

14.18.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

20reactions
kamilmysliwieccommented, Nov 16, 2021

You can start using forks now in the following way:

app.useGlobalPipes(new ValidationPipe(
  {
    validatorPackage: require('@nestjs/class-validator'),
    transformerPackage: require('@nestjs/class-transformer')
  }
));

We’ll keep using the original packages for the time being till we figure out what’s the best approach to migrate.

5reactions
jmcdo29commented, Nov 11, 2021

It would be a breaking change to just swap out class-validator for @nestjs/class-valdator because there’s nothing in the update that would tell npm/yarn/pnpm that a new package would be necessary to download, so people updating package versions would now get this new error seemingly out of nowhere on a minor update. We could do a multi-step try/catch, i.e. try for @nestjs/class-validator catch try class-validator catch throw error, but the less optional package requires we have, usually the better (it’ll also mean needing to update our webpack config in nest-cli to exclude the @nestjs/class-validator and @nestjs/class-transformer packages).

My plans would be to hold out until Nest v9 and have the forked package ready and maintained, and give instructions for how people could override the default if they wanted to, essentially making it opt-in until v9. Something like

@Injectable()
export class NestValidationPipe extends ValidationPipe {
  loadValidator() {
    return require('@nestjs/class-validator');
  }

  loadTransformer() {
    return require('@nestjs/class-transformer');
  }
}

Would be all that’s necessary to use this package (if it’s been published of course, I haven’t checked that part yet)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation | NestJS - A progressive Node.js framework
The ValidationPipe makes use of the powerful class-validator package and its declarative validation decorators. The ValidationPipe provides a convenient ...
Read more >
class-validator doesn't appear to do anything in NestJS ...
In the ValidationPipe.transform method, it returns the raw input, because metatype is undefined: async transform(value, metadata) { const { ...
Read more >
Fixing class-validator issues in a new Nest js project
So, I love the concept of DTO and I was trying to validate it using ValidationPipe , just like the doc says. It...
Read more >
Building a REST API with NestJS and Prisma: Input Validation ...
A NestJS validation pipe will check the arguments passed to a route. ... You will now use the class-validator package to add validation ......
Read more >
API payloads validation and transformation in NestJS - Medium
With NestJS ValidationPipe , handling several validations becomes much easier. ... We do this using decorators provided by the class-validator package, ...
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