endors 0.1.0

Validation library
Documentation
  • Coverage
  • 45.45%
    15 out of 33 items documented0 out of 24 items with examples
  • Size
  • Source code size: 14.31 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.21 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • jusexton/endors
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jusexton

Endors

Validation framework written in the rust programming language.

Quick Look

struct User {
    first_name: String,
    last_name: String,
    phone_number: Option<String>
}

struct UserValidator;
impl Validator<&User> for UserValidator {
    fn validate(&self, value: &User) -> Result<(), endors::Error> {
        // Perform many validations and collect all the results into a single result.
        collect_results!(
            validate!(value.first_name, Len { min: 1, max: 100 }), // Uses default len error message.
            validate!(
                value.first_name, 
                |s: &str| s.len() % 2 == 0 => "First name must have an even number of characters." // Custom error message
            ),
            validate!(
                value.last_name, 
                NotEqual(value.first_name) => "Last name must not equal first name."
            ),
            validate!(
                value.phone_number, 
                IsSome => "Phone number must be provided."
            ),
        )

        // OR

        // Question mark operator to fail fast and only return the first error that occurs.
        validate!(value.first_name, Len { min: 1, max: 100 })?;
        validate!(value.first_name, |s: &str| s.len() % 2 == 0 => "First name must have an even number of characters.")?;
        validate!(value.last_name, NotEqual(value.first_name) => "Last name must not equal first name.")?;
        validate!(value.phone_number, IsSome => "Phone number must be provided."?;
    }
}

let result = UserValidator.validate(&User { 
    first_name: "John".to_string(), 
    last_name: "Doe".to_string(),
    phone_number: Some("123-456-7890".to_string())
});
assert!(result.is_ok())