env-protector
v1.0.3
Published
Environment variables validator for Node.js applications
Downloads
269
Maintainers
Readme
🌱 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 validationnumber
: Numeric validation with optional min/maxboolean
: Boolean validationurl
: URL format validationemail
: Email format validationarray
: Array from comma-separated stringsenum
: Enumerated values validationpattern
: 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