Giter Site home page Giter Site logo

Conditional required env? about envalid HOT 11 OPEN

af avatar af commented on July 23, 2024 6
Conditional required env?

from envalid.

Comments (11)

af avatar af commented on July 23, 2024 5

Great feature idea, I could see this being pretty useful. I worry that an approach like requiredWhen that takes a string would be a bit too restrictive though. Maybe requiredWhen could point to a function that takes the full raw env object? eg:

envalid.cleanEnv(process.env, {
  USE_TLS: envalid.bool(),
  TLS_CA_CERT_PATH: envalid.str({
    requiredWhen: rawEnv => !!rawEnv.USE_TLS
  }),
  // ...
})

from envalid.

veeramarni avatar veeramarni commented on July 23, 2024 1

Seems to be a nice feature. I think we need to send a new PR on it.

from envalid.

SimenB avatar SimenB commented on July 23, 2024

Ooooh, I like that idea! That's not supported now, but I for one would love to see a PR for it 🙂

from envalid.

neezer avatar neezer commented on July 23, 2024

What about this?

envalid.cleanEnv(process.env, {
  USE_TLS: envalid.bool(),
  TLS_CA_CERT_PATH: envalid.str({
    requiredWhen: 'USE_TLS' // is true
  }),
  // ...
})
  • Should the subject of requiredWhen always have to be a boolean? Or should this work for any truthy value?
  • Should requiredWhen allow multiple requirements?

from envalid.

neezer avatar neezer commented on July 23, 2024

Maybe requiredWhen could point to a function that takes the full raw env object?

Seems like it would probably be simpler to implement and answers both of my previous questions too, so that's a 👍 .

from envalid.

antoine-pous avatar antoine-pous commented on July 23, 2024

Any news about this awesome feature? :)

from envalid.

rainum avatar rainum commented on July 23, 2024

@antoine-pous check #81 PR

from envalid.

antoine-pous avatar antoine-pous commented on July 23, 2024

@rainum that doesn't answer to my question, actually the feature is not merged and seem inactive from months. In a professional context i can't use a commit from a PR in progress :(

from envalid.

clarkey avatar clarkey commented on July 23, 2024

This would be an awesome feature 👍

from envalid.

af avatar af commented on July 23, 2024

Something like the requiredWhen approach above could still be viable if we pass in the raw env. I'm not sure how we could do it in a type-safe way though.

Something like this is possible already though using TypeScript and the json validator, if you can deal with having the related env vars stored together in json:

import * as e from 'envalid'

// Read env vars from .env
require('dotenv').config()

type TlsJsonType = {
  USE_TLS: false,
} | {
  USE_TLS: true,
  TLS_CA_CERT_PATH: string,
  TLS_CERT_PATH: string,
  TLS_KEY_PATH: string,
}

const env = e.cleanEnv(process.env, {
  // TS type-checks that this default value works with the union type
  TLS_CONFIG: e.json<TlsJsonType>({ default: { USE_TLS: false } }),
})

if (env.TLS_CONFIG.USE_TLS) {
  // Within this guard clause, TS knows this is a string
  console.log(env.TLS_CONFIG.TLS_KEY_PATH)
}

Of course, the json() validator won't validate that particular shape at runtime, but you could create a custom validator that does that work there as well

from envalid.

dbrenot-pelmorex avatar dbrenot-pelmorex commented on July 23, 2024

bumping this issue years later. Any progress?

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.