endors

Crates.ioendors
lib.rsendors
version0.1.0
sourcesrc
created_at2024-10-24 20:45:19.972841
updated_at2024-10-24 20:45:19.972841
descriptionValidation library
homepagehttps://github.com/jusexton/endors/
repositoryhttps://github.com/jusexton/endors/
max_upload_size
id1421772
size13,204
Justin Sexton (jusexton)

documentation

README

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())
Commit count: 5

cargo fmt