Adapters
Where Zod Config reads configuration from, and how sources are combined.
An adapter reads configuration from one source (environment variables, a file, AWS) and hands the raw data to Zod Config, which merges it and validates it with your schema.
Default adapter
By default, Zod Config loads the configuration from process.env, so you don't need to provide any adapter:
import { } from 'zod';
import { } from 'zod-config';
const = .({
: .().(/^\d+$/),
: .(),
});
const = await ({
: ,
});
// config is now type safe!
.(.);
.(.);Built-in adapters
Built-in adapters are imported from zod-config/<adapter-name>. Some need a peer dependency, which you install yourself.
| Adapter | Import | Reads | Peer dependency | Sync |
|---|---|---|---|---|
| Env | zod-config/env-adapter | process.env or a custom object | none | ✅ |
| JSON | zod-config/json-adapter | a .json file | none | ✅ |
| JSON5 | zod-config/json5-adapter | a .json5 file | json5 | ✅ |
| YAML | zod-config/yaml-adapter | a .yaml file | yaml | ✅ |
| TOML | zod-config/toml-adapter | a .toml file | smol-toml | ✅ |
| Dotenv | zod-config/dotenv-adapter | a .env file | dotenv | ✅ |
| Script | zod-config/script-adapter | a .ts, .js or .json file | jiti, optional, for TypeScript | ❌ |
| Directory | zod-config/directory-adapter | a directory of config files | none | ❌ |
| AWS Secrets Manager | zod-config/aws-secrets-manager-adapter | an AWS Secrets Manager secret | @aws-sdk/client-secrets-manager | ❌ |
| AWS SSM | zod-config/aws-ssm-adapter | AWS SSM Parameter Store | @aws-sdk/client-ssm | ❌ |
Synchronous adapters also work with loadConfigSync. Need another source? Write your own adapter.
Combining adapters
Pass an array to read from several sources. The results are deeply merged in the order of the adapters, so later adapters override earlier ones. See Combine multiple adapters.
The keys __proto__ and constructor are skipped when merging, and so are nested keys with a __proto__ or constructor segment when a nesting separator is used.
Shared adapter options
Every built-in adapter accepts these options on top of its own:
| Option | Type | Description |
|---|---|---|
regex | RegExp | Only keep keys that match this regular expression. |
silent | boolean | Don't log a warning when this adapter fails. See Silent mode. |
keyMatching | 'strict' | 'lenient' | How to match keys to the schema. See Lenient key matching. |
transform | (obj: { key: string; value: unknown }) => { key: string; value: unknown } | false | Transform or drop key-value pairs before processing. See Transform. |
silent, keyMatching and transform can also be set globally on loadConfig. When both are set, the adapter-level option takes precedence.