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

Groups are not used when performing transforms in validation.pipe #1374

Closed
snowbunny78 opened this issue Dec 12, 2018 · 3 comments
Closed

Groups are not used when performing transforms in validation.pipe #1374

snowbunny78 opened this issue Dec 12, 2018 · 3 comments

Comments

@snowbunny78
Copy link

I'm submitting a...


[ ] 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.

Current behavior

Groups are not used when performing transforms in validation.pipe

Expected behavior

The transform method respects the group(s) that are passed in the ValidatorOptions

Minimal reproduction of the problem with instructions

I have several modules in different containers, communicating over http. I want to be able to create a MyClass object with properties "visibleToAll" and "visibleToInternal" in the publicly-visible endpoint, send this around my other modules via http (each with a main.ts as below), and then return the object to the web front end with the "visibleToInternal" property stripped.

interface.ts

@Exclude()
export class MyClass {
    @Expose()
    visibleToAll: string;

    @Expose({ groups: ["internal"] })
    visibleToInternal: string
}

all modules main.ts

    app.useGlobalPipes(new ValidationPipe({
		transform: true,
		groups: ["internal"]
	}));

But when I post the object to another module, it hits the validation.pipe transform() method, where the groups attribute isn't being used in the classtoPlain and plainToClass transformations, so even internally, I only have access to the "visibleToAll" property, as the "visibleToInternal" property is being stripped.

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

As above

Environment


Nest version: 5.4.0
@kamilmysliwiec
Copy link
Member

See here:
https://github.com/nestjs/nest/blob/master/packages/common/pipes/validation.pipe.ts#L53-L56

const entity = classTransformer.plainToClass(
    metatype,
    this.toEmptyIfNil(value),
  );
const errors = await classValidator.validate(entity, this.validatorOptions);

Validation options are passed to the validate method, not into to the plainToClass. However though, we can extend ValidationOptions to take another property - TransformOptions that would be passed to the plainToClass function. PRs are welcome!

FionaLovett added a commit to FionaLovett/nest that referenced this issue Jan 2, 2019
allows plainToClass to expose class properties to defined groups as per issue nestjs#1374
FionaLovett added a commit to FionaLovett/nest that referenced this issue Jan 2, 2019
allows plainToClass to expose class properties to defined groups as per issue nestjs#1374
FionaLovett added a commit to FionaLovett/nest that referenced this issue Jan 2, 2019
allows plainToClass to expose class properties to defined groups as per issue nestjs#1374
FionaLovett added a commit to FionaLovett/nest that referenced this issue Jan 2, 2019
allows plainToClass to expose class properties to defined groups as per issue nestjs#1374
@kamilmysliwiec
Copy link
Member

Available in 5.6.1

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants