Skip to content
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

Invalid NODE_ENV message improvement #100

Closed
Rolandisimo opened this issue Jun 26, 2019 · 5 comments
Closed

Invalid NODE_ENV message improvement #100

Rolandisimo opened this issue Jun 26, 2019 · 5 comments

Comments

@Rolandisimo
Copy link

Rolandisimo commented Jun 26, 2019

throw new EnvError(`Value "${value}" not in choices [${spec.choices}]`)

In my company we use envalid but with non-default environment values. Thus, I got my builds failing with the error above.

Currently the function validateVar in envalidWithoutDotenv.js doesn't give a comprehensive error message with potential reasons for this issue and how to fix them. I had to actually go into the source code of envalid to understand the reason why my builds are failing and how to fix it.

My suggestion is to improve the error message and add guides for the user to understand how to fix this issue. Also, I suggest to add a part about NODE_ENV in the documentation that if it's not specified in cleanEnv then it takes the value of defaultNodeEnv from the same file on line 56.

I'd be more than glad to contribute.

@af
Copy link
Owner

af commented Jul 4, 2019

Hi @Rolandisimo, what would you suggest as an improved error message here?

@cmbirk
Copy link

cmbirk commented Aug 8, 2019

I'm running into the same issue. How can you specify valid choices for non-standard environments? As an example I need to tell it jesttest is a valid NODE_ENV

@Rolandisimo
Copy link
Author

Rolandisimo commented Aug 9, 2019

@cmbirk here you can see the default values for NODE_ENV https://github.com/af/envalid#envalidcleanenvenvironment-validators-options

From the source code:

// If validation for NODE_ENV isn't specified, use the default validation:
if (!varKeys.includes('NODE_ENV')) {
  defaultNodeEnv = validateVar({
    name: 'NODE_ENV',
    spec: str({ choices: ['development', 'test', 'production'] }),
    rawValue: env.NODE_ENV || 'production'
  })
}

Because in my project we have a qa environment, I had to specify it in the choices for some of the related test to work.
My workaround in Typescript:

// declarations.d.ts
declare type NODE_ENVIRONMENTS = "development" | "production" | "qa" | "test";

// env_variables.ts
NODE_ENV: str({
  choices: ["qa", "production", "development", "test"] as NODE_ENVIRONMENTS[],
  default: "development" as NODE_ENVIRONMENTS,
}),

@Rolandisimo
Copy link
Author

@af I wasn't sure about a good approach without refactoring the code too much. So, as an intermediate step I think it's not unreasonable to add a check for the specific NODE_ENV key inside validateVar function. Afterwards a message specifying that the developer might need to specify choices for NODE_ENV when calling cleanEnv.

What do you think?

@af
Copy link
Owner

af commented Jan 25, 2021

As of v7.0.0-beta.1, this library no longer ships a default NODE_ENV validator, so you can specify your own with whichever choices you wish. Here's a test case with an example: https://github.com/af/envalid/blob/master/tests/basics.test.ts#L138

This doesn't change the messaging of course, but if I understand correctly, if fixes the root issue here. If you think the error messages could use improvement still, please open up another PR or issue for that

@af af closed this as completed Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants