csv-validation 0.0.4__tar.gz → 0.0.5__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.4"
86
+ version = "0.0.5"
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.4"
3
+ version = "0.0.5"
4
4
  edition = "2021"
5
5
 
6
6
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: csv_validation
3
- Version: 0.0.4
3
+ Version: 0.0.5
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -1,3 +1,4 @@
1
+ use crate::Validation::Values;
1
2
  use std::collections::{HashMap, HashSet};
2
3
  use std::fs;
3
4
  use std::fs::File;
@@ -19,6 +20,7 @@ enum Validation {
19
20
  RegularExpression(String),
20
21
  Min(f64),
21
22
  Max(f64),
23
+ Values(Vec<String>),
22
24
  None
23
25
  }
24
26
 
@@ -38,12 +40,18 @@ struct ColumnValidationsSummary {
38
40
  validation_summaries: Vec<ValidationSummary>
39
41
  }
40
42
 
41
- /// Validate that CSV file complies with validations definition
42
43
  #[pyfunction]
43
- fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
44
- info!("Validating file {} against definition {}", path, definition_path);
44
+ fn validate_with_file(path: &str, definition_path: &str) -> PyResult<bool> {
45
+ info!("Validating file {} against definition file {}", path, definition_path);
46
+ let definition_string = fs::read_to_string(definition_path).unwrap();
47
+ validate(path, definition_string)
48
+ }
45
49
 
