async_graphql/validators/
url.rs1use std::str::FromStr;
2
3use crate::{InputType, InputValueError};
4
5pub fn url<T: AsRef<str> + InputType>(value: &T) -> Result<(), InputValueError<T>> {
6 if let Ok(true) = http::uri::Uri::from_str(value.as_ref())
7 .map(|uri| uri.scheme().is_some() && uri.authority().is_some())
8 {
9 Ok(())
10 } else {
11 Err("invalid url".into())
12 }
13}
14
15#[cfg(test)]
16mod tests {
17 use super::*;
18
19 #[test]
20 fn test_url() {
21 assert!(url(&"http".to_string()).is_err());
22 assert!(url(&"https://google.com".to_string()).is_ok());
23 assert!(url(&"http://localhost:80".to_string()).is_ok());
24 assert!(url(&"ftp://localhost:80".to_string()).is_ok());
25 }
26}