1.1.0 • Published 10 months ago

@envchecker/env-validator v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

EnvChecker

A powerful Node.js tool for validating environment variables against a predefined schema. Ensure your application's configuration is correct before startup.

Features

✨ Type Validation: Validate numbers, strings, URLs, JSON objects, and arrays šŸ”’ Security: Mark sensitive variables to prevent logging šŸŽÆ Pattern Matching: Use regex patterns to validate formats šŸ“ Custom Validation: Add your own validation functions šŸ”„ Conditional Validation: Require variables based on conditions šŸŽØ Pretty Output: Colorized CLI output for better readability

Installation

npm install @envchecker/env-validator

Quick Start

  1. Install the package - it will automatically create a default envchecker.config.js file in your project root:
// Default envchecker.config.js
module.exports = {
  REQUIRED_VARIABLES: {
    PORT: {
      type: 'number',
      required: true,
      min: 1024,
      max: 65535
    },
    NODE_ENV: {
      type: 'string',
      required: true,
      allowedValues: ['development', 'staging', 'production']
    },
    DATABASE_URL: {
      type: 'url',
      required: true,
      pattern: '^postgres://'
    },
    API_KEY: {
      type: 'string',
      required: true,
      sensitive: true,
      minLength: 32
    }
  }
};

You can modify this file to match your project's requirements.

  1. Use in your code:
const { validateEnv } = require('@envchecker/env-validator');
const config = require('./envchecker.config.js');

try {
  validateEnv(config);
  console.log('Environment variables are valid!');
} catch (error) {
  console.error('Validation failed:', error.errors);
  process.exit(1);
}
  1. Or use the CLI:
npx @envchecker/env-validator

Configuration Options

Variable Types

  • string: Text values

    NAME: { type: 'string', minLength: 1, maxLength: 100 }
  • number: Numeric values

    PORT: { type: 'number', min: 1024, max: 65535 }
  • boolean: True/false values

    DEBUG: { type: 'boolean' }
  • url: URL strings

    API_URL: { type: 'url', pattern: '^https://' }
  • json: JSON objects with schema validation

    CONFIG: {
      type: 'json',
      schema: {
        host: { type: 'string', required: true },
        port: { type: 'number', required: true }
      }
    }
  • array: Array values

    ALLOWED_IPS: { type: 'array' }

Validation Options

  • required: Make a variable mandatory

    API_KEY: { type: 'string', required: true }
  • pattern: Validate against a regex pattern

    EMAIL: { type: 'string', pattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$' }
  • sensitive: Mark variables as sensitive to prevent logging

    PASSWORD: { type: 'string', sensitive: true }
  • allowedValues: Restrict to specific values

    LOG_LEVEL: { type: 'string', allowedValues: ['debug', 'info', 'warn', 'error'] }
  • validate: Custom validation function

    VERSION: {
      type: 'string',
      validate: (value) => {
        if (!/^\d+\.\d+\.\d+$/.test(value)) {
          throw new Error('Must be a valid semantic version');
        }
      }
    }

Conditional Validation

Require variables based on conditions:

module.exports = {
  CONDITIONAL_VARIABLES: {
    SSL_CERT: {
      type: 'string',
      required: (env) => env.NODE_ENV === 'production'
    },
    REDIS_URL: {
      type: 'url',
      required: (env) => env.CACHE_ENABLED === 'true'
    }
  }
};

CLI Usage

# Basic validation
npx @envchecker/env-validator

# With verbose output
npx @envchecker/env-validator --verbose

# Using custom config file
npx @envchecker/env-validator --config ./config/env.config.js

Examples

Check out our examples directory for more detailed examples and use cases:

  • Basic validation
  • Advanced validation with conditional rules
  • Custom validation functions
  • Common configuration patterns

Contributing

We welcome contributions! Please check out our contributing guidelines for details.

License

MIT

Support

1.1.0

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago