Comments (11)
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.
Seems to be a nice feature. I think we need to send a new PR on it.
from envalid.
Ooooh, I like that idea! That's not supported now, but I for one would love to see a PR for it 🙂
from envalid.
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 aboolean
? Or should this work for any truthy value? - Should
requiredWhen
allow multiple requirements?
from envalid.
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.
Any news about this awesome feature? :)
from envalid.
@antoine-pous check #81 PR
from envalid.
@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.
This would be an awesome feature 👍
from envalid.
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.
bumping this issue years later. Any progress?
from envalid.
Related Issues (20)
- Help with Next.js HOT 5
- ts-expect-error problem with 7.2.1 HOT 4
- Make an optional case-insensitive flag HOT 2
- NPM package is not valid HOT 3
- Support `structuredClone` HOT 3
- How to access desc HOT 2
- Add parse options to str() HOT 1
- array validator HOT 1
- 7.3.0 breaking change? HOT 11
- Add option to load validator specs from .example.env file HOT 1
- Access value of env object by name HOT 1
- envalid in react-native for preview crashes without giving description of missing env HOT 2
- testOnly setting variable to Symbol instead of failing HOT 5
- Pick from validators HOT 2
- str({ devDefault: undefined }) generates an incorrect type HOT 2
- Require environment variables only if `NODE_ENV` is not set to `test` HOT 2
- Skipping env var validation in tests HOT 1
- Prevent process exit during tests HOT 1
- makeExactValidator is not exported HOT 1
- Combine envalid with Docker secrets
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from envalid.