fix(types): re-enable type checking and validate environment variables with zod - #108
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Production env validation currently yields generic “Required” errors when variables are unset, undermining the goal of actionable startup validation.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR re-enables Nuxt type checking and introduces Zod-based runtime config validation to make environment variable usage type-safe and fail-fast (especially in production), while tightening test typings to prevent future regressions.
Changes:
- Re-enabled Nuxt TypeScript type checking and reduced reliance on
@ts-expect-errorin tests. - Added Zod schemas + helpers for validating runtime config on both client (public) and server, plus a Nitro plugin to validate on startup.
- Added/updated unit tests and test TypeScript configuration to support stricter typing.
File summaries
| File | Description |
|---|---|
| tests/unit/utils/env.test.ts | Adds unit tests covering public + server env parsing and production enforcement. |
| tests/unit/utils/abusePatterns.test.ts | Removes @ts-expect-error now that containsAbusePattern accepts nullish input. |
| tests/types.d.ts | Introduces Node/Nitro global type declarations for unit tests. |
| tests/tsconfig.json | Adds test-specific TS config to resolve Node/Nitro globals and Nuxt generated types. |
| tests/server/api.test.ts | Tightens Nitro global mocks and avoids dynamic-property @ts-expect-error patterns. |
| tests/server/api-guard.test.ts | Improves typing around getHeader and useRuntimeConfig mocking. |
| tests/components/sections/Projects.test.ts | Adjusts assertions to satisfy stricter typing (but introduces weaker optional-chained assertions). |
| tests/components/elements/Chatbot.test.ts | Improves DOM typing for textarea querying in tests. |
| server/utils/env.ts | Adds a server helper to return validated runtime config. |
| server/plugins/env.ts | Adds a Nitro plugin to validate runtime config at server startup (strict in prod). |
| server/middleware/api-guard.ts | Removes unsafe type assertions from runtime config reads. |
| pnpm-lock.yaml | Locks the new zod dependency. |
| package.json | Adds zod as a dependency. |
| nuxt.config.ts | Re-enables typescript.typeCheck. |
| app/utils/env.ts | Implements Zod schemas + validation helpers for runtime config (public + server). |
| app/utils/abusePatterns.ts | Expands input type to accept `string |
| app/pages/index.vue | Switches to useEnv() for type-safe public env access. |
| app/composables/useEnv.ts | Adds a composable for validated public runtime config access. |
| app/components/sections/MapRD.vue | Switches to useEnv() for mapbox token retrieval. |
| app/components/layouts/Footer.vue | Switches to useEnv() for email retrieval. |
Review details
Files not reviewed (1)
- pnpm-lock.yaml: Generated file
Suppressed comments (2)
app/utils/env.ts:29
- Same issue as
allowedOrigin: ifchatSessionSecretis missing entirely in production, Zod will report a generic "Required" instead of the intended production-specific message. Add a default after the.min(...)so the error message stays actionable when the env var is unset.
chatSessionSecret: z
.string()
.min(
1,
'chatSessionSecret (NUXT_CHAT_SESSION_SECRET) is required in production'
),
tests/components/sections/Projects.test.ts:57
- Similarly for
titles[0]/titles[1]: using?.weakens the assertion output even though earlier expectations already ensure these elements exist. Use non-null assertions so a missing element fails loudly at the right place.
const titles = component.findAll('[data-testid="project-title"]');
expect(titles[0]?.text()).toBe('Project 1');
expect(titles[1]?.text()).toBe('Project 2');
- Files reviewed: 18/20 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
typescript.typeCheckinnuxt.config.tsand tightens test typings to avoid regressions (Closes Re-enable type checking and tighten test typings #84).tests/tsconfig.jsonandtests/types.d.tsfor proper Node and Nitro globals resolution in tests.app/utils/env.ts,app/composables/useEnv.ts,server/utils/env.ts, andserver/plugins/env.ts).@ts-expect-errorand type assertions across server and client components.tests/unit/utils/env.test.ts).Verification
pnpm test: 11 test suites / 50 tests passingpnpm typecheck: Passed with 0 errorspnpm lint: Passed with 0 errors