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

Docs for user defined env vars validation #2450

Merged
merged 8 commits into from
Jan 15, 2025
Merged

Conversation

infomiho
Copy link
Contributor

@infomiho infomiho commented Jan 12, 2025

This PR will cover:

  • The way users access the validated env object on the server and the client
  • How users can define their custom env vars validation

@infomiho infomiho mentioned this pull request Jan 12, 2025
5 tasks
@infomiho infomiho marked this pull request as ready for review January 13, 2025 14:28
@infomiho infomiho requested a review from sodic January 13, 2025 14:28
Comment on lines 237 to 245
export const serverEnvValidationSchema = z.object({
STRIPE_API_KEY: z.string({
required_error: 'STRIPE_API_KEY is required.',
}),
})

export const clientEnvValidationSchema = z.object({
REACT_APP_NAME: z.string().default('TODO App'),
})
Copy link
Contributor

Choose a reason for hiding this comment

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

The API should not change depending on whether the user uses TS or JS. We should always tell users to call defineEnvValidationSchema.

Since JS and TS don't differ in runtime, neither should our API. The only differences should be in the type space.

It's better for both us and our users:

  • Users will appreciate (or maybe even expect) the consistency.
  • Two APIs mean maintaining two runtime behaviors/execution paths. The difference might be trivial, but it's there. Plus, we might one day decide to do something extra in defineEnvValidationSchema.

I noticed the same pattern with userSignupFields, and I'd like to change it there too (and all other places, if there are more).

Note

I know Vue does it like this, but I still don't like it :)

I guess they did it to stay somewhat backward-compatible. If they could start over, they'd probably make it consistent (but this is just my theory, maybe they prefer not to burden the JS users).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point 👍

I agree with both this and the userSignupFields change, created an issue: #2455

web/docs/project/env-vars.md Outdated Show resolved Hide resolved
web/docs/project/env-vars.md Outdated Show resolved Hide resolved
web/docs/project/env-vars.md Outdated Show resolved Hide resolved
}
```

We defined schemas for both the client and the server env vars and told Wasp to use them. Wasp merges your env validation schemas with the built-in env vars validation schemas when it validates the `process.env` object on the server and the `import.meta.env` object on the client.
Copy link
Contributor

Choose a reason for hiding this comment

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

First sentence: we -> you.

The rest of the text already uses the second person, so that's great.


## API Reference

There are **Wasp-defined** and **user-defined** env vars. Wasp has built-in validation for its env vars and you can define your own validation for your env vars.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd flip the ordering to better emphasize the main point:

Suggested change
There are **Wasp-defined** and **user-defined** env vars. Wasp has built-in validation for its env vars and you can define your own validation for your env vars.
There are **Wasp-defined** and **user-defined** env vars. Wasp already comes with built-in validation for Wasp-defined env vars. For your env vars, you can define your own validation.

web/docs/project/env-vars.md Outdated Show resolved Hide resolved
```

</TabItem>
</Tabs>

The `env` object is a validated object that Wasp provides to access server env vars.

You can use `process.env.SOME_SECRET` directly in your code, but it's not recommended because it's not validated and can lead to runtime errors if the env var is not defined.
Copy link
Contributor

@sodic sodic Jan 14, 2025

Choose a reason for hiding this comment

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

Same suggestion as above. This sentence is kind of a mouthful.

import { env } from 'wasp/server'

console.log(env.SOME_SECRET)
// Wasp-defined
const serverUrl: string = env.WASP_SERVER_URL
Copy link
Contributor

@sodic sodic Jan 14, 2025

Choose a reason for hiding this comment

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

Why the : string part?

Maybe you wanted to emphasize that "this is now TypeScript." I understand the motivation, but IMO it's better always to document the exact TS code our users should write. If that's the same as JS code, so be it.

It's even better if the type-checker does its job silently. The fewer changes happening when you toggle JS/TS in the docs, the better - it means TS and Wasp are doing a good job.

Applies to other code boxes too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had the intent of showing it's no longer string | undefined but actually a validated string but if that didn't come across, I'll remove it 👍

infomiho and others added 6 commits January 15, 2025 12:55
Co-authored-by: Filip Sodić <filip.sodic@gmail.com>
Co-authored-by: Filip Sodić <filip.sodic@gmail.com>
Co-authored-by: Filip Sodić <filip.sodic@gmail.com>
Co-authored-by: Filip Sodić <filip.sodic@gmail.com>
@infomiho infomiho merged commit 4a22372 into miho-zod-env Jan 15, 2025
@infomiho infomiho deleted the miho-zod-env-docs branch January 15, 2025 14:48
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.

2 participants