Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use t3's ENV setup w/ Zod #319

Closed
wants to merge 2 commits into from
Closed

feat: Use t3's ENV setup w/ Zod #319

wants to merge 2 commits into from

Conversation

mthomps4
Copy link
Contributor

Changes

feat: Use t3's ENV setup w/ Zod to give strict errors if ENVs are missing
https://create.t3.gg/en/usage/env-variables#envmjs

@mthomps4 mthomps4 self-assigned this Jun 30, 2023
Comment on lines +9 to +23
const server = z.object({
APP_ENV: z.enum(['development', 'test', 'production']),
APP_URL: z.string(),
DATABASE_URL: z.string().url(),
DEBUG: z.boolean().optional(),
FC_GIT_COMMIT_SHA: z.string().optional(),
IS_CI: z.boolean().optional(),
NEXTAUTH_SECRET:
process.env.NODE_ENV === 'production' ? z.string().min(1) : z.string().min(1).optional(),
NEXTAUTH_URL: z.string().min(1),
NODE_ENV: z.enum(['development', 'test', 'production']),
PORT: z.number().int().positive(),
RENDER_EXTERNAL_URL: z.string().optional(),
SHOULD_MIGRATE: z.boolean(),
});
Copy link
Contributor

@cullylarson cullylarson Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve run into a lot of annoying issues using zod to validate config in this way and on a couple projects have ended up just scrapping it. A few reasons why:

  • You have to coerce to get booleans and numbers from env strings. This doesn't work the way you want by default (e.g. "false" will be coerced as true). You can write transformation functions for zod which works ok, but it isn't ideal.

  • Sometimes config for different stages have different requirements.

  • It's almost always cleaner to have a nested config structure (e.g. config.db.url vs config.DB_URL). This is especially true when you have a lot of env variables and complex config. By using a nested config, you at least constrain that complication in one place, where the config object is defined.

  • There was another reason and I can't remember it.

I’d avoid running it against config in this way. There are a few alternatives:

  1. The way we're doing config on Tachus (e.g. those envToString, etc. functions). This has worked great for me on a number of projects.

  2. Run a zod schema check on process.env itself to just verify that required values are provided and nothing more.

  3. Create your config variable using option no.1 above, but then run a zod schema check against that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all great comments Cully.
Reading through - I'm fine not moving along with Zod at the moment.

Comment on lines +69 to +71
const parsed = /** @type {MergedSafeParseReturn} */ isServer
? merged.safeParse(processEnv) // on server we can validate all env vars
: client.safeParse(processEnv); // on client we can only validate the ones that are exposed
Copy link
Contributor

@cullylarson cullylarson Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to fail for all the z.boolean and z.number values because all process.env values are strings.

* Specify your server-side environment variables schema here. This way you can ensure the app isn't
* built with invalid env vars.
*/
const server = z.object({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found a helpful naming convention when working zod to be including Schema at the end of schema variables (e.g. serverSchema).

@mthomps4
Copy link
Contributor Author

These are all great comments Cully.
Reading through - I'm okay not moving along with Zod, glad to know others have battle-tested this a bit.

@mthomps4 mthomps4 closed this Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants