From d46287e0833175e26ed9cc27930e2628eb73e31b Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 7 Nov 2018 12:35:52 +0100 Subject: [PATCH] implement string regex matcher --- Cargo.toml | 3 ++- src/json_schema.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 50d67172..cff7da10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ serde_json = "1.0.32" serde_derive = "1.0.80" url = "1.7.1" hyper = "0.12.13" -lazy_static = "1.1.0" \ No newline at end of file +lazy_static = "1.1.0" +regex = "1.0.6" diff --git a/src/json_schema.rs b/src/json_schema.rs index a73cec24..14e07b3f 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -2,6 +2,7 @@ use failure::*; use std::collections::HashMap; use serde_json::{json, Value}; use url::form_urlencoded; +use regex::Regex; pub type PropertyMap = HashMap<&'static str, Jss>; @@ -28,6 +29,7 @@ pub struct JssString { pub default: Option<&'static str>, pub min_length: Option, pub max_length: Option, + pub pattern: Option, } #[derive(Debug)] @@ -89,6 +91,7 @@ pub const DEFAULTSTRING: JssString = JssString { default: None, min_length: None, max_length: None, + pattern: None, }; #[macro_export] @@ -165,7 +168,13 @@ fn parse_simple_value(value_str: &str, schema: &Jss) -> Result { } } - Value::String(res) + if let Some(ref regex) = jss_string.pattern { + if !regex.is_match(&res) { + bail!("value does not match the regex pattern"); + } + } + + Value::String(res) } _ => bail!("unable to parse complex (sub) objects."), }; @@ -234,7 +243,7 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss, test_ } } - if test_required { + if test_required && errors.len() == 0 { for (name, prop_schema) in properties { let optional = match prop_schema { Jss::Boolean(jss_boolean) => jss_boolean.optional, @@ -298,6 +307,8 @@ fn test_query_string() { let res = parse_query_string("", &schema, true); assert!(res.is_ok()); + // TEST min_length and max_length + let schema = parameter!{name => ApiString!{ optional => false, min_length => Some(5), @@ -317,8 +328,29 @@ fn test_query_string() { let res = parse_query_string("name=abcdefghij", &schema, true); assert!(res.is_ok()); + // TEST regex pattern + let schema = parameter!{name => ApiString!{ + optional => false, + pattern => Some(Regex::new("test").unwrap()) + }}; + let res = parse_query_string("name=abcd", &schema, true); + assert!(res.is_err()); + + let res = parse_query_string("name=ateststring", &schema, true); + assert!(res.is_ok()); + + let schema = parameter!{name => ApiString!{ + optional => false, + pattern => Some(Regex::new("^test$").unwrap()) + }}; + + let res = parse_query_string("name=ateststring", &schema, true); + assert!(res.is_err()); + + let res = parse_query_string("name=test", &schema, true); + assert!(res.is_ok()); } #[test]