With Rust you might find yourself doing something like:
```rust let mut errors = Vec::new();
if let Err(err) = hasmaxlen(profileurl, 265) { errors.push(format!("profile URL {}", err)); } if let Err(err) = isweburl(profileurl) { errors.push(format!("profile URL {}", err)); } if let Err(err) = isnotforbidden(profile_url) { errors.push(format!("profile URL {}", err)); } ```
This crate defines a validate!
macro to turn the above into:
```rust
let mut errors = Vec::new();
validate![profileurl => for err in maxlen(265), weburl(), notforbidden() { errors.push(format!("profile URL {}", err)); } ]; ```
Notice how the first expression is automatically passed as the first argument to all listed validation functions and how we no longer have to duplicate the format literal.
Since needing to validate values inside Option
s is very common,
this crate also provides a validate_opt!
macro:
rust
validate_opt![profile_url, ...];
is equivalent to
rust
if let Some(profile_url) = profile_url {
validate![profile_url, ...];
}
You can easily define your own validation functions, you just need to take care that the input is passed as the first argument.