validatrix
A lightweight validator library for rust.
Validatrix contains no built-in validators, just traits and error types for your own custom validation.
Designed for cases where:
- possibly-valid types are built up of other possibly-valid types
- there is additional schema-level validation
- data is modelled as JSON-like, where sequences are ordered and maps' keys are stringy
The Display implementation of validatrix::Error can list multiple validation errors,
pointing to the location of the errors with JSONPath-like syntax,
although implementors can choose to fail fast instead.
Usage
use ;
// Implement `validatrix::Validate` on your structs.
// valid
let valid = A ;
valid.validate.unwrap;
// all of the value fields are fizz/buzz, and therefore invalid
let invalid = A ;
let err = invalid.validate.unwrap_err;
let validation_report = format!;
assert_eq!;
// the `Valid` wrapper type enforces validity
let valid_wrapped = try_new.expect;
assert!;
// `Valid` implements AsRef, Borrow, and Deref for the contained type
There is also an asynchronous variant in the validatrix::asynch module.
See also validatrix(::asynch)::ValidateContext,
which allows passing a reference to some external data as context for the validation.
Why not
Other validation crates have focused on providing validator functions and proc macros to decorate types. I found those validators are often trivial implement yourself, the DSLs for decorating fields just look worse than regular rust code, and composing custom and built-in validators behaved in unclear ways.
JSONSchema-like validators tend not to be good at schema-level validation.
Development
Make releases using cargo-release.
Use prek to manage pre-commit hooks.
To do
use smallvec to decrease allocationsDoesn't really help- use
Cow<str>(or alternative like hipstr, ecow etc.) for messages - investigate whether RAII can be used instead of pushing and popping accumulator prefixes
Accumulatorcould have a fail-fast mode- methods would return
Results (Errif fail-fast istrue, otherwiseOk) so they can be?'d and propagate - this would cause weirdness in the
&mut selfmethods which would then need to cede their failures to the returned errors
- methods would return
- replace
StringinFailurewith generic error