/src/validators/string_validators.rs

https://github.com/sunli829/async-graphql · Rust · 153 lines · 127 code · 15 blank · 11 comment · 34 complexity · aaf75d4bba230413536ee232b666a877 MD5 · raw file

  1. use once_cell::sync::Lazy;
  2. use regex::Regex;
  3. use crate::validators::InputValueValidator;
  4. use crate::Value;
  5. /// String minimum length validator
  6. pub struct StringMinLength {
  7. /// Must be greater than or equal to this value.
  8. pub length: i32,
  9. }
  10. impl InputValueValidator for StringMinLength {
  11. fn is_valid(&self, value: &Value) -> Result<(), String> {
  12. if let Value::String(s) = value {
  13. if s.len() < self.length as usize {
  14. Err(format!(
  15. "the value length is {}, must be greater than or equal to {}",
  16. s.len(),
  17. self.length
  18. ))
  19. } else {
  20. Ok(())
  21. }
  22. } else {
  23. Ok(())
  24. }
  25. }
  26. }
  27. /// String maximum length validator
  28. pub struct StringMaxLength {
  29. /// Must be less than or equal to this value.
  30. pub length: i32,
  31. }
  32. impl InputValueValidator for StringMaxLength {
  33. fn is_valid(&self, value: &Value) -> Result<(), String> {
  34. if let Value::String(s) = value {
  35. if s.len() > self.length as usize {
  36. Err(format!(
  37. "the value length is {}, must be less than or equal to {}",
  38. s.len(),
  39. self.length
  40. ))
  41. } else {
  42. Ok(())
  43. }
  44. } else {
  45. Ok(())
  46. }
  47. }
  48. }
  49. /// Chars in string minimum length validator.
  50. pub struct CharsMinLength {
  51. /// Must be greater than or equal to this value.
  52. pub length: i32,
  53. }
  54. impl InputValueValidator for CharsMinLength {
  55. fn is_valid(&self, value: &Value) -> Result<(), String> {
  56. if let Value::String(s) = value {
  57. if s.chars().count() < self.length as usize {
  58. Err(format!(
  59. "the value chars count is {}, must be greater than or equal to {}",
  60. s.chars().count(),
  61. self.length
  62. ))
  63. } else {
  64. Ok(())
  65. }
  66. } else {
  67. Ok(())
  68. }
  69. }
  70. }
  71. /// Chars in string maximum length validator.
  72. pub struct CharsMaxLength {
  73. /// Must be less than or equal to this value.
  74. pub length: i32,
  75. }
  76. impl InputValueValidator for CharsMaxLength {
  77. fn is_valid(&self, value: &Value) -> Result<(), String> {
  78. if let Value::String(s) = value {
  79. if s.chars().count() > self.length as usize {
  80. Err(format!(
  81. "the value chars count is {}, must be less than or equal to {}",
  82. s.chars().count(),
  83. self.length
  84. ))
  85. } else {
  86. Ok(())
  87. }
  88. } else {
  89. Ok(())
  90. }
  91. }
  92. }
  93. static EMAIL_RE: Lazy<Regex> = Lazy::new(|| {
  94. Regex::new("^(([0-9A-Za-z!#$%&'*+-/=?^_`{|}~&&[^@]]+)|(\"([0-9A-Za-z!#$%&'*+-/=?^_`{|}~ \"(),:;<>@\\[\\\\\\]]+)\"))@").unwrap()
  95. });
  96. /// Email validator
  97. pub struct Email {}
  98. impl InputValueValidator for Email {
  99. fn is_valid(&self, value: &Value) -> Result<(), String> {
  100. if let Value::String(s) = value {
  101. if !EMAIL_RE.is_match(s) {
  102. Err("invalid email format".to_string())
  103. } else {
  104. Ok(())
  105. }
  106. } else {
  107. Ok(())
  108. }
  109. }
  110. }
  111. static MAC_ADDRESS_RE: Lazy<Regex> =
  112. Lazy::new(|| Regex::new("^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$").unwrap());
  113. static MAC_ADDRESS_NO_COLON_RE: Lazy<Regex> =
  114. Lazy::new(|| Regex::new("^[0-9a-fA-F]{12}$").unwrap());
  115. /// MAC address validator
  116. pub struct MAC {
  117. /// Must include colon.
  118. pub colon: bool,
  119. }
  120. impl InputValueValidator for MAC {
  121. fn is_valid(&self, value: &Value) -> Result<(), String> {
  122. if let Value::String(s) = value {
  123. if self.colon {
  124. if !MAC_ADDRESS_RE.is_match(s) {
  125. Err("invalid MAC format".to_string())
  126. } else {
  127. Ok(())
  128. }
  129. } else if !MAC_ADDRESS_NO_COLON_RE.is_match(s) {
  130. Err("invalid MAC format".to_string())
  131. } else {
  132. Ok(())
  133. }
  134. } else {
  135. Ok(())
  136. }
  137. }
  138. }