Skip to content

feature: different strategy for nested transformations #1164

Open
@mptr

Description

@mptr

Description

I would appreciate a feature that lets you switch the default expose-strategy for nested Objects.

Proposed solution

I'd like to define my Classes like:

class Child {
  cAttr: number;
  
  @Exclude()
  invisible: number;
}
class Parent {
  pAttr: number;

  @ExposeNested() // this changes the default strategy for all sub-transformations to 'exposeAll'
  child: Child;
}

This would instanceToPlain-transform something like:

Default Strategy Output Description
exposeAll { pAttr: 3, child: { cAttr: 7 } } just usual behavior
excludeAll { child: { cAttr: 7 } } pAttr is excluded because default strategy
cAttr is visible because Strategy changed by decoator

The only dirty hack I found for this was to change the strategy by:

@Transform(p => { /* THIS IS BAD, DONT USE IT */
  p.options.strategy = 'exposeAll';
  return p.value;
})

but this will change the strategy not just for nested but for all following transformations, even in parent and so on.

Is there a not-so-hacky workarround for the time being?

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