async-graphql/src/validators/email.rs

29 lines
829 B
Rust
Raw Normal View History

use fast_chemail::is_valid_email;
2021-11-16 08:20:40 +00:00
2022-04-19 04:25:11 +00:00
use crate::{InputType, InputValueError};
2021-11-16 08:20:40 +00:00
pub fn email<T: AsRef<str> + InputType>(value: &T) -> Result<(), InputValueError<T>> {
if is_valid_email(value.as_ref()) {
2021-11-16 08:20:40 +00:00
Ok(())
} else {
Err("invalid email".into())
2021-11-16 08:20:40 +00:00
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_email() {
assert!(email(&"joe@example.com".to_string()).is_ok());
assert!(email(&"joe.test@example.com".to_string()).is_ok());
assert!(email(&"email@example-one.com".to_string()).is_ok());
assert!(email(&"1234567890@example.com".to_string()).is_ok());
2021-11-16 08:20:40 +00:00
assert!(email(&"plainaddress".to_string()).is_err());
assert!(email(&"@example.com".to_string()).is_err());
assert!(email(&"email.example.com".to_string()).is_err());
2021-11-16 08:20:40 +00:00
}
}