From 580cd6789c9fcf34bf68e4690dc4847b2a7b800a Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Thu, 5 Feb 2026 12:48:56 +0100 Subject: [PATCH] feat: Allow skipping MAIL_HOST environment variable validation during build processes using `SKIP_RUNTIME_ENV_VALIDATION`. --- Dockerfile | 2 +- lib/env.test.ts | 10 ++++++++++ lib/env.ts | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 14a70efa..8c186380 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,7 @@ ENV NEXT_PUBLIC_TARGET=$NEXT_PUBLIC_TARGET ENV DIRECTUS_URL=$DIRECTUS_URL # Validate environment variables during build -RUN npx tsx scripts/validate-env.ts +RUN SKIP_RUNTIME_ENV_VALIDATION=true npx tsx scripts/validate-env.ts RUN --mount=type=cache,target=/app/.next/cache npm run build diff --git a/lib/env.test.ts b/lib/env.test.ts index a0cc7aca..2c0cd20c 100644 --- a/lib/env.test.ts +++ b/lib/env.test.ts @@ -57,4 +57,14 @@ describe('envSchema', () => { }); expect(result.success).toBe(true); }); + + it('should skip MAIL_HOST requirement if SKIP_RUNTIME_ENV_VALIDATION is true', () => { + process.env.SKIP_RUNTIME_ENV_VALIDATION = 'true'; + const result = envSchema.safeParse({ + NEXT_PUBLIC_BASE_URL: 'https://example.com', + TARGET: 'production', + }); + expect(result.success).toBe(true); + delete process.env.SKIP_RUNTIME_ENV_VALIDATION; + }); }); diff --git a/lib/env.ts b/lib/env.ts index 8ac17769..59a3c1c4 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -57,8 +57,9 @@ export const envSchema = z .superRefine((data, ctx) => { const target = data.NEXT_PUBLIC_TARGET || data.TARGET; const isDev = target === 'development' || !target; + const isBuildTimeValidation = process.env.SKIP_RUNTIME_ENV_VALIDATION === 'true'; - if (!isDev && !data.MAIL_HOST) { + if (!isDev && !isBuildTimeValidation && !data.MAIL_HOST) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'MAIL_HOST is required in non-development environments',