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 {
|
2020-07-30 01:43:51 +00:00
|
|
|
fn is_valid(&self, value: &Value) -> Result<(), String> {
|
2020-03-22 01:34:32 +00:00
|
|
|
if let Value::List(values) = value {
|
2020-05-27 01:42:58 +00:00
|
|
|
if values.len() < self.length as usize {
|
2020-07-30 01:43:51 +00:00
|
|
|
Err(format!(
|
2020-05-30 20:09:16 +00:00
|
|
|
"the value length is {}, must be greater than or equal to {}",
|
2020-03-22 01:34:32 +00:00
|
|
|
values.len(),
|
|
|
|
self.length
|
|
|
|
))
|
|
|
|
} else {
|
2020-07-30 01:43:51 +00:00
|
|
|
Ok(())
|
2020-03-22 01:34:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-30 01:43:51 +00:00
|
|
|
Ok(())
|
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 {
|
2020-07-30 01:43:51 +00:00
|
|
|
fn is_valid(&self, value: &Value) -> Result<(), String> {
|
2020-03-22 01:34:32 +00:00
|
|
|
if let Value::List(values) = value {
|
2020-05-27 01:42:58 +00:00
|
|
|
if values.len() > self.length as usize {
|
2020-07-30 01:43:51 +00:00
|
|
|
Err(format!(
|
2020-05-30 20:09:16 +00:00
|
|
|
"the value length is {}, must be less than or equal to {}",
|
2020-03-22 01:34:32 +00:00
|
|
|
values.len(),
|
|
|
|
self.length
|
|
|
|
))
|
|
|
|
} else {
|
2020-07-30 01:43:51 +00:00
|
|
|
Ok(())
|
2020-03-22 01:34:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-30 01:43:51 +00:00
|
|
|
Ok(())
|
2020-03-22 01:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|