csv-validation 0.0.14__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.14"
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.14"
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.14
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
@@ -98,9 +98,8 @@ columns:
98
98
  - `decimal comma`: Validates decimal numbers using comma as decimal separator
99
99
  - `positive decimal point/positive decimal`: Validates positive decimal numbers using point as decimal separator
100
100
  - `positive decimal comma`: Validates positive decimal numbers using comma as decimal separator
101
- - `decimal scientific`: Validates decimal numbers in scientific notation (e.g. -23.02e-12)
102
- - `positive decimal scientific`: Validates positive decimal numbers in scientific notation (e.g. 23.02e-12)
103
- - More formats will be likely added in upcoming versions
101
+ - `decimal scientific`: Validates decimal numbers in scientific notation (e.g. 23.02e-12)
102
+ - More formats will be added in upcoming versions
104
103
 
105
104
  3. **Minimum Value** (`min`)
106
105
  - Check if numeric fields are greater than or equal to a specified value
@@ -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,12 +282,13 @@ 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+)?$")),
289
291
  "decimal scientific" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$")),
290
- "positive decimal scientific" => Ok(String::from("^$|^\\d+(\\.\\d+)?([eE][-+]?\\d+)?$")),
291
292
  _ => Err(PyRuntimeError::new_err(format!("Unknown format: {format}")))
292
293
  }
293
294
  }
@@ -388,7 +389,7 @@ impl CSVValidator {
388
389
  fn from_string(definition_string: &str) -> PyResult<Self> {
389
390
  let validations = get_validations(definition_string)?;
390
391
  let mut regex_map = HashMap::new();
391
-
392
+
392
393
  // Pre-Compile and save all Regex expressions
393
394
  for column_validation in &validations {
394
395
  for validation in &column_validation.validations {
@@ -486,8 +487,10 @@ impl CSVValidator {
486
487
  .unwrap();
487
488
 
488
489
  validation_summary.wrong_rows += 1;
489
- if validation_summary.wrong_values_sample.len() < MAX_SAMPLE_SIZE as usize {
490
- 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));
491
494
  }
492
495
  }
493
496
  is_valid_file = is_valid_file && valid;
@@ -556,7 +559,7 @@ impl CSVValidator {
556
559
  info!("[✔] OK - File matches the validations");
557
560
  }
558
561
  else {
559
- info!("[✖] FAIL: File DOESN'T match all validations");
562
+ info!("[✖] FAIL: File {} DOESN'T match all validations", file_path);
560
563
  }
561
564
  info!("");
562
565
  Ok(is_valid_file)
@@ -944,7 +947,7 @@ mod tests {
944
947
 
945
948
  test_format_validation("decimal scientific", test_cases);
946
949
  }
947
-
950
+
948
951
  // Helper function to test format validations
949
952
  fn test_format_validation(format: &str, test_cases: Vec<(&str, bool)>) {
950
953
  let definition = format!("
@@ -959,16 +962,16 @@ mod tests {
959
962
  let test_csv = format!("Test\n{}", value);
960
963
  let temp_file = format!("test/temp_format_test_{}.csv", format.replace(" ", "_"));
961
964
  std::fs::write(&temp_file, test_csv).unwrap();
962
-
965
+
963
966
  let result = validator.validate(&temp_file).unwrap();
964
967
  assert_eq!(
965
- result,
966
- expected,
967
- "Format '{}' validation failed for value '{}': expected {}, got {}",
968
+ result,
969
+ expected,
970
+ "Format '{}' validation failed for value '{}': expected {}, got {}",
968
971
  format, value, expected, result
969
972
  );
970
973
 
971
974
  std::fs::remove_file(&temp_file).unwrap();
972
975
  }
973
976
  }
974
- }
977
+ }
File without changes