-
Notifications
You must be signed in to change notification settings - Fork 7
Add runtime environment variable validation with fail-fast startup behavior. #44
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| const dotenv = require("dotenv"); | ||
| const { z } = require("zod"); | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const envSchema = z.object({ | ||
| VITE_SUPABASE_URL: z.string().url(), | ||
| VITE_SUPABASE_ANON_KEY: z.string().min(10), | ||
| PORT: z.string().optional() | ||
| }); | ||
|
|
||
| const result = envSchema.safeParse(process.env); | ||
|
|
||
| if (!result.success) { | ||
| console.error("\nβ Invalid environment configuration:\n"); | ||
|
|
||
| result.error.errors.forEach((err) => { | ||
| console.error(`- ${err.path.join(".")}: ${err.message}`); | ||
| }); | ||
|
|
||
| process.exit(1); // π₯ FAIL FAST | ||
| } | ||
|
|
||
| module.exports = result.data; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { createServer } from 'node:http'; | ||
| import { URL } from 'node:url'; | ||
| import { database, dbPath } from './db.js'; | ||
| require("./env"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π§© Analysis chainπ Script executed: # Check if backend/server.js exists and read its content
head -20 backend/server.jsRepository: fuzziecoder/Brocode-Party-Update-App Length of output: 942 π Script executed: # Check package.json for module type configuration
cat package.json | grep -A 5 -B 5 '"type"'Repository: fuzziecoder/Brocode-Party-Update-App Length of output: 296 π Script executed: # Verify what ./env refers to (file or directory)
ls -la backend/ | grep envRepository: fuzziecoder/Brocode-Party-Update-App Length of output: 137 π Script executed: # Check if there are ESM imports in backend/server.js
rg -n "^import\s|from\s" backend/server.jsRepository: fuzziecoder/Brocode-Party-Update-App Length of output: 203
This file uses ES module imports and is configured as ESM ( π§ Suggested fix-require("./env");
+import './env.js';π€ Prompt for AI Agents |
||
|
|
||
| const port = Number(process.env.PORT || 4000); | ||
|
|
||
|
|
||
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.
π§© Analysis chain
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 792
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 62
π Script executed:
rg -n 'PORT' backend/ --type js --type ts -A 2 -B 2Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 551
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 935
π Script executed:
rg -n 'z\.coerce' --type js --type tsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 62
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 106
π Script executed:
rg -n 'require.*zod|import.*zod' --type js --type tsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 126
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 147
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 62
π Script executed:
cat package-lock.json | head -50Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 1706
π Script executed:
Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 126
Add
zodto dependencies in package.json β backend/env.js will fail at runtime without it.The environment validation module requires zod but it's not listed in package.json. Once added, consider validating
PORTas a number to prevent the server from attempting to listen onNaNif an invalid value is provided:const envSchema = z.object({ VITE_SUPABASE_URL: z.string().url(), VITE_SUPABASE_ANON_KEY: z.string().min(10), - PORT: z.string().optional() + PORT: z.coerce.number().int().positive().optional() });This ensures fail-fast validation with a clear error message instead of silent NaN at startup.
π Committable suggestion
π€ Prompt for AI Agents