A robust environment variables validator for Node.js applications with TypeScript support.
- 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
npm install env-protector
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;
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
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}`
);
});
MIT