2018-11-05 14:20:27 +00:00
|
|
|
use failure::*;
|
2018-11-03 14:10:21 +00:00
|
|
|
use std::collections::HashMap;
|
2018-11-05 14:20:27 +00:00
|
|
|
use serde_json::{json, Value};
|
2018-11-06 12:58:05 +00:00
|
|
|
use url::form_urlencoded;
|
2018-11-07 11:35:52 +00:00
|
|
|
use regex::Regex;
|
2018-11-10 09:00:48 +00:00
|
|
|
use std::fmt;
|
2018-11-20 15:54:29 +00:00
|
|
|
use std::sync::Arc;
|
2018-11-01 12:05:45 +00:00
|
|
|
|
2018-11-15 15:56:28 +00:00
|
|
|
pub type PropertyMap = HashMap<&'static str, Schema>;
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2018-11-10 09:00:48 +00:00
|
|
|
#[derive(Debug, Fail)]
|
|
|
|
pub struct ParameterError {
|
|
|
|
error_list: Vec<Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParameterError {
|
|
|
|
|
2018-11-17 08:57:26 +00:00
|
|
|
pub fn new() -> Self {
|
2018-11-10 09:00:48 +00:00
|
|
|
Self { error_list: vec![] }
|
|
|
|
}
|
|
|
|
|
2018-11-17 08:57:26 +00:00
|
|
|
pub fn push(&mut self, value: Error) {
|
2018-11-10 09:00:48 +00:00
|
|
|
self.error_list.push(value);
|
|
|
|
}
|
|
|
|
|
2018-11-17 08:57:26 +00:00
|
|
|
pub fn len(&self) -> usize {
|
2018-11-10 09:00:48 +00:00
|
|
|
self.error_list.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ParameterError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let msg = self.error_list.iter().fold(String::from(""), |acc, item| {
|
|
|
|
acc + &item.to_string() + "\n"
|
|
|
|
});
|
|
|
|
|
|
|
|
write!(f, "{}", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 09:42:14 +00:00
|
|
|
#[derive(Debug)]
|
2018-11-15 15:56:28 +00:00
|
|
|
pub struct BooleanSchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
pub description: &'static str,
|
2018-11-06 12:10:10 +00:00
|
|
|
pub optional: bool,
|
2018-10-31 09:42:14 +00:00
|
|
|
pub default: Option<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-11-15 15:56:28 +00:00
|
|
|
pub struct IntegerSchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
pub description: &'static str,
|
2018-11-06 12:10:10 +00:00
|
|
|
pub optional: bool,
|
|
|
|
pub minimum: Option<isize>,
|
|
|
|
pub maximum: Option<isize>,
|
|
|
|
pub default: Option<isize>,
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-11-15 15:56:28 +00:00
|
|
|
pub struct StringSchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
pub description: &'static str,
|
2018-11-06 12:10:10 +00:00
|
|
|
pub optional: bool,
|
2018-11-03 14:10:21 +00:00
|
|
|
pub default: Option<&'static str>,
|
2018-10-31 09:42:14 +00:00
|
|
|
pub min_length: Option<usize>,
|
|
|
|
pub max_length: Option<usize>,
|
2018-11-22 10:23:49 +00:00
|
|
|
pub format: Option<Arc<ApiStringFormat>>,
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-11-15 15:56:28 +00:00
|
|
|
pub struct ArraySchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
pub description: &'static str,
|
2018-11-06 12:10:10 +00:00
|
|
|
pub optional: bool,
|
2018-11-20 15:54:29 +00:00
|
|
|
pub items: Arc<Schema>,
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-11-15 15:56:28 +00:00
|
|
|
pub struct ObjectSchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
pub description: &'static str,
|
2018-11-06 12:10:10 +00:00
|
|
|
pub optional: bool,
|
|
|
|
pub additional_properties: bool,
|
2018-11-20 15:54:29 +00:00
|
|
|
pub properties: HashMap<&'static str, Arc<Schema>>,
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-11-15 15:56:28 +00:00
|
|
|
pub enum Schema {
|
2018-10-31 09:42:14 +00:00
|
|
|
Null,
|
2018-11-15 15:56:28 +00:00
|
|
|
Boolean(BooleanSchema),
|
|
|
|
Integer(IntegerSchema),
|
|
|
|
String(StringSchema),
|
|
|
|
Object(ObjectSchema),
|
|
|
|
Array(ArraySchema),
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:56:28 +00:00
|
|
|
pub const DEFAULTBOOL: BooleanSchema = BooleanSchema {
|
2018-10-31 09:42:14 +00:00
|
|
|
description: "",
|
2018-11-06 12:10:10 +00:00
|
|
|
optional: false,
|
2018-10-31 09:42:14 +00:00
|
|
|
default: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! Boolean {
|
|
|
|
($($name:ident => $e:expr),*) => {{
|
2018-11-15 15:56:28 +00:00
|
|
|
Schema::Boolean(BooleanSchema { $($name: $e, )* ..DEFAULTBOOL})
|
2018-10-31 09:42:14 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:56:28 +00:00
|
|
|
pub const DEFAULTINTEGER: IntegerSchema = IntegerSchema {
|
2018-10-31 09:42:14 +00:00
|
|
|
description: "",
|
2018-11-06 12:10:10 +00:00
|
|
|
optional: false,
|
2018-10-31 09:42:14 +00:00
|
|
|
default: None,
|
|
|
|
minimum: None,
|
|
|
|
maximum: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! Integer {
|
|
|
|
($($name:ident => $e:expr),*) => {{
|
2018-11-15 15:56:28 +00:00
|
|
|
Schema::Integer(IntegerSchema { $($name: $e, )* ..DEFAULTINTEGER})
|
2018-10-31 09:42:14 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:56:28 +00:00
|
|
|
pub const DEFAULTSTRING: StringSchema = StringSchema {
|
2018-10-31 09:42:14 +00:00
|
|
|
description: "",
|
2018-11-06 12:10:10 +00:00
|
|
|
optional: false,
|
2018-10-31 09:42:14 +00:00
|
|
|
default: None,
|
|
|
|
min_length: None,
|
|
|
|
max_length: None,
|
2018-11-22 10:23:49 +00:00
|
|
|
format: None,
|
2018-10-31 09:42:14 +00:00
|
|
|
};
|
|
|
|
|
2018-11-07 11:55:33 +00:00
|
|
|
pub enum ApiStringFormat {
|
2018-11-07 12:25:47 +00:00
|
|
|
Enum(Vec<String>),
|
2018-11-07 12:01:14 +00:00
|
|
|
Pattern(Box<Regex>),
|
2018-11-20 15:54:29 +00:00
|
|
|
Complex(Arc<Schema>),
|
2018-11-22 10:23:49 +00:00
|
|
|
VerifyFn(fn(&str) -> Result<(), Error>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for ApiStringFormat {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
ApiStringFormat::VerifyFn(fnptr) => {
|
|
|
|
write!(f, "VerifyFn({:p}", fnptr)
|
|
|
|
}
|
|
|
|
ApiStringFormat::Enum(strvec) => {
|
|
|
|
write!(f, "Enum({:?}", strvec)
|
|
|
|
}
|
|
|
|
ApiStringFormat::Pattern(regex) => {
|
|
|
|
write!(f, "Pattern({:?}", regex)
|
|
|
|
}
|
|
|
|
ApiStringFormat::Complex(schema) => {
|
|
|
|
write!(f, "Complex({:?}", schema)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 11:55:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:42:14 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! ApiString {
|
|
|
|
($($name:ident => $e:expr),*) => {{
|
2018-11-15 15:56:28 +00:00
|
|
|
Schema::String(StringSchema { $($name: $e, )* ..DEFAULTSTRING})
|
2018-10-31 09:42:14 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:28:15 +00:00
|
|
|
|
2018-10-31 09:42:14 +00:00
|
|
|
#[macro_export]
|
2018-11-03 14:10:21 +00:00
|
|
|
macro_rules! parameter {
|
2018-11-15 15:48:36 +00:00
|
|
|
() => {{
|
2018-11-18 07:46:26 +00:00
|
|
|
ObjectSchema {
|
2018-11-15 15:48:36 +00:00
|
|
|
description: "",
|
|
|
|
optional: false,
|
|
|
|
additional_properties: false,
|
2018-11-20 15:54:29 +00:00
|
|
|
properties: HashMap::<&'static str, Arc<Schema>>::new(),
|
2018-11-18 07:46:26 +00:00
|
|
|
}
|
2018-11-15 15:48:36 +00:00
|
|
|
}};
|
2018-10-31 09:42:14 +00:00
|
|
|
($($name:ident => $e:expr),*) => {{
|
2018-11-18 07:46:26 +00:00
|
|
|
ObjectSchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
description: "",
|
2018-11-06 12:10:10 +00:00
|
|
|
optional: false,
|
|
|
|
additional_properties: false,
|
2018-11-03 14:10:21 +00:00
|
|
|
properties: {
|
2018-11-20 15:54:29 +00:00
|
|
|
let mut map = HashMap::<&'static str, Arc<Schema>>::new();
|
2018-11-03 14:10:21 +00:00
|
|
|
$(
|
|
|
|
map.insert(stringify!($name), $e);
|
|
|
|
)*
|
2018-11-05 14:20:27 +00:00
|
|
|
map
|
2018-11-03 14:10:21 +00:00
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
}
|
2018-11-15 15:48:36 +00:00
|
|
|
}};
|
2018-10-31 09:42:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-17 10:28:26 +00:00
|
|
|
pub fn parse_boolean(value_str: &str) -> Result<bool, Error> {
|
|
|
|
match value_str.to_lowercase().as_str() {
|
|
|
|
"1" | "on" | "yes" | "true" => Ok(true),
|
|
|
|
"0" | "off" | "no" | "false" => Ok(false),
|
|
|
|
_ => bail!("Unable to parse boolean option."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:56:28 +00:00
|
|
|
fn parse_simple_value(value_str: &str, schema: &Schema) -> Result<Value, Error> {
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2018-11-06 12:10:10 +00:00
|
|
|
let value = match schema {
|
2018-11-15 15:56:28 +00:00
|
|
|
Schema::Null => {
|
2018-11-07 11:14:52 +00:00
|
|
|
bail!("internal error - found Null schema.");
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
2018-11-16 15:47:23 +00:00
|
|
|
Schema::Boolean(_boolean_schema) => {
|
2018-11-17 10:28:26 +00:00
|
|
|
let res = parse_boolean(value_str)?;
|
2018-11-06 12:10:10 +00:00
|
|
|
Value::Bool(res)
|
|
|
|
}
|
2018-11-16 15:47:23 +00:00
|
|
|
Schema::Integer(integer_schema) => {
|
2018-11-06 12:10:10 +00:00
|
|
|
let res: isize = value_str.parse()?;
|
2018-11-07 10:55:08 +00:00
|
|
|
|
2018-11-16 15:47:23 +00:00
|
|
|
if let Some(minimum) = integer_schema.minimum {
|
2018-11-07 10:55:08 +00:00
|
|
|
if res < minimum {
|
2018-11-07 11:11:09 +00:00
|
|
|
bail!("value must have a minimum value of {}", minimum);
|
2018-11-07 10:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:47:23 +00:00
|
|
|
if let Some(maximum) = integer_schema.maximum {
|
2018-11-07 10:55:08 +00:00
|
|
|
if res > maximum {
|
2018-11-07 11:11:09 +00:00
|
|
|
bail!("value must have a maximum value of {}", maximum);
|
2018-11-07 10:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 12:10:10 +00:00
|
|
|
Value::Number(res.into())
|
|
|
|
}
|
2018-11-16 15:47:23 +00:00
|
|
|
Schema::String(string_schema) => {
|
2018-11-07 11:11:09 +00:00
|
|
|
let res: String = value_str.into();
|
|
|
|
let char_count = res.chars().count();
|
|
|
|
|
2018-11-16 15:47:23 +00:00
|
|
|
if let Some(min_length) = string_schema.min_length {
|
2018-11-07 11:11:09 +00:00
|
|
|
if char_count < min_length {
|
|
|
|
bail!("value must be at least {} characters long", min_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:47:23 +00:00
|
|
|
if let Some(max_length) = string_schema.max_length {
|
2018-11-07 11:11:09 +00:00
|
|
|
if char_count > max_length {
|
|
|
|
bail!("value may only be {} characters long", max_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:23:49 +00:00
|
|
|
if let Some(ref format) = string_schema.format {
|
|
|
|
match format.as_ref() {
|
|
|
|
ApiStringFormat::Pattern(ref regex) => {
|
|
|
|
if !regex.is_match(&res) {
|
|
|
|
bail!("value does not match the regex pattern");
|
|
|
|
}
|
2018-11-07 12:25:47 +00:00
|
|
|
}
|
2018-11-22 10:23:49 +00:00
|
|
|
ApiStringFormat::Enum(ref stringvec) => {
|
|
|
|
if stringvec.iter().find(|&e| *e == res) == None {
|
|
|
|
bail!("value is not defined in the enumeration.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ApiStringFormat::Complex(ref _subschema) => {
|
|
|
|
bail!("implement me!");
|
|
|
|
}
|
|
|
|
ApiStringFormat::VerifyFn(verify_fn) => {
|
|
|
|
verify_fn(&res)?;
|
2018-11-07 12:25:47 +00:00
|
|
|
}
|
2018-11-07 11:35:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:25:47 +00:00
|
|
|
Value::String(res)
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
2018-11-07 11:14:52 +00:00
|
|
|
_ => bail!("unable to parse complex (sub) objects."),
|
2018-11-06 12:10:10 +00:00
|
|
|
};
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
2018-11-18 07:46:26 +00:00
|
|
|
pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &ObjectSchema, test_required: bool) -> Result<Value, ParameterError> {
|
2018-11-05 14:20:27 +00:00
|
|
|
|
|
|
|
println!("QUERY Strings {:?}", data);
|
|
|
|
|
2018-11-06 12:10:10 +00:00
|
|
|
let mut params = json!({});
|
|
|
|
|
2018-11-10 09:00:48 +00:00
|
|
|
let mut errors = ParameterError::new();
|
2018-11-06 12:10:10 +00:00
|
|
|
|
2018-11-18 07:46:26 +00:00
|
|
|
let properties = &schema.properties;
|
|
|
|
let additional_properties = schema.additional_properties;
|
|
|
|
|
|
|
|
for (key, value) in data {
|
|
|
|
if let Some(prop_schema) = properties.get::<str>(key) {
|
2018-11-20 15:54:29 +00:00
|
|
|
match prop_schema.as_ref() {
|
2018-11-18 07:46:26 +00:00
|
|
|
Schema::Array(array_schema) => {
|
|
|
|
if params[key] == Value::Null {
|
|
|
|
params[key] = json!([]);
|
|
|
|
}
|
|
|
|
match params[key] {
|
|
|
|
Value::Array(ref mut array) => {
|
|
|
|
match parse_simple_value(value, &array_schema.items) {
|
|
|
|
Ok(res) => array.push(res),
|
2018-11-07 10:55:08 +00:00
|
|
|
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
match parse_simple_value(value, prop_schema) {
|
|
|
|
Ok(res) => {
|
|
|
|
if params[key] == Value::Null {
|
|
|
|
params[key] = res;
|
|
|
|
} else {
|
|
|
|
errors.push(format_err!("parameter '{}': duplicate parameter.", key));
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
},
|
|
|
|
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
} else {
|
|
|
|
if additional_properties {
|
|
|
|
match params[key] {
|
|
|
|
Value::Null => {
|
|
|
|
params[key] = Value::String(value.to_owned());
|
|
|
|
},
|
|
|
|
Value::String(ref old) => {
|
|
|
|
params[key] = Value::Array(
|
|
|
|
vec![Value::String(old.to_owned()), Value::String(value.to_owned())]);
|
|
|
|
}
|
|
|
|
Value::Array(ref mut array) => {
|
|
|
|
array.push(Value::String(value.to_string()));
|
2018-11-06 13:18:13 +00:00
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
|
2018-11-06 13:18:13 +00:00
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
} else {
|
|
|
|
errors.push(format_err!("parameter '{}': schema does not allow additional properties.", key));
|
2018-11-06 13:18:13 +00:00
|
|
|
}
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
2018-11-18 07:46:26 +00:00
|
|
|
}
|
2018-11-06 12:10:10 +00:00
|
|
|
|
2018-11-18 07:46:26 +00:00
|
|
|
if test_required && errors.len() == 0 {
|
|
|
|
for (name, prop_schema) in properties {
|
2018-11-20 15:54:29 +00:00
|
|
|
let optional = match prop_schema.as_ref() {
|
2018-11-18 07:46:26 +00:00
|
|
|
Schema::Boolean(boolean_schema) => boolean_schema.optional,
|
|
|
|
Schema::Integer(integer_schema) => integer_schema.optional,
|
|
|
|
Schema::String(string_schema) => string_schema.optional,
|
|
|
|
Schema::Array(array_schema) => array_schema.optional,
|
|
|
|
Schema::Object(object_schema) => object_schema.optional,
|
|
|
|
Schema::Null => true,
|
|
|
|
};
|
|
|
|
if optional == false && params[name] == Value::Null {
|
|
|
|
errors.push(format_err!("parameter '{}': parameter is missing and it is not optional.", name));
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 09:00:48 +00:00
|
|
|
if errors.len() > 0 {
|
2018-11-06 12:10:10 +00:00
|
|
|
Err(errors)
|
|
|
|
} else {
|
|
|
|
Ok(params)
|
|
|
|
}
|
2018-11-05 14:20:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 07:46:26 +00:00
|
|
|
pub fn parse_query_string(query: &str, schema: &ObjectSchema, test_required: bool) -> Result<Value, ParameterError> {
|
2018-11-06 12:58:05 +00:00
|
|
|
|
|
|
|
let param_list: Vec<(String, String)> =
|
|
|
|
form_urlencoded::parse(query.as_bytes()).into_owned().collect();
|
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
parse_parameter_strings(¶m_list, schema, test_required)
|
2018-11-06 12:58:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 14:10:21 +00:00
|
|
|
#[test]
|
2018-11-16 08:16:37 +00:00
|
|
|
fn test_schema1() {
|
2018-11-15 15:56:28 +00:00
|
|
|
let schema = Schema::Object(ObjectSchema {
|
2018-11-03 14:10:21 +00:00
|
|
|
description: "TEST",
|
2018-11-06 12:58:05 +00:00
|
|
|
optional: false,
|
|
|
|
additional_properties: false,
|
2018-11-03 14:10:21 +00:00
|
|
|
properties: {
|
|
|
|
let map = HashMap::new();
|
|
|
|
|
2018-11-06 12:58:05 +00:00
|
|
|
map
|
2018-11-02 08:44:18 +00:00
|
|
|
}
|
2018-11-03 14:10:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
println!("TEST Schema: {:?}", schema);
|
2018-11-02 08:44:18 +00:00
|
|
|
}
|
2018-11-03 08:08:01 +00:00
|
|
|
|
2018-11-07 11:11:09 +00:00
|
|
|
#[test]
|
|
|
|
fn test_query_string() {
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{name => Arc::new(ApiString!{ optional => false })};
|
2018-11-07 11:11:09 +00:00
|
|
|
|
|
|
|
let res = parse_query_string("", &schema, true);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{name => Arc::new(ApiString!{ optional => true })};
|
2018-11-07 11:11:09 +00:00
|
|
|
|
|
|
|
let res = parse_query_string("", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
2018-11-07 11:35:52 +00:00
|
|
|
// TEST min_length and max_length
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{name => Arc::new(ApiString!{
|
2018-11-07 11:11:09 +00:00
|
|
|
optional => false,
|
|
|
|
min_length => Some(5),
|
|
|
|
max_length => Some(10)
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
})};
|
2018-11-07 11:11:09 +00:00
|
|
|
|
|
|
|
let res = parse_query_string("name=abcd", &schema, true);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let res = parse_query_string("name=abcde", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("name=abcdefghijk", &schema, true);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let res = parse_query_string("name=abcdefghij", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
2018-11-07 11:35:52 +00:00
|
|
|
// TEST regex pattern
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{name => Arc::new(ApiString!{
|
2018-11-07 11:35:52 +00:00
|
|
|
optional => false,
|
2018-11-22 10:23:49 +00:00
|
|
|
format => Some(Arc::new(ApiStringFormat::Pattern(Box::new(Regex::new("test").unwrap()))))
|
2018-11-20 15:54:29 +00:00
|
|
|
})};
|
2018-11-07 11:11:09 +00:00
|
|
|
|
2018-11-07 11:35:52 +00:00
|
|
|
let res = parse_query_string("name=abcd", &schema, true);
|
|
|
|
assert!(res.is_err());
|
2018-11-07 11:11:09 +00:00
|
|
|
|
2018-11-07 11:35:52 +00:00
|
|
|
let res = parse_query_string("name=ateststring", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{name => Arc::new(ApiString!{
|
2018-11-07 11:35:52 +00:00
|
|
|
optional => false,
|
2018-11-22 10:23:49 +00:00
|
|
|
format => Some(Arc::new(ApiStringFormat::Pattern(Box::new(Regex::new("^test$").unwrap()))))
|
2018-11-20 15:54:29 +00:00
|
|
|
})};
|
2018-11-07 11:35:52 +00:00
|
|
|
|
|
|
|
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());
|
2018-11-07 12:25:47 +00:00
|
|
|
|
|
|
|
// TEST string enums
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{name => Arc::new(ApiString!{
|
2018-11-07 12:25:47 +00:00
|
|
|
optional => false,
|
2018-11-22 10:23:49 +00:00
|
|
|
format => Some(Arc::new(ApiStringFormat::Enum(vec!["ev1".into(), "ev2".into()])))
|
2018-11-20 15:54:29 +00:00
|
|
|
})};
|
2018-11-07 12:25:47 +00:00
|
|
|
|
|
|
|
let res = parse_query_string("name=noenum", &schema, true);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let res = parse_query_string("name=ev1", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("name=ev2", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("name=ev3", &schema, true);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
2018-11-07 11:11:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 10:55:08 +00:00
|
|
|
#[test]
|
|
|
|
fn test_query_integer() {
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{count => Arc::new(Integer!{ optional => false })};
|
2018-11-07 10:55:08 +00:00
|
|
|
|
|
|
|
let res = parse_query_string("", &schema, true);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{count => Arc::new(Integer!{
|
2018-11-07 10:55:08 +00:00
|
|
|
optional => true,
|
|
|
|
minimum => Some(-3),
|
|
|
|
maximum => Some(50)
|
2018-11-20 15:54:29 +00:00
|
|
|
})};
|
2018-11-07 10:55:08 +00:00
|
|
|
|
|
|
|
let res = parse_query_string("", &schema, true);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=abc", &schema, false);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=30", &schema, false);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=-1", &schema, false);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=300", &schema, false);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=-30", &schema, false);
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=50", &schema, false);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let res = parse_query_string("count=-3", &schema, false);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
2018-11-06 12:58:05 +00:00
|
|
|
#[test]
|
|
|
|
fn test_query_boolean() {
|
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{force => Arc::new(Boolean!{ optional => false })};
|
2018-11-06 12:58:05 +00:00
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("", &schema, true);
|
|
|
|
assert!(res.is_err());
|
2018-11-06 12:58:05 +00:00
|
|
|
|
2018-11-20 15:54:29 +00:00
|
|
|
let schema = parameter!{force => Arc::new(Boolean!{ optional => true })};
|
2018-11-06 12:58:05 +00:00
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("a=b", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_err());
|
2018-11-07 10:55:08 +00:00
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=yes", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=1", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=On", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=TRUE", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=TREU", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_err());
|
|
|
|
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=NO", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=0", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=off", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
2018-11-06 13:18:13 +00:00
|
|
|
let res = parse_query_string("force=False", &schema, true);
|
2018-11-06 12:58:05 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-03 14:10:21 +00:00
|
|
|
/*
|
2018-11-03 08:08:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_shema1() {
|
|
|
|
static PARAMETERS1: PropertyMap = propertymap!{
|
|
|
|
force => &Boolean!{
|
|
|
|
description => "Test for boolean options."
|
|
|
|
},
|
|
|
|
text1 => &ApiString!{
|
|
|
|
description => "A simple text string.",
|
|
|
|
min_length => Some(10),
|
|
|
|
max_length => Some(30)
|
|
|
|
},
|
|
|
|
count => &Integer!{
|
|
|
|
description => "A counter for everything.",
|
|
|
|
minimum => Some(0),
|
|
|
|
maximum => Some(10)
|
|
|
|
},
|
|
|
|
myarray1 => &Array!{
|
|
|
|
description => "Test Array of simple integers.",
|
|
|
|
items => &PVE_VMID
|
|
|
|
},
|
2018-11-15 15:56:28 +00:00
|
|
|
myarray2 => &Schema::Array(ArraySchema {
|
2018-11-03 08:08:01 +00:00
|
|
|
description: "Test Array of simple integers.",
|
|
|
|
optional: Some(false),
|
|
|
|
items: &Object!{description => "Empty Object."},
|
|
|
|
}),
|
|
|
|
myobject => &Object!{
|
|
|
|
description => "TEST Object.",
|
|
|
|
properties => &propertymap!{
|
|
|
|
vmid => &PVE_VMID,
|
|
|
|
loop => &Integer!{
|
|
|
|
description => "Totally useless thing.",
|
|
|
|
optional => Some(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
emptyobject => &Object!{description => "Empty Object."}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (k, v) in PARAMETERS1.entries {
|
|
|
|
println!("Parameter: {} Value: {:?}", k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2018-11-03 14:10:21 +00:00
|
|
|
*/
|