async_graphql/validators/
email.rs

1use fast_chemail::is_valid_email;
2
3use crate::{InputType, InputValueError};
4
5pub fn email<T: AsRef<str> + InputType>(value: &T) -> Result<(), InputValueError<T>> {
6    if is_valid_email(value.as_ref()) {
7        Ok(())
8    } else {
9        Err("invalid email".into())
10    }
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16
17    #[test]
18    fn test_email() {
19        assert!(email(&"joe@example.com".to_string()).is_ok());
20        assert!(email(&"joe.test@example.com".to_string()).is_ok());
21        assert!(email(&"email@example-one.com".to_string()).is_ok());
22        assert!(email(&"1234567890@example.com".to_string()).is_ok());
23
24        assert!(email(&"plainaddress".to_string()).is_err());
25        assert!(email(&"@example.com".to_string()).is_err());
26        assert!(email(&"email.example.com".to_string()).is_err());
27    }
28}