Skip to content

🌱A lightweight TypeScript package for validating environment variables, ensuring they are properly set and typed (e.g., string, number, boolean) to prevent runtime errors.

License

Notifications You must be signed in to change notification settings

anibalalpizar/env-protector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌱 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

About

🌱A lightweight TypeScript package for validating environment variables, ensuring they are properly set and typed (e.g., string, number, boolean) to prevent runtime errors.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published