Skip to content

Add -validate flag generating a Validate() error method - #130

Merged
dmarkham merged 1 commit into
masterfrom
add-validate
Oct 3, 2026
Merged

dmarkham merged 1 commit into
masterfrom
add-validate

Conversation

@dmarkham

@dmarkham dmarkham commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Based on #102 by @peczenyj, itself a port of #82 by @mbyio. Rebuilt on current master.

-validate generates:

func (i Color) Validate() error {
	if !i.IsAColor() {
		return fmt.Errorf("%v does not belong to Color values", i)
	}
	return nil
}

With -typederrors the error wraps enumerrs.ErrValueInvalid, same as TString() and the unmarshal methods. Opt-in, stdlib only, no change to existing output.

Tests

  • testdata/validate.go: contiguous enum. Every declared value returns nil; below, just above, and far out of range each return an error with the exact message; calls through a Validate() error interface.
  • testdata/validateTypedErrors.go: enum with gaps. Declared values return nil and do not match the sentinel; gap, zero, negative and out-of-range values match enumerrs.ErrValueInvalid under errors.Is and name the value; an unrelated sentinel does not match.
  • Two new goldens, plain and typed. No existing golden changed.
  • Mutation check: a Validate() that always returns nil and a builder that ignores -typederrors each fail the suite.

The end-to-end harness now takes extra flags per fixture instead of one typed-errors boolean.

Validate() returns nil for a declared constant and an error naming the
value otherwise, so enum fields can be checked through a common
interface instead of the type-specific IsAT() bool.

With -typederrors the error wraps enumerrs.ErrValueInvalid, matching
TString() and the unmarshal methods.

The end-to-end harness now takes extra flags per fixture instead of a
single typed-errors boolean. Two fixtures cover declared values, gap
values, out-of-range values, calls through an interface, and errors.Is
against the sentinel.

Based on #102 by peczenyj, a port of #82 by mbyio.
@dmarkham
dmarkham merged commit 3d1ad0d into master Oct 3, 2026
3 checks passed
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.

1 participant