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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -777,12 +777,13 @@ The `@Transform` decorator is given more arguments to let you configure how you
@Transform(({ value, key, obj, type }) => value)
```

| Argument | Description |
| -------- | --------------------------------------------- |
| `value` | The property value before the transformation. |
| `key` | The name of the transformed property. |
| `obj` | The transformation source object. |
| `type` | The transformation type. |
| Argument | Description |
| --------- | ------------------------------------------------------- |
| `value` | The property value before the transformation. |
| `key` | The name of the transformed property. |
| `obj` | The transformation source object. |
| `type` | The transformation type. |
| `options` | The options object passed to the transformation method. |

## Other decorators[⬆](#table-of-contents)

Expand Down
2 changes: 1 addition & 1 deletion src/TransformOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export class TransformOperationExecutor {
}

metadatas.forEach(metadata => {
value = metadata.transformFn({ value, key, obj, type: transformationType });
value = metadata.transformFn({ value, key, obj, type: transformationType, options: this.options });
});

return value;
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/metadata/transform-fn-params.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { TransformationType } from '../../enums';
import { ClassTransformOptions } from '../class-transformer-options.interface';

export interface TransformFnParams {
value: any;
key: string;
obj: any;
type: TransformationType;
options: ClassTransformOptions;
}
17 changes: 13 additions & 4 deletions test/functional/custom-transform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/camelcase */
import 'reflect-metadata';
import { classToClass, classToPlain, plainToClass, TransformFnParams } from '../../src/index';
import { classToClass, classToPlain, ClassTransformOptions, plainToClass, TransformFnParams } from '../../src/index';
import { defaultMetadataStorage } from '../../src/storage';
import { Expose, Transform, Type } from '../../src/decorators';
import { TransformationType } from '../../src/enums';
Expand Down Expand Up @@ -121,11 +121,13 @@ describe('custom transformation decorator', () => {
let keyArg: string;
let objArg: any;
let typeArg: TransformationType;
let optionsArg: ClassTransformOptions;

function transformCallback({ value, key, obj, type }: TransformFnParams): any {
function transformCallback({ value, key, obj, type, options }: TransformFnParams): any {
keyArg = key;
objArg = obj;
typeArg = type;
optionsArg = options;
return value;
}

Expand All @@ -138,19 +140,26 @@ describe('custom transformation decorator', () => {
const plainUser = {
name: 'Johny Cage',
};
const options: ClassTransformOptions = {
groups: ['user', 'user.email'],
version: 2,
};

plainToClass(User, plainUser);
plainToClass(User, plainUser, options);
expect(keyArg).toBe('name');
expect(objArg).toEqual(plainUser);
expect(typeArg).toEqual(TransformationType.PLAIN_TO_CLASS);
expect(optionsArg).toBe(options);

const user = new User();
user.name = 'Johny Cage';
optionsArg = undefined;

classToPlain(user);
classToPlain(user, options);
expect(keyArg).toBe('name');
expect(objArg).toEqual(user);
expect(typeArg).toEqual(TransformationType.CLASS_TO_PLAIN);
expect(optionsArg).toBe(options);
});

let model: any;
Expand Down