Skip to content

Commit d40552b

Browse files
feat: pass ClassTransformOptions to the transformFn of @Transform (#294)
1 parent 022f63b commit d40552b

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,13 @@ The `@Transform` decorator is given more arguments to let you configure how you
777777
@Transform(({ value, key, obj, type }) => value)
778778
```
779779

780-
| Argument | Description |
781-
| -------- | --------------------------------------------- |
782-
| `value` | The property value before the transformation. |
783-
| `key` | The name of the transformed property. |
784-
| `obj` | The transformation source object. |
785-
| `type` | The transformation type. |
780+
| Argument | Description |
781+
| --------- | ------------------------------------------------------- |
782+
| `value` | The property value before the transformation. |
783+
| `key` | The name of the transformed property. |
784+
| `obj` | The transformation source object. |
785+
| `type` | The transformation type. |
786+
| `options` | The options object passed to the transformation method. |
786787

787788
## Other decorators[](#table-of-contents)
788789

src/TransformOperationExecutor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export class TransformOperationExecutor {
385385
}
386386

387387
metadatas.forEach(metadata => {
388-
value = metadata.transformFn({ value, key, obj, type: transformationType });
388+
value = metadata.transformFn({ value, key, obj, type: transformationType, options: this.options });
389389
});
390390

391391
return value;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { TransformationType } from '../../enums';
2+
import { ClassTransformOptions } from '../class-transformer-options.interface';
23

34
export interface TransformFnParams {
45
value: any;
56
key: string;
67
obj: any;
78
type: TransformationType;
9+
options: ClassTransformOptions;
810
}

test/functional/custom-transform.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/camelcase */
22
import 'reflect-metadata';
3-
import { classToClass, classToPlain, plainToClass, TransformFnParams } from '../../src/index';
3+
import { classToClass, classToPlain, ClassTransformOptions, plainToClass, TransformFnParams } from '../../src/index';
44
import { defaultMetadataStorage } from '../../src/storage';
55
import { Expose, Transform, Type } from '../../src/decorators';
66
import { TransformationType } from '../../src/enums';
@@ -121,11 +121,13 @@ describe('custom transformation decorator', () => {
121121
let keyArg: string;
122122
let objArg: any;
123123
let typeArg: TransformationType;
124+
let optionsArg: ClassTransformOptions;
124125

125-
function transformCallback({ value, key, obj, type }: TransformFnParams): any {
126+
function transformCallback({ value, key, obj, type, options }: TransformFnParams): any {
126127
keyArg = key;
127128
objArg = obj;
128129
typeArg = type;
130+
optionsArg = options;
129131
return value;
130132
}
131133

@@ -138,19 +140,26 @@ describe('custom transformation decorator', () => {
138140
const plainUser = {
139141
name: 'Johny Cage',
140142
};
143+
const options: ClassTransformOptions = {
144+
groups: ['user', 'user.email'],
145+
version: 2,
146+
};
141147

142-
plainToClass(User, plainUser);
148+
plainToClass(User, plainUser, options);
143149
expect(keyArg).toBe('name');
144150
expect(objArg).toEqual(plainUser);
145151
expect(typeArg).toEqual(TransformationType.PLAIN_TO_CLASS);
152+
expect(optionsArg).toBe(options);
146153

147154
const user = new User();
148155
user.name = 'Johny Cage';
156+
optionsArg = undefined;
149157

150-
classToPlain(user);
158+
classToPlain(user, options);
151159
expect(keyArg).toBe('name');
152160
expect(objArg).toEqual(user);
153161
expect(typeArg).toEqual(TransformationType.CLASS_TO_PLAIN);
162+
expect(optionsArg).toBe(options);
154163
});
155164

156165
let model: any;

0 commit comments

Comments
 (0)