csv-validation 0.0.3__tar.gz → 0.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,7 +83,7 @@ dependencies = [
83
83
 
84
84
  [[package]]
85
85
  name = "csv_validation"
86
- version = "0.0.3"
86
+ version = "0.0.4"
87
87
  dependencies = [
88
88
  "csv",
89
89
  "flate2",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "csv_validation"
3
- version = "0.0.3"
3
+ version = "0.0.4"
4
4
  edition = "2021"
5
5
 
6
6
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: csv_validation
3
- Version: 0.0.3
3
+ Version: 0.0.4
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -1,4 +1,4 @@
1
- use std::collections::HashSet;
1
+ use std::collections::{HashMap, HashSet};
2
2
  use std::fs;
3
3
  use std::fs::File;
4
4
  use std::io::{BufReader, Read};
@@ -12,9 +12,11 @@ use regex::Regex;
12
12
  use yaml_rust2::YamlLoader;
13
13
  use crate::Validation::{RegularExpression};
14
14
 
15
- #[derive(Debug)]
15
+ const MAX_SAMPLE_SIZE:u16 = 10;
16
+
17
+ #[derive(Debug, Clone)]
16
18
  enum Validation {
17
- RegularExpression(Regex),
19
+ RegularExpression(String),
18
20
  Min(f64),
19
21
  Max(f64),
20
22
  None
@@ -25,6 +27,17 @@ struct ColumnValidations {
25
27
  validations: Vec<Validation>
26
28
  }
27
29
 
30
+ struct ValidationSummary {
31
+ validation: Validation,
32
+ wrong_rows: usize,
33
+ wrong_values_sample: Vec<String>
34
+ }
35
+
36
+ struct ColumnValidationsSummary {
37
+ column_name: String,
38
+ validation_summaries: Vec<ValidationSummary>
39
+ }
40
+
28
41
  /// Validate that CSV file complies with validations definition
29
42
  #[pyfunction]
30
43
  fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
@@ -32,6 +45,19 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
32
45
 
33
46
  let validations = get_validations(definition_path);
34
47
 
48
+ // Pre-Compile and save all Regex expressions to save time in execution
49
+ let mut regex_map = HashMap::new();
50
+ for column_validation in &validations {
51
+ for validation in &column_validation.validations {
52
+ match validation {
53
+ RegularExpression(regex) => {
54
+ regex_map.insert(regex, Regex::new(regex.as_str()).unwrap());
55
+ },
56
+ _ => continue
57
+ }
58
+ }
59
+ }
60
+
35
61
  // Build the CSV reader
36
62
  let mut rdr = get_reader_from(path);
37
63
 
@@ -44,6 +70,8 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
44
70
  return Ok(false);
45
71
  }
46
72
 
73
+ // Second validation: If column names match, check if also the values match the validations
74
+ let mut validation_summaries_map = build_validation_summaries_map(&validations);
47
75
  let mut is_valid_file = true;
48
76
  for result in rdr.records() {
49
77
  let record = result.unwrap();
@@ -51,23 +79,75 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
51
79
  let value = next_column.0;
52
80
  let _column_name = &next_column.1.column_name;
53
81
  for validation in &next_column.1.validations {
54
- let valid = apply_validation(value, validation);
82
+ let valid = apply_validation(value, validation, &regex_map);
55
83
  if !valid {
56
- debug!("Column {}: Value {} isn't valid for {:?}",
57
- _column_name, value, validation);
84
+ let validation_summary_list = validation_summaries_map.get_mut(_column_name).unwrap();
85
+ let validation_summary = validation_summary_list
86
+ .iter_mut()
87
+ .find(|val_sum|
88
+ std::mem::discriminant(&val_sum.validation) == std::mem::discriminant(validation)).unwrap();
89
+ validation_summary.wrong_rows += 1;
90
+ if validation_summary.wrong_values_sample.len() < MAX_SAMPLE_SIZE as usize {
91
+ validation_summary.wrong_values_sample.push(value.to_string());
92
+ }
58
93
  }
59
94
  is_valid_file = is_valid_file && valid;
60
95
  }
61
96
  }
62
97
  }
63
98
 
64
- info!("File matches the validations");
99
+ // Fill the ColumnValidationSummary for each column
100
+ let mut column_validation_summaries = Vec::new();
101
+ for column_validation in &validations {
102
+ let validation_summary_for_column =
103
+ validation_summaries_map.remove(&column_validation.column_name).unwrap();
104
+ let column_validation_summary = ColumnValidationsSummary {
105
+ column_name: column_validation.column_name.clone(),
106
+ validation_summaries: validation_summary_for_column
107
+ };
108
+ column_validation_summaries.push(column_validation_summary);
109
+ }
110
+
111
+ debug!("VALIDATIONS SUMMARY");
112
+ debug!("==================================================================================");
113
+ for column_validation_summary in column_validation_summaries {
114
+ debug!("Column: '{}'", column_validation_summary.column_name);
115
+ for validation_summary in column_validation_summary.validation_summaries {
116
+ debug!("\tValidation {:?} => Wrong Rows: {} | Wrong Values Sample: {:?}", &validation_summary.validation,
117
+ &validation_summary.wrong_rows, &validation_summary.wrong_values_sample);
118
+ }
119
+ }
120
+
121
+ if is_valid_file {
122
+ info!("OK: File matches the validations");
123
+ }
124
+ else {
125
+ info!("NO OK: File DOESN'T match validations");
126
+ }
65
127
  Ok(is_valid_file)
66
128
  }
67
129
 
68
- fn apply_validation(value: &str, validation: &Validation) -> bool {
130
+ fn build_validation_summaries_map(validations: &Vec<ColumnValidations>) -> HashMap<String, Vec<ValidationSummary>> {
131
+ let mut validation_summaries_map = HashMap::new();
132
+ for validation in validations {
133
+ let mut validation_summaries = Vec::new();
134
+ for column_validation in &validation.validations {
135
+ let validation_summary =
136
+ ValidationSummary{validation: (*column_validation).clone(), wrong_rows: 0, wrong_values_sample: Vec::new()};
137
+ validation_summaries.push(validation_summary);
138
+ }
139
+ validation_summaries_map.insert(validation.column_name.clone(), validation_summaries);
140
+ }
141
+
142
+ validation_summaries_map
143
+ }
144
+
145
+ fn apply_validation(value: &str, validation: &Validation, regex_map: &HashMap<&String, Regex>) -> bool {
69
146
  match validation {
70
- RegularExpression(regex) => regex.is_match(value),
147
+ RegularExpression(regex) => {
148
+ let regex = regex_map.get(regex).unwrap();
149
+ regex.is_match(value)
150
+ },
71
151
  Validation::Min(min) => {
72
152
  match value.parse::<f64>() {
73
153
  Ok(value) => value >= *min,
@@ -122,7 +202,7 @@ fn get_validations(definition_path: &str) -> Vec<ColumnValidations> {
122
202
  let mut validation: Validation = Validation::None;
123
203
  match key {
124
204
  "name" => { name = value.as_str().unwrap(); column_names.push(name); }
125
- "regex" => { validation = Validation::RegularExpression(Regex::new(&value.as_str().unwrap()).unwrap()); }
205
+ "regex" => { validation = Validation::RegularExpression(String::from(value.as_str().unwrap())); }
126
206
  "min" => { validation = Validation::Min(value.as_i64().unwrap() as f64); }
127
207
  "max" => { validation = Validation::Max(value.as_i64().unwrap() as f64); }
128
208
  _ => panic!("Unknown validation: {key}")
File without changes