Created
October 29, 2024 12:27
-
-
Save t18n/7e4d90166b1d5ffbc04bb21b10e28058 to your computer and use it in GitHub Desktop.
Simple environment variable validation with Zod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dotenv/config'; | |
import { z } from 'zod'; | |
/* | |
* By default, the zod library will throw an error if the environment variable is missing. | |
* All environment variables are string by default. | |
* */ | |
function zodEnvCheck(value: string) { | |
return z.string({ message: `Environment variable missing: ${value}` }).parse(process.env[value]); | |
} | |
export const CONFIG = { | |
LOG_LEVEL: zodEnvCheck('LOG_LEVEL'), | |
ENVIRONMENT: zodEnvCheck('ENVIRONMENT'), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If an environment variable is missing, Zod will throw an error in this format
so you know exactly which one is missing :)