@@ -6,27 +6,55 @@ import {defaultMetadataStorage} from "../../src/storage";
6
6
import { Expose , Type } from "../../src/decorators" ;
7
7
import { expect } from "chai" ;
8
8
9
+ describe ( "implicit type conversion" , ( ) => {
10
+ it ( "should run only when enabled" , ( ) => {
11
+ defaultMetadataStorage . clear ( ) ;
12
+
13
+ class SimpleExample {
14
+
15
+ @Expose ( )
16
+ readonly implicitTypeNumber : number ;
17
+
18
+ @Expose ( )
19
+ readonly implicitTypeString : string ;
20
+ }
21
+
22
+ const result1 : SimpleExample = plainToClass ( SimpleExample , {
23
+ implicitTypeNumber : "100" ,
24
+ implicitTypeString : 133123 ,
25
+ } , { enableImplicitConversion : true } ) ;
26
+
27
+ const result2 : SimpleExample = plainToClass ( SimpleExample , {
28
+ implicitTypeNumber : "100" ,
29
+ implicitTypeString : 133123 ,
30
+ } , { enableImplicitConversion : false } ) ;
31
+
32
+ expect ( result1 ) . to . deep . equal ( { implicitTypeNumber : 100 , implicitTypeString : "133123" } ) ;
33
+ expect ( result2 ) . to . deep . equal ( { implicitTypeNumber : "100" , implicitTypeString : 133123 } ) ;
34
+ } ) ;
35
+ } ) ;
36
+
9
37
describe ( "implicit and explicity type declarations" , ( ) => {
10
38
11
39
defaultMetadataStorage . clear ( ) ;
12
40
13
41
class Example {
14
-
42
+
15
43
@Expose ( )
16
44
readonly implicitTypeViaOtherDecorator : Date ;
17
45
18
46
@Type ( )
19
47
readonly implicitTypeViaEmptyTypeDecorator : number ;
20
-
21
- @Type ( ( ) => String )
48
+
49
+ @Type ( ( ) => String )
22
50
readonly explicitType : string ;
23
51
}
24
-
25
- const result : Example = plainToClass ( Example , {
26
- implicitTypeViaOtherDecorator : "2018-12-24T12:00:00Z" ,
27
- implicitTypeViaEmptyTypeDecorator : "100" ,
28
- explicitType : 100 ,
29
- } ) ;
52
+
53
+ const result : Example = plainToClass ( Example , {
54
+ implicitTypeViaOtherDecorator : "2018-12-24T12:00:00Z" ,
55
+ implicitTypeViaEmptyTypeDecorator : "100" ,
56
+ explicitType : 100 ,
57
+ } , { enableImplicitConversion : true } ) ;
30
58
31
59
it ( "should use implicitly defined design:type to convert value when no @Type decorator is used" , ( ) => {
32
60
expect ( result . implicitTypeViaOtherDecorator ) . to . be . instanceOf ( Date ) ;
@@ -45,14 +73,14 @@ describe("implicit and explicity type declarations", () => {
45
73
46
74
} ) ;
47
75
48
- describe ( "plainToClass transforms builtin primitive types properly" , ( ) => {
76
+ describe ( "plainToClass transforms built-in primitive types properly" , ( ) => {
49
77
50
78
defaultMetadataStorage . clear ( ) ;
51
79
52
80
class Example {
53
81
54
82
@Type ( )
55
- date : Date ;
83
+ date : Date ;
56
84
57
85
@Type ( )
58
86
string : string ;
@@ -61,27 +89,27 @@ describe("plainToClass transforms builtin primitive types properly", () => {
61
89
string2 : string ;
62
90
63
91
@Type ( )
64
- number : number ;
92
+ number : number ;
65
93
66
94
@Type ( )
67
95
number2 : number ;
68
-
96
+
69
97
@Type ( )
70
98
boolean : boolean ;
71
-
99
+
72
100
@Type ( )
73
101
boolean2 : boolean ;
74
102
}
75
-
76
- const result : Example = plainToClass ( Example , {
77
- date : "2018-12-24T12:00:00Z" ,
78
- string : "100" ,
79
- string2 : 100 ,
103
+
104
+ const result : Example = plainToClass ( Example , {
105
+ date : "2018-12-24T12:00:00Z" ,
106
+ string : "100" ,
107
+ string2 : 100 ,
80
108
number : "100" ,
81
109
number2 : 100 ,
82
110
boolean : 1 ,
83
111
boolean2 : 0 ,
84
- } ) ;
112
+ } , { enableImplicitConversion : true } ) ;
85
113
86
114
it ( "should recognize and convert to Date" , ( ) => {
87
115
expect ( result . date ) . to . be . instanceOf ( Date ) ;
0 commit comments