Skip to content

feat: pass ClassTransformOptions to the transformFn of @Transform #294

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

Merged

Conversation

lucas-gregoire
Copy link
Contributor

@lucas-gregoire lucas-gregoire commented Sep 13, 2019

The goal of this PR is to be able to access to ClassTransformOptions provided by transformation methods directly inside the transformFn of @Transform decorators.

class User {
    @Expose()
    name: string;

    @Expose()
    @Transform((_0, _1, _2, options) => options.groups)
    groups: string[];
}

plainToClass(
    User,
    { name: 'Johny Cage' },
    { groups: ['user.full'] },
)

// Result
// User {
//    name: 'Johny Cage',
//    groups: ['user.full'],
// }

Another use-case could be:
If i want to use a transformation method (e.g. plainToClass) inside a @Transform and extend the groups, I can do:

class User {
    @Expose()
    name: string;

    @Expose()
    @Transform(
        (value, _0, _1, { groups: inheritedGroups }) => plainToClass(
            Foo, 
            value, 
            { groups: [...inheritedGroups, 'additional_group'] }
        ),
        { toClassOnly: true }
    )
    foos: Foo[];
}

This pull request fixes following issues:

@cojack
Copy link
Contributor

cojack commented Dec 1, 2019

Hmm can we trigger somehow travis to make a run once again for this MR, to check why the heck tests fail?

@lucas-gregoire lucas-gregoire force-pushed the feature/transmit-options branch from a6479d2 to 0d42970 Compare December 3, 2019 12:38
@lucas-gregoire
Copy link
Contributor Author

I force-pushed the last commit to trigger a new build.
But there is a compatibility issue between gulp@3 and node@12 (current stable): gulpjs/gulp#2324
So it's necessary to upgrade gulp to v4

@lucas-gregoire
Copy link
Contributor Author

lucas-gregoire commented Dec 6, 2019

#266
#263

@stewartmcgown
Copy link

Is something blocking this PR being merged? Seems like a very useful feature. Happy to review if one is needed.

@julien-sarazin
Copy link

Hi there, I need this feature so hard. Please make class-transformer great again!

@NoNameProvided
Copy link
Member

This is related to #330. We need to rebase this after that one is merged.

@lucas-gregoire lucas-gregoire force-pushed the feature/transmit-options branch from 0d42970 to 273b32c Compare September 22, 2020 12:53
@lucas-gregoire lucas-gregoire force-pushed the feature/transmit-options branch from 273b32c to 5b9fbdf Compare September 22, 2020 12:55
@lucas-gregoire
Copy link
Contributor Author

Rebase is done, fell free to comment/review 😉

@NoNameProvided NoNameProvided changed the title feat: transmit ClassTransformOptions to the transformFn of @Transform feat: pass ClassTransformOptions to the transformFn of @Transform Jan 14, 2021
@NoNameProvided NoNameProvided merged commit d40552b into typestack:develop Jan 14, 2021
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2021
@lucas-gregoire lucas-gregoire deleted the feature/transmit-options branch March 23, 2021 13:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants