async_graphql/validators/
ip.rs1use std::{net::IpAddr, str::FromStr};
2
3use crate::{InputType, InputValueError};
4
5pub fn ip<T: AsRef<str> + InputType>(value: &T) -> Result<(), InputValueError<T>> {
6 if IpAddr::from_str(value.as_ref()).is_ok() {
7 Ok(())
8 } else {
9 Err("invalid ip".into())
10 }
11}
12
13#[cfg(test)]
14mod tests {
15 use super::*;
16
17 #[test]
18 fn test_ip() {
19 assert!(ip(&"1.1.1.1".to_string()).is_ok());
20 assert!(ip(&"255.0.0.0".to_string()).is_ok());
21 assert!(ip(&"256.1.1.1".to_string()).is_err());
22 assert!(ip(&"fe80::223:6cff:fe8a:2e8a".to_string()).is_ok());
23 assert!(ip(&"::ffff:254.42.16.14".to_string()).is_ok());
24 assert!(ip(&"2a02::223:6cff :fe8a:2e8a".to_string()).is_err());
25 }
26}