S
SolidJS3w ago
sh03

Using zod to validate env vars

Is there a pattern similar to the one recommend in the React world for accessing env vars only after they have been parsed & checked by zod? Something like this: https://creatures.sh/blog/env-type-safety-and-validation/#validating-environment-variables ------------------------------------- What have I tried? I've read: https://docs.solidjs.com/configuration/environment-variables but here the type safety doesn't make it so the server won't start without those variables (which is what I'd prefer). Moreover Zod also enables safety checks like checking that a string is a URL or is a specific set of possible strings that just defining the types doesn't.
10 Replies
TaQuanMinhLong
:Worry_Think: Does putting the envSchema.parse within entry-server file work?
Atila
Atila3w ago
the instructions in that post are for any JS app, so it works the same way for Solid for TS, you can also extend the global.d.ts like such:
declare namespace NodeJS {
interface ProcessEnv {
readonly NODE_ENV: "development" | "production";
readonly AUTH_URL: string;
readonly VITE_AUTH_PATH: string;
readonly GITHUB_CLIENT_ID: string;
readonly GITHUB_CLIENT_SECRET: string;
readonly AUTH_SECRET: string;
}
}

interface ImportMetaEnv {
readonly VITE_AUTH_PATH: string;
}
declare namespace NodeJS {
interface ProcessEnv {
readonly NODE_ENV: "development" | "production";
readonly AUTH_URL: string;
readonly VITE_AUTH_PATH: string;
readonly GITHUB_CLIENT_ID: string;
readonly GITHUB_CLIENT_SECRET: string;
readonly AUTH_SECRET: string;
}
}

interface ImportMetaEnv {
readonly VITE_AUTH_PATH: string;
}
sh03
sh03OP3w ago
I have a module like the one described in the post but if I import it in a component then the hydration seems to get blocked or JS somehow can't run anymore and only the SSR page is rendered. Probably because it accesses process.env from the top level or something. No errors, nothing. And put it in a context of some sort? or how were you imagining accessing it from other modules?
TaQuanMinhLong
It's just for the server's modules So in any server context, it should be within process.env It's expected to run once only before everything's ever started :Worry_Think:
sh03
sh03OP3w ago
Oh you mean just validate it there and then use process.env when you need it? If so, then I was wondering if I could instead of using process.env (which is untyped) use env (a custom variable) which is the result of z.parse So that I both have env validated and type safe
TaQuanMinhLong
As soon as you parsed
envSchema.parse(process.env)
envSchema.parse(process.env)
You're expecting it to have required ENV variables Then after that you can just
declare global {
namespace NodeJS {
interface ProcessEnv extends Zod.infer<typeof envSchema> {}
}
}
declare global {
namespace NodeJS {
interface ProcessEnv extends Zod.infer<typeof envSchema> {}
}
}
Your process.env is now typed
sh03
sh03OP3w ago
It doesn't seem as ergonomic as just having an export const env = envSchema.parse(process.env) but it will work, thanks 👍
TaQuanMinhLong
:Worry_CoffeeHMM:
sh03
sh03OP3w ago
Don't 😟 be 🙂
TaQuanMinhLong
:Worry_CoffeeSmile:
Want results from more Discord servers?
Add your server