Skip to content

Commit d132a08

Browse files
Merge branch 'master' into fix-overwrite-default-values
2 parents 759a374 + 60aa96c commit d132a08

File tree

8 files changed

+344
-85
lines changed

8 files changed

+344
-85
lines changed

CHANGELOG.md

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,100 @@
11
# Changelog and release notes
22

3+
### 0.2.1
4+
5+
#### Added
6+
7+
- add option to strip unkown properties via using the `excludeExtraneousValues` option
8+
39
### 0.2.0 [BREAKING CHANGE]
410

511
#### Added
612

7-
- add documentation for using `Set`s and `Map`s
8-
- add opotion to pass a discriminator function to convert values into different types based on custom conditions
9-
- added support for polymorphism based on a named type property
13+
- add documentation for using `Set`s and `Map`s
14+
- add opotion to pass a discriminator function to convert values into different types based on custom conditions
15+
- added support for polymorphism based on a named type property
1016

1117
#### Fixed
1218

13-
- fix bug when transforming `null` values as primitives
19+
- fix bug when transforming `null` values as primitives
1420

1521
### 0.1.10
1622

1723
#### Fixed
1824

19-
- improve MetadataStorage perf by changing from Arrays to ES6 Maps by @sheiidan
20-
- fixed getAncestor issue with unknown nested properties by @247GradLabs
25+
- improve MetadataStorage perf by changing from Arrays to ES6 Maps by @sheiidan
26+
- fixed getAncestor issue with unknown nested properties by @247GradLabs
2127

2228
### 0.1.9
2329

2430
#### Fixed
2531

26-
- objects with `null` prototype are converted properly now
27-
- objects with unknown non primitive properties are converted properly now
28-
- corrected a typo in the README.md
29-
- fixed the deserialize example in the README.md
32+
- objects with `null` prototype are converted properly now
33+
- objects with unknown non primitive properties are converted properly now
34+
- corrected a typo in the README.md
35+
- fixed the deserialize example in the README.md
3036

3137
### 0.1.4
3238

3339
#### Added
3440

35-
- added `TransformClassToPlain` and `TransformClassToClass` decorators
41+
- added `TransformClassToPlain` and `TransformClassToClass` decorators
3642

3743
### 0.1.0
3844

3945
#### Added
4046

41-
- renamed library from `constructor-utils` to `class-transformer`
42-
- completely renamed most of names
43-
- renamed all main methods: `plainToConstructor` now is `plainToClass` and `constructorToPlain` is `classToPlain`, etc.
44-
- `plainToConstructorArray` method removed - now `plainToClass` handles it
45-
- `@Skip()` decorator renamed to `@Exclude()`
46-
- added `@Expose` decorator
47-
- added lot of new options: groups, versioning, custom names, etc.
48-
- methods and getters that should be exposed must be decorated with `@Expose` decorator
49-
- added `excludedPrefix` to class transform options that allows exclude properties that start with one of the given prefix
47+
- renamed library from `constructor-utils` to `class-transformer`
48+
- completely renamed most of names
49+
- renamed all main methods: `plainToConstructor` now is `plainToClass` and `constructorToPlain` is `classToPlain`, etc.
50+
- `plainToConstructorArray` method removed - now `plainToClass` handles it
51+
- `@Skip()` decorator renamed to `@Exclude()`
52+
- added `@Expose` decorator
53+
- added lot of new options: groups, versioning, custom names, etc.
54+
- methods and getters that should be exposed must be decorated with `@Expose` decorator
55+
- added `excludedPrefix` to class transform options that allows exclude properties that start with one of the given prefix
5056

5157
### 0.0.22
5258

5359
#### Fixed
5460

55-
- fixed array with primitive types being converted
61+
- fixed array with primitive types being converted
5662

5763
### 0.0.18-0.0.21
5864

5965
#### Fixed
6066

61-
- fixed bugs when getters are not converted with es6 target
67+
- fixed bugs when getters are not converted with es6 target
6268

6369
### 0.0.17
6470

6571
#### Fixed
6672

67-
- fixed issue #4
68-
- added type guessing during transformation from constructor to plain object
69-
- added sample with generics
73+
- fixed issue #4
74+
- added type guessing during transformation from constructor to plain object
75+
- added sample with generics
7076

7177
### 0.0.16
7278

7379
#### Changed
7480

75-
- renamed `constructor-utils/constructor-utils` to `constructor-utils` package namespace
81+
- renamed `constructor-utils/constructor-utils` to `constructor-utils` package namespace
7682

7783
### 0.0.15
7884

7985
#### Removed
8086

81-
- removed code mappings from package
87+
- removed code mappings from package
8288

8389
### 0.0.14
8490

8591
#### Removed
8692

87-
- removed `import "reflect-metadata"` from source code. Now reflect metadata should be included like any other shims.
93+
- removed `import "reflect-metadata"` from source code. Now reflect metadata should be included like any other shims.
8894

8995
### 0.0.13
9096

9197
#### Changed
9298

93-
- Library has changed its name from `serializer.ts` to `constructor-utils`.
94-
- Added `constructor-utils` namespace.
99+
- Library has changed its name from `serializer.ts` to `constructor-utils`.
100+
- Added `constructor-utils` namespace.

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,62 @@ import {deserializeArray} from "class-transformer";
245245
let photos = deserializeArray(Photo, photos);
246246
```
247247

248+
## Enforcing type-safe instance
249+
250+
The default behaviour of the `plainToClass` method is to set *all* properties from the plain object,
251+
even those which are not specified in the class.
252+
253+
```typescript
254+
import {plainToClass} from "class-transformer";
255+
256+
class User {
257+
id: number
258+
firstName: string
259+
lastName: string
260+
}
261+
262+
const fromPlainUser = {
263+
unkownProp: 'hello there',
264+
firstName: 'Umed',
265+
lastName: 'Khudoiberdiev',
266+
}
267+
268+
console.log(plainToClass(User, fromPlainUser))
269+
270+
// User {
271+
// unkownProp: 'hello there',
272+
// firstName: 'Umed',
273+
// lastName: 'Khudoiberdiev',
274+
// }
275+
```
276+
277+
If this behaviour does not suit your needs, you can use the `excludeExtraneousValues` option
278+
in the `plainToClass` method while *exposing all your class properties* as a requirement.
279+
280+
```typescript
281+
import {Expose, plainToClass} from "class-transformer";
282+
283+
class User {
284+
@Expose() id: number;
285+
@Expose() firstName: string;
286+
@Expose() lastName: string;
287+
}
288+
289+
const fromPlainUser = {
290+
unkownProp: 'hello there',
291+
firstName: 'Umed',
292+
lastName: 'Khudoiberdiev',
293+
}
294+
295+
console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true }))
296+
297+
// User {
298+
// id: undefined,
299+
// firstName: 'Umed',
300+
// lastName: 'Khudoiberdiev'
301+
// }
302+
```
303+
248304
## Working with nested objects
249305

250306
When you are trying to transform objects that have nested objects,
@@ -805,4 +861,4 @@ usages.
805861

806862
## Release notes
807863

808-
See information about breaking changes and release notes [here](https://github.com/pleerock/class-transformer/tree/master/doc/release-notes.md).
864+
See information about breaking changes and release notes [here](https://github.com/pleerock/class-transformer/tree/master/doc/release-notes.md).

0 commit comments

Comments
 (0)