Skip to content

Add Configuration Module with Environment Schema Validation#140

Merged
ahmadogo merged 2 commits intoMentoNest:mainfrom
RUKAYAT-CODER:feat-config
Jan 24, 2026
Merged

Add Configuration Module with Environment Schema Validation#140
ahmadogo merged 2 commits intoMentoNest:mainfrom
RUKAYAT-CODER:feat-config

Conversation

@RUKAYAT-CODER
Copy link
Copy Markdown
Contributor

⚙️ Add Configuration Module with Environment Schema Validation

Module: Common / Configuration
Type: Feature / Infrastructure


📖 Overview

This PR introduces a centralized, typed configuration module that loads environment variables, validates them at application startup, and exposes a safe, ergonomic API for accessing configuration across the app.

The goal is to fail fast on misconfiguration, eliminate scattered process.env usage, and provide a single source of truth for all environment-dependent settings.


🎯 Goals

  • Provide typed, safe access to configuration values
  • Validate environment variables at boot time
  • Exit early with clear, actionable errors on invalid config
  • Support multiple environments (development, test, production)
  • Offer an ergonomic API for consumers (e.g. config.getDatabaseUrl())

🧩 Solution Overview

Config Module

  • ConfigModule and ConfigService added under:
    • src/common/config (or src/config)
  • Loads environment files based on NODE_ENV:
    • .env
    • .env.development
    • .env.test
    • .env.production
  • Registered globally (or imported into AppModule) for app-wide access

🔐 Environment Validation

  • Environment variables are validated before app bootstrap
  • Application exits immediately if validation fails
  • Errors clearly indicate:
    • Which key is missing or invalid
    • Why validation failed

Validation Strategy

One of the following approaches is used:

  • Option A: Joi
    • Centralized schema (validationSchema.ts)
    • Concise, declarative validation
  • Option B: class-validator
    • ConfigDto with typed fields
    • Validation performed after env loading

🧠 Typed Access Pattern

  • Centralized config keys (e.g. constants or enum)
  • No direct process.env access outside the ConfigModule
  • Typed getters exposed by ConfigService, such as:
    • getDatabaseUrl()
    • getRedisUrl()
    • getPort()
    • getNodeEnv()

Example usage:

constructor(private readonly config: ConfigService) {}

const dbUrl = this.config.getDatabaseUrl();

close #75

@RUKAYAT-CODER
Copy link
Copy Markdown
Contributor Author

Created PR kindly review

@ahmadogo ahmadogo merged commit 120e9ca into MentoNest:main Jan 24, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants