Skip to content

add environment variable validation with Joi in opsce module#861

Merged
yusuftomilola merged 1 commit into
DistinctCodes:mainfrom
Habibah371:Add-environment-variable-validation-with-Joi-in-opsce-module
Jun 1, 2026
Merged

add environment variable validation with Joi in opsce module#861
yusuftomilola merged 1 commit into
DistinctCodes:mainfrom
Habibah371:Add-environment-variable-validation-with-Joi-in-opsce-module

Conversation

@Habibah371
Copy link
Copy Markdown
Contributor

PR: Add Environment Variable Validation with Joi in Opsce Module

Summary

This PR introduces centralized environment variable validation using Joi within ConfigModule.forRoot() to ensure the application fails fast when required configuration is missing or invalid.

Previously, the application could start successfully even when critical environment variables such as database credentials or JWT secrets were absent, potentially leading to insecure behavior, runtime failures, or misconfigured deployments. With this change, configuration is validated during startup and descriptive errors are provided when validation fails.

Changes Implemented

Joi-Based Environment Validation

  • Added a comprehensive Joi validation schema to ConfigModule.forRoot()
  • Validation executes during application bootstrap
  • Application startup is blocked if any required configuration is missing or invalid

Required Environment Variables

The following variables are now mandatory and validated on startup:

  • DATABASE_URL
  • JWT_SECRET
  • JWT_REFRESH_SECRET
  • REDIS_HOST
  • REDIS_PORT
  • PORT

Validation includes:

  • Required presence checks
  • Type validation
  • Port number validation where applicable
  • Non-empty secret enforcement

Optional Variables with Safe Defaults

Added validation and default values for optional configuration:

  • THROTTLE_TTL
  • THROTTLE_LIMIT
  • UPLOAD_DIR

Defaults are applied only when values are not explicitly provided.

Startup Failure Protection

  • Added descriptive validation error messages
  • Invalid configuration causes immediate application termination
  • Prevents deployments from running in broken or insecure states
  • Improves operational visibility during deployment and CI/CD execution

Security Improvements

  • Removed hardcoded fallback secrets from configuration sources
  • Ensured JWT-related secrets must be explicitly provided through environment variables
  • Eliminated insecure default credential behavior

Validation Examples

Valid Configuration

DATABASE_URL=postgresql://user:password@localhost:5432/assetsup
JWT_SECRET=super-secret-key
JWT_REFRESH_SECRET=refresh-secret-key
REDIS_HOST=localhost
REDIS_PORT=6379
PORT=3000
THROTTLE_TTL=60
THROTTLE_LIMIT=100
UPLOAD_DIR=uploads

Startup Failure Example

Configuration validation error:
"JWT_SECRET" is required

The application exits immediately and refuses to start until the configuration is corrected.

Testing

Added tests covering:

  • Successful startup with valid configuration
  • Missing required variables
  • Invalid port values
  • Invalid secret configurations
  • Optional variable default assignment
  • Validation error reporting

Acceptance Criteria

  • Joi schema validates all required environment variables
  • Optional environment variables validated with safe defaults
  • Application exits with descriptive errors when validation fails
  • No hardcoded fallback secrets remain in configuration files
  • Configuration validation runs during application startup
  • Automated tests added for validation scenarios

Benefits

  • Prevents insecure application startup
  • Detects configuration issues early in deployment pipelines
  • Improves reliability across development, staging, and production environments
  • Enforces consistent configuration management practices
  • Reduces runtime failures caused by missing environment variables
    Closes [BE-32] Add environment variable validation with Joi in opsce module #757

add environment variable validation with Joi in opsce module
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@Habibah371 is attempting to deploy a commit to the naijabuz's projects Team on Vercel.

A member of the Team first needs to authorize it.

@yusuftomilola yusuftomilola merged commit 4558179 into DistinctCodes:main Jun 1, 2026
1 of 8 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.

[BE-32] Add environment variable validation with Joi in opsce module

2 participants