@@ -28,8 +28,6 @@ which support all options of their `class-transformer` respective counterparts `
28
28
29
29
### ` InjectTransform `
30
30
31
- Replace your ` Transform ` decorator with the dependency injection enabled ` InjectTransform ` decorator.
32
-
33
31
To inject a dependencies pass an array of injection tokens to the ` inject ` option. They will be passed
34
32
as additional arguments to your transform function, in the order they were given:
35
33
@@ -75,10 +73,9 @@ export class MyDTO {
75
73
76
74
### ` InjectType `
77
75
78
- This decorator allows you to provide a dependency injection enabled type injector. Like the
79
- type transformer you can use the type injector's class body to scaffold your dependencies.
80
-
81
- Its ` inject ` function is called with the same arguments as the ` Type ` function would have been.
76
+ A ` TypeInjector ` lets you inject types similar to the ` Type ` decorator. Its ` inject ` function is
77
+ called with the same arguments as the ` Type ` function would have been and should return the type
78
+ to be used.
82
79
83
80
The following example illustrates how you could return different DTO types (and thereby different
84
81
validation schemes when used with ` class-validator ` ), based on a supposed client's
@@ -89,17 +86,35 @@ configuration:
89
86
class ClientDtoInjector implements TypeInjector {
90
87
constructor (
91
88
private readonly service : ClientConfigurationService
92
- ) {
93
- }
89
+ ) {}
94
90
95
91
inject(type ? : TypeHelpOptions ) {
96
92
const client = type .object [' client' ] ?? ' default' ;
97
93
const clientConfig = this .service .getClientConfiguration (client );
98
- const dto = clientConfig .getNestedDTO (type .newObject , type .property );
99
- return dto ;
94
+ if (clientConfig .accountType === ' named' ) {
95
+ return NamedAccountDTO ;
96
+ } else if (clientConfig .accountType === ' numbered' ) {
97
+ return NumberedAccountDTO ;
98
+ }
99
+ return AccountDTO ;
100
100
}
101
101
}
102
102
103
+ class AccountDTO {
104
+ @IsString ()
105
+ name: string ;
106
+ }
107
+
108
+ class NamedAccountDTO extends AccountDTO {
109
+ @IsString ()
110
+ id: string
111
+ }
112
+
113
+ class NumberedAccountDTO extends AccountDTO {
114
+ @IsNumber ()
115
+ id: number
116
+ }
117
+
103
118
class OpenAccountDTO {
104
119
@IsString ()
105
120
client: string ;
0 commit comments