1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use crate::validators::InputValueValidator;
use graphql_parser::schema::Value;
use once_cell::sync::Lazy;
use regex::Regex;

/// String minimum length validator
pub struct StringMinLength {
    /// Must be greater than or equal to this value.
    pub length: usize,
}

impl InputValueValidator for StringMinLength {
    fn is_valid(&self, value: &Value) -> Option<String> {
        if let Value::String(s) = value {
            if s.len() < self.length {
                Some(format!(
                    "The value length is {}, but the length must be greater than or equal to {}",
                    s.len(),
                    self.length
                ))
            } else {
                None
            }
        } else {
            None
        }
    }
}

/// String maximum length validator
pub struct StringMaxLength {
    /// Must be less than or equal to this value.
    pub length: usize,
}

impl InputValueValidator for StringMaxLength {
    fn is_valid(&self, value: &Value) -> Option<String> {
        if let Value::String(s) = value {
            if s.len() > self.length {
                Some(format!(
                    "The value length is {}, but the length must be less than or equal to {}",
                    s.len(),
                    self.length
                ))
            } else {
                None
            }
        } else {
            None
        }
    }
}

static EMAIL_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new("^(([0-9A-Za-z!#$%&'*+-/=?^_`{|}~&&[^@]]+)|(\"([0-9A-Za-z!#$%&'*+-/=?^_`{|}~ \"(),:;<>@\\[\\\\\\]]+)\"))@").unwrap()
});

/// Email validator
pub struct Email {}

impl InputValueValidator for Email {
    fn is_valid(&self, value: &Value) -> Option<String> {
        if let Value::String(s) = value {
            if !EMAIL_RE.is_match(s) {
                Some("invalid email format".to_string())
            } else {
                None
            }
        } else {
            None
        }
    }
}

static MAC_ADDRESS_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new("^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$").unwrap());
static MAC_ADDRESS_NO_COLON_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new("^[0-9a-fA-F]{12}$").unwrap());

/// MAC address validator
pub struct MAC {
    /// Must include colon.
    pub colon: bool,
}

impl InputValueValidator for MAC {
    fn is_valid(&self, value: &Value) -> Option<String> {
        if let Value::String(s) = value {
            if self.colon {
                if !MAC_ADDRESS_RE.is_match(s) {
                    Some("invalid email format".to_string())
                } else {
                    None
                }
            } else if !MAC_ADDRESS_NO_COLON_RE.is_match(s) {
                Some("invalid email format".to_string())
            } else {
                None
            }
        } else {
            None
        }
    }
}