-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path.env.validator.js
42 lines (34 loc) · 1 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
// @ts-check
const { z } = require('zod');
/**
* Update this when adding/editing/removing environment variables
*/
const envSchema = z.object({
NEXT_PUBLIC_BASE_URL: z.string().url(),
NEXT_PUBLIC_ARCHIVE_YEAR: 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,
};