[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/core](../../README) / [env](../README) / EnvSchema

# Variable: EnvSchema

> `const` **EnvSchema**: `object`

Defined in: [env/EnvSchema.ts:69](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/env/EnvSchema.ts#L69)

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`](../type-aliases/EnvOutput)\<`S`\>

Parse and validate all env variables against the schema.

#### Type Parameters

##### S

`S` *extends* `Record`\<`string`, [`Def`](../classes/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`](../type-aliases/EnvOutput)\<`S`\>

#### Throws

EnvSchemaError when any variable fails validation.

## Example

```ts
// 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
```
