Skip to Content
Zod Config

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.

AdapterImportReadsPeer dependencySync
Envzod-config/env-adapterprocess.env or a custom objectnone✅
JSONzod-config/json-adaptera .json filenone✅
JSON5zod-config/json5-adaptera .json5 filejson5✅
YAMLzod-config/yaml-adaptera .yaml fileyaml✅
TOMLzod-config/toml-adaptera .toml filesmol-toml✅
Dotenvzod-config/dotenv-adaptera .env filedotenv✅
Scriptzod-config/script-adaptera .ts, .js or .json filejiti, optional, for TypeScript❌
Directoryzod-config/directory-adaptera directory of config filesnone❌
AWS Secrets Managerzod-config/aws-secrets-manager-adapteran AWS Secrets Manager secret@aws-sdk/client-secrets-manager❌
AWS SSMzod-config/aws-ssm-adapterAWS 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:

OptionTypeDescription
regexRegExpOnly keep keys that match this regular expression.
silentbooleanDon'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 } | falseTransform 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.

On this page