-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Enable users to define their env var validations #2363
base: miho-zod-env
Are you sure you want to change the base?
Conversation
69e2944
to
f0ffeec
Compare
3d27ce2
to
c606b6b
Compare
@infomiho could you please describe how this works, what is the experience for the user? I see there are no docs yet, and it would help me with reviewing so I don't have to guess it from the code + so I can consider if code does indeed do what the idea was. |
@Martinsos I've updated the PR description to give you a proper intro into the task I was tackling |
@infomiho from the high level / DX perspective, sounds good to me, thanks for the detailed description! If they define the same var as Wasp already defines, what happens? Conflict or override or nothing? What do we want to happen, is there any benefit in them being able to override Wasp's var definitions? Maybe conflict is the best? |
@Martinsos right now, since we merge their schema into ours, they can override our rules: example how that works If their validation rules are less strict or wrong, they will get a type error (change We can merge our schema into theirs, therefore our rules would always win - that seems okay to me, even good enough. I think, we could go through their schema keys and our schema keys and if they are conflicts, output a custom error. But this is something I would need to check. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
Two main requests: the public API change and updating the TS SDK.
What was the naming problem you mentioned?
@@ -1,25 +1,7 @@ | |||
import * as z from 'zod' | |||
|
|||
const redColor = '\x1b[31m' | |||
// PUBLIC API | |||
export type EnvValidationFn = () => z.ZodObject<any> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Table pls if not yet 😬
const serverCommonSchema = z.object({ | ||
{=# envValidationFn.isDefined =} | ||
{=& envValidationFn.importStatement =} | ||
const userServerEnvSchema = {= envValidationFn.importIdentifier =}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We talked about this in person. The verdict was we'll make the API similar to userSignupFields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"type": "module",
was added in the package.json
I guess somebody added it when testing the Wasp TS config 🤷
waspc/src/Wasp/AppSpec/App/Client.hs
Outdated
@@ -16,6 +16,7 @@ data Client = Client | |||
{ setupFn :: Maybe ExtImport, | |||
rootComponent :: Maybe ExtImport, | |||
-- We expect the base dir to start with a slash e.g. /client | |||
baseDir :: Maybe String | |||
baseDir :: Maybe String, | |||
envValidationFn :: Maybe ExtImport |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to update the TS SDK
The idea
Wasp uses Zod to define its validation rules for the env variables it expects e.g. it defines that
JWT_SECRET
is required and a string. After this validation passes, Zod gives us a validated object that we can safely use knowing that e.g.JWT_SECRET
is present and it is a string:Users need their own user-land env vars e.g.
STRIPE_API_KEY
. Some users validate their env vars with manual checks to make sure that the env var is set before running the app e.g.Since Wasp already uses Zod to validate env vars, we want to expose the same mechanism to our users. There are two main benefits:
env
object for both Wasp defined env vars and their own user-land env varsHow this works
Users need to define their env vars validation by defining a Zod object schema. They then declare that in the Wasp file. Something like this:
and then:
Wasp merges this user defined schema with its built-in env vars validation schema when it validates the
process.env
object on the server and theimport.meta.env
object on the client.Now users can use the
env
object to access their env vars like this:Caveats
env.ts
EnvValidationFn
withsatisfies