Skip to content

Commit 3b9a404

Browse files
committed
fix(internal): 🐛 Split environment variable validation into those required for DB and the rest
1 parent 8d75787 commit 3b9a404

File tree

3 files changed

+69
-29
lines changed

3 files changed

+69
-29
lines changed

drizzle.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Config } from "drizzle-kit";
22
import "dotenv/config";
33

4-
import { env } from "~/env";
4+
import { dbEnv as env } from "~/db-env";
55

66
export default {
77
schema: "./src/server/db/schema.ts",

src/db-env.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { createEnv } from "@t3-oss/env-nextjs";
2+
import { z } from "zod";
3+
/**
4+
* @import { type ZodType } from "zod"
5+
*/
6+
7+
/**
8+
* @template {ZodType} T
9+
* @param {T} validator
10+
*/
11+
export function optionalInDev(validator) {
12+
return process.env.NODE_ENV === "production"
13+
? validator
14+
: validator.optional();
15+
}
16+
17+
export const dbEnv = createEnv({
18+
/**
19+
* Specify your server-side environment variables schema here. This way you can ensure the app
20+
* isn't built with invalid env vars.
21+
*/
22+
server: {
23+
DATABASE_URL: z.string().url(),
24+
TURSO_AUTH_TOKEN: optionalInDev(z.string()),
25+
TURSO_ORG_SLUG: optionalInDev(z.string()),
26+
NODE_ENV: z
27+
.enum(["development", "test", "production"])
28+
.default("development"),
29+
VERCEL_GIT_COMMIT_REF: z.string().optional(),
30+
VERCEL_GIT_PULL_REQUEST_ID: z.coerce.number().int().optional(),
31+
},
32+
33+
/**
34+
* Specify your client-side environment variables schema here. This way you can ensure the app
35+
* isn't built with invalid env vars. To expose them to the client, prefix them with
36+
* `NEXT_PUBLIC_`.
37+
*/
38+
client: {
39+
// NEXT_PUBLIC_CLIENTVAR: z.string(),
40+
},
41+
42+
/**
43+
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
44+
* middlewares) or client-side so we need to destruct manually.
45+
*/
46+
runtimeEnv: {
47+
DATABASE_URL: process.env.DATABASE_URL,
48+
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
49+
TURSO_ORG_SLUG: process.env.TURSO_ORG_SLUG,
50+
NODE_ENV: process.env.NODE_ENV,
51+
VERCEL_GIT_COMMIT_REF: process.env.VERCEL_GIT_COMMIT_REF,
52+
VERCEL_GIT_PULL_REQUEST_ID: process.env.VERCEL_GIT_PULL_REQUEST_ID,
53+
},
54+
/**
55+
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
56+
* useful for Docker builds.
57+
*/
58+
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
59+
/**
60+
* Makes it so that empty strings are treated as undefined. `SOME_VAR: z.string()` and
61+
* `SOME_VAR=''` will throw an error.
62+
*/
63+
emptyStringAsUndefined: true,
64+
});

src/env.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
import { createEnv } from "@t3-oss/env-nextjs";
22
import { z } from "zod";
3-
/**
4-
* @import { type ZodType } from "zod"
5-
*/
3+
import { dbEnv, optionalInDev } from "~/db-env";
64

7-
/**
8-
* @template {ZodType} T
9-
* @param {T} validator
10-
*/
11-
function optionalInDev(validator) {
12-
return process.env.NODE_ENV === "production"
13-
? validator
14-
: validator.optional();
15-
}
16-
17-
export const env = createEnv({
5+
export const authEnv = createEnv({
186
/**
197
* Specify your server-side environment variables schema here. This way you can ensure the app
208
* isn't built with invalid env vars.
219
*/
2210
server: {
23-
DATABASE_URL: z.string().url(),
24-
TURSO_AUTH_TOKEN: optionalInDev(z.string()),
25-
TURSO_ORG_SLUG: optionalInDev(z.string()),
26-
NODE_ENV: z
27-
.enum(["development", "test", "production"])
28-
.default("development"),
2911
NEXTAUTH_SECRET: optionalInDev(z.string()),
3012
NEXTAUTH_URL: z.preprocess(
3113
// This makes Vercel deployments not fail if you don't set NEXTAUTH_URL
@@ -36,8 +18,6 @@ export const env = createEnv({
3618
),
3719
DISCORD_CLIENT_ID: optionalInDev(z.string()),
3820
DISCORD_CLIENT_SECRET: optionalInDev(z.string()),
39-
VERCEL_GIT_COMMIT_REF: z.string().optional(),
40-
VERCEL_GIT_PULL_REQUEST_ID: z.coerce.number().int().optional(),
4121
},
4222

4323
/**
@@ -54,16 +34,10 @@ export const env = createEnv({
5434
* middlewares) or client-side so we need to destruct manually.
5535
*/
5636
runtimeEnv: {
57-
DATABASE_URL: process.env.DATABASE_URL,
58-
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
59-
TURSO_ORG_SLUG: process.env.TURSO_ORG_SLUG,
60-
NODE_ENV: process.env.NODE_ENV,
6137
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
6238
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
6339
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
6440
DISCORD_CLIENT_SECRET: process.env.DISCORD_CLIENT_SECRET,
65-
VERCEL_GIT_COMMIT_REF: process.env.VERCEL_GIT_COMMIT_REF,
66-
VERCEL_GIT_PULL_REQUEST_ID: process.env.VERCEL_GIT_PULL_REQUEST_ID,
6741
},
6842
/**
6943
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
@@ -76,3 +50,5 @@ export const env = createEnv({
7650
*/
7751
emptyStringAsUndefined: true,
7852
});
53+
54+
export const env = { ...dbEnv, ...authEnv };

0 commit comments

Comments
 (0)