-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add default value for NODE_ENV
#2507
Conversation
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.
@infomiho I tried to define list of all necessary env vars and somehow ensure (in the type system) that both serverProdSchema
and serverDevSchema
:
- Define constraints for all env vars (
NODE_ENV
,WASP_SERVER
...). - Can only make the existing constraints stricter.
- Define no other constraints (to prevent typos).
The second point is optional The original list doesn't need to define any constraints, it can just be a list and leave all the constraining to serverProdSchema
and serverDevSchema
.
I was hoping Zod supports something that, but seemingly none of its methods (e.g., extend
, merge
) fit my use case.
I also tried to use a type definition and satisfies
, but couldn't make it work. GPTs kept suggesting complicated custom generic functions, so I gave up (don't think it's worth it)
It would work something like this (I'm making up the narrow
method):
// This doesn't have to be Zod, it could just be a normal type, perhaps that's even better.
// (but I couldn't make that work either).
const envRequirements = z.object({
NODE_ENV: z.enum(['development', 'production']),
WASP_SERVER_URL: serverUrlSchema,
WASP_WEB_CLIENT_URL: clientUrlSchema,
JWT_SECRET: z.string(),
});
const serverDevSchema = envRequirements.narrow({
NODE_ENV: z.literal('development'),
WASP_SERVER_URL: serverUrlSchema
.default('http://localhost:3001'),
WASP_WEB_CLIENT_URL: clientUrlSchema
.default('http://localhost:3000/'),
JWT_SECRET: jwtTokenSchema
.default('DEVJWTSECRET'),
})
const serverProdSchema = envRequirements.narrow({
NODE_ENV: z.literal('production'),
WSP_SERVER_URL: serverUrlSchema, // error: typo
WASP_SERVER_URL: z.string(), // error: widens constraints
// Error for missing JWT_SECRET, no constraints defined
})
Did I miss something, can Zod do this?
If not, I'll keep it as is. A custom generic function is too much IMO.
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.
Too bad we couldn't do it like you imagined, I see unions in Zod 4 will get some love, but it's unclear when it will go out - that might help us in the future.
The solution you implemented is good enough, I don't see anything better you could have done.
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.
LGTM, tested locally 👍
Fixes this:
@infomiho Couldn't find a better way to set a default value. Is this ok?