New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@envchecker/env-validator

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@envchecker/env-validator

A powerful environment variable validator for Node.js applications with schema validation, type checking, and security features

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
0
Created
Source

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

  • 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.

  • 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);
}
  • 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

Keywords

env

FAQs

Package last updated on 20 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts