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: add environment variables validation #147

Merged
merged 11 commits into from
Jul 12, 2022

Conversation

piotrski
Copy link
Contributor

@piotrski piotrski commented Jul 7, 2022

Add runtime environment variables validation

  • I reviewed linter warnings + errors, resolved formatting, types and other issues related to my work
  • The PR title follows the convention we established conventional-commit
  • I performed a functional test on my final commit

In #140 @KATT suggested adding runtime environment variables validation using zod. Since env.js is imported inside next.config.js it can't ba a typescript file. There are two templates for env.js, one for just prisma env-prisma.js and another one for prisma + next-auth env-prisma-auth.js


Screenshots

Screenshot 2022-07-07 at 20 58 14

💯

Copy link
Collaborator

@KATT KATT left a comment

Choose a reason for hiding this comment

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

On my phone, but think you should add something to the tsconfig too in order to get the file validated with ts ☺

@Fedeorlandau
Copy link
Contributor

Hi there! Please remember to update the PR title so we get it on the changelog.

feat: add environment variables validation

Cheers!

@piotrski
Copy link
Contributor Author

piotrski commented Jul 7, 2022

On my phone, but think you should add something to the tsconfig too in order to get the file validated with ts ☺

I've added **/*.js to tsconfig.

@piotrski
Copy link
Contributor Author

piotrski commented Jul 7, 2022

Hi there! Please remember to update the PR title so we get it on the changelog.

feat: add environment variables validation

Cheers!

Should I change it to something else?

@Fedeorlandau
Copy link
Contributor

Hi there! Please remember to update the PR title so we get it on the changelog.
feat: add environment variables validation
Cheers!

Should I change it to something else?

It's perfect now! Thanks

Quick question, what happens when just "next-auth + trpc" or just "next-auth" is enabled?

@piotrski
Copy link
Contributor Author

piotrski commented Jul 7, 2022

Hi there! Please remember to update the PR title so we get it on the changelog.
feat: add environment variables validation
Cheers!

Should I change it to something else?

It's perfect now! Thanks

Quick question, what happens when just "next-auth + trpc" or just "next-auth" is enabled?

I am thinking of adding the boilerplate env.js to the base and overwriting when installing more packages this way we can just import it into the base next.config.js

Copy link
Member

@t3dotgg t3dotgg left a comment

Choose a reason for hiding this comment

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

I do really like this

@juliusmarminge
Copy link
Member

The only problem with this as it stands, is that it would make the initial app fail to build because of the example .env not following this validation-schema.

I just added a CI step that builds the default t3-app and this would fail if we do not include a better .env by default that can be parsed with this validation.

@juliusmarminge
Copy link
Member

There are two templates for env.js, one for just prisma env-prisma.js and another one for prisma + next-auth env-prisma-auth.js

Also include one for just NextAuth.js env-auth.js to cover all the cases.

@piotrski
Copy link
Contributor Author

piotrski commented Jul 8, 2022

I've moved schema to env-schema.js to keep the code DRY and added env validation to the base template. Now addons are just need to override env-schema.js. I've tested base config, prisma, prisma + auth, auth, auth + trpc

Copy link
Member

@juliusmarminge juliusmarminge left a comment

Choose a reason for hiding this comment

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

I like this. Left some comments. As for the concern in my comment previously, would this prevent the app from building if the .env schema is not valid or is it just runtime?

EDIT: looks like the CI answered my question. Should we also include a .env that can be validated or what are your thoughts on this?

> [email protected] build /home/runner/work/create-t3-app/ci-test-app
[8](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:9)
> next build
[9](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:10)
[10](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:11)
❌ Invalid environment variables:
[11](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:12)
 DATABASE_URL: Required
[12](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:13)
 NEXTAUTH_SECRET: Required
[13](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:14)
 NEXTAUTH_URL: Required
[14](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:15)
 GITHUB_ID: Required
[15](https://github.com/t3-oss/create-t3-app/runs/7252027908?check_suite_focus=true#step:8:16)
 GITHUB_SECRET: Required

src/installers/next-auth.ts Outdated Show resolved Hide resolved
src/installers/prisma.ts Outdated Show resolved Hide resolved
template/addons/prisma/env-prisma-auth.js Outdated Show resolved Hide resolved
@juliusmarminge
Copy link
Member

Looks good! Update the branch and I'll merge

@piotrski
Copy link
Contributor Author

Looks good! Update the branch and I'll merge

Updated 👍

@juliusmarminge
Copy link
Member

Thank you 🙏!

@juliusmarminge juliusmarminge merged commit 0f0b56d into t3-oss:main Jul 12, 2022
devvianto605 pushed a commit to devvianto605/create-devviantex-nextjs-app that referenced this pull request Jun 9, 2024
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

5 participants