Skip to content

Instantly share code, notes, and snippets.

@juanchoperezj
Created August 10, 2023 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanchoperezj/4fb0a150f295903b83470fb98a6d8d87 to your computer and use it in GitHub Desktop.
Save juanchoperezj/4fb0a150f295903b83470fb98a6d8d87 to your computer and use it in GitHub Desktop.
Validate env complete implementation reading the env file variables a compare them against the zod object
import fs from 'fs';
import path from 'path';
import Environment from '../src/@types/env';
const APP_ENV = process.env.APP_ENV ?? 'dev';
const isDev = APP_ENV === 'dev';
const envFile = path.join(__dirname, isDev ? '../.env' : `../.env.${APP_ENV}`);
(() => {
const fileExists = fs.existsSync(envFile);
if (!fileExists) {
console.error(
`❌ Missing .env.${APP_ENV} file. Make sure you have .env.${APP_ENV} file in the root directory.`,
);
throw new Error(`Missing .env.${APP_ENV} file. Check terminal for more details`);
}
const content = fs.readFileSync(envFile, 'utf8');
const envVars = content.split('\n');
const jsonObject: { [key: string]: string } = {};
envVars.forEach((envVar: string) => {
if (envVar === '') {
return;
}
const [key, value] = envVar.split('=');
jsonObject[key] = value;
});
const parsed = Environment.safeParse(jsonObject);
if (!parsed.success) {
console.error(
'❌ Invalid environment variables:',
parsed.error.flatten().fieldErrors,
`\n❌ Missing variables in .env.${APP_ENV} file, Make sure all required variables are defined in the .env.${APP_ENV} file.`,
);
throw new Error('Invalid environment variables, Check terminal for more details ');
}
})();
export default {};
import fs from 'fs';
import path from 'path';
import NativeConfig from './env';
const APP_ENV = process.env.APP_ENV ?? 'dev';
const isDev = APP_ENV === 'dev';
const envFile = path.join(__dirname, isDev ? '../.env' : `../.env.${APP_ENV}`);
(() => {
const fileExists = fs.existsSync(envFile);
if (!fileExists) {
console.error(
`❌ Missing .env.${APP_ENV} file. Make sure you have .env.${APP_ENV} file in the root directory.`,
);
throw new Error(`Missing .env.${APP_ENV} file. Check terminal for more details`);
}
// ...
})();
export default {};
// ... prev code
const content = fs.readFileSync(envFile, 'utf8');
const envVars = content.split('\n');
const jsonObject: { [key: string]: string } = {};
envVars.forEach((envVar: string) => {
if (envVar === '') {
return;
}
const [key, value] = envVar.split('=');
jsonObject[key] = value;
});
const parsed = NativeConfig.safeParse(jsonObject);
if (!parsed.success) {
console.error(
'❌ Invalid environment variables:',
parsed.error.flatten().fieldErrors,
`\n❌ Missing variables in .env.${APP_ENV} file, Make sure all required variables are defined in the .env.${APP_ENV} file.`,
);
throw new Error('Invalid environment variables, Check terminal for more details ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment