Comments (8)
This is a really good point, I don't like that dotenv
changes process.env
, and I agree it'd be better if we didn't do that.
I think we can take an even simpler approach though, and maintain the existing cleanEnv()
API (I like passing in process.env
as an argument, as it's more explicit and makes for easy testing). The latest dotenv version provides a parse()
function that returns an object of env vars, rather than mutating process.env
. Inside cleanEnv()
we could do something like this:
const cleanEnv = (inputEnv, config) => {
const dotEnvVars = require('dotenv').parse(dotenvBuffer)
const fullEnv = Object.assign({}, inputEnv, dotEnvVars)
// Rest of validation logic here
}
Loading the .env file will add a bit of complexity but I think it's worth it to avoid mutating global state and keeping the API small and tidy. Thoughts?
from envalid.
That's another way to do it, although I think that the whole envalid
library would be much more reusable if it is just doing cleanup and validation. For example, what you propose wouldn't be able to handle my case:
I would like to have variables defined on the following levels ordered by highest priority:
- environment variables (process.env);
- .env file variables;
- default deploy environment variables (variables defined in code per deployment environment - live, test,etc);
- envalid spec defaults;
If cleanEnv
calls dotenv internally it doesn't allow me to compose my environment configuration. If you insist on keeping the current behavior, I can propose adding another method or an option that disables the dotenv usage.
A few more notes about your example:
- Technically, the removal of the
dotenv.config
call is a breaking change ;) - The example above has a bug because
dotEnvVars
would overwriteinputEnv
and that's not howdotenv
works - environment variables are with higher priority than variables from the .env file. You can check how it is implemented indotenv
.
from envalid.
Sorry about the bugs, I was just sketching something quickly without actually looking at the existing code :) Yes, my example is a breaking change. I think it's worth it though to prevent dumping more stuff on process.env
, and further enforcing the discipline of using a validated env object instead of accessing process.env
directly.
You bring up some good points about making the environment more composable. I'm definitely not against adding a new API that gives this kind of control, I think it's a good idea. I think I'd like to continue calling dotenv
inside cleanEnv()
though, since that's what the users of this library are already used to. I welcome suggestions on this new API!
from envalid.
Maybe just add a new option to cleanEnv
- loadDotenv ? So to call cleanEnv without loading dotenv you can do it like that:
envalid.cleanEnv(process.env, spec, { loadDotenv: false });
Sounds good?
from envalid.
@ibratoev Yep that sounds good 👍
from envalid.
@ibratoev Pushed some new commits to master for this (the next release will be 3.0 as this is a breaking change). Note that I changed the new option to dotEnvPath
for some added flexibility; you can set it to null
and dotenv processing will be skipped. I think these changes resolve this issue, let me know if otherwise.
from envalid.
👍
@af, even better that you can change the .env
path now ;)
I will send you a PR with the Typescript declarations later this week ;)
from envalid.
Sounds good, thanks!
from envalid.
Related Issues (20)
- Add parse options to str() HOT 1
- array validator HOT 1
- 7.3.0 breaking change? HOT 11
- Add option to load validator specs from .example.env file HOT 1
- Access value of env object by name HOT 1
- envalid in react-native for preview crashes without giving description of missing env HOT 2
- testOnly setting variable to Symbol instead of failing HOT 5
- Pick from validators HOT 2
- str({ devDefault: undefined }) generates an incorrect type HOT 2
- Require environment variables only if `NODE_ENV` is not set to `test` HOT 2
- Skipping env var validation in tests HOT 1
- Prevent process exit during tests HOT 1
- makeExactValidator is not exported HOT 1
- Combine envalid with Docker secrets
- Issue with envalid in test. HOT 1
- Typescript, typing the environment variables returns never HOT 1
- Waiting for an async reporter callback HOT 3
- How to see description and help for optional env? HOT 4
- Add a warn option
- devDefault does not work when NODE_ENV is undefined HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from envalid.