csv-validation 0.0.9__tar.gz → 0.0.11__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.9 → csv_validation-0.0.11}/Cargo.lock +1 -1
- {csv_validation-0.0.9 → csv_validation-0.0.11}/Cargo.toml +1 -1
- {csv_validation-0.0.9 → csv_validation-0.0.11}/PKG-INFO +1 -1
- {csv_validation-0.0.9 → csv_validation-0.0.11}/src/lib.rs +151 -38
- {csv_validation-0.0.9 → csv_validation-0.0.11}/.github/workflows/CI.yml +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/.gitignore +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/LICENSE +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/README.md +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/build.sh +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/pyproject.toml +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/test/empty_values.csv +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/test/test_file.csv +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/test/test_file.csv.gz +0 -0
- {csv_validation-0.0.9 → csv_validation-0.0.11}/test/test_validations.yml +0 -0
|
@@ -11,12 +11,13 @@ use log::{debug, error, info};
|
|
|
11
11
|
use pyo3::exceptions::{PyRuntimeError};
|
|
12
12
|
use pyo3::prelude::*;
|
|
13
13
|
use regex::Regex;
|
|
14
|
-
use yaml_rust2::YamlLoader;
|
|
14
|
+
use yaml_rust2::{Yaml, YamlLoader};
|
|
15
15
|
use serde::{Deserialize, Serialize};
|
|
16
16
|
use crate::Validation::{RegularExpression};
|
|
17
17
|
|
|
18
18
|
const MAX_SAMPLE_SIZE:u16 = 10;
|
|
19
19
|
const DEFAULT_COLUMN_SEPARATOR:u8 = b',';
|
|
20
|
+
const DEFAULT_DECIMAL_SEPARATOR:char = '.';
|
|
20
21
|
|
|
21
22
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
22
23
|
enum Validation {
|
|
@@ -90,35 +91,20 @@ fn build_validation_summaries_map(validations: &Vec<ColumnValidations>) -> HashM
|
|
|
90
91
|
validation_summaries_map
|
|
91
92
|
}
|
|
92
93
|
|
|
93
|
-
fn
|
|
94
|
-
|
|
95
|
-
RegularExpression { expression: exp, alias: _ } => {
|
|
96
|
-
let regex = regex_map.get(exp).unwrap();
|
|
97
|
-
Ok(regex.is_match(value))
|
|
98
|
-
},
|
|
99
|
-
Validation::Min(min) => {
|
|
100
|
-
match value.parse::<f64>() {
|
|
101
|
-
Ok(value) => Ok(value >= *min),
|
|
102
|
-
Err(_) => Ok(false)
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
Validation::Max(max) => {
|
|
106
|
-
match value.parse::<f64>() {
|
|
107
|
-
Ok(value) => Ok(value <= *max),
|
|
108
|
-
Err(_) => Ok(false)
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
Validation::Values(values) => {
|
|
112
|
-
let regex_str = get_regex_string_for_values(values);
|
|
113
|
-
let regex = regex_map.get(®ex_str).unwrap();
|
|
114
|
-
Ok(regex.is_match(value))
|
|
115
|
-
}
|
|
116
|
-
Validation::None => Err(PyRuntimeError::new_err("'None' validation has no implementation"))
|
|
117
|
-
}
|
|
94
|
+
fn get_regex_string_for_values(values: &Vec<String>) -> String {
|
|
95
|
+
format!("^$|^(?:{})$", values.iter().map(|s| regex_escape(s)).collect::<Vec<_>>().join("|"))
|
|
118
96
|
}
|
|
119
97
|
|
|
120
|
-
fn
|
|
121
|
-
|
|
98
|
+
fn regex_escape(text: &str) -> String {
|
|
99
|
+
let metacharacters = r"\.^$|()?*+[]{}";
|
|
100
|
+
let mut escaped = String::with_capacity(text.len());
|
|
101
|
+
for ch in text.chars() {
|
|
102
|
+
if metacharacters.contains(ch) {
|
|
103
|
+
escaped.push('\\');
|
|
104
|
+
}
|
|
105
|
+
escaped.push(ch);
|
|
106
|
+
}
|
|
107
|
+
escaped
|
|
122
108
|
}
|
|
123
109
|
|
|
124
110
|
/// Infers the file compression type and returns the corresponding buffered reader
|
|
@@ -197,14 +183,14 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
|
|
|
197
183
|
})
|
|
198
184
|
}
|
|
199
185
|
"min" => {
|
|
200
|
-
let num = value
|
|
201
|
-
.ok_or_else(|| PyRuntimeError::new_err("Min value
|
|
202
|
-
Ok(Validation::Min(num
|
|
186
|
+
let num = yaml_value_as_f64(value)
|
|
187
|
+
.ok_or_else(|| PyRuntimeError::new_err(format!("Min value {:?} is not a number", value)))?;
|
|
188
|
+
Ok(Validation::Min(num))
|
|
203
189
|
}
|
|
204
190
|
"max" => {
|
|
205
|
-
let num = value
|
|
206
|
-
.ok_or_else(|| PyRuntimeError::new_err("Max value
|
|
207
|
-
Ok(Validation::Max(num
|
|
191
|
+
let num = yaml_value_as_f64(value)
|
|
192
|
+
.ok_or_else(|| PyRuntimeError::new_err(format!("Max value {:?} is not a number", value)))?;
|
|
193
|
+
Ok(Validation::Max(num))
|
|
208
194
|
}
|
|
209
195
|
"values" => {
|
|
210
196
|
let values = value.as_vec()
|
|
@@ -258,6 +244,14 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
|
|
|
258
244
|
Ok(column_validations)
|
|
259
245
|
}
|
|
260
246
|
|
|
247
|
+
fn yaml_value_as_f64(val: &Yaml) -> Option<f64> {
|
|
248
|
+
match val {
|
|
249
|
+
Yaml::Real(s) => s.parse().ok(),
|
|
250
|
+
Yaml::Integer(i) => Some(*i as f64),
|
|
251
|
+
_ => None,
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
261
255
|
fn get_regex_for_format(format: &str) -> PyResult<String> {
|
|
262
256
|
match format {
|
|
263
257
|
"integer" => Ok(String::from("^$|^-?\\d+$")),
|
|
@@ -305,9 +299,11 @@ fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<C
|
|
|
305
299
|
struct CSVValidator {
|
|
306
300
|
validations: Vec<ColumnValidations>,
|
|
307
301
|
regex_map: HashMap<String, Regex>,
|
|
308
|
-
separator: u8
|
|
302
|
+
separator: u8,
|
|
303
|
+
decimal_separator: char
|
|
309
304
|
}
|
|
310
305
|
|
|
306
|
+
// Methods Exposed to Python
|
|
311
307
|
#[pymethods]
|
|
312
308
|
impl CSVValidator {
|
|
313
309
|
#[new]
|
|
@@ -315,7 +311,8 @@ impl CSVValidator {
|
|
|
315
311
|
CSVValidator {
|
|
316
312
|
validations: Vec::new(),
|
|
317
313
|
regex_map: HashMap::new(),
|
|
318
|
-
separator: DEFAULT_COLUMN_SEPARATOR
|
|
314
|
+
separator: DEFAULT_COLUMN_SEPARATOR,
|
|
315
|
+
decimal_separator: DEFAULT_DECIMAL_SEPARATOR
|
|
319
316
|
}
|
|
320
317
|
}
|
|
321
318
|
|
|
@@ -346,7 +343,7 @@ impl CSVValidator {
|
|
|
346
343
|
}
|
|
347
344
|
}
|
|
348
345
|
|
|
349
|
-
Ok(CSVValidator { validations, regex_map, separator: DEFAULT_COLUMN_SEPARATOR })
|
|
346
|
+
Ok(CSVValidator { validations, regex_map, separator: DEFAULT_COLUMN_SEPARATOR, decimal_separator: DEFAULT_DECIMAL_SEPARATOR })
|
|
350
347
|
}
|
|
351
348
|
|
|
352
349
|
fn set_separator(&mut self, separator: String) -> PyResult<()> {
|
|
@@ -359,6 +356,16 @@ impl CSVValidator {
|
|
|
359
356
|
}
|
|
360
357
|
}
|
|
361
358
|
|
|
359
|
+
fn set_decimal_separator(&mut self, decimal_separator: String) -> PyResult<()> {
|
|
360
|
+
if decimal_separator.len() == 1 {
|
|
361
|
+
self.decimal_separator = decimal_separator.chars().next().unwrap();
|
|
362
|
+
Ok(())
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
Err(PyRuntimeError::new_err(format!("Wrong decimal separator {decimal_separator}. It can only have one character")))
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
362
369
|
fn validate(&self, file_path: &str) -> PyResult<bool> {
|
|
363
370
|
// Build the CSV reader
|
|
364
371
|
let mut rdr = get_reader_from(file_path, self.separator)?;
|
|
@@ -382,7 +389,7 @@ impl CSVValidator {
|
|
|
382
389
|
let value = next_column.0;
|
|
383
390
|
let _column_name = &next_column.1.column_name;
|
|
384
391
|
for validation in &next_column.1.validations {
|
|
385
|
-
let valid = apply_validation(value, validation, &self.regex_map)?;
|
|
392
|
+
let valid = self.apply_validation(value, validation, &self.regex_map)?;
|
|
386
393
|
if !valid {
|
|
387
394
|
let validation_summary_list = validation_summaries_map.get_mut(_column_name).unwrap();
|
|
388
395
|
let validation_summary = validation_summary_list
|
|
@@ -456,6 +463,48 @@ impl CSVValidator {
|
|
|
456
463
|
}
|
|
457
464
|
}
|
|
458
465
|
|
|
466
|
+
// Internal methods that are NOT Exposed in Python
|
|
467
|
+
impl CSVValidator {
|
|
468
|
+
fn apply_validation(&self, value: &str, validation: &Validation, regex_map: &HashMap<String, Regex>) -> PyResult<bool> {
|
|
469
|
+
match validation {
|
|
470
|
+
RegularExpression { expression: exp, alias: _ } => {
|
|
471
|
+
let regex = regex_map.get(exp).unwrap();
|
|
472
|
+
Ok(regex.is_match(value))
|
|
473
|
+
},
|
|
474
|
+
Validation::Min(min) => {
|
|
475
|
+
if value.is_empty() {
|
|
476
|
+
Ok(true)
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
let normalized_value = value.replace(self.decimal_separator, ".");
|
|
480
|
+
match normalized_value.parse::<f64>() {
|
|
481
|
+
Ok(value) => Ok(value >= *min),
|
|
482
|
+
Err(_) => Ok(false)
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
},
|
|
486
|
+
Validation::Max(max) => {
|
|
487
|
+
if value.is_empty() {
|
|
488
|
+
Ok(true)
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
let normalized_value = value.replace(self.decimal_separator, ".");
|
|
492
|
+
match normalized_value.parse::<f64>() {
|
|
493
|
+
Ok(value) => Ok(value <= *max),
|
|
494
|
+
Err(_) => Ok(false)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
},
|
|
498
|
+
Validation::Values(values) => {
|
|
499
|
+
let regex_str = get_regex_string_for_values(values);
|
|
500
|
+
let regex = regex_map.get(®ex_str).unwrap();
|
|
501
|
+
Ok(regex.is_match(value))
|
|
502
|
+
}
|
|
503
|
+
Validation::None => Err(PyRuntimeError::new_err("'None' validation has no implementation"))
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
459
508
|
/// A Python module implemented in Rust.
|
|
460
509
|
#[pymodule]
|
|
461
510
|
fn csv_validation(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
@@ -597,6 +646,70 @@ mod tests {
|
|
|
597
646
|
assert!(CSVValidator::from_string(&definition).is_err());
|
|
598
647
|
}
|
|
599
648
|
|
|
649
|
+
#[test]
|
|
650
|
+
fn test_csv_validator_min_max_validation() {
|
|
651
|
+
let definition = String::from("
|
|
652
|
+
columns:
|
|
653
|
+
- name: First Column
|
|
654
|
+
- name: Second Column
|
|
655
|
+
- name: Third Column
|
|
656
|
+
min: -23
|
|
657
|
+
max: 1978
|
|
658
|
+
");
|
|
659
|
+
let validator = CSVValidator::from_string(&definition).unwrap();
|
|
660
|
+
assert!(validator.validate("test/test_file.csv").unwrap());
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
#[test]
|
|
664
|
+
fn test_csv_validator_min_max_validation_empty_values_are_ok_by_default() {
|
|
665
|
+
let definition = String::from("
|
|
666
|
+
columns:
|
|
667
|
+
- name: First Column
|
|
668
|
+
min: -23
|
|
669
|
+
max: 1978
|
|
670
|
+
- name: Second Column
|
|
671
|
+
");
|
|
672
|
+
let validator = CSVValidator::from_string(&definition).unwrap();
|
|
673
|
+
assert!(validator.validate("test/empty_values.csv").unwrap());
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
#[test]
|
|
677
|
+
fn test_list_of_values_with_special_characters() {
|
|
678
|
+
let definition = String::from("
|
|
679
|
+
columns:
|
|
680
|
+
- name: ColumnA
|
|
681
|
+
values: ['A+', '[B]', 'C$']
|
|
682
|
+
");
|
|
683
|
+
|
|
684
|
+
let path_of_file_to_validate = "test/test_file_with_special_characters.csv";
|
|
685
|
+
let file_content = "ColumnA\nA+\n[B]\nC$";
|
|
686
|
+
std::fs::write(path_of_file_to_validate, file_content).unwrap();
|
|
687
|
+
|
|
688
|
+
let validator = CSVValidator::from_string(&definition).unwrap();
|
|
689
|
+
assert!(validator.validate(path_of_file_to_validate).unwrap());
|
|
690
|
+
std::fs::remove_file(path_of_file_to_validate).unwrap();
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
#[test]
|
|
694
|
+
fn test_min_max_with_custom_decimal_separator() {
|
|
695
|
+
let definition = String::from("
|
|
696
|
+
columns:
|
|
697
|
+
- name: ColumnA
|
|
698
|
+
min: -5
|
|
699
|
+
max: 27.8
|
|
700
|
+
");
|
|
701
|
+
|
|
702
|
+
let path_of_file_to_validate = "test/test_file_with_custom_decimals.csv";
|
|
703
|
+
let file_content = "ColumnA\n0\n4,3\n27,8";
|
|
704
|
+
std::fs::write(path_of_file_to_validate, file_content).unwrap();
|
|
705
|
+
|
|
706
|
+
let mut validator = CSVValidator::from_string(&definition).unwrap();
|
|
707
|
+
validator.set_separator(String::from(";")).unwrap();
|
|
708
|
+
validator.set_decimal_separator(String::from(",")).unwrap();
|
|
709
|
+
assert!(validator.validate(path_of_file_to_validate).unwrap());
|
|
710
|
+
std::fs::remove_file(path_of_file_to_validate).unwrap();
|
|
711
|
+
}
|
|
712
|
+
|
|
600
713
|
#[test]
|
|
601
714
|
fn test_format_integer() {
|
|
602
715
|
let test_cases = vec![
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|