Skip to content

Commit

Permalink
improved example with clearer return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Helveg committed Dec 8, 2024
1 parent 0319991 commit 7ac8e62
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ which support all options of their `class-transformer` respective counterparts `

### `InjectTransform`

Replace your `Transform` decorator with the dependency injection enabled `InjectTransform` decorator.

To inject a dependencies pass an array of injection tokens to the `inject` option. They will be passed
as additional arguments to your transform function, in the order they were given:

Expand Down Expand Up @@ -75,10 +73,9 @@ export class MyDTO {

### `InjectType`

This decorator allows you to provide a dependency injection enabled type injector. Like the
type transformer you can use the type injector's class body to scaffold your dependencies.

Its `inject` function is called with the same arguments as the `Type` function would have been.
A `TypeInjector` lets you inject types similar to the `Type` decorator. Its `inject` function is
called with the same arguments as the `Type` function would have been and should return the type
to be used.

The following example illustrates how you could return different DTO types (and thereby different
validation schemes when used with `class-validator`), based on a supposed client's
Expand All @@ -89,17 +86,35 @@ configuration:
class ClientDtoInjector implements TypeInjector {
constructor(
private readonly service: ClientConfigurationService
) {
}
) {}

inject(type?: TypeHelpOptions) {
const client = type.object['client'] ?? 'default';
const clientConfig = this.service.getClientConfiguration(client);
const dto = clientConfig.getNestedDTO(type.newObject, type.property);
return dto;
if (clientConfig.accountType === 'named') {
return NamedAccountDTO;
} else if (clientConfig.accountType === 'numbered') {
return NumberedAccountDTO;
}
return AccountDTO;
}
}

class AccountDTO {
@IsString()
name: string;
}

class NamedAccountDTO extends AccountDTO {
@IsString()
id: string
}

class NumberedAccountDTO extends AccountDTO {
@IsNumber()
id: number
}

class OpenAccountDTO {
@IsString()
client: string;
Expand Down

0 comments on commit 7ac8e62

Please sign in to comment.