Dotenv adapter
Load configuration from a .env file.
Loads the configuration from a .env file. To build nested objects from flat keys, set nestingSeparator.
Install
This adapter needs the dotenv peer dependency, if you don't have it already:
npm install dotenvThe dotenv adapter is synchronous, so it also works with loadConfigSync.
Usage
import { } from 'zod';
import { } from 'zod-config';
import { } from 'zod-config/dotenv-adapter';
import path from 'path';
const = .({
: .().(/^\d+$/),
: .(),
});
const = path.(, '.env');
const = await ({
: ,
: ({ : }),
});
// using filter regex to match only the keys we need
const = await ({
: ,
: ({
: ,
: /^MY_APP_/,
}),
});
// using nesting separator to create nested objects
// .env file content: DATABASE_HOST=localhost\nDATABASE_PORT=5432
const = await ({
: .({
: .({
: .(),
: .(),
}),
}),
: ({
: ,
: '_',
}),
});Options
| Option | Type | Description |
|---|---|---|
path | string | Path to the .env file. Required. |
nestingSeparator | string | Separator used to build nested objects from flat keys, e.g. _. Only top-level keys are split. |
Also accepts the shared adapter options: regex, silent, keyMatching and transform.