-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Safe getter. #43
Comments
If you misspell the key in your config, you're going to get a validation error by default, right? Unless the env var is also misspelled, which seems unlikely. As for the second part, check out the |
Currently a validation error will only be thrown if the variable is mispelled inside the
Absolutely, I retract the second part entirely. |
Ah I see, I misunderstood the question. Typos in the app could definitely be a source of errors and confusion. I'm not sure about baking a solution into envalid at this time, but a good approach if you want to throw would be to wrap the cleaned env object in an ES6 proxy. Then you can throw an error (using the |
Yep, it's hard to make typos in It's so easy to implement that I debated whether to open up an issue. But I figured that it could help other people from making the mistakes I did, I also thought that a library that dealt with validating the environment could also validate references to it. I'll look into the proxies, I'm currently doing this on all of my projects: // config.js
const envalid = require('envalid');
const { str, email, bool, num, url } = envalid;
const env = envalid.cleanEnv(
process.env,
{
// ... Declare config variables.
}
);
function get(key) {
if (env.hasOwnProperty(key)) {
return env[key];
}
throw new Error('Missing config key: ' + key);
}
module.exports = { get };
// Elsewhere...
const Config = require('./config');
getApiResponse(Config.get('API_ERL')); // Throws, should be URL Anyways, thanks for the great libary and fast responses 👍 |
Yeah it's a very valid point. I'll mull it over for a bit and consider adding a proxy wrapper by default. Thanks for the feedback! |
We use a proxy at work as well. function makeImmutable (mutable) {
return new Proxy(mutable, {
get (target, name) {
// These two checks are needed because calling console.log on a
// proxy that throws crashes the entire process. This whitelists
// the necessary properties for `console.log(config)` to work.
if (['inspect', Symbol.toStringTag].includes(name)) {
return mutable[name];
}
if (name.toString() === 'Symbol(util.inspect.custom)') {
return mutable[name];
}
const val = mutable[name];
if (val === undefined) {
throw new Error(`Config key not found: ${name}`);
} else {
return val;
}
},
set (name) {
throw new Error(`Config values can not be changed: ${name}`);
},
});
} (should probably use |
Cool, thanks for the example @SimenB. Any other gotchas you're aware of that would preclude including a proxy wrapper by default when the |
Nah. We've been using that code in production for close to a year without issues |
While envalid is great for validating and parsing the variables from the environment, It's still possible to be affected by typos:
It would be great to have a safeguard against this type of problem built into the library.
The impementation can be as trivial as:
Also possible are more fancy get functions. I have used something like this in the past:
The text was updated successfully, but these errors were encountered: