Skip to content

About

Assert required environment variables with typed results and clear errors

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

env-assert

Assert required environment variables with typed results and clear, aggregated errors.

Zero runtime dependencies. TypeScript-first.

Install

npm install env-assert

Usage

import { assertEnv, EnvAssertError } from "env-assert";

const env = assertEnv({
  PORT: { type: "int", min: 1, max: 65535, default: 3000 },
  DATABASE_URL: { type: "url" },
  NODE_ENV: { type: "enum", values: ["development", "test", "production"] as const },
  DEBUG: { type: "bool", default: false },
  OPTIONAL_FLAG: { type: "string", required: false },
});

// env.PORT: number
// env.DATABASE_URL: string
// env.NODE_ENV: "development" | "test" | "production"

If anything is missing or invalid, assertEnv throws EnvAssertError listing all problems (not just the first).

Spec options

Type Notes
string (default) non-empty when required
int / float optional min / max
bool accepts true/false/1/0/yes/no/on/off
url must parse with new URL()
enum values: readonly string[]

required: false or a default makes a key optional.

License

MIT © Oliver Massaad

About

Assert required environment variables with typed results and clear errors

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages