@@ -157,5 +157,63 @@ describe("custom transformation decorator", () => {
157
157
objArg . should . be . equal ( user ) ;
158
158
typeArg . should . be . equal ( TransformationType . CLASS_TO_PLAIN ) ;
159
159
} ) ;
160
+
161
+ let model : any ;
162
+ it ( 'should serialize json into model instance of class Person' , ( ) => {
163
+ expect ( ( ) => {
164
+ const json = {
165
+ name : 'John Doe' ,
166
+ address : {
167
+ street : 'Main Street 25' ,
168
+ tel : '5454-534-645' ,
169
+ zip : 10353 ,
170
+ country : 'West Samoa'
171
+ } ,
172
+ age : 25 ,
173
+ hobbies : [
174
+ { type : 'sport' , name : 'sailing' } ,
175
+ { type : 'relax' , name : 'reading' } ,
176
+ { type : 'sport' , name : 'jogging' } ,
177
+ { type : 'relax' , name : 'movies' }
178
+ ]
179
+ } ;
180
+ class Hobby {
181
+ public type : string ;
182
+ public name : string ;
183
+ }
184
+ class Address {
185
+ public street : string ;
186
+
187
+ @Expose ( { name : 'tel' } )
188
+ public telephone : string ;
189
+
190
+ public zip : number ;
191
+
192
+ public country : string ;
193
+ }
194
+ class Person {
195
+ public name : string ;
196
+
197
+ @Type ( ( ) => Address )
198
+ public address : Address ;
199
+
200
+ @Type ( ( ) => Hobby )
201
+ @Transform ( value => value . filter ( hobby => hobby . type === 'sport' ) , { toClassOnly : true } )
202
+ public hobbies : Hobby [ ] ;
203
+
204
+ public age : number ;
205
+ }
206
+ model = plainToClass ( Person , json ) ;
207
+ expect ( model instanceof Person ) ;
208
+ expect ( model . address instanceof Address ) ;
209
+ model . hobbies . forEach ( hobby => expect ( hobby instanceof Hobby && hobby . type === 'sport' ) ) ;
210
+ } ) . not . toThrowError ( ) ;
211
+ } ) ;
212
+
213
+ it ( 'should serialize a model into json' , ( ) => {
214
+ expect ( ( ) => {
215
+ classToPlain ( model ) ;
216
+ } ) . not . toThrowError ( ) ;
217
+ } ) ;
160
218
161
219
} ) ;
0 commit comments