nestjs / nestjs/config

Access validated config from custom configuration files

Open
#1,908 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
589
Forks
103
Avg merge
7h 25m
Merged PRs (30d)
31

Description

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:

  1. Redundant type conversion
  2. Potential type inconsistencies
  3. Decreased performance
  4. Additional boilerplate code
Example of the problem:
// validation schema
const validationSchema = Joi.object({
  PORT: Joi.number().default(3000),
  DEBUG: Joi.boolean().default(false)
});

// custom configuration file
export default registerAs('database', () => ({
  port: parseInt(process.env.PORT, 10), // Redundant parsing
  debug: process.env.DEBUG === 'true', // Redundant parsing
}));
Describe the solution you'd like

Solution

Implement a globally accessible store for parsed and validated configuration values via process.parsedEnv or global.parsedEnv that:

  • Maintains type safety through TypeScript declarations
  • Remains read-only to prevent runtime modifications
  • Is available during module initialization
  • Provides proper typing information for IDE support

Example implementation:

// After validation
process.parsedEnv = readonlyValidatedConfig;
Potential drawbacks:
  • Slight increase in memory usage
  • Maybe need to maintain some additional TypeScript types
  • Potential for confusion between process.env and process.parsedEnv
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:

// Previous approach
const port = parseInt(process.env.PORT, 10);

// New approach
const port = 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:

  1. Eliminating redundant type parsing and validation
  2. Ensuring type consistency throughout the application
  3. Reducing boilerplate code in configuration files
  4. Improving maintainability by centralizing parsed values
  5. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by tracing where Joi validation writes values and how custom configuration files are initialized, then define completion as typed, read-only access to converted values during module initialization with documentation coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.