-
-
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
Post 0.16.0 cleanup #2490
base: main
Are you sure you want to change the base?
Post 0.16.0 cleanup #2490
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.
Simple enough, approved
@infomiho Sorry, should have marked this one as Draft as it's still WIP. I'll ping you for another review :) |
export const env = ensureEnvSchema( | ||
{ NODE_ENV: serverDevSchema.shape.NODE_ENV.value, ...process.env }, | ||
serverEnvSchema, | ||
) |
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.
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 vaules (
NODE_ENV
,WASP_SERVER
...). - Can only make the existing constraints stricter.
- Define no other constraints (to prevent typos).
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.
This got moved from SdkGenerator.Server.Operations
, nothing new here.
No description provided.