1 unstable release
0.1.0 | Oct 24, 2024 |
---|
#4 in #len
10KB
239 lines
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())
Dependencies
~245–700KB
~16K SLoC