Skip to content

feat: allow defining different groups for toPlain and toClass operations #378

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

Open
NoNameProvided opened this issue Jul 29, 2020 · 5 comments · May be fixed by #1259
Open

feat: allow defining different groups for toPlain and toClass operations #378

NoNameProvided opened this issue Jul 29, 2020 · 5 comments · May be fixed by #1259
Labels
type: feature Issues related to new features.

Comments

@NoNameProvided
Copy link
Member

Description

Note: Requested by @slavafomin, this issue is created from #57.

Users should be able to allow defining different groups for plainToClass and classToPlain. Example:

@Exclude()
class User {
  @Expose({ toClassOnly: true, groups: ["create", "update"] })
  @Expose({ toPlainOnly: true })
  public email?: string;

  @Expose({ toClassOnly: true, groups: ["create", "update"] })
  @Expose({ toPlainOnly: true })
  public firstName?: string;

  @Expose({ toClassOnly: true, groups: ["create"] })
  @Expose({ toPlainOnly: true })
  public password?: string;
}
const instance = plainToClass(User, {
  email: "[email protected]",
  firstName: "John",
  password: "12345"
}, { groups: ["update"] });

/* Instance should be { firstName: 'John', email: 'email@example.com'  }*/
// parameter is a User instance
const json= classToPlain(<User>{
  email: "[email protected]",
  firstName: "John",
  password: "12345"
});

/* JSON should be { firstName: 'John', password: '12345', email: 'email@example.com'  }*/

We need to discuss what is the motivation behind this and can we achieve this with the current toolset or not.

@NoNameProvided NoNameProvided added the type: discussion Issues discussing any topic. label Jul 29, 2020
@NoNameProvided NoNameProvided added type: feature Issues related to new features. and removed type: discussion Issues discussing any topic. labels Jul 29, 2020
@exejutable

This comment has been minimized.

@NoNameProvided NoNameProvided changed the title feat: allow defining different groups for toPlain and toClass operatiosn feat: allow defining different groups for toPlain and toClass operations Feb 14, 2021
@AdrianMcGrathFlailsoft
Copy link

We have come across a use case where this would be extremely helpful. In our situation, we needed to set the "name" attribute differently based on whether we were going classToPlain or plainToClass.

@CodingMeSwiftly
Copy link

Any news? We also came across some use cases for this. Not only multiple @Expose but also multiple @Transform would be really nice to handle one single class that maps to multiple sources and targets. Right now we have to duplicate the class if we want to map to multiple sources/targets.

@miapolis
Copy link

miapolis commented Apr 24, 2022

Any updates on this? Would be super helpful for reusing classes efficiently with both functions. Another example, fetching from a database and having the document model transformed with all specified properties exposed, but when serializing for a user it makes use of the groups.

I guess you could just have a single group you apply to everything when fetching from the database, but it would be nice to have this option as well.

Iran-110 added a commit to Iran-110/class-transformer that referenced this issue Jul 2, 2022
Iran-110 added a commit to Iran-110/class-transformer that referenced this issue Jul 2, 2022
@Iran-110 Iran-110 linked a pull request Jul 2, 2022 that will close this issue
6 tasks
@n08i40k
Copy link

n08i40k commented Jan 15, 2025

Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Issues related to new features.
Development

Successfully merging a pull request may close this issue.

6 participants