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