csv-validation 0.0.13__tar.gz → 0.0.15__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.13"
86
+ version = "0.0.15"
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.13"
3
+ version = "0.0.15"
4
4
  edition = "2021"
5
5
  readme = "README.md"
6
6
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: csv_validation
3
- Version: 0.0.13
3
+ Version: 0.0.15
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -67,7 +67,7 @@ struct ColumnValidations {
67
67
  struct ValidationSummary {
68
68
  validation: Validation,
69
69
  wrong_rows: usize,
70
- wrong_values_sample: Vec<String>
70
+ samples_rownum_and_wrong_value: Vec<(usize, String)>
71
71
  }
72
72
 
73
73
  #[derive(Serialize, Deserialize)]
@@ -87,11 +87,11 @@ struct FileValidationSummary {
87
87
 
88
88
  impl ValidationSummary {
89
89
  fn get_wrong_values_details(&self) -> String {
90
- let wrong_samples = &self.wrong_values_sample
90
+ let wrong_samples = &self.samples_rownum_and_wrong_value
91
91
  .iter()
92
- .map(|s| format!("'{}'", s))
92
+ .map(|s| format!("[row {}: '{}']", s.0, s.1))
93
93
  .collect::<Vec<String>>()
94
- .join(",");
94
+ .join(", ");
95
95
  format!("Wrong Rows: {} | Sample: {}", self.wrong_rows, wrong_samples)
96
96
  }
97
97
  }
@@ -102,7 +102,7 @@ fn build_validation_summaries_map(validations: &Vec<ColumnValidations>) -> HashM
102
102
  let mut validation_summaries = Vec::new();
103
103
  for column_validation in &validation.validations {
104
104
  let validation_summary =
105
- ValidationSummary{validation: (*column_validation).clone(), wrong_rows: 0, wrong_values_sample: Vec::new()};
105
+ ValidationSummary{validation: (*column_validation).clone(), wrong_rows: 0, samples_rownum_and_wrong_value: Vec::new()};
106
106
  validation_summaries.push(validation_summary);
107
107
  }
108
108
  validation_summaries_map.insert(validation.column_name.clone(), validation_summaries);
@@ -155,7 +155,7 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
155
155
  // Read the YAML definition with the validations
156
156
  let config = YamlLoader::load_from_str(definition_string)
157
157
  .map_err(|e| PyRuntimeError::new_err(format!("Invalid YAML format: {}", e)))?;
158
-
158
+
159
159
  if config.is_empty() {
160
160
  return Err(PyRuntimeError::new_err("Empty YAML definition"));
161
161
  }
@@ -178,7 +178,7 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
178
178
  for column in columns_vec {
179
179
  let column_def = column.as_hash()
180
180
  .ok_or_else(|| PyRuntimeError::new_err("Invalid YAML: each column must be a mapping"))?;
181
-
181
+
182
182
  let mut column_name = "";
183
183
  let mut validations = vec![];
184
184
 
@@ -186,7 +186,7 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
186
186
  let key = validation_definition.0.as_str()
187
187
  .ok_or_else(|| PyRuntimeError::new_err("Invalid YAML: validation key must be a string"))?;
188
188
  let value = validation_definition.1;
189
-
189
+
190
190
  let validation = match key {
191
191
  "name" => {
192
192
  column_name = value.as_str()
@@ -197,9 +197,9 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
197
197
  "regex" => {
198
198
  let expr = value.as_str()
199
199
  .ok_or_else(|| PyRuntimeError::new_err("Regex must be a string"))?;
200
- Ok(Validation::RegularExpression {
201
- expression: String::from(expr),
202
- alias: String::from("regex")
200
+ Ok(Validation::RegularExpression {
201
+ expression: String::from(expr),
202
+ alias: String::from("regex")
203
203
  })
204
204
  }
205
205
  "min" => {
@@ -226,9 +226,9 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
226
226
  let format = value.as_str()
227
227
  .ok_or_else(|| PyRuntimeError::new_err("Format must be a string"))?;
228
228
  let regex_for_format = get_regex_for_format(format)?;
229
- Ok(Validation::RegularExpression {
230
- expression: regex_for_format,
231
- alias: format.to_string()
229
+ Ok(Validation::RegularExpression {
230
+ expression: regex_for_format,
231
+ alias: format.to_string()
232
232
  })
233
233
  }
234
234
  "extra" => {
@@ -282,7 +282,9 @@ fn get_regex_for_format(format: &str) -> PyResult<String> {
282
282
  match format {
283
283
  "integer" => Ok(String::from("^$|^-?\\d+$")),
284
284
  "positive integer" => Ok(String::from("^$|^\\d+$")),
285
+ "negative integer" => Ok(String::from("^$|^-\\d+$")),
285
286
  "decimal" | "decimal point" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?$")),
287
+ "negative decimal point" => Ok(String::from("^$|^-\\d+(\\.\\d+)?$")),
286
288
  "decimal comma" => Ok(String::from("^$|^-?\\d+(,\\d+)?$")),
287
289
  "positive decimal point" | "positive decimal" => Ok(String::from("^$|^\\d+(\\.\\d+)?$")),
288
290
  "positive decimal comma" => Ok(String::from("^$|^\\d+(,\\d+)?$")),
@@ -387,7 +389,7 @@ impl CSVValidator {
387
389
  fn from_string(definition_string: &str) -> PyResult<Self> {
388
390
  let validations = get_validations(definition_string)?;
389
391
  let mut regex_map = HashMap::new();
390
-
392
+
391
393
  // Pre-Compile and save all Regex expressions
392
394
  for column_validation in &validations {
393
395
  for validation in &column_validation.validations {
@@ -485,8 +487,10 @@ impl CSVValidator {
485
487
  .unwrap();
486
488
 
487
489
  validation_summary.wrong_rows += 1;
488
- if validation_summary.wrong_values_sample.len() < MAX_SAMPLE_SIZE as usize {
489
- validation_summary.wrong_values_sample.push(value.to_string());
490
+ if validation_summary.samples_rownum_and_wrong_value.len() < MAX_SAMPLE_SIZE as usize {
491
+ let wrong_value = value.to_string();
492
+ let wrong_rownum = validated_rows + 1;
493
+ validation_summary.samples_rownum_and_wrong_value.push((wrong_rownum, wrong_value));
490
494
  }
491
495
  }
492
496
  is_valid_file = is_valid_file && valid;
@@ -555,7 +559,7 @@ impl CSVValidator {
555
559
  info!("[✔] OK - File matches the validations");
556
560
  }
557
561
  else {
558
- info!("[✖] FAIL: File DOESN'T match all validations");
562
+ info!("[✖] FAIL: File {} DOESN'T match all validations", file_path);
559
563
  }
560
564
  info!("");
561
565
  Ok(is_valid_file)
@@ -943,7 +947,7 @@ mod tests {
943
947
 
944
948
  test_format_validation("decimal scientific", test_cases);
945
949
  }
946
-
950
+
947
951
  // Helper function to test format validations
948
952
  fn test_format_validation(format: &str, test_cases: Vec<(&str, bool)>) {
949
953
  let definition = format!("
@@ -958,16 +962,16 @@ mod tests {
958
962
  let test_csv = format!("Test\n{}", value);
959
963
  let temp_file = format!("test/temp_format_test_{}.csv", format.replace(" ", "_"));
960
964
  std::fs::write(&temp_file, test_csv).unwrap();
961
-
965
+
962
966
  let result = validator.validate(&temp_file).unwrap();
963
967
  assert_eq!(
964
- result,
965
- expected,
966
- "Format '{}' validation failed for value '{}': expected {}, got {}",
968
+ result,
969
+ expected,
970
+ "Format '{}' validation failed for value '{}': expected {}, got {}",
967
971
  format, value, expected, result
968
972
  );
969
973
 
970
974
  std::fs::remove_file(&temp_file).unwrap();
971
975
  }
972
976
  }
973
- }
977
+ }
File without changes