forked from BearStudio/start-ui-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.env.validator.js
47 lines (37 loc) Β· 1.19 KB
/
.env.validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// @ts-check
const { z } = require("zod");
/**
* Update this when adding/editing/removing environment variables
*/
const envSchema = z.object({
NEXT_PUBLIC_API_BASE_URL: z.string().url().optional(),
NEXT_PUBLIC_DEV_ENV_NAME: z.string().optional(),
NEXT_PUBLIC_DEV_ENV_COLOR_SCHEME: z.string().optional(),
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME: z.string().optional(),
NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET: z.string().optional(),
});
/**
* This file is included in `/next.config.js` which ensures the app isn't built with invalid env vars.
*/
const _env = envSchema.safeParse(process.env);
const formatErrors = (
/** @type {import('zod').ZodFormattedError<Map<string,string>,string>} */
errors,
) =>
Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value)
return `${name}: ${value._errors.join(", ")}\n`;
})
.filter(Boolean);
if (!_env.success) {
console.error(
"β Invalid environment variables\nπ Fix the following environment variables or update the `.env.validator.js` file.\n",
...formatErrors(_env.error.format()),
);
process.exit(1);
}
console.log('β
Environment variables validation')
module.exports = {
envSchema
}