1
1
/* eslint-disable @typescript-eslint/camelcase */
2
2
import 'reflect-metadata' ;
3
- import { classToClass , classToPlain , plainToClass } from '../../src/index' ;
3
+ import { classToClass , classToPlain , plainToClass , TransformFnParams } from '../../src/index' ;
4
4
import { defaultMetadataStorage } from '../../src/storage' ;
5
5
import { Expose , Transform , Type } from '../../src/decorators' ;
6
6
import { TransformationType } from '../../src/enums' ;
@@ -12,7 +12,7 @@ describe('custom transformation decorator', () => {
12
12
13
13
class User {
14
14
@Expose ( { name : 'user_name' } )
15
- @Transform ( value => value . toUpperCase ( ) )
15
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
16
16
name : string ;
17
17
}
18
18
@@ -31,8 +31,8 @@ describe('custom transformation decorator', () => {
31
31
id : number ;
32
32
name : string ;
33
33
34
- @Transform ( value => value . toString ( ) , { toPlainOnly : true } )
35
- @Transform ( value => dayjs ( value ) , { toClassOnly : true } )
34
+ @Transform ( ( { value } ) => value . toString ( ) , { toPlainOnly : true } )
35
+ @Transform ( ( { value } ) => dayjs ( value ) , { toClassOnly : true } )
36
36
date : Date ;
37
37
}
38
38
@@ -70,11 +70,11 @@ describe('custom transformation decorator', () => {
70
70
name : string ;
71
71
72
72
@Type ( ( ) => Date )
73
- @Transform ( value => dayjs ( value ) , { since : 1 , until : 2 } )
73
+ @Transform ( ( { value } ) => dayjs ( value ) , { since : 1 , until : 2 } )
74
74
date : Date ;
75
75
76
76
@Type ( ( ) => Date )
77
- @Transform ( value => value . toString ( ) , { groups : [ 'user' ] } )
77
+ @Transform ( ( { value } ) => value . toString ( ) , { groups : [ 'user' ] } )
78
78
lastVisitDate : Date ;
79
79
}
80
80
@@ -119,10 +119,12 @@ describe('custom transformation decorator', () => {
119
119
it ( '@Transform decorator callback should be given correct arguments' , ( ) => {
120
120
defaultMetadataStorage . clear ( ) ;
121
121
122
+ let keyArg : string ;
122
123
let objArg : any ;
123
124
let typeArg : TransformationType ;
124
125
125
- function transformCallback ( value : any , obj : any , type : TransformationType ) : any {
126
+ function transformCallback ( { value, key, obj, type } : TransformFnParams ) : any {
127
+ keyArg = key ;
126
128
objArg = obj ;
127
129
typeArg = type ;
128
130
return value ;
@@ -139,13 +141,15 @@ describe('custom transformation decorator', () => {
139
141
} ;
140
142
141
143
plainToClass ( User , plainUser ) ;
144
+ expect ( keyArg ) . toBe ( 'name' ) ;
142
145
expect ( objArg ) . toEqual ( plainUser ) ;
143
146
expect ( typeArg ) . toEqual ( TransformationType . PLAIN_TO_CLASS ) ;
144
147
145
148
const user = new User ( ) ;
146
149
user . name = 'Johny Cage' ;
147
150
148
151
classToPlain ( user ) ;
152
+ expect ( keyArg ) . toBe ( 'name' ) ;
149
153
expect ( objArg ) . toEqual ( user ) ;
150
154
expect ( typeArg ) . toEqual ( TransformationType . CLASS_TO_PLAIN ) ;
151
155
} ) ;
@@ -191,7 +195,7 @@ describe('custom transformation decorator', () => {
191
195
public address : Address ;
192
196
193
197
@Type ( ( ) => Hobby )
194
- @Transform ( value => value . filter ( ( hobby : any ) => hobby . type === 'sport' ) , { toClassOnly : true } )
198
+ @Transform ( ( { value } ) => value . filter ( ( hobby : any ) => hobby . type === 'sport' ) , { toClassOnly : true } )
195
199
public hobbies : Hobby [ ] ;
196
200
197
201
public age : number ;
@@ -224,7 +228,7 @@ describe('custom transformation decorator', () => {
224
228
}
225
229
226
230
class Programming extends Hobby {
227
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
231
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
228
232
specialAbility : string ;
229
233
}
230
234
@@ -278,7 +282,7 @@ describe('custom transformation decorator', () => {
278
282
}
279
283
280
284
class Programming extends Hobby {
281
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
285
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
282
286
specialAbility : string ;
283
287
}
284
288
@@ -330,7 +334,7 @@ describe('custom transformation decorator', () => {
330
334
}
331
335
332
336
class Programming extends Hobby {
333
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
337
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
334
338
specialAbility : string ;
335
339
}
336
340
@@ -383,7 +387,7 @@ describe('custom transformation decorator', () => {
383
387
}
384
388
385
389
class Programming extends Hobby {
386
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
390
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
387
391
specialAbility : string ;
388
392
}
389
393
@@ -431,7 +435,7 @@ describe('custom transformation decorator', () => {
431
435
}
432
436
433
437
class Programming extends Hobby {
434
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
438
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
435
439
specialAbility : string ;
436
440
}
437
441
@@ -479,7 +483,7 @@ describe('custom transformation decorator', () => {
479
483
}
480
484
481
485
class Programming extends Hobby {
482
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
486
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
483
487
specialAbility : string ;
484
488
}
485
489
@@ -530,7 +534,7 @@ describe('custom transformation decorator', () => {
530
534
}
531
535
532
536
class Programming extends Hobby {
533
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
537
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
534
538
specialAbility : string ;
535
539
}
536
540
@@ -578,7 +582,7 @@ describe('custom transformation decorator', () => {
578
582
}
579
583
580
584
class Programming extends Hobby {
581
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
585
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
582
586
specialAbility : string ;
583
587
}
584
588
@@ -634,7 +638,7 @@ describe('custom transformation decorator', () => {
634
638
}
635
639
636
640
class Programming extends Hobby {
637
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
641
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
638
642
specialAbility : string ;
639
643
}
640
644
@@ -682,7 +686,7 @@ describe('custom transformation decorator', () => {
682
686
}
683
687
684
688
class Programming extends Hobby {
685
- @Transform ( ( value : string ) => value . toUpperCase ( ) )
689
+ @Transform ( ( { value } ) => value . toUpperCase ( ) )
686
690
specialAbility : string ;
687
691
}
688
692
0 commit comments