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

Include all user env variables on the env object by default #2524

Open
infomiho opened this issue Feb 21, 2025 · 1 comment
Open

Include all user env variables on the env object by default #2524

infomiho opened this issue Feb 21, 2025 · 1 comment
Labels
enhancement New feature or request hm Requires more thought

Comments

@infomiho
Copy link
Contributor

Based on Discord:

Including all variables
pros: drop in replacement
cons: people might not validate their env vars

Excluding all the variables
pros: people are forced to validate vars (which is a success pit we want them in)
cons: people are annoyed/confused that they have to add validation for each variable they want to use

Now - we include only user variables that the users defined validation for (using the envValidationSchema feature) on the env object which users can import from wasp/server and wasp/client. While this has the benefit that it forces the user to define validations for env vars they want exposed on the env object.

The downside is that adding new env vars in the .env.client or .env.server does make them available as new fields on the env object with type of string | undefined (as they normally appear on the process.env or import.meta.env).

Implementation

We extend the ensureEnvSchema with & { [key: string]: string | undefined } and passthrough():

export function ensureEnvSchema<Schema extends z.ZodObject<any>>(
  data: unknown,
  schema: Schema
): z.infer<Schema> & { [key: string]: string | undefined } {
  try {
    return schema.passthrough().parse(data)
  } catch (e) {
    ...
  }
}

and this results in this type:

const env: {
    REACT_APP_NAME: string;
    REACT_APP_API_URL: string;
} & {
    [key: string]: string | undefined;
}
@infomiho infomiho added enhancement New feature or request hm Requires more thought labels Feb 21, 2025
@Martinsos
Copy link
Member

Idea: detect which vars they have, but are not validated, and then print a warning about it: hey I see you have vars you haven't validated, if you don't do so, you can't access them via env object, how about you validate them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hm Requires more thought
Projects
None yet
Development

No branches or pull requests

2 participants