2020-03-22 01:34:32 +00:00
|
|
|
use crate::validators::InputValueValidator;
|
2020-05-09 09:55:04 +00:00
|
|
|
use crate::Value;
|
2020-03-22 01:34:32 +00:00
|
|
|
|
|
|
|
/// List minimum length validator
|
|
|
|
pub struct ListMinLength {
|
|
|
|
/// Must be greater than or equal to this value.
|
2020-05-27 01:42:58 +00:00
|
|
|
pub length: i32,
|
2020-03-22 01:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputValueValidator for ListMinLength {
|
|
|
|
fn is_valid(&self, value: &Value) -> Option<String> {
|
|
|
|
if let Value::List(values) = value {
|
2020-05-27 01:42:58 +00:00
|
|
|
if values.len() < self.length as usize {
|
2020-03-22 01:34:32 +00:00
|
|
|
Some(format!(
|
2020-03-30 02:45:41 +00:00
|
|
|
"the value length is {}, but the length must be greater than or equal to {}",
|
2020-03-22 01:34:32 +00:00
|
|
|
values.len(),
|
|
|
|
self.length
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-19 02:28:52 +00:00
|
|
|
None
|
2020-03-22 01:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List maximum length validator
|
|
|
|
pub struct ListMaxLength {
|
|
|
|
/// Must be less than or equal to this value.
|
2020-05-27 01:42:58 +00:00
|
|
|
pub length: i32,
|
2020-03-22 01:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputValueValidator for ListMaxLength {
|
|
|
|
fn is_valid(&self, value: &Value) -> Option<String> {
|
|
|
|
if let Value::List(values) = value {
|
2020-05-27 01:42:58 +00:00
|
|
|
if values.len() > self.length as usize {
|
2020-03-22 01:34:32 +00:00
|
|
|
Some(format!(
|
2020-03-30 02:45:41 +00:00
|
|
|
"the value length is {}, but the length must be less than or equal to {}",
|
2020-03-22 01:34:32 +00:00
|
|
|
values.len(),
|
|
|
|
self.length
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|