Last active
June 17, 2024 09:47
-
-
Save amanzrx4/ac1ae8c3add05f3dc402abbee9eb5fe0 to your computer and use it in GitHub Desktop.
type safe env variable parsing with valibot
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 { config } from 'dotenv'; | |
import type { InferInput, ValiError } from 'valibot'; | |
import { object, parse, string } from 'valibot'; | |
config(); | |
const envVariables = object({ | |
MY_KEY: string(), | |
}); | |
parse(envVariables, process.env); | |
try { | |
parse(envVariables, process.env); | |
} catch (e) { | |
console.error((e as ValiError<typeof envVariables>).message); | |
process.exit(1); | |
} | |
declare global { | |
// eslint-disable-next-line @typescript-eslint/no-namespace | |
namespace NodeJS { | |
interface ProcessEnv extends InferInput<typeof envVariables> {} | |
} | |
} | |
export {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment