validate-env-vars
Version:
A lightweight utility to check the presence and validity of environment variables, as specified by a Zod schema
30 lines (26 loc) • 1.31 kB
TypeScript
import * as z4 from 'zod/v4/core';
type EnvObject = z4.$ZodObject<{
[k: string]: z4.$ZodType<string | undefined>;
}>;
type ZodSafeParseReturnType = z4.util.SafeParseResult<Record<string, unknown>>;
interface Config {
schema: EnvObject;
envPath?: string;
exitOnError?: boolean;
logVars?: boolean;
}
interface InnerConfig extends Required<Omit<Config, 'exitOnError' | 'envPath'>> {
vars: Record<string, string>;
}
/**
* Validate environment variables against a Zod schema
*
* @param {Config} options The configuration object
* @property {EnvObject} schema The schema to validate against (must be z.object with string-based fields)
* @property {string} envPath - The path to the .env file. If none is provided, no file will be loaded`
* @property {boolean} exitOnError - Whether to exit the process or throw if validation fails. Defaults to `false`
* @property {boolean} logVars - Whether to output successfully parsed variables to the console. Defaults to `true`
* @throws {Error} If a required environment variable is missing or invalid and `exitOnError` is `false`
*/
declare function validateEnvVars({ schema, envPath, exitOnError, logVars, }: Config): void;
export { type Config, type EnvObject, type InnerConfig, type ZodSafeParseReturnType, validateEnvVars as default };