env/load.ts

Generic environment loading from Zod schemas.

Provides load_env which iterates Zod schema keys, gets env values, and validates. Apps handle error messages themselves (they're always app-specific).

Declarations
#

3 declarations

view source

EnvValidationError
#

env/load.ts view source

Error thrown when environment validation fails.

Contains structured information for apps to format their own error messages.

inheritance

extends:
  • Error

raw

The raw env values that were read.

type Record<string, string | undefined>

readonly

zod_error

The Zod validation error.

type z.core.$ZodError

readonly

all_undefined

True if every env var was undefined (nothing loaded).

type boolean

readonly

constructor

type new (raw: Record<string, string | undefined>, zod_error: $ZodError<unknown>): EnvValidationError

raw
type Record<string, string | undefined>
zod_error
type $ZodError<unknown>

format_issues

Format Zod validation issues as human-readable strings.

type (): string[]

returns string[]

array of formatted issue strings like "PORT: Expected number"

load_env
#

env/load.ts view source

<T extends z.ZodObject>(schema: T, get_env: (key: string) => string | undefined): output<T>

Load and validate env vars against a Zod schema.

Reads each key from the schema using get_env, then validates. Throws EnvValidationError on failure.

schema

Zod object schema defining expected env vars

type T

get_env

function to read an env var by key

type (key: string) => string | undefined

returns

output<T>

validated env object

log_env_validation_error
#

env/load.ts view source

(error: EnvValidationError, label?: string | undefined): void

Log formatted env validation issues to stderr.

Handles the common case: labels each Zod issue with an optional prefix. Callers who want app-specific "getting started" instructions should check error.all_undefined before calling this.

error

the env validation error

label?

optional prefix for log lines (e.g., 'tx daemon', 'env')

type string | undefined
optional

returns

void