Skip to content

Conversation

@luisawatkins
Copy link

Problem

The application had basic environment variable validation that would cause runtime crashes with generic error messages when variables were missing or invalid.

Solution

Implemented a comprehensive environment variable validation system with:

🔍 Validation Functions

  • Required Variables: Proper validation with detailed error messages
  • Optional Variables: Default values with warnings instead of failures
  • Numeric Validation: Type checking for numeric environment variables
  • URL Validation: Proper URL format validation with parsing
  • DID Validation: Decentralized Identifier format validation

Developer Tools

  • Validation Script: npm run validate-env for easy validation
  • Environment Template: Complete env.example with documentation
  • Test Script: Test validation functions with mock data
  • Health Check: Runtime environment health monitoring

Enhanced Error Handling

  • Structured Logging: All validation events properly logged
  • Clear Error Messages: Descriptive errors with context and solutions
  • Validation Summary: Comprehensive reporting with errors, warnings, and validated variables

Files Changed

  • lib/server/constants.ts - Main validation system implementation
  • package.json - Added validation script command
  • scripts/validate-env.mjs - Command-line validation tool
  • scripts/test-env-validation.mjs - Test script for validation functions
  • env.example - Complete environment variable documentation

Environment Variables Validated

Required Variables:

  • SERVER_IDENTITY_PRIVATE_KEY - Server cryptographic key
  • TELEGRAM_BOT_TOKEN - Telegram bot authentication
  • SESSION_PASSWORD - Session security
  • NEXT_PUBLIC_SERVER_DID - Server identifier (DID format)
  • NEXT_PUBLIC_STORACHA_SERVICE_URL - Service URL (valid URL format)
  • NEXT_PUBLIC_STORACHA_SERVICE_DID - Service identifier (DID format)
  • NEXT_PUBLIC_STORACHA_RECEIPTS_URL - Receipts URL (valid URL format)
  • NEXT_PUBLIC_TELEGRAM_API_ID - Telegram API ID (numeric)
  • NEXT_PUBLIC_TELEGRAM_API_HASH - Telegram API hash
  • NEXT_PUBLIC_POINTS_PER_BYTE - Points calculation (numeric)

Optional Variables (with defaults):

  • SESSION_COOKIE_NAME - Default: 'tg-backups'
  • NEXT_PUBLIC_VERSION - Default: '1.0.0'
  • version - Default: '0.0.0'

Usage

Validate Environment Variables:

npm run validate-env

Runtime Health Check:

import { checkEnvironmentHealth } from '@/lib/server/constants'

if (!checkEnvironmentHealth()) {
  // Handle environment issues
}

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.

1 participant