Skip to content

Instantly share code, notes, and snippets.

@Keats
Created February 1, 2016 10:46
Show Gist options
  • Save Keats/32d26f699dcc13ebd41b to your computer and use it in GitHub Desktop.
Save Keats/32d26f699dcc13ebd41b to your computer and use it in GitHub Desktop.
#[derive(Deserialize)]
pub struct SignupData {
#[validate(email)]
email: String,
// this would first run min_length and then call validate_unique_username
#[validate(min_length=2, custom=validate_unique_username)]
username: String,
#[validate(min_length=8, max_length=255)]
password: String,
}
pub fn validate_unique_username(username String) -> Option<Something> {
...
}
// And to use
// first basic serialization errors
let data: SignupData = serde_json::from_str(&serialized);
// then a magic method added () -> Option<ValidationError> with ValidationError being a hashmap
let errors = data.validate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment