Skip to content

Commit b7a7563

Browse files
vsamofalNikaple
authored andcommitted
docs: updated documentation for substitution feature
1 parent b39c92c commit b7a7563

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,17 @@ TypedConfigModule.forRoot({
504504
});
505505
```
506506

507-
## Uses of environment variable substitutions
507+
## Uses of variable substitutions
508508

509-
The `${PORT}` substitution feature lets you use environment variable in some nice ways.
509+
The `${PORT}` substitution feature lets you use environment variable in some nice ways. You can also provide default values, and reference another variable in a config
510510

511511
If you have config file with like the below one
512512

513513
```yaml
514514
database:
515515
host: 127.0.0.1
516-
port: ${PORT}
516+
port: ${PORT:-12345}
517+
url: ${database.host}:${database:port}
517518
```
518519
519520
And you have set environment variable for port
@@ -537,6 +538,33 @@ And you will get new config like below one
537538
database:
538539
host: 127.0.0.1
539540
port: 9000
541+
url: 127.0.0.1:9000
542+
```
543+
544+
if you won't set environment variable for port, then you will get new config like below one
545+
546+
```yaml
547+
database:
548+
host: 127.0.0.1
549+
port: 12345
550+
url: 127.0.0.1:12345
551+
```
552+
553+
> [!NOTE]
554+
> when you use variable substitution the values can be string in case if you use default variable or env variable, and you need to apply transformer to class fields to get the correct type of the value.
555+
556+
```ts
557+
export class Config {
558+
@IsString()
559+
public readonly host!: string;
560+
561+
@IsNumber()
562+
@Type(() => Number)
563+
public readonly port!: number;
564+
565+
@IsString()
566+
public readonly url!: string;
567+
}
540568
```
541569

542570
## Default values

0 commit comments

Comments
 (0)