Skip to content

Correct-by-construction validation library for the Rust Language

Notifications You must be signed in to change notification settings

mirosval/valibuk

Repository files navigation

Valibuk

Valibuk is a library and a set of macros implementing the correct-by-construction pattern.

Correct-by-construction is a pattern that leverages the type system to guard against bugs that can come from improperly validating inputs. It does so by having an "unvalidated" type and a "validated" type. The only way of obtaining an instance of the validated type is to run all the defined validations on the unvalidated type. Then the correctness is achieved by using the correct type.

A small example

// 1. Having a T -> Result<T, E> validator
fn is_positive(i: i32) -> Result<i32, String> {
if i > 0 {
Ok(i)
} else {
Err("wrong".to_string())
}
}
// 3. Derive (1) the `unvalidated` type and a `std::convert::TryFrom` trait
#[derive(Validated)]
// 2. And a struct
struct A {
#[validator(is_positive)] // Apply the function from (1) as validator
a: i32,
}
fn main() {
let i: i32 = 1;
// 4. Construct the instance of the original type from the unvalidated version
let a = A::try_from(UnvalidatedA { a: i }).expect("valid instance");
assert_eq!(a.a, i);
}

See more examples in tests and examples.

TODO

  • Move validator registrations into macro annotations
  • Support fields without validating
  • Add UI tests using trybuild
  • Support structs with lifetime params
  • Support structs with generics
  • Support global validators (take the whole struct)
  • Add validator combinators

About

Correct-by-construction validation library for the Rust Language

Resources

Stars

Watchers

Forks

Packages

No packages published