Repository navigation
feat: add Validation, which accumulates errors instead of stopping - #27
Merged
Merged
Conversation
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>
16 of 19 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
control.Validation[E, T]. WhereEitherandResultstop at the first failure,Validationruns 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 signatureIsValid,IsInvalid,Get,Errors(a copy),OrElseMap[U],MapError[F],FlatMap[U](sequential, stops at the first invalid step),Fold[U]Check(value, checks...),Sequence(validations...),Zip(a, b)FromEither(coversResult),ToEither,ToOption,ToResult(errors.Join)Decisions
[]E, in the order found — no combine function, no constraint onE. Vavr ends up with aSeqof errors too.Checkcovers several checks on one value,Sequencevalidations of one type, andZippairs two of different types; zipping again combines any number, at the cost of nested pairs.Combine2…CombineNwere considered and dropped.Zipis a package function, not a method: a method returningValidation[E, Pair[T, U]]gives that type aZipof its own, without end, and the compiler rejects the instantiation cycle. Explained in the godoc and README.errors.Joindrops nil errors, soToResult(Invalid[error, T](nil))is a failedResultwith a nil cause, asErr(nil)already is — pinned by a test and documented.Bug caught by a test
Zipfirst appended to its left operand's errors in place. A validation built by accumulation has spare capacity, so twoZips from it overwrote each other.Zipnow always builds a fresh slice; the test coversCheckandSequencetoo.Performance
Valid, a passingCheck(27 ns for three checks), a validZip.Invalidallocates once, whatever the number of errors.Sequencecounts 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 byAllocsPerRuntests;BenchmarkSequencekept.Checks
gofmt,go vet,golangci-lint(0 issues),go test -race -cover ./..., the no-dependencygo listcheck, andgo mod tidyleavinggo.mod/go.sumunchanged. README gains aValidationsection backed byExampleValidationandExampleZip.This change was prepared by an AI agent and may not have been independently reviewed or tested.
🤖 Generated with Claude Code