From ab672c124eabf729c9f62c1ff304a683b8fdb430 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 24 Nov 2018 12:33:44 +0100 Subject: [PATCH] ArraySchema: add method to check array length --- src/api/schema.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/api/schema.rs b/src/api/schema.rs index e82e8b1f..a9b859ec 100644 --- a/src/api/schema.rs +++ b/src/api/schema.rs @@ -165,6 +165,23 @@ impl ArraySchema { self.max_length = Some(max_length); self } + + fn check_length(&self, length: usize) -> Result<(), Error> { + + if let Some(min_length) = self.min_length { + if length < min_length { + bail!("array must contain at least {} elements", min_length); + } + } + + if let Some(max_length) = self.max_length { + if length > max_length { + bail!("array may only contain {} elements", max_length); + } + } + + Ok(()) + } } #[derive(Debug)] @@ -345,18 +362,7 @@ fn parse_property_string(value_str: &str, schema: &Schema) -> Result bail!("unable to parse array element: {}", err), } } - - if let Some(min_length) = array_schema.min_length { - if array.len() < min_length { - bail!("array must contain at least {} elements", min_length); - } - } - - if let Some(max_length) = array_schema.max_length { - if array.len() > max_length { - bail!("array may only contain {} elements", max_length); - } - } + array_schema.check_length(array.len())?; return Ok(array.into()); } @@ -459,7 +465,7 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &ObjectSche match params[key] { Value::Array(ref mut array) => { match parse_simple_value(value, &array_schema.items) { - Ok(res) => array.push(res), + Ok(res) => array.push(res), // fixme: check_length?? Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)), } }