UNPKG

validate-env-vars

Version:

A lightweight utility to check the presence and validity of environment variables, as specified by a Zod schema

37 lines (33 loc) 2.38 kB
import { z } from 'zod'; export { enum as envEnum, literal as envLiteral, string as envString } from 'zod'; declare const envNonEmptyString: () => z.ZodEffects<z.ZodString, string, string>; declare const envInteger: () => z.ZodString; type ZodEnvTypes = z.ZodString | z.ZodEnum<[string, ...string[]]> | z.ZodLiteral<string> | ReturnType<typeof envNonEmptyString> | ReturnType<typeof envInteger>; type ZodEnvTypesWithEffects = ZodEnvTypes | z.ZodEffects<ZodEnvTypes>; type ZodEnvTypesWithUnion = ZodEnvTypes | z.ZodUnion<[ZodEnvTypes, ...ZodEnvTypes[]]>; type ZodEnvTypesOptional = ZodEnvTypes | z.ZodOptional<ZodEnvTypesWithUnion>; type ZodEnvTypesAll = ZodEnvTypes | ZodEnvTypesWithEffects | ZodEnvTypesWithUnion | ZodEnvTypesOptional | z.ZodUnion<[ZodEnvTypesOptional, ...ZodEnvTypesOptional[]]>; type SchemaType<T extends Record<string, ZodEnvTypesAll>> = { [K in keyof T]: T[K]; }; declare const envObject: <T extends Record<string, ZodEnvTypesAll>>(obj: T) => z.ZodObject<SchemaType<T>, "strip", z.ZodTypeAny, { [k in keyof z.objectUtil.addQuestionMarks<z.baseObjectOutputType<SchemaType<T>>, any>]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<SchemaType<T>>, any>[k]; }, { [k_1 in keyof z.baseObjectInputType<SchemaType<T>>]: z.baseObjectInputType<SchemaType<T>>[k_1]; }>; type EnvObject = ReturnType<typeof envObject>; type ZodSafeParseReturnType = z.SafeParseReturnType<Record<string, unknown>, Record<string, unknown>>; interface Config { schema: EnvObject; envPath?: string; exitOnError?: boolean; logVars?: boolean; } /** * Validate environment variables against a Zod schema * * @param {Config} options The configuration object * @property {EnvObject} schema The schema to validate against * @property {string} envPath - The path to the .env file. Defaults to `'.env'` * @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 EnvObject, type ZodSafeParseReturnType, validateEnvVars as default, envInteger, envNonEmptyString, envObject };