Created
August 10, 2023 17:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... 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; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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