Skip to content

feature: expose defaultMetadataStorage #815

Open
@MHekert

Description

@MHekert

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions