Skip to content

feat: add Validation, which accumulates errors instead of stopping - #27

Merged
glours merged 1 commit into
mainfrom
validation
Oct 2, 2026
Merged

glours merged 1 commit into
mainfrom
validation

Conversation

@glours

@glours glours commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Adds control.Validation[E, T]. Where Either and Result stop at the first failure, Validation runs every check and reports all the problems at once. Closes #2, supersedes the 2021 draft #10.

API

  • Valid, Invalid(err, more...) — at least one error guaranteed by the signature
  • IsValid, IsInvalid, Get, Errors (a copy), OrElse
  • Map[U], MapError[F], FlatMap[U] (sequential, stops at the first invalid step), Fold[U]
  • Accumulation: Check(value, checks...), Sequence(validations...), Zip(a, b)
  • Conversions: FromEither (covers Result), ToEither, ToOption, ToResult (errors.Join)

Decisions

  • Errors are a []E, in the order found — no combine function, no constraint on E. Vavr ends up with a Seq of errors too.
  • The zero value is valid: no error collected means nothing was found wrong.
  • No arity ceiling. Check covers several checks on one value, Sequence validations of one type, and Zip pairs two of different types; zipping again combines any number, at the cost of nested pairs. Combine2…CombineN were considered and dropped.
  • Zip is a package function, not a method: a method returning Validation[E, Pair[T, U]] gives that type a Zip of its own, without end, and the compiler rejects the instantiation cycle. Explained in the godoc and README.
  • errors.Join drops nil errors, so ToResult(Invalid[error, T](nil)) is a failed Result with a nil cause, as Err(nil) already is — pinned by a test and documented.

Bug caught by a test

Zip first appended to its left operand's errors in place. A validation built by accumulation has spare capacity, so two Zips from it overwrote each other. Zip now always builds a fresh slice; the test covers Check and Sequence too.

Performance

  • The valid path allocates nothing: Valid, a passing Check (27 ns for three checks), a valid Zip.
  • Invalid allocates once, whatever the number of errors.
  • Sequence counts errors first and allocates once at the exact size: 1000 failures went from 43 KB / 12 allocs / 5.6 µs to 16 KB / 1 alloc / 3.1 µs. Pinned by AllocsPerRun tests; BenchmarkSequence kept.

Checks

gofmt, go vet, golangci-lint (0 issues), go test -race -cover ./..., the no-dependency go list check, and go mod tidy leaving go.mod/go.sum unchanged. README gains a Validation section backed by ExampleValidation and ExampleZip.


This change was prepared by an AI agent and may not have been independently reviewed or tested.

🤖 Generated with Claude Code

Either and Result short-circuit: the first failure wins. Validation runs every
check it is given and reports all the problems at once, which is what a form,
a config file or a request body needs.

Errors are kept as a []E, in the order they were found, rather than combined
through a function or a constraint on E: it is what Vavr ends up with too, and
it asks nothing of the error type. The zero value is valid, since no error
collected means nothing was found wrong.

Accumulation has no arity ceiling. Check runs several checks on one value,
Sequence gathers validations of one type, and Zip pairs two of different
types; zipping again combines any number, at the cost of nesting the pairs.
Zip is a function, not a method: a method returning Validation[E, Pair[T, U]]
would give that type a Zip of its own, without end, and the compiler rejects
the instantiation cycle.

A test caught Zip appending to the errors of its first operand in place: a
validation built by accumulation has spare capacity, so two Zips from it
overwrote each other. Zip now always builds a fresh slice.

The valid path allocates nothing: Valid, a passing Check and a valid Zip are
free. Invalid builds its errors in one allocation, and Sequence counts the
errors first so that it allocates once, at the exact size, whichever way it
goes: a thousand failures now take 16 KB in one allocation instead of 43 KB
in twelve. Both are pinned by allocation tests.

ToResult joins the errors with errors.Join so errors.Is still reaches every
cause, and FromEither converts a Result as well as any Either.

Closes #2

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
@glours glours mentioned this pull request Oct 2, 2026
16 of 19 tasks
@glours
glours merged commit aa8d2b7 into main Oct 2, 2026
5 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.

Add Validation type in control package

1 participant