You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const port =this.configService.get<number>('PORT');
186
-
186
+
187
187
// this is invalid as URL is not a property on the EnvironmentVariables interface
188
188
const url =this.configService.get<string>('URL');
189
189
}
190
190
```
191
191
192
-
> warning **Notice** If you have nested properties in your config, like in the `database.host` example above, the interface must have a matching `'database.host': string;` property. Otherwise a TypeScript error will be thrown.
192
+
> warning **Notice** If you have nested properties in your config, like in the `database.host` example above, the interface must have a matching `'database.host': string;` property. Otherwise a TypeScript error will be thrown.
193
193
194
194
#### Configuration namespaces
195
195
@@ -256,9 +256,12 @@ export class DatabaseModule {}
256
256
257
257
#### Schema validation
258
258
259
-
It is standard practice to throw an exception during application startup if required environment variables haven't been provided or if they don't meet certain validation rules. The `@nestjs/config` package enables use of the [Joi](https://github.com/hapijs/joi) npm package to support this type of validation. With Joi, you define an object schema and validate JavaScript objects against it.
259
+
It is standard practice to throw an exception during application startup if required environment variables haven't been provided or if they don't meet certain validation rules. The `@nestjs/config` package enables two different ways to do this:
260
+
261
+
-[Joi](https://github.com/hapijs/joi) built-in validator. With Joi, you define an object schema and validate JavaScript objects against it.
262
+
- A custom `validate()` function which takes environment variables as an input.
260
263
261
-
Install Joi (and its types, for **TypeScript** users):
264
+
To use Joi, we must install Joi package (and its types, for **TypeScript** users):
Note that once you decide to pass a `validationOptions` object, any settings you do not explicitly pass will default to `Joi` standard defaults (not the `@nestjs/config` defaults). For example, if you leave `allowUnknowns` unspecified in your custom `validationOptions` object, it will have the `Joi` default value of `false`. Hence, it is probably safest to specify **both** of these settings in your custom object.
324
327
328
+
#### Custom validate function
329
+
330
+
Alternatively, you can specify a **synchronous**`validate` function that takes an object containing the environment variables (from env file and process) and returns an object containing validated environment variables so that you can convert/mutate them if needed. If the function throws an error, it will prevent the application from bootstrapping.
331
+
332
+
In this example, we'll proceed with the `class-transformer` and `class-validator` packages. First, we have to define:
333
+
334
+
- a class with validation constraints,
335
+
- a validate function that makes use of the `plainToClass` and `validateSync` functions.
0 commit comments