caarlos0 / caarlos0/env

Option to require env vars to be set and not empty by default

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

Description

It looks like there is no way to require env vars to be set and not empty by default?

https://go.dev/play/p/fiRtVlFW3if
```
package main

import (
"fmt"

"github.com/caarlos0/env/v10"
)

func main() {
type Config struct {
Username string `env:"EX_USERNAME" envDefault:"admin"`
Password string `env:"EX_PASSWORD"`
}

var cfg Config
if err := env.ParseWithOptions(&cfg, env.Options{
Environment: map[string]string{
"EX_USERNAME": "https://google.com",
"EX_PASSWORD": "",
},
RequiredIfNoDef: true,
}); err != nil {
fmt.Println(err)
}

fmt.Printf("%+v\n", cfg)
}
```

Results in
```
{Username:https://google.com Password:}

Program exited.
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with ParseWithOptions and the RequiredIfNoDef option, using the example in the issue as the reproduction case. Determine how an explicitly empty EX_PASSWORD is handled when required variables have no defaults. Done means the parser can require such values by default and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.