Repository navigation
Conversation
Owner
Author
|
Someone wanting to check whether their data is valid would do something like use validatrix::{Validate, Valid, Validator, Policy, Level};
let my_data = // ...
// as before
my_data.validate().unwrap();
// returns on the first error
my_data.validate_policy(Policy::FAST).unwrap();
// treats warnings like errors
my_data.validate_policy(Policy::STRICT).unwrap();
// Even if validation is successful, we can get all the non-error diagnostics
let details = my_data.validate_policy(Policy::DETAILED).unwrap();
for d in details.iter() {
println!("{d}");
}
// Wrap the data in a newtype so the type system guarantees validity
let (validated, details) = Valid::try_new(my_data).unwrap();
// Re-use the same `levels` config
let (validated, details) = Validator::new(Policy::STRICT).validate(my_data).unwrap();An implementor would do use validatrix::{Validate, Level, DiagnosticError, Accumulator};
impl Validate for MyStruct {
fn validate_inner(&self, &mut accum: Accumulator) -> Result<(), DiagnosticError> {
if self.field_0 > 5 {
// this may ignore the message,
// store it to be raised at the end of the root `validate` (returning Ok(()) here,
// or raise immediately based on the accumulator's policy
accum.diagnose_at("field_0", Level::INFO, "field_2 is over 5")?;
}
if self.field_1 > 5 {
accum.with_key("field_1", |a| a.diagnose(Level::WARNING, "field_1 is over 5"))?;
}
if self.field_2 > 5 {
accum.diagnose_at("field_2", Level::ERROR, "field_2 is over 5")?;
}
if self.field_3 > 5 {
accum.diagnose_at("field_3", Level::CRITICAL, "field_3 is over 5")?;
}
Ok(())
}
} |
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.
validate_policymethod which allows configuring accumulators to fail fast, or ignore low-level diagnosticsBreaking change: all the methods now return Results so users need to scatter
?all over their code, but that should be it.Deserialising into
Valid<T>uses the fast path.