-
Notifications
You must be signed in to change notification settings - Fork 65
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
Contradictory behaviour when NODE_ENV is undefined and no validation rule set #65
Comments
I agree Does that make sense? Would love to se a PR for it! |
Agreed, this is contradictory, thanks for pointing it out. The original intent was for NODE_ENV validation to be "baked in", and if you ever forgot to set the env var, IMO the fix here is that in this case |
I think this is the key: "actual node env should then be used through out" combined with using a default So something like: function cleanEnv(inputEnv, specs, options = {}) {
let output = {}
const errors = {}
// If validation for NODE_ENV isn't specified, use the default validation:
specs = Object.assign(
{
NODE_ENV: validateVar({
name: 'NODE_ENV',
spec: str({ choices: ['development', 'test', 'production'] }),
rawValue: env.NODE_ENV || 'production'
})
},
specs
)
const env =
options.dotEnvPath !== null ? extendWithDotEnv(inputEnv, options.dotEnvPath) : inputEnv
const varKeys = Object.keys(specs)
// the rest... Then everything should work correctly, right? |
Yes that looks like it should do the trick |
Just ran into this confusion myself refactoring #81. @SimenB 's code is on the right track, but won't work as-is: the keys in I like the intent behind this feature, but honestly I'm wary of its auto-magical nature. There's nothing on NodeJS docs about According to a Nodejitsu engineer from back in the day:
This leads me to wanting to treat it like any other Since there's no official spec about what its values should or shouldn't be (just convention)—or what will or won't happen if its not set (depends on your code and packages installed)—I think its probably safer, easier, and simpler to leave this definition in user-land. My two cents. |
Fiiiiinally fixed this, Also:
This will be the case in v7, NODE_ENV is just like any other env var and you need to add a validator for it if you want it to be validated. |
@af I have a small question about this fix: Shouldn't devDefault also work when I set |
@MikeYermolayev here's the current logic for when devDefault is used. So in the case above, if NODE_ENV is not set to
|
@af well looks like it's not true. I assume |
If you define validation for NODE_ENV and it is undefined then due to [1]
isProduction
will return true andisDev
returns false.However due to [2] dev defaults will be used. This behaviour is contradictory and confusing.
I'd submit a PR but I'm not 100% on what the fix would be, I think possibly that:
[1] https://github.com/af/envalid/blob/master/index.js#L79
[2] https://github.com/af/envalid/blob/master/index.js#L89
The text was updated successfully, but these errors were encountered: