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

IsNotEmpty at Creation + IsEmpty at Update #194

Closed
lapwat opened this issue Jul 30, 2019 · 7 comments
Closed

IsNotEmpty at Creation + IsEmpty at Update #194

lapwat opened this issue Jul 30, 2019 · 7 comments

Comments

@lapwat
Copy link

lapwat commented Jul 30, 2019

Hello everyone,

I cannot make this one working:

  @IsNotEmpty({ groups: [CREATE] })
  @IsEmpty({ groups: [UPDATE] })
  name: string

When I try to create with a name in my payload, the validator blocks the call: name must be empty.

Best,

@michaelyali
Copy link
Member

Hi,
are you sure that you set those decorators right?
Here is the working example:

@IsOptional({ groups: [UPDATE] })
@IsNotEmpty({ groups: [CREATE] })

I'm not sure that it's related to the crud lib itself, I have a feeling that you mess up with something in your project :(
If it's still an issue please provide me with the example repo.

@bamandine
Copy link

Hi,

I have the same issue : I have a required field for the creation of my object, but I don't want this field to be modified after.

With the @IsOptional({ groups: [UPDATE] }) option, the field is not required but I can still modify it if I put it on the request body.

Also, I try this :

  @IsDefined({ groups: [CREATE] })
  @IsEmpty({ groups: [UPDATE] })
  name: string

But same result : on PUT and PATCH, If I don't add the name field into the request body, I have an error :

"isDefined": "name should not be null or undefined"

@bamandine
Copy link

So, after some tests, we have found what was the problem: we had a ValidationPipe defined globally in our project. Once this global ValidationPipe removed, then the 2 validations are working.

@lapwat
Copy link
Author

lapwat commented Aug 14, 2019

@bamandine I don't understand how a guard put at the global level can influence the behaviour of the decorator.

Disabling the global guard is a workaround ? Or is there some logical explanation for it to work ?

@bamandine
Copy link

@lapwat The global ValidationPipe had not been configured to use CRUD extension groups. I guess it was related.

@alisherafat01
Copy link

same issue for me, global validaton pipe should not be disabled for this issue and there is some problem with this lib

@rrufus
Copy link

rrufus commented Jan 15, 2023

@alisherafat01 I came across the same issue, I found the solution was on https://docs.nestjs.com/pipes#global-scoped-pipes

Global pipes are used across the whole application, for every controller and every route handler.

Note that in terms of dependency injection, global pipes registered from outside of any module (with useGlobalPipes() as in the example above) cannot inject dependencies since the binding has been done outside the context of any module. In order to solve this issue, you can set up a global pipe directly from any module using the following construction:

import { Module } from '@nestjs/common';
import { APP_PIPE } from '@nestjs/core';

@Module({
  providers: [
    {
      provide: APP_PIPE,
      useClass: ValidationPipe,
    },
  ],
})
export class AppModule {}

Having this in your AppModule makes the global validation pipe work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants