preflight

Crates.iopreflight
lib.rspreflight
version0.1.3
sourcesrc
created_at2021-01-21 00:17:08.396581
updated_at2021-01-23 10:48:26.62396
descriptionA macro to pipe user input through multiple validation functions and iterate over the resulting errors.
homepage
repositoryhttps://git.push-f.com/preflight
max_upload_size
id344674
size8,011
Martin Fischer (not-my-profile)

documentation

README

preflight

The validate! macro validates a given input with a given set of validation functions and lets you conveniently iterate over the errors. For example:

#[macro_use] extern crate preflight;
use preflight::validators::{max_len, url_with_scheme};

let mut errors = Vec::new();

validate![profile_url
    => for err in max_len(265), url_with_scheme(&["http", "https"]) {
        errors.push(format!("profile URL {}", err));
    }
];

In the above example the validate! macro expands to:

if let Err(err) = max_len(profile_url, 265) {
    errors.push(format!("profile URL {}", err));
}
if let Err(err) = url_with_scheme(profile_url, &["http", "https"]) {
    errors.push(format!("profile URL {}", err));
}

Notice how the first expression is automatically passed as the first argument to all listed validation functions.

Since needing to validate values inside Options is very common, this crate also provides a validate_opt! macro:

validate_opt![profile_url, ...];

is equivalent to

if let Some(profile_url) = profile_url {
    validate![profile_url, ...];
}

You can easily define your own validation functions, you just need to return a Result and take the input as the first argument.

Commit count: 0

cargo fmt