Skip to content

Instantly share code, notes, and snippets.

@joao-coimbra
Last active January 30, 2024 13:37
Show Gist options
  • Select an option

  • Save joao-coimbra/caf0a81d1f59d566b582b2ee8f99cc17 to your computer and use it in GitHub Desktop.

Select an option

Save joao-coimbra/caf0a81d1f59d566b582b2ee8f99cc17 to your computer and use it in GitHub Desktop.
import { z } from "zod";
const nodeEnv = z.enum(["development", "production"]);
function requiredOn(env: z.infer<typeof nodeEnv>) {
return (value: any) => !(env === process.env.NODE_ENV && !value);
}
const envSchema = z.object({
NODE_ENV: nodeEnv.default('development'),
DATABASE_URL: z.string().min(1),
REQUIRED_ENV: z.string().refine(requiredOn('production'))
// MORE VARIABLES
})
export const env = envSchema.parse(process.env)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment