caarlos0 / caarlos0/env

Feature Request: Conditional env validation (envEnableIf)

Open
#413 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
6.3k
Forks
284
Avg merge
1d 17h
Merged PRs (30d)
1

Description

Currently it is not possible to conditionally require environment variables based on another environment variable.

A feature like `envEnableIf` would be very helpful for cases where some configuration is only needed when a feature is enabled.

For example, assume a server supports email sending:

```go
type Config struct {
EmailEnabled bool `env:"EMAIL_ENABLED"`
SMTPHost string `env:"SMTP_HOST" envEnableIf:"EMAIL_ENABLED"`
SMTPPort int `env:"SMTP_PORT" envEnableIf:"EMAIL_ENABLED"`
}
```

Expected behavior:

* If `EMAIL_ENABLED=false`, SMTP variables are ignored.
* If `EMAIL_ENABLED=true`, `SMTP_HOST`, `SMTP_PORT`, etc. should be required and validated.

This helps validate related configuration only when the feature is enabled, instead of requiring those variables all the time.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.