process-env-schema
v0.1.0
Published
Validate process.env with a lightweight typed schema: string, number, boolean, port, URL, enum, regex—loadValidatedEnv or non-throwing validateEnv
Downloads
407
Maintainers
Readme
env-config-validator
Validate process.env with a small typed schema—strings, numbers, booleans, ports, URLs, enums, and regex patterns. No Zod/Yup dependency; fits boot scripts and server entrypoints.
const { loadValidatedEnv } = require('env-config-validator');
const env = loadValidatedEnv({
NODE_ENV: { type: 'string', enum: ['development', 'production', 'test'] },
PORT: { type: 'port', default: '3000' },
DATABASE_URL: { type: 'url', required: true },
LOG_JSON: { type: 'boolean', default: 'false' },
});
// env.PORT is a number; env.DATABASE_URL is a stringInstall
npm install env-config-validatorAPI
validateEnv(schema, env?)
Returns { ok, errors, values } without throwing—use in CLIs that print all mistakes at once.
const { validateEnv } = require('env-config-validator');
const result = validateEnv(schema, process.env);
if (!result.ok) {
for (const e of result.errors) console.error(e.key, e.message);
}loadValidatedEnv(schema, env?)
Throws Error with a joined message when validation fails; return type is inferred from the schema.
Field spec (EnvFieldSpec)
| Property | Purpose |
| --- | --- |
| type | string | number | boolean | port | url |
| required | Default true when no default |
| default | Fallback string from env |
| enum | Allowed string values |
| pattern | RegExp check |
| description | Documentation only |
Booleans accept 1/0, true/false, yes/no, on/off (case-insensitive).
When to use
- Replace ad-hoc
if (!process.env.FOO)chains at startup - Share one schema between local
.envand deployment platforms - Lighter alternative when you only need env scalars, not full object validation
Why teams pick this
vs Zod for 5 env vars: tiny typed schema for process.env at boot—no schema framework tax.
Search terms: env validation, environment variables, dotenv, config schema, port validate, twelve-factor, node startup, typescript, process.env
