Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions apps/core/server/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ const envSchema = z.object({
// =========================================
// Database
// =========================================
DATABASE_URL: z.string().url("DATABASE_URL must be a valid PostgreSQL URL"),
DATABASE_URL: z
.string()
.min(1, "DATABASE_URL is required")
.refine(
(val) => {
// Accept postgres:// or postgresql:// connection strings
// These are valid connection strings but may not pass standard URL validation
return val.startsWith("postgres://") || val.startsWith("postgresql://");
},
{
message:
"DATABASE_URL must be a valid PostgreSQL connection string (postgres:// or postgresql://)",
},
),
Comment on lines +30 to +40
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved conciseness and robustness, you can use a regular expression to validate the DATABASE_URL protocol. This is a common pattern for this type of string validation and makes the intent clearer with less code, while also removing the need for a multi-line function body and comments.

    .refine(
      (val) => /^postgres(ql)?:\/\//.test(val),
      {
        message:
          "DATABASE_URL must be a valid PostgreSQL connection string (postgres:// or postgresql://)",
      }
    ),


// =========================================
// Authentication (Required for API server, optional for workers)
Expand Down Expand Up @@ -147,7 +160,10 @@ const envSchema = z.object({
.optional()
.transform((val) => {
if (!val) return [];
return val.split(",").map((origin) => origin.trim()).filter(Boolean);
return val
.split(",")
.map((origin) => origin.trim())
.filter(Boolean);
}),
FRONTEND_URL: z
.string()
Expand Down Expand Up @@ -257,6 +273,13 @@ const envSchema = z.object({
const parsed = envSchema.safeParse(process.env);

if (!parsed.success) {
// Format errors in a human-readable way for console output
const errorMessages = parsed.error.issues.map((issue) => {
const path = issue.path.join(".");
return ` - ${path}: ${issue.message}`;
});

// Log structured data for log aggregation systems
logger.error(
{
validation: "failed",
Comment on lines 273 to 285

Choose a reason for hiding this comment

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

Error Handling and Recovery

The current logic exits the process immediately if environment variable validation fails. While this is robust, it may be beneficial to provide more granular error reporting or fallback mechanisms, especially in development or CI environments. For example, you could allow the process to continue with warnings in non-production environments, or provide a summary of missing variables for easier debugging. This would improve developer experience and maintainability.

Expand All @@ -265,6 +288,28 @@ if (!parsed.success) {
},
"Environment variable validation failed",
);

// Also print to console for Railway logs visibility
console.error(
"\n╔════════════════════════════════════════════════════════════╗",
);
console.error(
"║ ENVIRONMENT VARIABLE VALIDATION FAILED ║",
);
console.error(
"╠════════════════════════════════════════════════════════════╣",
);
console.error(
"║ The following environment variables have issues: ║",
);
console.error(
"╚════════════════════════════════════════════════════════════╝\n",
);
console.error(errorMessages.join("\n"));
console.error(
"\n[Help] Check .env file and Railway environment variables.\n",
);
Comment on lines +293 to +311
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve readability and performance, you can consolidate the multiple console.error calls into a single one. Building an array of strings and joining them for a single output call is cleaner and reduces I/O operations.

  console.error(
    [
      "\n╔════════════════════════════════════════════════════════════╗",
      "║           ENVIRONMENT VARIABLE VALIDATION FAILED           ║",
      "╠════════════════════════════════════════════════════════════╣",
      "║ The following environment variables have issues:           ║",
      "╚════════════════════════════════════════════════════════════╝\n",
      errorMessages.join("\n"),
      "\n[Help] Check .env file and Railway environment variables.\n",
    ].join("\n")
  );


process.exit(1);
}

Expand Down
Loading