46
- let validations = get_validations(definition_path);
50
+ /// Validate that CSV file complies with a validations definition file
51
+ #[pyfunction]
52
+ fn validate(path: &str, definition_string: String) -> PyResult<bool> {
53
+ debug!("Validating file {} against definition:\n {}", path, definition_string);
54
+ let validations = get_validations(definition_string.as_str());
47
55
 
48
56
  // Pre-Compile and save all Regex expressions to save time in execution
49
57
  let mut regex_map = HashMap::new();
@@ -51,7 +59,11 @@ fn validate(path: &str, definition_path: &str) -> PyResult<bool> {
51
59
  for validation in &column_validation.validations {
52
60
  match validation {
53
61
  RegularExpression(regex) => {
54
- regex_map.insert(regex, Regex::new(regex.as_str()).unwrap());
62
+ regex_map.insert(regex.to_string(), Regex::new(regex.as_str()).unwrap());
63
+ },
64
+ Values(values) => {
65
+ let regex_str = get_regex_string_for_values(values);
66
+ regex_map.insert(regex_str.clone(), Regex::new(regex_str.as_str()).unwrap());
55
67
  },
56
68
  _ => continue
57
69
  }
@@ -142,7 +154,7 @@ fn build_validation_summaries_map(validations: &Vec<ColumnValidations>) -> HashM
142
154
  validation_summaries_map
143
155
  }
144
156
 
145
- fn apply_validation(value: &str, validation: &Validation, regex_map: &HashMap<&String, Regex>) -> bool {
157
+ fn apply_validation(value: &str, validation: &Validation, regex_map: &HashMap<String, Regex>) -> bool {
146
158
  match validation {
147
159
  RegularExpression(regex) => {
148
160
  let regex = regex_map.get(regex).unwrap();
@@ -160,10 +172,19 @@ fn apply_validation(value: &str, validation: &Validation, regex_map: &HashMap<&S
160
172
  Err(_) => false
161
173
  }
162
174
  },
163
- Validation::None => panic!("None validation cannot be applied")
175
+ Validation::Values(values) => {
176
+ let regex_str = get_regex_string_for_values(values);
177
+ let regex = regex_map.get(&regex_str).unwrap();
178
+ regex.is_match(value)
179
+ }
180
+ Validation::None => panic!("Validation of type 'None' cannot be applied")
164
181
  }
165
182
  }
166
183
 
184
+ fn get_regex_string_for_values(values: &Vec<String>) -> String {
185
+ values.join("|")
186
+ }
187
+
167
188
  /// Infers the file compression type and returns the corresponding buffered reader
168
189
  fn get_reader_from(path: &str) -> Reader<Box<dyn Read>> {
169
190
  let buf_reader = BufReader::new(File::open(Path::new(path)).unwrap());
@@ -184,10 +205,9 @@ fn is_gzip_file(path: &str) -> bool {
184
205
  bytes[0] == 0x1f && bytes[1] == 0x8b
185
206
  }
186
207
 
187
- fn get_validations(definition_path: &str) -> Vec<ColumnValidations> {
208
+ fn get_validations(definition_string: &str) -> Vec<ColumnValidations> {
188
209
  // Read the YAML definition with the validations
189
- let config =
190
- YamlLoader::load_from_str(fs::read_to_string(definition_path).unwrap().as_str()).unwrap();
210
+ let config = YamlLoader::load_from_str(definition_string).unwrap();
191
211
  // Get the column names list and each associated validation
192
212
  let columns = &config[0]["columns"];
193
213
  let mut column_names = vec![];
@@ -205,6 +225,13 @@ fn get_validations(definition_path: &str) -> Vec<ColumnValidations> {
205
225
  "regex" => { validation = Validation::RegularExpression(String::from(value.as_str().unwrap())); }
206
226
  "min" => { validation = Validation::Min(value.as_i64().unwrap() as f64); }
207
227
  "max" => { validation = Validation::Max(value.as_i64().unwrap() as f64); }
228
+ "values" => {
229
+ validation = Validation::Values(value.as_vec().unwrap()
230
+ .iter()
231
+ .map(|v| String::from(v.as_str().unwrap()))
232
+ .collect()
233
+ );
234
+ }
208
235
  _ => panic!("Unknown validation: {key}")
209
236
  }
210
237
 
@@ -253,13 +280,14 @@ fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<C
253
280
  fn csv_validation(m: &Bound<'_, PyModule>) -> PyResult<()> {
254
281
  pyo3_log::init();
255
282
  m.add_function(wrap_pyfunction!(validate, m)?)?;
283
+ m.add_function(wrap_pyfunction!(validate_with_file, m)?)?;
256
284
  Ok(())
257
285
  }
258
286
 
259
287
  #[cfg(test)]
260
288
  mod tests {
261
289
  use simple_logger::SimpleLogger;
262
- use crate::validate;
290
+ use crate::{validate, validate_with_file};
263
291
 
264
292
  #[test]
265
293
  fn init_logger() {
@@ -267,17 +295,41 @@ mod tests {
267
295
  }
268
296
 
269
297
  #[test]
270
- fn test_validate_csv() {
271
- assert!(validate("test/test_file.csv", "test/test_validations.yml").unwrap());
298
+ fn test_validate_csv_with_file() {
299
+ assert!(validate_with_file("test/test_file.csv", "test/test_validations.yml").unwrap());
272
300
  }
273
301
 
274
302
  #[test]
275
- fn test_validate_csv_gz() {
276
- assert!(validate("test/test_file.csv.gz", "test/test_validations.yml").unwrap());
303
+ fn test_validate_csv_gz_with_file() {
304
+ assert!(validate_with_file("test/test_file.csv.gz", "test/test_validations.yml").unwrap());
277
305
  }
278
306
 
279
307
  #[test]
280
308
  fn test_wrong_headers() {
281
- assert!(!validate("test/test_file.csv", "test/test_validations_wrong_headers.yml").unwrap());
309
+ let definition = String::from("
310
+ columns:
311
+ - name: First Column
312
+ - name: Second Column
313
+ - name: Wrong Column
314
+ - name: Expected Column Not In File
315
+ ");
316
+ assert!(!validate("test/test_file.csv", definition).unwrap());
317
+ }
318
+
319
+ #[test]
320
+ fn test_validate_csv() {
321
+ let definition = String::from("
322
+ columns:
323
+ - name: First Column
324
+ regex: ^.+$
325
+ - name: Second Column
326
+ values: [one_value, or_another]
327
+ - name: Third Column
328
+ regex: ^-?[0-9]+$
329
+ min: -23
330
+ max: 2000
331
+ ");
332
+
333
+ assert!(validate("test/test_file.csv", definition).unwrap());
282
334
  }
283
335
  }
@@ -0,0 +1,5 @@
1
+ First Column,Second Column,Third Column
2
+ first_val,one_value,67
3
+ second_val,or_another,1
4
+ third_val,or_another,1978
5
+ fourth_val,one_value,-23
@@ -1,5 +0,0 @@
1
- First Column,Second Column,Third Column
2
- first_val,other_value,67
3
- second_val,other second,1
4
- third_val,and_third,1978
5
- fourth_val,new ole,-23
@@ -1,5 +0,0 @@
1
- columns:
2
- - name: First Column
3
- - name: Second Column
4
- - name: Wrong Column
5
- - name: Expected Column Not In File
File without changes