Repository navigation
Conversation
Replace the silent `getEnvVar` fallback with a zod schema for the public variables consumed by lib/config.ts. URL values must parse with `new URL` and use http(s), trailing slashes are normalised, the Stellar network is an enum, the app name must be non-empty after trimming and the analytics id is optional. `parseEnv(source)` is pure and throws one aggregated, readable error; the exported `env` object keeps its previous shape and defaults. In production `validateEnv()` (run on import) throws on invalid values; outside production invalid keys fall back to defaults with a warning so local work is not blocked. Adds lib/__tests__/env.test.ts covering empty/whitespace values, ftp:// and scheme-less hosts, uppercase networks, whitespace padding, defaults and the production/dev validation paths. Closes Predictify-org#1007. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
The repo's CI (typecheck, tests, build) is currently red on main because of several unrelated merges. Fix them so the env-validation change can land with a green pipeline: - lib/events-store.ts: replace the truncated `mockEvents` stub with a re-export of the canonical fixtures (watchlist page imports it); fixes syntax errors. - components/navbar/Navbar.tsx: merge duplicated lucide imports and give every NAV_ITEMS entry a translation key (Watchlist/Creator). - components/navbar/MobileBottomTabs.tsx + i18n catalogs: use `labelKey` for the Watchlist tab and add the missing nav.watchlist/nav.creator messages. - app/layout.tsx, app/metadata.ts: `manifest` belongs to Metadata, not Viewport. - components/activity-timeline: memoise the generated activity list so the effect cannot trigger an infinite setState loop; fix stale pagination tests. - creator/ics/navbar/layout-metadata tests: close a missing `it` block, fold against a real calendar event with Buffer.byteLength, render before querying, and query union metadata types safely. - PWA/install-prompt tests: convert leftover vitest imports/mocks to Jest. - a11y-routes mock: keep the real i18n formatters and fall back to English. - i18n formatRelative: use numeric "always" for stable output across ICU. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
|
@divysam is attempting to deploy a commit to the Jagadeesh B's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@divysam Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This branch has not been deployed
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
Closes #1007.
getEnvVarinlib/env.tsreturned''silently and accepted any string, so amalformed
NEXT_PUBLIC_API_URLorNEXT_PUBLIC_APP_URL(e.g.ftp://…, a hostwithout a scheme, or a whitespace-only value) flowed straight into
lib/config.tsand from there into metadata, SEO and API clients. It alsosilently treated an explicitly empty value (
process.env[key] || default) asunset, while a whitespace-only value passed through unchanged.
This PR validates the public environment with
zod(already a dependency),fails fast in production, and keeps local development unblocked.
What changed
lib/env.tsZod schema for the public variables exported from the module (
envkeepsits existing shape and defaults, so
lib/config.tsneeds no change):NEXT_PUBLIC_STELLAR_NETWORK— enumtestnet | mainnet, case-insensitive(
MAINNETis accepted and normalised), defaulttestnet.NEXT_PUBLIC_API_URL/NEXT_PUBLIC_APP_URL— must parse withnew URLand use
http:/https:; trailing slashes are normalised away; defaulthttp://localhost:3000/api/http://localhost:3000.NEXT_PUBLIC_APP_NAME— non-empty after trimming, defaultPredictify.NEXT_PUBLIC_ANALYTICS_ID— optional; empty/whitespace disables analytics.NEXT_PUBLIC_USE_MOCKS—true | false, defaultfalse.NODE_ENV— non-empty string, defaultdevelopment.parseEnv(source: Record<string, string | undefined>)— new pure,testable entry point. Missing keys use the documented defaults; invalid input
throws a single aggregated error listing every problem, e.g.
resolveEnv(source)— non-throwing helper used to build the exportedenv. Only the offending keys fall back to their defaults; valid values arepreserved.
validateEnv()— applies the same rules toprocess.env; throws theaggregated error in production (and runs automatically on import, as
before) and warns in development/test while falling back to defaults.
Invalid values can therefore never leak through the exported
env.Tests —
lib/__tests__/env.test.ts(new)21 cases covering:
MAINNET) accepted and normalised;ftp://, scheme-lesslocalhost:3000, empty andwhitespace-only URLs, empty/whitespace app names and unknown networks;
'',' 'and unset all yield'');resolveEnvfallback keeps valid values and replaces only invalid ones;validateEnv()warns outside production and throws in production;envkeeps its key set/defaults and stays compatible withlib/config.ts.Docs
docs/API.mdnow documents the zod validation, the accepted values and theproduction/dev failure behaviour.
Unrelated repairs required for a green CI
mainis currently red for reasons unrelated to this issue (recent merges withno CI run), so
pnpm type-check,pnpm testandpnpm buildall failed beforethis branch even touched them. These are isolated in a second commit
(
fix: repair pre-existing typecheck, test and build failures on main):lib/events-store.ts: a truncatedexport const mockEvents: Event[] = [stub (left by the watchlist merge) was a syntax error and broke every import.
It is now a re-export of the canonical
lib/fixtures/events.Navbar.tsx: duplicatedlucide-reactimports andNAV_ITEMSentrieswithout a
key(→t(undefined)); added the missingnav.watchlist/nav.creatormessages in en/es/fr and fixed the matchingMobileBottomTabstab.
app/layout.tsx/app/metadata.ts:manifestis aMetadatafield, not aViewportfield.activity-timeline.tsx: the default mock list was regenerated on everyrender, so its effect looped forever (
Maximum update depth exceeded). It isnow memoised; stale pagination tests were updated to page groups with
pageSize=1.});increator.test.ts(nestedits), icsfolding test (built a real
MarketCalendarEvent,Buffer.byteLengthinsteadof the jsdom-missing
TextEncoder), navbar a11y test queried beforerendering,
layout-metadataunion-type access.in
a11y-routes.test.tsx.formatRelativenow usesnumeric: "always"so output is stable across ICUversions (French
dans 2 joursinstead ofaprès-demain).If you would prefer these split into a separate PR, I'm happy to do that.
Verification (Node 24, pnpm 10.18.3)
pnpm type-checkpnpm test --ci --maxWorkers=4pnpm buildpnpm test -- lib/__tests__/env.test.tspnpm lintreact/no-unescaped-entities, etc.); new/changed files are lint-cleanEdge cases explicitly exercised:
ftp://,localhost:3000without scheme,uppercase
MAINNET, whitespace-padded values, empty/whitespace URLs and appname, missing variables using defaults, multi-error aggregation, and
production vs. development failure behaviour.
No build output, binaries,
.vscode/or workflow changes are included.Notes for reviewers
required variable is an error in production and a warning + default elsewhere
(documented in
docs/API.md).NEXT_PUBLIC_USE_MOCKSis now validated astrue | false; previously anystring was accepted and anything other than
"true"silently behaved asfalse.envremains a plain object with the same keys/defaults, solib/config.tsand the existing consumers are untouched.
Checklist
Closes #1007)