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
Is there an existing issue that is already proposing this?
I have searched the existing issues
Is your feature request related to a problem? Please describe it
TLDR;
Inability to access validated and parsed environmental values in custom configuration files. The validated values are stored in the configuration object of ConfigService under a constant key, but are inaccessible in custom configuration files and cannot be injected.
Problem
When utilizing @nestjs/config with Joi validation and multiple custom configuration files, we encounter a significant type preservation issue. After Joi validation and conversion of configuration values (e.g., numbers, booleans), these values are written back to process.env, which exclusively supports string values. Consequently, when accessing these previously validated values from process.env in custom configuration files, developers must redundantly re-parse them from strings to their appropriate types, leading to:
This enhancement requires minimal documentation updates as it's not a breaking change. A simple addition to the documentation would suffice:
// Previous approachconstport=parseInt(process.env.PORT,10);// New approachconstport=process.parsedEnv.PORT;// Automatically typed as number after conversion is done
Migration is straightforward as:
No breaking changes to existing functionality
Gradual adoption possible
Needed TypeScript types will be included for IDE support
What is the motivation / use case for changing the behavior?
Motivation and benefits
The primary motivation is to improve developer experience and reduce potential errors in NestJS applications by:
Eliminating redundant type parsing and validation
Ensuring type consistency throughout the application
Reducing boilerplate code in configuration files
Improving maintainability by centralizing parsed values
Enhancing TypeScript integration and type safety
Key use cases:
Applications with complex and rapidly changing configuration requirements
Environments where type safety is crucial
Development scenarios requiring consistent configuration access across multiple modules
The text was updated successfully, but these errors were encountered:
h4ckthepl4net
changed the title
How to access validated config from custom configuration files?
Access validated config from custom configuration files
Dec 29, 2024
Is there an existing issue that is already proposing this?
Is your feature request related to a problem? Please describe it
TLDR;
Inability to access validated and parsed environmental values in custom configuration files. The validated values are stored in the configuration object of ConfigService under a constant key, but are inaccessible in custom configuration files and cannot be injected.
Problem
When utilizing @nestjs/config with Joi validation and multiple custom configuration files, we encounter a significant type preservation issue. After Joi validation and conversion of configuration values (e.g., numbers, booleans), these values are written back to process.env, which exclusively supports string values. Consequently, when accessing these previously validated values from process.env in custom configuration files, developers must redundantly re-parse them from strings to their appropriate types, leading to:
Example of the problem:
Describe the solution you'd like
Solution
Implement a globally accessible store for parsed and validated configuration values via
process.parsedEnv
orglobal.parsedEnv
that:Example implementation:
Potential drawbacks:
Teachability, documentation, adoption, migration strategy
Usage, Migration, Docs
This enhancement requires minimal documentation updates as it's not a breaking change. A simple addition to the documentation would suffice:
Migration is straightforward as:
What is the motivation / use case for changing the behavior?
Motivation and benefits
The primary motivation is to improve developer experience and reduce potential errors in NestJS applications by:
Key use cases:
The text was updated successfully, but these errors were encountered: