Write your own adapter
Load configuration from any source by implementing the Adapter or SyncAdapter type.
An adapter is a plain object with a name and a read function. Zod Config calls read, merges what it returns with the other adapters, and validates the result with your schema. The package exports the Adapter and SyncAdapter types for this.
Asynchronous adapter
Implement Adapter when read returns a promise, for example to call an API. It works with loadConfig:
import { } from 'zod';
import { , type } from 'zod-config';
const = (: string): => ({
: 'http adapter',
: async () => {
const = await ();
return .();
},
});
const = await ({
: .({ : .() }),
: ('https://example.com/config.json'),
});Synchronous adapter
Implement SyncAdapter when read returns the data directly. It works with both loadConfig and loadConfigSync; loadConfigSync only accepts SyncAdapters.
import { } from 'zod';
import { , type } from 'zod-config';
const = (): => ({
: 'argv adapter',
: () =>
.(
.
.(() => .('--') && .('='))
.(() => .(2).('=', 2)),
),
});
const = ({
: .({ : ..().(3000) }),
: (),
});What an adapter can set
| Property | Type | Description |
|---|---|---|
name | string | Name of the adapter, used in warnings. Required. |
read | () => Promise<data> (Adapter) or () => data (SyncAdapter) | Returns the raw configuration data. Required. |
nestingSeparator | string | Build nested objects from flat keys using this separator. Only top-level keys are split. |
silent | boolean | Don't log a warning when read throws. |
keyMatching | 'strict' | 'lenient' | How to match keys to the schema. |
transform | (obj: { key: string; value: unknown }) => { key: string; value: unknown } | false | Transform or drop key-value pairs before processing. |
If read throws, Zod Config logs a warning (unless silent) and continues with empty data from that adapter. Validation then decides whether loading fails.
To accept the same options as the built-in adapters, take them as parameters and set them on the returned object. Filtering by regex is up to your read function: it is not applied automatically.
Built an adapter others could use? Open a PR or an issue on GitHub.