Skip to content

Add fail-fast and perf improvements - #5

Merged
clbarnes merged 15 commits into
mainfrom
failfast
Nov 26, 2026
Merged

clbarnes merged 15 commits into
mainfrom
failfast

Conversation

@clbarnes

@clbarnes clbarnes commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner
  • Adds a validate_policy method which allows configuring accumulators to fail fast, or ignore low-level diagnostics
  • Improves performance ~10% by reducing allocations in the accumulator

Breaking 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.

@clbarnes

clbarnes commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner Author

Now addresses #3 and #4.

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(())
    }
}

@clbarnes
clbarnes merged commit db2646f into main Nov 26, 2026
28 of 30 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.

1 participant