mcpField
E
environment-validation

environment-validation

github.com/loxosceles-devDevelopment135 installs
Repository

About environment-validation

Environment Validation is a reference pattern for developers to check required environment variables before scripts or applications begin business logic. It emphasizes early failure, clear missing-variable errors, and shows an environment manager for CLI architectures.

Key features

  1. 01Checks required environment variables at entry points
  2. 02Lists all missing configuration at once
  3. 03Validates before business logic begins
  4. 04Uses a shared environment manager for complex projects
  5. 05Applies to deployment scripts, Lambda, and CLIs

Use cases

  1. 01Avoid deployment scripts failing midway due to missing configuration
  2. 02Catch environment problems when a Lambda starts
  3. 03Validate configuration before CLI commands run
  4. 04Centralize environment checks shared across entry points

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/loxosceles-dev/dev-skills --skill environment-validation

After installing, type/environment-validationin Claude Code to confirm it loaded.

Details

From the project README

Environment Validation Pattern

This is a reference pattern. Learn from the approach, adapt to your context — don't copy verbatim.

Problem: Scripts fail halfway through due to missing environment variables, wasting time and leaving systems in inconsistent states.

Solution: Validate all required environment variables at the start, before any operations begin.


Pattern

Validate Early: Check all required configuration before starting business logic.

// ✅ Preferred: Validate at entry point
function validateEnvironment() {
  const required = ['AWS_REGION', 'PROJECT_ID', 'ENVIRONMENT'];
  const missing = required.filter(key => !process.env[key]);
  
  if (missing.length > 0) {
    throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
  }
}

// At script/app entry point
validateEnvironment();
// Now proceed with business logic

Environment Manager Pattern (for complex projects):

// shared/env-loader.ts
export function loadAndValidateEnv() {
  const required = ['AWS_REGION', 'PROJECT_ID', 'ENVIRONMENT'];
  const missing = required.filter(key => !process.env[key]);
  
  if (missing.length > 0) {
    throw new Error(`Missing: ${missing.join(', ')}`);
  }
  
  return {
    awsRegion: process.env.AWS_REGION!,
    projectId: process.env.PROJECT_ID!,
    environment: process.env.ENVIRONMENT!
  };
}

// Usage in scripts/handlers
const config = loadAndValidateEnv();
// All env vars validated, proceed with business logic

Benefits

  • Fail Fast: Catch configuration errors immediately
  • Clear Errors: Know exactly what's missing
  • Clean Code: Business logic not cluttered with validation
  • Consistent: Single validation point for entire application

When to Use

  • Always: For deployment scripts, Lambda handlers, CLI tools
  • Entry Points: Validate at application/script start
  • Shared Config: Use environment manager for complex projects

Integration with CLI Architecture

The 3-Tier CLI Architecture includes an EnvironmentManager that implements this pattern:

// core/env-manager.ts
export class EnvironmentManager extends BaseManager {
  loadEnv(stage: string): Record<string, string> {
    this.log(`Loading environment for ${stage}`);
    
    // Validate required variables
    const required = ['AWS_REGION', 'PROJECT_ID'];
    const missing = required.filter(key => !process.env[key]);
    
    if (missing.length > 0) {
      throw new Error(`Missing: ${missing.join(', ')}`);
    }
    
    return process.env;
  }
}

This ensures all CLI commands validate environment before executing business logic.


Related


Progressive Improvement

If the developer corrects a behavior that this skill should have prevented, suggest a specific amendment to this skill to prevent the same correction in the future.

Comments

Markdown supported