-
Notifications
You must be signed in to change notification settings - Fork 521
Description
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.