Skip to content

feature: expose defaultMetadataStorage #815

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
MHekert opened this issue Jul 11, 2021 · 1 comment · May be fixed by #996
Open

feature: expose defaultMetadataStorage #815

MHekert opened this issue Jul 11, 2021 · 1 comment · May be fixed by #996
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@MHekert
Copy link

MHekert commented Jul 11, 2021

Description

As for now there is no option to combine multiple classes with class-transformer decorators using mixins pattern because metadata is stored internally inside package.

There is no way to apply metadata from one class to another. Similar approach is possible for class-validator since it exposes metadata (code example). I tested code snippet for class-validator and coping class-transformer metadata using exposed defaultMetadataStorage singleton (using modified version of this package). Both things work.

const transformFunc = ({ value }) => parseInt(value);

class YearDTO {
  @Transform(transformFunc)
  year: number;
}

class MonthDTO {
  @Transform(transformFunc)
  month: number;
}

class CombinedDTO { }
interface CombinedDTO extends YearDTO, MonthDTO { }

applyMixins(CombinedDTO, [YearDTO, MonthDTO]);

const result = plainToInstance(CombinedDTO, {
  year: "2021",
  month: "2"
})

As is now year and month won't be parsed. After adding additional logic to applyMixins with findTransformMetadatas and addTransformMetadata from MetadataStorage it will be parsed.

Proposed solution

Expose defaultMetadataStorage singleton. It would allow for manual coping of metadata to derived class.

add export in index.ts:

export * from './storage';

I am happy to open PR with this change.

@MHekert MHekert added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Jul 11, 2021
@MHekert MHekert linked a pull request Nov 21, 2021 that will close this issue
6 tasks
@vlad-ostrovski
Copy link

Any news on this?

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

Successfully merging a pull request may close this issue.

2 participants