Closed LeonidKrutovsky closed 4 years ago
For this structure we could generate following code (using https://github.com/Keats/validator):
#[derive(Default, PartialEq, Debug)]
pub struct Name (String);
impl Validate for Name {
fn validate(&self) -> Result<(), ValidationErrors> {
if self.0.len() > 64 {
let mut errors = ValidationErrors::new();
errors.add("Wrong name length", ValidationError::new("Name is too long"));
return Err(errors);
}
Ok(())
}
}
Code above could be tested with this:
let mut name: Name = Default::default();
name.0 = "...".to_string();
match name.validate() {
Ok(_) => println!("ok"),
Err(e) => println!("{}", e)
};
Note that
We could create a macros that generates impl Validate for us. We want something like that:
#[derive(Default, PartialEq, Debug, Validate)]
#[ValidateLengthMax = 64]
pub struct Name (String);