Skip to content
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

[Feature] Enable overriding of global ValidationPipe in controller (as in #2390) #7779

Closed
lucasvianav opened this issue Aug 6, 2021 · 1 comment

Comments

@lucasvianav
Copy link

Feature Request

Add an option to make local pipes override global ones.

Is your feature request related to a problem? Please describe.

Not really a problem but it's due to a global ValidationPipe taking precedence over a local one.

Describe the solution you'd like

I would like to use a global validation pipe and be able to override it at module/controller/method level in order to specify different options when needed.

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

I have a project in which I've the ValidationPipe globally in main.ts:

async function bootstrap () {
    const app = await NestFactory.create(AppModule)
    app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }))
    useContainer(app.select(AppModule), { fallbackOnErrors: true })
    await app.listen(process.env.API_PORT || 3000)
}
bootstrap()

But I have some classes with different validations depending on the group used, like so:

export class SocialMediaDto {
    @IsEnum(AllowedMemberSocialMedia, { groups: ['member'] })
    @IsEnum(AllowedCompanySocialMedia, { groups: ['company'] })
    @IsNewSocialMedia({ always: true })
    name: string

    @IsSocialMediaValueOK({ always:  true })
    value: string
}

The method that creates social media for a member would validate with group 'member' and the method that creates social media for the company would validate with group 'company'.

I would like to use the global validation pipe (in order to validate the majority of classes that do not use different groups) and be able to override it at module/controller/method level in order to specify a group when needed.

Another use case is the one described in #2390.

@kamilmysliwiec
Copy link
Member

Thanks for your suggestion!

This has been discussed in the past and we decided to not implement it in the foreseeable future.

@nestjs nestjs locked and limited conversation to collaborators Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants