@@ -11,7 +11,7 @@ Class-transformer allows you to transform plain object to some instance of class
11
11
Also it allows to serialize / deserialize object based on criteria.
12
12
This tool is super useful on both frontend and backend.
13
13
14
- Example how to use with angular 2 in [ plunker] ( http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R ) .
14
+ Example how to use with angular 2 in [ plunker] ( http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R ) .
15
15
Source code is available [ here] ( https://github.com/pleerock/class-transformer-demo ) .
16
16
17
17
## What is class-transformer
@@ -103,7 +103,7 @@ Here is example how it will look like:
103
103
``` typescript
104
104
fetch (" users.json" ).then ((users : Object []) => {
105
105
const realUsers = plainToClass (User , users );
106
- // now each user in realUsers is instance of User class
106
+ // now each user in realUsers is instance of User class
107
107
});
108
108
```
109
109
@@ -247,7 +247,7 @@ let photos = deserializeArray(Photo, photos);
247
247
248
248
## Enforcing type-safe instance
249
249
250
- The default behaviour of the ` plainToClass ` method is to set * all* properties from the plain object,
250
+ The default behaviour of the ` plainToClass ` method is to set * all* properties from the plain object,
251
251
even those which are not specified in the class.
252
252
253
253
``` typescript
@@ -274,7 +274,7 @@ console.log(plainToClass(User, fromPlainUser))
274
274
// }
275
275
```
276
276
277
- If this behaviour does not suit your needs, you can use the ` excludeExtraneousValues ` option
277
+ If this behaviour does not suit your needs, you can use the ` excludeExtraneousValues ` option
278
278
in the ` plainToClass ` method while * exposing all your class properties* as a requirement.
279
279
280
280
``` typescript
@@ -297,7 +297,7 @@ console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true })
297
297
// User {
298
298
// id: undefined,
299
299
// firstName: 'Umed',
300
- // lastName: 'Khudoiberdiev'
300
+ // lastName: 'Khudoiberdiev'
301
301
// }
302
302
```
303
303
@@ -309,7 +309,7 @@ Since Typescript does not have good reflection abilities yet,
309
309
we should implicitly specify what type of object each property contain.
310
310
This is done using ` @Type ` decorator.
311
311
312
- Lets say we have an album with photos.
312
+ Lets say we have an album with photos.
313
313
And we are trying to convert album plain object to class object:
314
314
315
315
``` typescript
@@ -532,7 +532,7 @@ In this case you don't need to `@Exclude()` a whole class.
532
532
533
533
## Skipping private properties, or some prefixed properties
534
534
535
- If you name your private properties with a prefix, lets say with ` _ ` ,
535
+ If you name your private properties with a prefix, lets say with ` _ ` ,
536
536
then you can exclude such properties from transformation too:
537
537
538
538
``` typescript
@@ -563,7 +563,7 @@ export class User {
563
563
get name() {
564
564
return this .firstName + " " + this .lastName ;
565
565
}
566
-
566
+
567
567
}
568
568
569
569
const user = new User ();
@@ -750,8 +750,8 @@ export class Photo {
750
750
}
751
751
```
752
752
753
- Now when you call ` plainToClass ` and send a plain representation of the Photo object,
754
- it will convert a date value in your photo object to moment date.
753
+ Now when you call ` plainToClass ` and send a plain representation of the Photo object,
754
+ it will convert a date value in your photo object to moment date.
755
755
` @Transform ` decorator also supports groups and versioning.
756
756
757
757
### Advanced usage
@@ -765,7 +765,7 @@ The `@Transform` decorator is given more arguments to let you configure how you
765
765
| Argument | Description
766
766
| --------------------| ---------------------------------------------------------------------------------|
767
767
| ` value ` | The property value before the transformation.
768
- | ` obj ` | The transformation source object.
768
+ | ` obj ` | The transformation source object.
769
769
| ` type ` | The transformation type.
770
770
771
771
## Other decorators
@@ -799,7 +799,7 @@ class User {
799
799
}
800
800
801
801
class UserController {
802
-
802
+
803
803
@TransformClassToPlain ({ groups: [' user.email' ] })
804
804
getUser() {
805
805
const user = new User ();
@@ -825,6 +825,31 @@ Once TypeScript team provide us better runtime type reflection tools, generics w
825
825
There are some tweaks however you can use, that maybe can solve your problem.
826
826
[ Checkout this example.] ( https://github.com/pleerock/class-transformer/tree/master/sample/sample4-generics )
827
827
828
+ ## Implicit type conversion
829
+
830
+ > ** NOTE** If you use class-validator together with class-transformer you propably DON'T want to enable this function.
831
+
832
+ Enables automatic conversion between built-in types based on type information provided by Typescript. Disabled by default.
833
+
834
+ ``` ts
835
+ import { IsString } from ' class-validator'
836
+
837
+ class MyPayload {
838
+
839
+ @IsString ()
840
+ prop: string
841
+ }
842
+
843
+
844
+ const result1 = plainToClass (MyPayload , { prop: 1234 }, { enableImplicitConversion: true });
845
+ const result2 = plainToClass (MyPayload , { prop: 1234 }, { enableImplicitConversion: false });
846
+
847
+ /**
848
+ * result1 will be `{ prop: "1234" }` - notice how the prop value has been converted to string.
849
+ * result2 will be `{ prop: 1234 }` - default behaviour
850
+ */
851
+ ```
852
+
828
853
## How does it handle circular references?
829
854
830
855
Circular references are ignored.
@@ -851,7 +876,7 @@ this.http
851
876
852
877
You can also inject a class ` ClassTransformer ` as a service in ` providers ` , and use its methods.
853
878
854
- Example how to use with angular 2 in [ plunker] ( http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R ) .
879
+ Example how to use with angular 2 in [ plunker] ( http://plnkr.co/edit/Mja1ZYAjVySWASMHVB9R ) .
855
880
Source code is [ here] ( https://github.com/pleerock/class-transformer-demo ) .
856
881
857
882
## Samples
0 commit comments