Failed to validate email address with macro #[validate(email)]
d2jvkpn opened this issue · 2 comments
d2jvkpn commented
**Reproducing:
/* Cargo.toml::dependencies
serde = { version = "1", features = ["derive"] }
serde_json = "1"
validator = { version = "0.17", features = ["derive"] }
*/
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct Account {
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(email)]
email: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn t_validation() {
let json_string = r#"{"email": "hello"}"#;
assert!(serde_json::from_str::<Account>(json_string).is_err());
}
}
Keats commented
You need to call the .validate()
method as shown in the first code snippet of the README. We don't hook in serde
d2jvkpn commented
Thank you!