csv-validation 0.0.8__tar.gz → 0.0.10__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.8 → csv_validation-0.0.10}/Cargo.lock +1 -1
- {csv_validation-0.0.8 → csv_validation-0.0.10}/Cargo.toml +1 -1
- {csv_validation-0.0.8 → csv_validation-0.0.10}/PKG-INFO +33 -9
- {csv_validation-0.0.8 → csv_validation-0.0.10}/README.md +32 -8
- {csv_validation-0.0.8 → csv_validation-0.0.10}/src/lib.rs +90 -19
- {csv_validation-0.0.8 → csv_validation-0.0.10}/.github/workflows/CI.yml +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/.gitignore +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/LICENSE +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/build.sh +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/pyproject.toml +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/test/empty_values.csv +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/test/test_file.csv +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/test/test_file.csv.gz +0 -0
- {csv_validation-0.0.8 → csv_validation-0.0.10}/test/test_validations.yml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: csv_validation
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.10
|
|
4
4
|
Classifier: Programming Language :: Rust
|
|
5
5
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
6
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
@@ -8,7 +8,7 @@ License-File: LICENSE
|
|
|
8
8
|
Requires-Python: >=3.8
|
|
9
9
|
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
10
10
|
|
|
11
|
-
# CSV Validation
|
|
11
|
+
# CSV Validation Library
|
|
12
12
|
|
|
13
13
|
Blazing fast format validations for your CSV files
|
|
14
14
|
|
|
@@ -59,6 +59,10 @@ columns:
|
|
|
59
59
|
"""
|
|
60
60
|
|
|
61
61
|
validator = CSVValidator.from_string(validation_rules)
|
|
62
|
+
# Optionally set a custom column separator
|
|
63
|
+
validator.set_separator(";") # Default is comma (,)
|
|
64
|
+
|
|
65
|
+
# Validate a CSV file
|
|
62
66
|
is_valid = validator.validate("data.csv")
|
|
63
67
|
```
|
|
64
68
|
|
|
@@ -126,13 +130,33 @@ columns:
|
|
|
126
130
|
# other validations...
|
|
127
131
|
```
|
|
128
132
|
|
|
129
|
-
|
|
133
|
+
### Supported Formats
|
|
134
|
+
|
|
135
|
+
- `integer`: Any integer number (positive or negative)
|
|
136
|
+
- `positive integer`: Any positive integer number
|
|
137
|
+
- `decimal`: Any decimal number (positive or negative)
|
|
138
|
+
- `positive decimal point`: Any positive decimal number using point as decimal separator
|
|
139
|
+
- `positive decimal comma`: Any positive decimal number using comma as decimal separator
|
|
140
|
+
- `decimal scientific`: Any decimal number in scientific notation
|
|
141
|
+
- `non_empty`: Any non-empty value
|
|
142
|
+
|
|
143
|
+
### Column Separators
|
|
144
|
+
|
|
145
|
+
By default, the library uses comma (,) as the column separator. You can change this using the `set_separator` method:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
validator = CSVValidator.from_file("rules.yml")
|
|
149
|
+
validator.set_separator(";") # Use semicolon as separator
|
|
150
|
+
validator.set_separator("\t") # Use tab as separator
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Error Handling
|
|
130
154
|
|
|
131
|
-
The
|
|
132
|
-
-
|
|
133
|
-
-
|
|
134
|
-
- Sample of invalid values
|
|
135
|
-
-
|
|
155
|
+
The library provides detailed validation reports through Python's logging system. When a validation fails, you'll get information about:
|
|
156
|
+
- Which columns failed validation
|
|
157
|
+
- What type of validation failed
|
|
158
|
+
- Sample of invalid values
|
|
159
|
+
- Number of rows that failed each validation
|
|
136
160
|
|
|
137
161
|
## Development
|
|
138
162
|
|
|
@@ -142,7 +166,7 @@ The validation process provides detailed feedback including:
|
|
|
142
166
|
- Python 3.6+ (for Python bindings)
|
|
143
167
|
- Cargo and standard Rust tooling
|
|
144
168
|
|
|
145
|
-
### Building from Source
|
|
169
|
+
### Building from Source (Rust version)
|
|
146
170
|
|
|
147
171
|
```bash
|
|
148
172
|
# Clone the repository
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# CSV Validation
|
|
1
|
+
# CSV Validation Library
|
|
2
2
|
|
|
3
3
|
Blazing fast format validations for your CSV files
|
|
4
4
|
|
|
@@ -49,6 +49,10 @@ columns:
|
|
|
49
49
|
"""
|
|
50
50
|
|
|
51
51
|
validator = CSVValidator.from_string(validation_rules)
|
|
52
|
+
# Optionally set a custom column separator
|
|
53
|
+
validator.set_separator(";") # Default is comma (,)
|
|
54
|
+
|
|
55
|
+
# Validate a CSV file
|
|
52
56
|
is_valid = validator.validate("data.csv")
|
|
53
57
|
```
|
|
54
58
|
|
|
@@ -116,13 +120,33 @@ columns:
|
|
|
116
120
|
# other validations...
|
|
117
121
|
```
|
|
118
122
|
|
|
119
|
-
|
|
123
|
+
### Supported Formats
|
|
124
|
+
|
|
125
|
+
- `integer`: Any integer number (positive or negative)
|
|
126
|
+
- `positive integer`: Any positive integer number
|
|
127
|
+
- `decimal`: Any decimal number (positive or negative)
|
|
128
|
+
- `positive decimal point`: Any positive decimal number using point as decimal separator
|
|
129
|
+
- `positive decimal comma`: Any positive decimal number using comma as decimal separator
|
|
130
|
+
- `decimal scientific`: Any decimal number in scientific notation
|
|
131
|
+
- `non_empty`: Any non-empty value
|
|
132
|
+
|
|
133
|
+
### Column Separators
|
|
134
|
+
|
|
135
|
+
By default, the library uses comma (,) as the column separator. You can change this using the `set_separator` method:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
validator = CSVValidator.from_file("rules.yml")
|
|
139
|
+
validator.set_separator(";") # Use semicolon as separator
|
|
140
|
+
validator.set_separator("\t") # Use tab as separator
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Error Handling
|
|
120
144
|
|
|
121
|
-
The
|
|
122
|
-
-
|
|
123
|
-
-
|
|
124
|
-
- Sample of invalid values
|
|
125
|
-
-
|
|
145
|
+
The library provides detailed validation reports through Python's logging system. When a validation fails, you'll get information about:
|
|
146
|
+
- Which columns failed validation
|
|
147
|
+
- What type of validation failed
|
|
148
|
+
- Sample of invalid values
|
|
149
|
+
- Number of rows that failed each validation
|
|
126
150
|
|
|
127
151
|
## Development
|
|
128
152
|
|
|
@@ -132,7 +156,7 @@ The validation process provides detailed feedback including:
|
|
|
132
156
|
- Python 3.6+ (for Python bindings)
|
|
133
157
|
- Cargo and standard Rust tooling
|
|
134
158
|
|
|
135
|
-
### Building from Source
|
|
159
|
+
### Building from Source (Rust version)
|
|
136
160
|
|
|
137
161
|
```bash
|
|
138
162
|
# Clone the repository
|
|
@@ -5,7 +5,7 @@ use std::fs::File;
|
|
|
5
5
|
use std::io::{BufReader, Read};
|
|
6
6
|
use std::iter::zip;
|
|
7
7
|
use std::path::Path;
|
|
8
|
-
use csv::Reader;
|
|
8
|
+
use csv::{Reader, ReaderBuilder};
|
|
9
9
|
use flate2::bufread::GzDecoder;
|
|
10
10
|
use log::{debug, error, info};
|
|
11
11
|
use pyo3::exceptions::{PyRuntimeError};
|
|
@@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize};
|
|
|
16
16
|
use crate::Validation::{RegularExpression};
|
|
17
17
|
|
|
18
18
|
const MAX_SAMPLE_SIZE:u16 = 10;
|
|
19
|
+
const DEFAULT_COLUMN_SEPARATOR:u8 = b',';
|
|
19
20
|
|
|
20
21
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
21
22
|
enum Validation {
|
|
@@ -96,15 +97,25 @@ fn apply_validation(value: &str, validation: &Validation, regex_map: &HashMap<St
|
|
|
96
97
|
Ok(regex.is_match(value))
|
|
97
98
|
},
|
|
98
99
|
Validation::Min(min) => {
|
|
99
|
-
|
|
100
|
-
Ok(
|
|
101
|
-
|
|
100
|
+
if value.is_empty() {
|
|
101
|
+
Ok(true)
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
match value.parse::<f64>() {
|
|
105
|
+
Ok(value) => Ok(value >= *min),
|
|
106
|
+
Err(_) => Ok(false)
|
|
107
|
+
}
|
|
102
108
|
}
|
|
103
109
|
},
|
|
104
110
|
Validation::Max(max) => {
|
|
105
|
-
|
|
106
|
-
Ok(
|
|
107
|
-
|
|
111
|
+
if value.is_empty() {
|
|
112
|
+
Ok(true)
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
match value.parse::<f64>() {
|
|
116
|
+
Ok(value) => Ok(value <= *max),
|
|
117
|
+
Err(_) => Ok(false)
|
|
118
|
+
}
|
|
108
119
|
}
|
|
109
120
|
},
|
|
110
121
|
Validation::Values(values) => {
|
|
@@ -121,16 +132,20 @@ fn get_regex_string_for_values(values: &Vec<String>) -> String {
|
|
|
121
132
|
}
|
|
122
133
|
|
|
123
134
|
/// Infers the file compression type and returns the corresponding buffered reader
|
|
124
|
-
fn get_reader_from(path: &str) -> PyResult<Reader<Box<dyn Read>>> {
|
|
135
|
+
fn get_reader_from(path: &str, separator: u8) -> PyResult<Reader<Box<dyn Read>>> {
|
|
125
136
|
let buf_reader = BufReader::new(File::open(Path::new(path))?);
|
|
126
137
|
if is_gzip_file(path)? {
|
|
127
138
|
debug!("File is gzipped");
|
|
128
139
|
let read_capacity = 10 * 1024_usize.pow(2);
|
|
129
140
|
let reader = BufReader::with_capacity(read_capacity, GzDecoder::new(buf_reader));
|
|
130
|
-
Ok(
|
|
141
|
+
Ok(ReaderBuilder::new()
|
|
142
|
+
.delimiter(separator)
|
|
143
|
+
.from_reader(Box::new(reader)))
|
|
131
144
|
}
|
|
132
145
|
else {
|
|
133
|
-
Ok(
|
|
146
|
+
Ok(ReaderBuilder::new()
|
|
147
|
+
.delimiter(separator)
|
|
148
|
+
.from_reader(Box::new(buf_reader)))
|
|
134
149
|
}
|
|
135
150
|
}
|
|
136
151
|
|
|
@@ -258,7 +273,8 @@ fn get_regex_for_format(format: &str) -> PyResult<String> {
|
|
|
258
273
|
"integer" => Ok(String::from("^$|^-?\\d+$")),
|
|
259
274
|
"positive integer" => Ok(String::from("^$|^\\d+$")),
|
|
260
275
|
"decimal" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?$")),
|
|
261
|
-
"positive decimal" => Ok(String::from("^$|^\\d+(\\.\\d+)?$")),
|
|
276
|
+
"positive decimal point" | "positive decimal" => Ok(String::from("^$|^\\d+(\\.\\d+)?$")),
|
|
277
|
+
"positive decimal comma" => Ok(String::from("^$|^\\d+(,\\d+)?$")),
|
|
262
278
|
"decimal scientific" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$")),
|
|
263
279
|
"non_empty" => Ok(String::from("^.+$")),
|
|
264
280
|
_ => Err(PyRuntimeError::new_err(format!("Unknown format: {format}")))
|
|
@@ -276,15 +292,17 @@ fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<C
|
|
|
276
292
|
|
|
277
293
|
if expected_column_names != headers {
|
|
278
294
|
if expected_column_names.len() != headers.len() {
|
|
295
|
+
info!("The number of columns in the CSV file doesn't match the validations:");
|
|
279
296
|
let expected_columns_set: HashSet<String> = expected_column_names.iter().cloned().collect();
|
|
280
297
|
let headers_set: HashSet<String> = headers.iter().cloned().collect();
|
|
281
|
-
|
|
282
|
-
|
|
298
|
+
info!("These column names in the CSV file were not expected: {:?}", headers_set.difference(&expected_columns_set));
|
|
299
|
+
info!("These expected column names were missing in the CSV file: {:?}", expected_columns_set.difference(&headers_set));
|
|
283
300
|
}
|
|
284
301
|
else {
|
|
302
|
+
info!("The CSV file has the same number of columns than the validations but some names are different:");
|
|
285
303
|
for (expected_column, header) in zip(expected_column_names, headers) {
|
|
286
304
|
if expected_column != header {
|
|
287
|
-
|
|
305
|
+
info!("{:?} != {:?}", expected_column, header);
|
|
288
306
|
}
|
|
289
307
|
}
|
|
290
308
|
}
|
|
@@ -296,7 +314,8 @@ fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<C
|
|
|
296
314
|
#[pyclass]
|
|
297
315
|
struct CSVValidator {
|
|
298
316
|
validations: Vec<ColumnValidations>,
|
|
299
|
-
regex_map: HashMap<String, Regex
|
|
317
|
+
regex_map: HashMap<String, Regex>,
|
|
318
|
+
separator: u8
|
|
300
319
|
}
|
|
301
320
|
|
|
302
321
|
#[pymethods]
|
|
@@ -305,7 +324,8 @@ impl CSVValidator {
|
|
|
305
324
|
fn new() -> Self {
|
|
306
325
|
CSVValidator {
|
|
307
326
|
validations: Vec::new(),
|
|
308
|
-
regex_map: HashMap::new()
|
|
327
|
+
regex_map: HashMap::new(),
|
|
328
|
+
separator: DEFAULT_COLUMN_SEPARATOR
|
|
309
329
|
}
|
|
310
330
|
}
|
|
311
331
|
|
|
@@ -336,12 +356,22 @@ impl CSVValidator {
|
|
|
336
356
|
}
|
|
337
357
|
}
|
|
338
358
|
|
|
339
|
-
Ok(CSVValidator { validations, regex_map })
|
|
359
|
+
Ok(CSVValidator { validations, regex_map, separator: DEFAULT_COLUMN_SEPARATOR })
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
fn set_separator(&mut self, separator: String) -> PyResult<()> {
|
|
363
|
+
if separator.len() == 1 {
|
|
364
|
+
self.separator = separator.chars().next().unwrap() as u8;
|
|
365
|
+
Ok(())
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
Err(PyRuntimeError::new_err(format!("Wrong separator {separator}. It can only have one character")))
|
|
369
|
+
}
|
|
340
370
|
}
|
|
341
371
|
|
|
342
372
|
fn validate(&self, file_path: &str) -> PyResult<bool> {
|
|
343
373
|
// Build the CSV reader
|
|
344
|
-
let mut rdr = get_reader_from(file_path)?;
|
|
374
|
+
let mut rdr = get_reader_from(file_path, self.separator)?;
|
|
345
375
|
|
|
346
376
|
// First validation: Ensure column names and order are exactly as expected
|
|
347
377
|
if validate_column_names(&mut rdr, &self.validations)? {
|
|
@@ -396,6 +426,7 @@ impl CSVValidator {
|
|
|
396
426
|
// TODO: Decide how to return the results of the validations
|
|
397
427
|
let _validation_result_json = serde_json::to_string(&column_validation_summaries).unwrap();
|
|
398
428
|
|
|
429
|
+
let mut failed_columns = HashMap::new();
|
|
399
430
|
info!("VALIDATIONS SUMMARY");
|
|
400
431
|
info!("==================================================================================");
|
|
401
432
|
info!("Rows: {} | Columns: {}", validated_rows, column_validation_summaries.len());
|
|
@@ -409,6 +440,13 @@ impl CSVValidator {
|
|
|
409
440
|
};
|
|
410
441
|
info!("\tValidation {:?} => Wrong Rows: {}{}", validation_summary.validation,
|
|
411
442
|
validation_summary.wrong_rows, wrong_values_sample);
|
|
443
|
+
if validation_summary.wrong_rows > 0 {
|
|
444
|
+
let column_name = &column_validation_summary.column_name;
|
|
445
|
+
if !failed_columns.contains_key(column_name) {
|
|
446
|
+
failed_columns.insert(column_name.clone(), vec!());
|
|
447
|
+
}
|
|
448
|
+
failed_columns.get_mut(column_name).unwrap().push(validation_summary);
|
|
449
|
+
}
|
|
412
450
|
}
|
|
413
451
|
}
|
|
414
452
|
|
|
@@ -416,7 +454,13 @@ impl CSVValidator {
|
|
|
416
454
|
info!("OK: File matches the validations");
|
|
417
455
|
}
|
|
418
456
|
else {
|
|
419
|
-
info!("NO OK: File DOESN'T match validations");
|
|
457
|
+
info!("NO OK: File DOESN'T match validations. Failed Validations:");
|
|
458
|
+
for (column_name, failed_validations) in failed_columns.into_iter() {
|
|
459
|
+
info!("COLUMN '{}' :", column_name);
|
|
460
|
+
for validation_summary in failed_validations {
|
|
461
|
+
info!("\tValidation {:?} => Wrong Rows: {}", validation_summary.validation, validation_summary.wrong_rows)
|
|
462
|
+
}
|
|
463
|
+
}
|
|
420
464
|
}
|
|
421
465
|
Ok(is_valid_file)
|
|
422
466
|
}
|
|
@@ -563,6 +607,33 @@ mod tests {
|
|
|
563
607
|
assert!(CSVValidator::from_string(&definition).is_err());
|
|
564
608
|
}
|
|
565
609
|
|
|
610
|
+
#[test]
|
|
611
|
+
fn test_csv_validator_min_max_validation() {
|
|
612
|
+
let definition = String::from("
|
|
613
|
+
columns:
|
|
614
|
+
- name: First Column
|
|
615
|
+
- name: Second Column
|
|
616
|
+
- name: Third Column
|
|
617
|
+
min: -23
|
|
618
|
+
max: 1978
|
|
619
|
+
");
|
|
620
|
+
let validator = CSVValidator::from_string(&definition).unwrap();
|
|
621
|
+
assert!(validator.validate("test/test_file.csv").unwrap());
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
#[test]
|
|
625
|
+
fn test_csv_validator_min_max_validation_empty_values_are_ok_by_default() {
|
|
626
|
+
let definition = String::from("
|
|
627
|
+
columns:
|
|
628
|
+
- name: First Column
|
|
629
|
+
min: -23
|
|
630
|
+
max: 1978
|
|
631
|
+
- name: Second Column
|
|
632
|
+
");
|
|
633
|
+
let validator = CSVValidator::from_string(&definition).unwrap();
|
|
634
|
+
assert!(validator.validate("test/empty_values.csv").unwrap());
|
|
635
|
+
}
|
|
636
|
+
|
|
566
637
|
#[test]
|
|
567
638
|
fn test_format_integer() {
|
|
568
639
|
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
|