functional, fluent and extensible validation toolkit for Go.
This library is under active development, feature PRs are welcomed
When validating large objects with sophisticated business logic and intertwined value dependencies, imperative programming often makes it hard to tell the actual logic underneath, thus making it hard to maintain. To add salt to injury, Go's verbose error handling makes it even harder to maintain.
This project aims at providing a validation API that is almost fluent in natural language, composable to represent sophisticated logic, and extensible wherever the defaults fall short.
In addition, with the assumption that the validation code already knows how the validated value should be like, this library would try to stay away from reflection as much as possible, while having a minimal API surface area.
go get -u github.com/imulab/check
The library is designed with minimal API surface, it has only a few concepts:
check.Step
is the core concept, it takes in anyinterface{}
value with assumed type, and applies validation to it. It returns an error when validation fails.check.Skip
is the special error to be returned bycheck.Step
, to skip all remaining steps.check.That
is the main entrypoint for validation, it accepts multiplecheck.Step
to execute sequentially.check.AnyErr
can chain multiplecheck.That
together to eagerly return any error.
// Check str variable is not empty.
check.That(str, stringz.IsNotEmpty)
// Check str is "a" or "b", as long as it is not empty. If str is
// not "a" and "b", return a customErr.
check.That(str,
check.Optional.When(stringz.IsEmpty),
stringz.In("a", "b").Err(customErr),
)
// Check all elements of the string slice is "a", "b", or "c", or
// return a customErr.
check.That(slice,
slicez.OfString.All(stringz.In("a", "b", "c")),
).Err(customErr)