-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
118 lines (109 loc) · 3.77 KB
/
Copy pathmod.rs
File metadata and controls
118 lines (109 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
mod rule;
use rule::{
contains_key::apply_contains_key,
contains_type::apply_contains_type,
item_of_collection::apply_item_of_collection,
number_range::apply_number_range,
regex::apply_regex,
violation::{DataTypeNotFoundRuleViolation, DataTypeRuleError, DataTypeRuleViolation},
};
use tucana::shared::{ExecutionDataType, ValidationFlow, Value, execution_data_type_rule::Config};
pub struct VerificationResult;
pub fn verify_flow(flow: ValidationFlow, body: Value) -> Result<(), DataTypeRuleError> {
let input_type = match &flow.input_type_identifier {
Some(r) => r.clone(),
None => return Ok(()), //Returns directly because no rule is given. The body is ok and will not be concidered
};
let data_type = match flow
.data_types
.iter()
.find(|dt| dt.identifier == input_type)
{
Some(dt) => dt.clone(),
None => {
return Err(DataTypeRuleError {
violations: vec![DataTypeRuleViolation::DataTypeNotFound(
DataTypeNotFoundRuleViolation {
data_type: input_type,
},
)],
});
}
};
verify_data_type_rules(body, data_type, &flow.data_types)
}
//Verifies the rules on the datatype of the body thats given
fn verify_data_type_rules(
body: Value,
data_type: ExecutionDataType,
availabe_data_types: &Vec<ExecutionDataType>,
) -> Result<(), DataTypeRuleError> {
let mut violations: Vec<DataTypeRuleViolation> = Vec::new();
for rule in data_type.rules {
let rule_config = match rule.config {
None => continue,
Some(config) => config,
};
match rule_config {
Config::NumberRange(config) => {
match apply_number_range(config, &body, &String::from("value")) {
Ok(_) => continue,
Err(violation) => {
violations.extend(violation.violations);
continue;
}
}
}
Config::ItemOfCollection(config) => {
match apply_item_of_collection(config, &body, "key") {
Ok(_) => continue,
Err(violation) => {
violations.extend(violation.violations);
continue;
}
}
}
Config::ContainsType(config) => {
match apply_contains_type(config, &availabe_data_types, &body) {
Ok(_) => continue,
Err(violation) => {
violations.extend(violation.violations);
continue;
}
}
}
Config::Regex(config) => {
match apply_regex(config, &body) {
Ok(_) => continue,
Err(violation) => {
violations.extend(violation.violations);
continue;
}
};
}
Config::ContainsKey(config) => {
match apply_contains_key(config, &body, &availabe_data_types) {
Ok(_) => continue,
Err(violation) => {
violations.extend(violation.violations);
continue;
}
};
}
}
}
if violations.is_empty() {
Ok(())
} else {
Err(DataTypeRuleError { violations })
}
}
fn get_data_type_by_id(
data_types: &Vec<ExecutionDataType>,
identifier: &String,
) -> Option<ExecutionDataType> {
data_types
.iter()
.find(|data_type| &data_type.identifier == identifier)
.cloned()
}