Skip to content

fix: required flags aren't enforced when a default resolves to empty - #624

Open
james-lawrence wants to merge 2 commits into
alecthomas:masterfrom
james-lawrence:james.lawrence/ensure-required-applies-when-interpolation-resolves-to-zero
Open

fix: required flags aren't enforced when a default resolves to empty#624
james-lawrence wants to merge 2 commits into
alecthomas:masterfrom
james-lawrence:james.lawrence/ensure-required-applies-when-interpolation-resolves-to-zero

Conversation

@james-lawrence

@james-lawrence james-lawrence commented Jul 18, 2026

Copy link
Copy Markdown

Not sure how you want to handle this situation but, test cases + a potential fix:

HasDefault is computed once in hydrateTag from the raw, uninterpolated tag string, so a default like "${var}" looks non-empty even when var is unset. checkMissingFlags only checks flag.Set, and Value.Parse sets Set = true unconditionally when applying a default - so a required field whose ${var}-templated default resolves to "" was silently treated as satisfied instead of missing.

Recompute HasDefault in interpolateValue once Default is actually resolved, after both Default and Enum have been interpolated. Skip the demotion when "" is itself a declared enum member (enum:"one,two,") since that's a legitimate, deliberately-chosen value rather than a missing one.

HasDefault is computed once in hydrateTag from the raw, uninterpolated
tag string, so a default like "${var}" looks non-empty even when var
is unset. checkMissingFlags only checks flag.Set, and Value.Parse sets
Set = true unconditionally when applying a default - so a required
field whose ${var}-templated default resolves to "" was silently
treated as satisfied instead of missing.

Recompute HasDefault in interpolateValue once Default is actually
resolved, after both Default and Enum have been interpolated. Skip
the demotion when "" is itself a declared enum member (enum:"one,two,")
since that's a legitimate, deliberately-chosen value rather than a
missing one.

@alecthomas alecthomas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one requested change!

Comment thread kong_test.go Outdated
// confusing "bool value must be true, 1, ..." error instead of a plain
// "required" one).
func TestRequiredFlagWithDefault(t *testing.T) {
// assert.Contains(err.Error(), "missing flags") rather than a bare

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can ytou strip all these verbose comments out please?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants