csv-validation 0.0.2__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.
- {csv_validation-0.0.2 → csv_validation-0.0.4}/Cargo.lock +1 -1
- {csv_validation-0.0.2 → csv_validation-0.0.4}/Cargo.toml +1 -1
- {csv_validation-0.0.2 → csv_validation-0.0.4}/PKG-INFO +1 -1
- {csv_validation-0.0.2 → csv_validation-0.0.4}/src/lib.rs +115 -13
- csv_validation-0.0.4/test/test_validations_wrong_headers.yml +5 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/.github/workflows/CI.yml +0 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/.gitignore +0 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/build.sh +0 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/pyproject.toml +0 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/test/test_file.csv +0 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/test/test_file.csv.gz +0 -0
- {csv_validation-0.0.2 → csv_validation-0.0.4}/test/test_validations.yml +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
use std::collections::{HashMap, HashSet};
|
|
1
2
|
use std::fs;
|
|
2
3
|
use std::fs::File;
|
|
3
4
|
use std::io::{BufReader, Read};
|
|
@@ -11,9 +12,11 @@ use regex::Regex;
|
|
|
11
12
|
use yaml_rust2::YamlLoader;
|
|
12
13
|
use crate::Validation::{RegularExpression};
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
const MAX_SAMPLE_SIZE:u16 = 10;
|
|
16
|
+
|
|
17
|
+
#[derive(Debug, Clone)]
|
|
15
18
|
enum Validation {
|
|
16
|
-
RegularExpression(
|
|
19
|
+
RegularExpression(String),
|
|
17
20
|
Min(f64),
|
|
18
21
|
Max(f64),
|
|
19
22
|
None
|
|
@@ -24,6 +27,17 @@ struct ColumnValidations {
|
|
|
24
27
|
validations: Vec<Validation>
|
|
25
28
|
}
|
|
26
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
|
+
|
|
27
41
|
/// Validate that CSV file complies with validations definition
|
|
28
42
|
#[pyfunction]
|
|
29
43
|
fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
|
|
@@ -31,6 +45,19 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
|
|
|
31
45
|
|
|
32
46
|
let validations = get_validations(definition_path);
|
|
33
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
|
+
|
|
34
61
|
// Build the CSV reader
|
|
35
62
|
let mut rdr = get_reader_from(path);
|
|
36
63
|
|
|
@@ -43,6 +70,8 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
|
|
|
43
70
|
return Ok(false);
|
|
44
71
|
}
|
|
45
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);
|
|
46
75
|
let mut is_valid_file = true;
|
|
47
76
|
for result in rdr.records() {
|
|
48
77
|
let record = result.unwrap();
|
|
@@ -50,23 +79,75 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
|
|
|
50
79
|
let value = next_column.0;
|
|
51
80
|
let _column_name = &next_column.1.column_name;
|
|
52
81
|
for validation in &next_column.1.validations {
|
|
53
|
-
let valid = apply_validation(value, validation);
|
|
82
|
+
let valid = apply_validation(value, validation, ®ex_map);
|
|
54
83
|
if !valid {
|
|
55
|
-
|
|
56
|
-
|
|
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
|
+
}
|
|
57
93
|
}
|
|
58
94
|
is_valid_file = is_valid_file && valid;
|
|
59
95
|
}
|
|
60
96
|
}
|
|
61
97
|
}
|
|
62
98
|
|
|
63
|
-
|
|
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
|
+
}
|
|
64
127
|
Ok(is_valid_file)
|
|
65
128
|
}
|
|
66
129
|
|
|
67
|
-
fn
|
|
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 {
|
|
68
146
|
match validation {
|
|
69
|
-
RegularExpression(regex) =>
|
|
147
|
+
RegularExpression(regex) => {
|
|
148
|
+
let regex = regex_map.get(regex).unwrap();
|
|
149
|
+
regex.is_match(value)
|
|
150
|
+
},
|
|
70
151
|
Validation::Min(min) => {
|
|
71
152
|
match value.parse::<f64>() {
|
|
72
153
|
Ok(value) => value >= *min,
|
|
@@ -121,7 +202,7 @@ fn get_validations(definition_path: &str) -> Vec<ColumnValidations> {
|
|
|
121
202
|
let mut validation: Validation = Validation::None;
|
|
122
203
|
match key {
|
|
123
204
|
"name" => { name = value.as_str().unwrap(); column_names.push(name); }
|
|
124
|
-
"regex" => { validation = Validation::RegularExpression(
|
|
205
|
+
"regex" => { validation = Validation::RegularExpression(String::from(value.as_str().unwrap())); }
|
|
125
206
|
"min" => { validation = Validation::Min(value.as_i64().unwrap() as f64); }
|
|
126
207
|
"max" => { validation = Validation::Max(value.as_i64().unwrap() as f64); }
|
|
127
208
|
_ => panic!("Unknown validation: {key}")
|
|
@@ -140,15 +221,31 @@ fn get_validations(definition_path: &str) -> Vec<ColumnValidations> {
|
|
|
140
221
|
}
|
|
141
222
|
|
|
142
223
|
fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<ColumnValidations>) -> bool {
|
|
143
|
-
let
|
|
224
|
+
let expected_column_names = validations.iter()
|
|
144
225
|
.map(|v| v.column_name.clone())
|
|
145
226
|
.collect::<Vec<String>>();
|
|
146
|
-
debug!("Expected Column Names: {:?}",
|
|
227
|
+
debug!("Expected Column Names: {:?}", expected_column_names);
|
|
147
228
|
|
|
148
|
-
let headers: Vec
|
|
229
|
+
let headers: Vec<String> = reader.headers().unwrap().iter().map(|s| String::from(s) ).collect();
|
|
149
230
|
debug!("Actual Column Names: {:?}", headers);
|
|
150
231
|
|
|
151
|
-
|
|
232
|
+
if expected_column_names != headers {
|
|
233
|
+
if expected_column_names.len() != headers.len() {
|
|
234
|
+
let expected_columns_set: HashSet<String> = expected_column_names.iter().cloned().collect();
|
|
235
|
+
let headers_set: HashSet<String> = headers.iter().cloned().collect();
|
|
236
|
+
debug!("File headers not in expected columns: {:?}", headers_set.difference(&expected_columns_set));
|
|
237
|
+
debug!("Columns in expected columns not in file headers: {:?}", expected_columns_set.difference(&headers_set));
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
for (expected_column, header) in zip(expected_column_names, headers) {
|
|
241
|
+
if expected_column != header {
|
|
242
|
+
debug!("{:?} != {:?}", expected_column, header);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return false
|
|
247
|
+
}
|
|
248
|
+
true
|
|
152
249
|
}
|
|
153
250
|
|
|
154
251
|
/// A Python module implemented in Rust.
|
|
@@ -178,4 +275,9 @@ mod tests {
|
|
|
178
275
|
fn test_validate_csv_gz() {
|
|
179
276
|
assert!(validate("test/test_file.csv.gz", "test/test_validations.yml").unwrap());
|
|
180
277
|
}
|
|
278
|
+
|
|
279
|
+
#[test]
|
|
280
|
+
fn test_wrong_headers() {
|
|
281
|
+
assert!(!validate("test/test_file.csv", "test/test_validations_wrong_headers.yml").unwrap());
|
|
282
|
+
}
|
|
181
283
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|