Skip to content

Latest commit

 

History

History
107 lines (87 loc) · 2.31 KB

README.md

File metadata and controls

107 lines (87 loc) · 2.31 KB

🌱 env-protector

A robust environment variables validator for Node.js applications with TypeScript support.

🚀 Features

  • Type validation for environment variables
  • Support for multiple data types (string, number, boolean, url, email, array, enum, pattern)
  • Custom validation rules
  • TypeScript support
  • Detailed error messages

📦 Installation

npm install env-protector

💻 Usage

const { EnvValidator } = require("env-protector");
// or with ES modules:
import { EnvValidator } from "env-protector";

const envValidator = new EnvValidator({
  PORT: {
    type: "number",
    required: true,
    min: 1024,
    max: 65535,
    message: "Port must be between 1024 and 65535",
  },
  DATABASE_URL: {
    type: "string",
    required: true,
    message: "Database URL is required",
  },
  API_KEY: {
    type: "pattern",
    required: true,
    pattern: /^sk_test_[a-zA-Z0-9]+$/,
    message: "Invalid API key format",
  },
});

const result = envValidator.validate();

if (!result.isValid) {
  console.error("Environment validation failed:");
  result.errors.forEach((error) => {
    console.error(`- ${error.message}`);
  });
  process.exit(1);
}

// Use the validated environment variables
const { PORT, DATABASE_URL, API_KEY } = result.validatedEnv;

🔧 Supported Types

  • string: Basic string validation
  • number: Numeric validation with optional min/max
  • boolean: Boolean validation
  • url: URL format validation
  • email: Email format validation
  • array: Array from comma-separated strings
  • enum: Enumerated values validation
  • pattern: Regular expression pattern validation

🌟 Example with Express

require("dotenv").config();
const { EnvValidator } = require("env-protector");
const express = require("express");

// Validate environment variables
const envValidator = new EnvValidator({
  PORT: {
    type: "number",
    required: true,
    min: 1024,
    max: 65535,
  },
  NODE_ENV: {
    type: "enum",
    required: true,
    enum: ["development", "production", "test"],
    default: "development",
  },
});

const { validatedEnv } = envValidator.validate();
const app = express();

app.listen(validatedEnv.PORT, () => {
  console.log(
    `Server running in ${validatedEnv.NODE_ENV} mode on port ${validatedEnv.PORT}`
  );
});

📄 License

MIT