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

Support for optional data formatting based on the serialization strategy #82

Open
BerciTheBeast opened this issue Jan 7, 2020 · 3 comments

Comments

@BerciTheBeast
Copy link

BerciTheBeast commented Jan 7, 2020

I propose the addition of optional formatting of values based on the serialization strategy that was used.

Example:
In model:

@prop(...)
public firstName: string;

@prop({
...
    serializable: ['short', 'full'],
    //some formatting logic based on serialization
})
public lastName: string;

Usage:

user.serialize('short'); // {firstName: 'John', lastName: 'S.'}
user.serialize('full'); // {firstName: 'John', lastName: 'Smith'}
@xpepermint
Copy link
Member

Thanks for the proposal. Do you have a use case for this? I'm not sure if that's a good practice to have getters with "unexpected" results. For what you describe I would suggest implementing your own getter for a specific field, adding "transform field" function or simply having 2 fields.

Workaround 1:

class User {
  @prop({
    serializable: ['short', 'full'],
    getter() { },
  })
  public lastName: string;
  public shortenLastNameFormat() {...}
}
const user = new User();
user.shortenLastNameFormat();
user.serialize();

Workaround 2:

class User {
  @prop({
    serializable: ['short', 'full'],
    getter() { },
  })
  public lastName: string;
  public serialize(f) {
    const s = super.serialize(f);
    if (f == 'short') s.firstName = 'X';
    return s;
  }
}
const user = new User();
user.shortenLastNameFormat();
user.serialize();

@BerciTheBeast
Copy link
Author

BerciTheBeast commented Jan 7, 2020

A simple use case would be if we wanted to format the data based on certain permissions.

Let's say, for example, 2 types of people can access a list of objects "person" with the property lastName. One type is "supervisor", the other type is "user".

The supervisor should be able to see the full lastName - so they can perform supervision efficiently, while user should only be able to see the first letter, to prevent them from looking up the "person" in question.


Aside from that, thank you for the workarounds.

@xpepermint
Copy link
Member

I see. Let me think about how would be the right way to handle this case.

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

No branches or pull requests

2 participants