implement string regex matcher
This commit is contained in:
parent
144f0f5eda
commit
d46287e083
|
@ -19,3 +19,4 @@ serde_derive = "1.0.80"
|
||||||
url = "1.7.1"
|
url = "1.7.1"
|
||||||
hyper = "0.12.13"
|
hyper = "0.12.13"
|
||||||
lazy_static = "1.1.0"
|
lazy_static = "1.1.0"
|
||||||
|
regex = "1.0.6"
|
||||||
|
|
|
@ -2,6 +2,7 @@ use failure::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use url::form_urlencoded;
|
use url::form_urlencoded;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
pub type PropertyMap = HashMap<&'static str, Jss>;
|
pub type PropertyMap = HashMap<&'static str, Jss>;
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ pub struct JssString {
|
||||||
pub default: Option<&'static str>,
|
pub default: Option<&'static str>,
|
||||||
pub min_length: Option<usize>,
|
pub min_length: Option<usize>,
|
||||||
pub max_length: Option<usize>,
|
pub max_length: Option<usize>,
|
||||||
|
pub pattern: Option<Regex>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -89,6 +91,7 @@ pub const DEFAULTSTRING: JssString = JssString {
|
||||||
default: None,
|
default: None,
|
||||||
min_length: None,
|
min_length: None,
|
||||||
max_length: None,
|
max_length: None,
|
||||||
|
pattern: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
@ -165,7 +168,13 @@ fn parse_simple_value(value_str: &str, schema: &Jss) -> Result<Value, Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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."),
|
_ => 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 {
|
for (name, prop_schema) in properties {
|
||||||
let optional = match prop_schema {
|
let optional = match prop_schema {
|
||||||
Jss::Boolean(jss_boolean) => jss_boolean.optional,
|
Jss::Boolean(jss_boolean) => jss_boolean.optional,
|
||||||
|
@ -298,6 +307,8 @@ fn test_query_string() {
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
|
||||||
|
// TEST min_length and max_length
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{
|
let schema = parameter!{name => ApiString!{
|
||||||
optional => false,
|
optional => false,
|
||||||
min_length => Some(5),
|
min_length => Some(5),
|
||||||
|
@ -317,8 +328,29 @@ fn test_query_string() {
|
||||||
let res = parse_query_string("name=abcdefghij", &schema, true);
|
let res = parse_query_string("name=abcdefghij", &schema, true);
|
||||||
assert!(res.is_ok());
|
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]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue