Skip to main content
zerotal

Documentation


Documentation / zerotal / env / EnvSchema

Variable: EnvSchema

const EnvSchema: object

Defined in: packages/core/src/env/EnvSchema.ts:69

Define and validate a strict environment schema at import time.

The schema is parsed once when the module is first imported. If any variable fails validation, the application refuses to start and prints every failure in a single readable error — not just the first one.

All fields of the returned object are read-only and fully typed.

Type Declaration

define()

readonly define<S>(schema, source?): EnvOutput<S>

Parse and validate all env variables against the schema.

Type Parameters

S

S extends Record<string, Def<unknown>>

Parameters

schema

S

A plain object whose values are Def<T> instances from t.*.

source?

Record<string, string | undefined> = ...

Optional env source (defaults to Bun.env). Pass a plain object in tests to avoid polluting the real environment.

Returns

EnvOutput<S>

Throws

EnvSchemaError when any variable fails validation.

Example

// app/config/env.ts
import { EnvSchema, t } from '@zerotal/core/env';

export const env = EnvSchema.define({
  PORT:          t.number().default(3000),
  NODE_ENV:      t.enum(['development', 'production', 'testing']).required(),
  DB_HOST:       t.string().required(),
  DB_PASS:       t.string().when('NODE_ENV', 'production', t.required()),
  STRIPE_SECRET: t.string().when('NODE_ENV', 'production', t.required()),
  DEBUG:         t.boolean().default(false),
  BASE_URL:      t.url().default('http://localhost:3000'),
});

// Anywhere else:
import { env } from './config/env.ts';
console.log(env.PORT);     // number — never undefined
console.log(env.DB_HOST);  // string — validated at boot