csv-validation 0.0.16__tar.gz → 0.0.17__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.16 → csv_validation-0.0.17}/.github/workflows/CI.yml +6 -3
- {csv_validation-0.0.16 → csv_validation-0.0.17}/Cargo.lock +1 -1
- {csv_validation-0.0.16 → csv_validation-0.0.17}/Cargo.toml +1 -1
- {csv_validation-0.0.16 → csv_validation-0.0.17}/PKG-INFO +1 -1
- {csv_validation-0.0.16 → csv_validation-0.0.17}/build.sh +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/src/lib.rs +108 -3
- csv_validation-0.0.17/test/duplicated_unique_keys.csv +6 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/.gitignore +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/LICENSE +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/MANIFEST.in +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/README.md +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/csv_validation.pyi +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/pyproject.toml +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/test/empty_values.csv +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/test/test_file.csv +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/test/test_file.csv.gz +0 -0
- {csv_validation-0.0.16 → csv_validation-0.0.17}/test/test_validations.yml +0 -0
|
@@ -45,7 +45,7 @@ jobs:
|
|
|
45
45
|
uses: PyO3/maturin-action@v1
|
|
46
46
|
with:
|
|
47
47
|
target: ${{ matrix.platform.target }}
|
|
48
|
-
args: --release --out dist
|
|
48
|
+
args: --release --out dist -i python3.10
|
|
49
49
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
|
|
50
50
|
manylinux: auto
|
|
51
51
|
- name: Upload wheels
|
|
@@ -76,7 +76,7 @@ jobs:
|
|
|
76
76
|
uses: PyO3/maturin-action@v1
|
|
77
77
|
with:
|
|
78
78
|
target: ${{ matrix.platform.target }}
|
|
79
|
-
args: --release --out dist
|
|
79
|
+
args: --release --out dist -i python3.10
|
|
80
80
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
|
|
81
81
|
manylinux: musllinux_1_2
|
|
82
82
|
- name: Upload wheels
|
|
@@ -102,6 +102,9 @@ jobs:
|
|
|
102
102
|
architecture: ${{ matrix.platform.target }}
|
|
103
103
|
- name: Build wheels
|
|
104
104
|
uses: PyO3/maturin-action@v1
|
|
105
|
+
env:
|
|
106
|
+
# Compile for newers versions using ABI stable version
|
|
107
|
+
PYO3_USE_ABI3_FORWARD_COMPATIBILITY: 1
|
|
105
108
|
with:
|
|
106
109
|
target: ${{ matrix.platform.target }}
|
|
107
110
|
args: --release --out dist --find-interpreter
|
|
@@ -130,7 +133,7 @@ jobs:
|
|
|
130
133
|
uses: PyO3/maturin-action@v1
|
|
131
134
|
with:
|
|
132
135
|
target: ${{ matrix.platform.target }}
|
|
133
|
-
args: --release --out dist
|
|
136
|
+
args: --release --out dist -i python3.10
|
|
134
137
|
sccache: ${{ !startsWith(github.ref, 'refs/tags/') }}
|
|
135
138
|
- name: Upload wheels
|
|
136
139
|
uses: actions/upload-artifact@v4
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: csv_validation
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.17
|
|
4
4
|
Classifier: Programming Language :: Rust
|
|
5
5
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
6
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
File without changes
|
|
@@ -349,7 +349,9 @@ struct CSVValidator {
|
|
|
349
349
|
validations: Vec<ColumnValidations>,
|
|
350
350
|
regex_map: HashMap<String, Regex>,
|
|
351
351
|
separator: u8,
|
|
352
|
-
decimal_separator: char
|
|
352
|
+
decimal_separator: char,
|
|
353
|
+
// Optional list of column names that compose a unique key constraint
|
|
354
|
+
unique_key_columns: Option<Vec<String>>
|
|
353
355
|
}
|
|
354
356
|
|
|
355
357
|
// Methods Exposed to Python
|
|
@@ -361,7 +363,8 @@ impl CSVValidator {
|
|
|
361
363
|
validations: Vec::new(),
|
|
362
364
|
regex_map: HashMap::new(),
|
|
363
365
|
separator: DEFAULT_COLUMN_SEPARATOR,
|
|
364
|
-
decimal_separator: DEFAULT_DECIMAL_SEPARATOR
|
|
366
|
+
decimal_separator: DEFAULT_DECIMAL_SEPARATOR,
|
|
367
|
+
unique_key_columns: None
|
|
365
368
|
}
|
|
366
369
|
}
|
|
367
370
|
|
|
@@ -408,7 +411,28 @@ impl CSVValidator {
|
|
|
408
411
|
}
|
|
409
412
|
}
|
|
410
413
|
|
|
411
|
-
|
|
414
|
+
// Parse optional root-level unique configuration (str or [str])
|
|
415
|
+
let config = YamlLoader::load_from_str(definition_string)
|
|
416
|
+
.map_err(|e| PyRuntimeError::new_err(format!("Invalid YAML format: {}", e)))?;
|
|
417
|
+
let unique_key_columns: Option<Vec<String>> = if !config.is_empty() {
|
|
418
|
+
let unique_yaml = &config[0]["unique"];
|
|
419
|
+
if unique_yaml.is_badvalue() {
|
|
420
|
+
None
|
|
421
|
+
} else if let Some(s) = unique_yaml.as_str() {
|
|
422
|
+
Some(vec![s.to_string()])
|
|
423
|
+
} else if let Some(vec) = unique_yaml.as_vec() {
|
|
424
|
+
let cols = vec.iter().map(|y| {
|
|
425
|
+
y.as_str()
|
|
426
|
+
.ok_or_else(|| PyRuntimeError::new_err("Invalid YAML: 'unique' array must contain only strings"))
|
|
427
|
+
.map(|s| s.to_string())
|
|
428
|
+
}).collect::<PyResult<Vec<String>>>()?;
|
|
429
|
+
if cols.is_empty() { None } else { Some(cols) }
|
|
430
|
+
} else {
|
|
431
|
+
return Err(PyRuntimeError::new_err("Invalid YAML: 'unique' must be a string or an array of strings"));
|
|
432
|
+
}
|
|
433
|
+
} else { None };
|
|
434
|
+
|
|
435
|
+
Ok(CSVValidator { validations, regex_map, separator: DEFAULT_COLUMN_SEPARATOR, decimal_separator: DEFAULT_DECIMAL_SEPARATOR, unique_key_columns })
|
|
412
436
|
}
|
|
413
437
|
|
|
414
438
|
#[pyo3(text_signature = "(self, separator)")]
|
|
@@ -473,9 +497,51 @@ impl CSVValidator {
|
|
|
473
497
|
let mut is_valid_file = true;
|
|
474
498
|
let mut validated_rows = 0;
|
|
475
499
|
|
|
500
|
+
// Prepare unique key tracking if requested
|
|
501
|
+
let mut unique_key_indices: Option<Vec<usize>> = None;
|
|
502
|
+
let mut unique_key_counts: HashMap<String, usize> = HashMap::new();
|
|
503
|
+
let mut duplicated_keys_pretty_counts: HashMap<String, usize> = HashMap::new();
|
|
504
|
+
if let Some(unique_cols) = &self.unique_key_columns {
|
|
505
|
+
// Build header -> index map
|
|
506
|
+
let headers: Vec<String> = rdr.headers().unwrap().iter().map(|s| String::from(s)).collect();
|
|
507
|
+
let mut idxs = Vec::with_capacity(unique_cols.len());
|
|
508
|
+
for col in unique_cols {
|
|
509
|
+
match headers.iter().position(|h| h == col) {
|
|
510
|
+
Some(i) => idxs.push(i),
|
|
511
|
+
None => return Err(PyRuntimeError::new_err(format!("Unique key column '{}' not found among CSV headers", col)))
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
unique_key_indices = Some(idxs);
|
|
515
|
+
}
|
|
516
|
+
|
|
476
517
|
// Iterate over the CSV file and validate each row
|
|
477
518
|
for result in rdr.records() {
|
|
478
519
|
let record = result.unwrap();
|
|
520
|
+
|
|
521
|
+
// Unique key check (single-pass)
|
|
522
|
+
if let Some(indices) = &unique_key_indices {
|
|
523
|
+
// Build raw and pretty keys
|
|
524
|
+
let mut raw_parts: Vec<String> = Vec::with_capacity(indices.len());
|
|
525
|
+
let mut pretty_parts: Vec<String> = Vec::with_capacity(indices.len());
|
|
526
|
+
if let Some(unique_cols) = &self.unique_key_columns {
|
|
527
|
+
for (i, idx) in indices.iter().enumerate() {
|
|
528
|
+
let val = record.get(*idx).unwrap_or("");
|
|
529
|
+
raw_parts.push(val.to_string());
|
|
530
|
+
let col_name = &unique_cols[i];
|
|
531
|
+
pretty_parts.push(format!("{}='{}'", col_name, val));
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
let raw_key = raw_parts.join("\u{1F}"); // Use non-printable unit separator
|
|
535
|
+
let pretty_key = format!("({})", pretty_parts.join(", "));
|
|
536
|
+
let new_count = unique_key_counts.get(&raw_key).cloned().unwrap_or(0) + 1;
|
|
537
|
+
unique_key_counts.insert(raw_key, new_count);
|
|
538
|
+
if new_count >= 2 {
|
|
539
|
+
let dup_count = duplicated_keys_pretty_counts.get(&pretty_key).cloned().unwrap_or(1) + 1;
|
|
540
|
+
// Store occurrences as the actual total occurrences
|
|
541
|
+
duplicated_keys_pretty_counts.insert(pretty_key, dup_count);
|
|
542
|
+
is_valid_file = false;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
479
545
|
for next_column in zip(record.iter(), self.validations.iter()) {
|
|
480
546
|
let value = next_column.0;
|
|
481
547
|
let _column_name = &next_column.1.column_name;
|
|
@@ -525,6 +591,26 @@ impl CSVValidator {
|
|
|
525
591
|
info!("FILE: {}", file_path);
|
|
526
592
|
info!("Rows: {} | Columns: {}", validation_process_summary.total_rows, validation_process_summary.total_columns);
|
|
527
593
|
|
|
594
|
+
let mut unique_validation_ok = true;
|
|
595
|
+
// If there are duplicated keys, report them first
|
|
596
|
+
if let Some(_) = &self.unique_key_columns {
|
|
597
|
+
info!("UNIQUE KEY: column(s): {:?}", &self.unique_key_columns);
|
|
598
|
+
if !duplicated_keys_pretty_counts.is_empty() {
|
|
599
|
+
info!("");
|
|
600
|
+
info!("DUPLICATED KEYS");
|
|
601
|
+
info!("--------------------------------------------------");
|
|
602
|
+
for (key, count) in duplicated_keys_pretty_counts.iter() {
|
|
603
|
+
info!(" - {} -> occurrences: {}", key, count);
|
|
604
|
+
}
|
|
605
|
+
unique_validation_ok = false;
|
|
606
|
+
} else {
|
|
607
|
+
info!("");
|
|
608
|
+
info!("UNIQUE KEYS CHECK");
|
|
609
|
+
info!("--------------------------------------------------");
|
|
610
|
+
info!(" - No duplicates found");
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
528
614
|
let correct_columns = validation_process_summary.correct_columns.len();
|
|
529
615
|
let wrong_columns = validation_process_summary.wrong_columns.len();
|
|
530
616
|
|
|
@@ -554,6 +640,9 @@ impl CSVValidator {
|
|
|
554
640
|
}
|
|
555
641
|
}
|
|
556
642
|
|
|
643
|
+
// Adding other validation results before returning the final result
|
|
644
|
+
is_valid_file = is_valid_file && unique_validation_ok;
|
|
645
|
+
|
|
557
646
|
info!("");
|
|
558
647
|
info!("VALIDATION RESULT");
|
|
559
648
|
info!("--------------------------------------------------");
|
|
@@ -768,6 +857,22 @@ mod tests {
|
|
|
768
857
|
assert!(!validator.validate("test/empty_values.csv").unwrap());
|
|
769
858
|
}
|
|
770
859
|
|
|
860
|
+
#[test]
|
|
861
|
+
fn test_unique_key_validation() {
|
|
862
|
+
let definition_with_non_empty_column = String::from("
|
|
863
|
+
unique: First Column
|
|
864
|
+
columns:
|
|
865
|
+
- name: First Column
|
|
866
|
+
- name: Second Column
|
|
867
|
+
");
|
|
868
|
+
let validator = CSVValidator::from_string(&definition_with_non_empty_column).unwrap();
|
|
869
|
+
|
|
870
|
+
// Validation is OK as there are no duplicated keys
|
|
871
|
+
assert!(validator.validate("test/empty_values.csv").unwrap());
|
|
872
|
+
// Validation is not OK as the first column contains the value 456 twice
|
|
873
|
+
assert!(!validator.validate("test/duplicated_unique_keys.csv").unwrap());
|
|
874
|
+
}
|
|
875
|
+
|
|
771
876
|
#[test]
|
|
772
877
|
fn test_csv_validator_invalid_format() {
|
|
773
878
|
let definition = String::from("
|
|
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
|