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,
}
#[allow(missing_docs)]
#[derive(Debug, PartialEq)]
pub struct RuleError {
pub locations: Vec<Pos>,

View File

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

View File

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

View File

@ -12,7 +12,7 @@ impl InputValueValidator for ListMinLength {
if let Value::List(values) = value {
if values.len() < self.length as usize {
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(),
self.length
))
@ -36,7 +36,7 @@ impl InputValueValidator for ListMaxLength {
if let Value::List(values) = value {
if values.len() > self.length as usize {
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(),
self.length
))

View File

@ -79,7 +79,7 @@ where
B: InputValueValidator,
{
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 s.len() < self.length as usize {
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(),
self.length
))
@ -38,7 +38,7 @@ impl InputValueValidator for StringMaxLength {
if let Value::String(s) = value {
if s.len() > self.length as usize {
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(),
self.length
))
@ -88,12 +88,12 @@ impl InputValueValidator for MAC {
if let Value::String(s) = value {
if self.colon {
if !MAC_ADDRESS_RE.is_match(s) {
Some("invalid email format".to_string())
Some("invalid MAC format".to_string())
} else {
None
}
} else if !MAC_ADDRESS_NO_COLON_RE.is_match(s) {
Some("invalid email format".to_string())
Some("invalid MAC format".to_string())
} else {
None
}

File diff suppressed because it is too large Load Diff