Merge pull request #119 from kevinschoonover/master

Add tests for Input Validators
This commit is contained in:
Sunli 2020-06-02 09:06:59 +08:00 committed by GitHub
commit 2b2f193800
7 changed files with 1594 additions and 16 deletions

View File

@ -325,6 +325,7 @@ pub enum ParseRequestError {
PayloadTooLarge, PayloadTooLarge,
} }
#[allow(missing_docs)]
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct RuleError { pub struct RuleError {
pub locations: Vec<Pos>, pub locations: Vec<Pos>,

View File

@ -146,7 +146,7 @@ pub use context::{
}; };
pub use error::{ pub use error::{
Error, ErrorExtensions, FieldError, FieldResult, InputValueError, InputValueResult, Error, ErrorExtensions, FieldError, FieldResult, InputValueError, InputValueResult,
ParseRequestError, QueryError, ResultExt, ParseRequestError, QueryError, ResultExt, RuleError,
}; };
pub use look_ahead::Lookahead; pub use look_ahead::Lookahead;
pub use parser::{Pos, Positioned, Value}; pub use parser::{Pos, Positioned, Value};

View File

@ -15,7 +15,7 @@ impl InputValueValidator for IntRange {
if let Value::Int(n) = value { if let Value::Int(n) = value {
if *n < self.min || *n > self.max { if *n < self.min || *n > self.max {
Some(format!( Some(format!(
"the value is {}, but the range must be between {} and {}", "the value is {}, must be between {} and {}",
*n, self.min, self.max *n, self.min, self.max
)) ))
} else { } else {
@ -80,7 +80,7 @@ impl InputValueValidator for IntNonZero {
fn is_valid(&self, value: &Value) -> Option<String> { fn is_valid(&self, value: &Value) -> Option<String> {
if let Value::Int(n) = value { if let Value::Int(n) = value {
if *n == 0 { if *n == 0 {
Some(format!("the value is {}, but must be nonzero", *n,)) Some(format!("the value is {}, must be nonzero", *n,))
} else { } else {
None None
} }
@ -100,7 +100,10 @@ impl InputValueValidator for IntEqual {
fn is_valid(&self, value: &Value) -> Option<String> { fn is_valid(&self, value: &Value) -> Option<String> {
if let Value::Int(n) = value { if let Value::Int(n) = value {
if *n != self.value { if *n != self.value {
Some(format!("the value is {}, must be equal {}", *n, self.value)) Some(format!(
"the value is {}, must be equal to {}",
*n, self.value
))
} else { } else {
None None
} }

View File

@ -12,7 +12,7 @@ impl InputValueValidator for ListMinLength {
if let Value::List(values) = value { if let Value::List(values) = value {
if values.len() < self.length as usize { if values.len() < self.length as usize {
Some(format!( Some(format!(
"the value length is {}, but the length must be greater than or equal to {}", "the value length is {}, must be greater than or equal to {}",
values.len(), values.len(),
self.length self.length
)) ))
@ -36,7 +36,7 @@ impl InputValueValidator for ListMaxLength {
if let Value::List(values) = value { if let Value::List(values) = value {
if values.len() > self.length as usize { if values.len() > self.length as usize {
Some(format!( Some(format!(
"the value length is {}, but the length must be less than or equal to {}", "the value length is {}, must be less than or equal to {}",
values.len(), values.len(),
self.length self.length
)) ))

View File

@ -79,7 +79,7 @@ where
B: InputValueValidator, B: InputValueValidator,
{ {
fn is_valid(&self, value: &Value) -> Option<String> { fn is_valid(&self, value: &Value) -> Option<String> {
self.0.is_valid(value).and(self.1.is_valid(value)) self.0.is_valid(value).or(self.1.is_valid(value))
} }
} }

View File

@ -14,7 +14,7 @@ impl InputValueValidator for StringMinLength {
if let Value::String(s) = value { if let Value::String(s) = value {
if s.len() < self.length as usize { if s.len() < self.length as usize {
Some(format!( Some(format!(
"the value length is {}, but the length must be greater than or equal to {}", "the value length is {}, must be greater than or equal to {}",
s.len(), s.len(),
self.length self.length
)) ))
@ -38,7 +38,7 @@ impl InputValueValidator for StringMaxLength {
if let Value::String(s) = value { if let Value::String(s) = value {
if s.len() > self.length as usize { if s.len() > self.length as usize {
Some(format!( Some(format!(
"the value length is {}, but the length must be less than or equal to {}", "the value length is {}, must be less than or equal to {}",
s.len(), s.len(),
self.length self.length
)) ))
@ -88,12 +88,12 @@ impl InputValueValidator for MAC {
if let Value::String(s) = value { if let Value::String(s) = value {
if self.colon { if self.colon {
if !MAC_ADDRESS_RE.is_match(s) { if !MAC_ADDRESS_RE.is_match(s) {
Some("invalid email format".to_string()) Some("invalid MAC format".to_string())
} else { } else {
None None
} }
} else if !MAC_ADDRESS_NO_COLON_RE.is_match(s) { } else if !MAC_ADDRESS_NO_COLON_RE.is_match(s) {
Some("invalid email format".to_string()) Some("invalid MAC format".to_string())
} else { } else {
None None
} }

File diff suppressed because it is too large Load Diff