Skip to content

docs: document that an empty env var falls back to envDefault - #428

Open
milanobrtlik wants to merge 1 commit into
caarlos0:mainfrom
milanobrtlik:docs/empty-value-vs-envdefault
Open

docs: document that an empty env var falls back to envDefault#428
milanobrtlik wants to merge 1 commit into
caarlos0:mainfrom
milanobrtlik:docs/empty-value-vs-envdefault

Conversation

@milanobrtlik

Copy link
Copy Markdown

envDefault is documented as "sets the default value for the field", which
doesn't mention that a variable set to an empty value gets the default too —
the behavior introduced in #248 and released in v7.0.0.

It is easy to miss, because neither required nor notEmpty guards against it:
required is already satisfied by the default, and notEmpty only sees the
value after the default was applied.

No behavior changes, just documentation of what already happens:

  • a note in the Caveats section, next to the unexported-fields one
  • a pointer on the envDefault bullet
  • a runnable example, so the claim is covered by go test and shows up on
    pkg.go.dev

Judging by #245 and #406, this keeps coming up.

If a field declares an envDefault, a variable set to an empty value is handled
like an unset one. Neither required nor notEmpty guards against it: required is
already satisfied by the default, and notEmpty only sees the value after the
default was applied.

Documents the existing behavior and the limitation behind it, and adds a
runnable example. No behavior changes.

Signed-off-by: Milan Obrtlík <milan@obrtlik.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant