-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValidationPipe still uses class-validator package #8562
Comments
I'm pretty sure it will soon. To avoid breaking changes, I think we can still use |
sadly the |
My point is that changing this will be a breaking change as Nest will try to load Nest can release a patch that still loads |
nevermind. Since |
It would be a breaking change to just swap out 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) |
This is also a problem with class-transformer and ClassSerializerInterceptor, you can patch it using the following code: /* eslint-disable global-require */
/* eslint-disable @typescript-eslint/no-var-requires */
import {
ClassSerializerInterceptor as NestClassSerializerInterceptor,
ClassSerializerInterceptorOptions,
Injectable,
Optional,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
@Injectable()
export class ClassSerializerInterceptor extends NestClassSerializerInterceptor {
constructor(
protected readonly reflector: Reflector,
@Optional()
protected readonly defaultOptions: ClassSerializerInterceptorOptions = {},
) {
super(reflector, {
...defaultOptions,
transformerPackage: require('@nestjs/class-transformer'),
});
}
} |
FYI that if you are using below v8.2, FelipeEmerim's code sample won't work since before v8.2 that class had a less flexible constructor. I'm guessing that if you use a previous nest version you couldn't extend ClassSerializerInterceptor easily and instead would just want to copy/paste that class into your project if this matters enough to you. Maybe there is a better way? In any case, thanks for your code snippets @FelipeEmerim and @jmcdo29 |
I guess using https://www.npmjs.com/package/patch-package will be better as you can just replace |
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. |
Hey, It's been almost a year now. Any update on this? |
Same here. Doesn't work using app.useGlobalPipes(new ValidationPipe({
transform: true
})); or even app.useGlobalPipes(new ValidationPipe({
validatorPackage: require('@nestjs/class-validator'),
transformerPackage: require('@nestjs/class-transformer')
})); But works using @UsePipes(new ValidationPipe({transform: true}))
@Controller()
export class MyFooController {
} I was looking for a way of using the Config Module to apply this, but still no joy. Cheers |
@KikoCosmetics can you create a minimum reproduction and a new issue if the |
@jmcdo29 ok I'll try to setup a webcontainer on Stackblitz...see what happens! |
@jmcdo29 Done 😁 https://stackblitz.com/edit/node-toz46t If you uncomment the just the global: in the controller: |
@KikoCosmetics in your |
You're right it works now! For anyone reading this in the future, if you actually go further in the "Global scoped pipes" section in the docs, there's a cooler way of achieving the same result, which is using import {APP_PIPE} from "@nestjs/core";
import {Module, ValidationPipe} from "@nestjs/common";
// ...
providers: [
{
provide: APP_PIPE,
useFactory: function (): ValidationPipe {
return new ValidationPipe({
transform: true,
whitelist: true, // remove extra props
})
}
}
]
// ... |
It appears the latest versions of class-transformer and class-validator are no longer flagged for the critical security vulnerability. So maybe nestjs/class-transformer and nestjs/class-validator should be marked as deprecated? |
There haven't been any releases lately so I doubt the vulnerability has been fixed. Can't run the test right now though. Also @nestjs packages introduced more than that fix, there were many other resolved issues. |
Regarding the original question, I didn't like patch-package nor specifying packages manually in every pipe, we have decided to go with package aliasing: "dependencies": {
"class-transformer": "npm:@nestjs/[email protected]",
"class-validator": "npm:@nestjs/[email protected]",
} Works like a charm and didn't require any code to set it up. |
Is there an existing issue for this?
Current behavior
When changing the
class-validator
package to instead use@nestjs\class-validator
, I found thatValidationPipe
still requires theclass-validator
package.This is also verified by this page:
https://docs.nestjs.com/techniques/validationUsePipes(new ValidationPipe
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
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
@nestjs/class-validator
NestJS version
8.2.0
Packages versions
Node.js version
14.18.1
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: