Git Product home page Git Product logo

Comments (5)

af avatar af commented on May 18, 2024

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

from envalid.

cmbirk avatar cmbirk commented on May 18, 2024

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

from envalid.

Rolandisimo avatar Rolandisimo commented on May 18, 2024

@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,
}),

from envalid.

Rolandisimo avatar Rolandisimo commented on May 18, 2024

@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?

from envalid.

af avatar af commented on May 18, 2024

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

from envalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.