Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20, 22]
node-version: [20, 22, 24]
steps:
- name: Checkout
uses: actions/checkout@v6
Expand Down
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Works with Zod, Valibot, ArkType, and other Standard Schema-compatible validatio
- 🔌 **Standard Schema compliant** - Works with any compatible validation library
- 🌐 **Runtime agnostic** - Runs anywhere (Node, Bun, Deno, browsers)
- 🏗️ **Structured configuration** - Supports nested config objects
- 🧮 **Computed values** - Derive values from parsed config with full type inference
- 🚦 **Environment detection** - `isProduction`, `isTest`, `isDevelopment` flags
- 📜 **Detailed error reporting** - See all validation failures at once
- 🚀 **Lightweight** - Single dependency (type-fest), zero runtime overhead
Expand Down Expand Up @@ -132,6 +133,101 @@ type Config = InferEnv<typeof envSchema>;
// { apiKey: string; db: { host: string } }
```

### Computed Values

Use `createConfig` to derive computed values from your parsed configuration with full type inference:

```typescript
import { createConfig, envvar } from 'envase';
import { z } from 'zod';

const config = createConfig(process.env, {
schema: {
db: {
host: envvar('DB_HOST', z.string()),
port: envvar('DB_PORT', z.coerce.number()),
name: envvar('DB_NAME', z.string()),
},
api: {
key: envvar('API_KEY', z.string()),
},
},
computed: {
dbConnectionString: (raw) =>
`postgres://${raw.db.host}:${raw.db.port}/${raw.db.name}`,
apiKeyPrefix: (raw) => raw.api.key.slice(0, 8),
},
});

// config.db.host -> string
// config.db.port -> number
// config.dbConnectionString -> string
// config.apiKeyPrefix -> string
```

The `raw` parameter in computed functions is fully typed based on your schema, providing autocomplete and type checking. Computed values are calculated after schema validation, so you always work with parsed values (e.g., `port` is a `number`, not a string).

#### Nested Computed Values

Computed values can be nested to merge with your schema structure:

```typescript
import { createConfig, envvar } from 'envase';
import { z } from 'zod';

const config = createConfig(process.env, {
schema: {
aws: {
accessKeyId: envvar('AWS_ACCESS_KEY_ID', z.string()),
secretAccessKey: envvar('AWS_SECRET_ACCESS_KEY', z.string()),
},
},
computed: {
aws: {
credentials: (raw) => ({
accessKeyId: raw.aws.accessKeyId,
secretAccessKey: raw.aws.secretAccessKey,
}),
},
},
});

// Result type:
// {
// aws: {
// accessKeyId: string;
// secretAccessKey: string;
// credentials: { accessKeyId: string; secretAccessKey: string };
// }
// }
```

You can also mix flat and nested computed values:

```typescript
const config = createConfig(process.env, {
schema: {
db: {
host: envvar('DB_HOST', z.string()),
port: envvar('DB_PORT', z.coerce.number()),
},
},
computed: {
// Flat at root level
dbUrl: (raw) => `${raw.db.host}:${raw.db.port}`,
// Nested under existing schema key
db: {
connectionString: (raw) => `postgres://${raw.db.host}:${raw.db.port}`,
},
},
});

// config.dbUrl -> string
// config.db.host -> string
// config.db.port -> number
// config.db.connectionString -> string
```

## CLI

Automatically generate and validate markdown documentation from your environment variable schemas.
Expand Down Expand Up @@ -277,6 +373,16 @@ This helps pair the raw env name with the shape you expect it to conform to.

Validates envvars against the schema and returns a typed configuration object.

### `createConfig`

`createConfig(env, options)`

Validates envvars and optionally computes derived values. Returns a merged object containing both the parsed config and computed values. All types are inferred from the schema and computed functions.

- `env` - Environment variables object (e.g., `process.env`)
- `options.schema` - Environment variable schema (same format as `parseEnv`)
- `options.computed` - Optional object where each key is a function receiving the parsed config and returning a derived value

### `detectNodeEnv`

`detectNodeEnv(env: Record<string, string | undefined>)`
Expand Down
Loading