csv-validation 0.0.12__tar.gz → 0.0.14__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.12"
86
+ version = "0.0.14"
87
87
  dependencies = [
88
88
  "csv",
89
89
  "flate2",
@@ -92,7 +92,6 @@ dependencies = [
92
92
  "pyo3-log",
93
93
  "regex",
94
94
  "serde",
95
- "serde_json",
96
95
  "simple_logger",
97
96
  "yaml-rust2",
98
97
  ]
@@ -389,18 +388,6 @@ dependencies = [
389
388
  "syn",
390
389
  ]
391
390
 
392
- [[package]]
393
- name = "serde_json"
394
- version = "1.0.140"
395
- source = "registry+https://github.com/rust-lang/crates.io-index"
396
- checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
397
- dependencies = [
398
- "itoa",
399
- "memchr",
400
- "ryu",
401
- "serde",
402
- ]
403
-
404
391
  [[package]]
405
392
  name = "simple_logger"
406
393
  version = "5.0.0"
@@ -1,7 +1,8 @@
1
1
  [package]
2
2
  name = "csv_validation"
3
- version = "0.0.12"
3
+ version = "0.0.14"
4
4
  edition = "2021"
5
+ readme = "README.md"
5
6
 
6
7
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8
  [lib]
@@ -15,7 +16,6 @@ log = "0.4.26"
15
16
  pyo3 = "0.23.3"
16
17
  pyo3-log = "0.12.1"
17
18
  regex = "1.11.1"
18
- serde_json = "1.0.140"
19
19
  simple_logger = "5.0.0"
20
20
  yaml-rust2 = "0.10.0"
21
21
  serde = { version = "1.0.218", features = ["derive"] }
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: csv_validation
3
+ Version: 0.0.14
4
+ Classifier: Programming Language :: Rust
5
+ Classifier: Programming Language :: Python :: Implementation :: CPython
6
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.8
@@ -1,13 +1,3 @@
1
- Metadata-Version: 2.4
2
- Name: csv_validation
3
- Version: 0.0.12
4
- Classifier: Programming Language :: Rust
5
- Classifier: Programming Language :: Python :: Implementation :: CPython
6
- Classifier: Programming Language :: Python :: Implementation :: PyPy
7
- License-File: LICENSE
8
- Requires-Python: >=3.8
9
- Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
10
-
11
1
  # CSV Validation Library
12
2
 
13
3
  Blazing fast format validations for your CSV files
@@ -104,12 +94,13 @@ columns:
104
94
  - Available formats:
105
95
  - `integer`: Validates any integer number (positive or negative)
106
96
  - `positive integer`: Validates positive integer numbers
107
- - `decimal`: Validates any decimal number (positive or negative) using point as decimal separator
97
+ - `decimal/decimal point`: Validates any decimal number (positive or negative) using point as decimal separator
98
+ - `decimal comma`: Validates decimal numbers using comma as decimal separator
108
99
  - `positive decimal point/positive decimal`: Validates positive decimal numbers using point as decimal separator
109
100
  - `positive decimal comma`: Validates positive decimal numbers using comma as decimal separator
110
- - `decimal scientific`: Validates decimal numbers in scientific notation (e.g. 23.02e-12)
111
- - `non_empty`: Validates that field contains at least one character
112
- - More formats will be added in upcoming versions
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
113
104
 
114
105
  3. **Minimum Value** (`min`)
115
106
  - Check if numeric fields are greater than or equal to a specified value
@@ -120,7 +111,12 @@ columns:
120
111
  5. **Value Set** (`values`)
121
112
  - Ensure fields only contain values from a predefined set
122
113
 
123
- You can add as many validations as you want for the same column, but take into account that the
114
+ 6. **Extra** (`extra`)
115
+ - Extra validations that normally complement one of the other types
116
+ - Available extras:
117
+ - `non_empty`: Validates that field contains at least one character
118
+
119
+ You can add as many validation types as you want for the same column, but take into account that the
124
120
  column only will be considered correct if all the validations are OK.
125
121
 
126
122
  ## Global Validations
@@ -130,7 +126,7 @@ column only will be considered correct if all the validations are OK.
130
126
  Empty values (empty string '') are considered correct and accepted by default.
131
127
  However, if your data must always have some content,
132
128
  you can add a global `empty_not_ok` flag at the root level of your YAML definition to automatically
133
- add the `format: non_empty` validation to all columns:
129
+ add the `extra: non_empty` validation to all columns:
134
130
 
135
131
  ```yaml
136
132
  empty_not_ok: true
@@ -166,6 +162,40 @@ The library provides detailed validation reports through Python's logging system
166
162
  - Sample of invalid values
167
163
  - Number of rows that failed each validation
168
164
 
165
+ ### Example of validation report
166
+
167
+ ```
168
+ VALIDATIONS SUMMARY
169
+ ==================================================
170
+ FILE: /test.csv
171
+ Rows: 232230 | Columns: 5
172
+
173
+ CORRECT COLUMNS: 3/5
174
+ --------------------------------------------------
175
+ - County: [✔] OK
176
+ ✔ - RegularExpression { expression: "^.*$", alias: "regex" }
177
+ - City: [✔] OK
178
+ ✔ - RegularExpression { expression: "^.*$", alias: "regex" }
179
+ - Model Year: [✔] OK
180
+ ✔ - RegularExpression { expression: "^[0-9]+$", alias: "regex" }
181
+ ✔ - Min(1999.0)
182
+ ✔ - Max(2025.78)
183
+
184
+ WRONG COLUMNS: 2/5
185
+ --------------------------------------------------
186
+ - State: [✖] FAIL
187
+ ✖ - Values(["WA", "OR", "NY", "DC", "CA", "TX", "FL", "OK", "MO", "KS", "VA", "MA", "MO", "NC", "IL", "AL", "WY", "CO", "PA", "WI", "MD", "NV", "AZ"])
188
+ "Wrong Rows: 93 | Sample: 'GA','NJ','CT','NJ','CT','CT','CT','NE','CT','NH'"
189
+ - Postal Code: [✖] FAIL
190
+ ✔ - RegularExpression { expression: "^$|^-?\\d+$", alias: "integer" }
191
+ ✖ - RegularExpression { expression: "^.+$", alias: "non_empty" }
192
+ "Wrong Rows: 4 | Sample: '','','',''"
193
+
194
+ VALIDATION RESULT
195
+ --------------------------------------------------
196
+ [✖] FAIL: File DOESN'T match all validations
197
+ ```
198
+
169
199
  ## Development
170
200
 
171
201
  ### Prerequisites
@@ -223,4 +253,4 @@ SOFTWARE.
223
253
 
224
254
  ## Contributing
225
255
 
226
- Contributions are welcome! Please feel free to submit a Pull Request.
256
+ Contributions are welcome! Please feel free to submit a Pull Request.
@@ -231,6 +231,15 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
231
231
  alias: format.to_string()
232
232
  })
233
233
  }
234
+ "extra" => {
235
+ let extra = value.as_str()
236
+ .ok_or_else(|| PyRuntimeError::new_err("Extra must be a string"))?;
237
+ let regex_for_extra = get_regex_for_extra(extra)?;
238
+ Ok(Validation::RegularExpression {
239
+ expression: regex_for_extra,
240
+ alias: extra.to_string()
241
+ })
242
+ }
234
243
  _ => Err(PyRuntimeError::new_err(format!("Unknown validation: {key}")))
235
244
  }?;
236
245
 
@@ -252,10 +261,7 @@ fn get_validations(definition_string: &str) -> PyResult<Vec<ColumnValidations>>
252
261
  let empty_not_ok =
253
262
  config[0]["empty_not_ok"].as_bool().map_or(false, |value| value);
254
263
  if empty_not_ok {
255
- let non_empty_validation = RegularExpression {
256
- expression: String::from("^.+$"),
257
- alias: String::from("non_empty")
258
- };
264
+ let non_empty_validation = validation_for_non_empty();
259
265
  for column_validations in column_validations.iter_mut() {
260
266
  column_validations.validations.push(non_empty_validation.clone());
261
267
  }
@@ -276,15 +282,34 @@ fn get_regex_for_format(format: &str) -> PyResult<String> {
276
282
  match format {
277
283
  "integer" => Ok(String::from("^$|^-?\\d+$")),
278
284
  "positive integer" => Ok(String::from("^$|^\\d+$")),
279
- "decimal" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?$")),
285
+ "decimal" | "decimal point" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?$")),
286
+ "decimal comma" => Ok(String::from("^$|^-?\\d+(,\\d+)?$")),
280
287
  "positive decimal point" | "positive decimal" => Ok(String::from("^$|^\\d+(\\.\\d+)?$")),
281
288
  "positive decimal comma" => Ok(String::from("^$|^\\d+(,\\d+)?$")),
282
289
  "decimal scientific" => Ok(String::from("^$|^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$")),
283
- "non_empty" => Ok(String::from("^.+$")),
290
+ "positive decimal scientific" => Ok(String::from("^$|^\\d+(\\.\\d+)?([eE][-+]?\\d+)?$")),
284
291
  _ => Err(PyRuntimeError::new_err(format!("Unknown format: {format}")))
285
292
  }
286
293
  }
287
294
 
295
+ fn get_regex_for_extra(extra: &str) -> PyResult<String> {
296
+ match extra {
297
+ "non_empty" => Ok(regex_for_non_empty()),
298
+ _ => Err(PyRuntimeError::new_err(format!("Unknown extra: {extra}")))
299
+ }
300
+ }
301
+
302
+ fn validation_for_non_empty() -> Validation {
303
+ RegularExpression {
304
+ expression: regex_for_non_empty(),
305
+ alias: String::from("non_empty")
306
+ }
307
+ }
308
+
309
+ fn regex_for_non_empty() -> String {
310
+ String::from("^.+$")
311
+ }
312
+
288
313
  fn validate_column_names(reader: &mut Reader<Box<dyn Read>>, validations: &Vec<ColumnValidations>) -> PyResult<bool> {
289
314
  info!("Validating column names and order...");
290
315
  let expected_column_names = validations.iter()
@@ -723,6 +748,21 @@ mod tests {
723
748
  assert!(!validator.validate("test/empty_values.csv").unwrap());
724
749
  }
725
750
 
751
+ #[test]
752
+ fn test_extra_non_empty_column() {
753
+ let definition_with_non_empty_column = String::from("
754
+ columns:
755
+ - name: First Column
756
+ format: positive integer
757
+ extra: non_empty
758
+ - name: Second Column
759
+ values: ['A', 'B', 'C']
760
+ ");
761
+ let validator = CSVValidator::from_string(&definition_with_non_empty_column).unwrap();
762
+ // Validation is not OK as the second column contains empty values
763
+ assert!(!validator.validate("test/empty_values.csv").unwrap());
764
+ }
765
+
726
766
  #[test]
727
767
  fn test_csv_validator_invalid_format() {
728
768
  let definition = String::from("
@@ -1,216 +0,0 @@
1
- # CSV Validation Library
2
-
3
- Blazing fast format validations for your CSV files
4
-
5
- This is a Python lib with a Rust core that will allow you to validate huge CSV files (GBs) in seconds (or in a few minutes for really huge files) using a minimal amount of memory.
6
-
7
- ## Features
8
-
9
- - ✨ Validate both plain and gzipped CSV files
10
- - 🔍 Multiple validation types supported:
11
- - Correct column name and order
12
- - Regular expressions
13
- - Well-known formats (integer, decimal, etc.)
14
- - Minimum/Maximum numerical value checks
15
- - Value set validation (allowed values)
16
- - 🦀 + 🐍 Rust lib with Python bindings included
17
- - 📝 Detailed validation summaries with sample invalid values
18
- - 🚀 High performance with optimizations like regex pre-compilation
19
- - 📊 Support for large CSV files
20
-
21
- ## Installation
22
-
23
- ### Python
24
-
25
- ```bash
26
- pip install csv_validation
27
- ```
28
-
29
- ## Usage
30
-
31
- ### Python
32
-
33
- #### You can provide a file with the validation rules
34
- ```python
35
- from csv_validation import CSVValidator
36
-
37
- validator = CSVValidator.from_file("validation_rules.yaml")
38
- is_valid = validator.validate("data.csv")
39
- ```
40
-
41
- #### You can also create a validator from a string
42
- ```python
43
- validation_rules = """
44
- columns:
45
- - name: Name
46
- regex: ^[A-Za-z\s]{2,50}$
47
- - name: Age
48
- format: positive_integer
49
- max: 120
50
- """
51
-
52
- validator = CSVValidator.from_string(validation_rules)
53
- # Optionally set a custom column separator
54
- validator.set_separator(";") # Default is comma (,)
55
-
56
- # Validate a CSV file
57
- is_valid = validator.validate("data.csv")
58
- ```
59
-
60
- ### Validation Definition Format
61
-
62
- Create a small, easy-to-read YAML file with your validation rules. Example:
63
-
64
- ```yaml
65
- columns:
66
- - name: Name
67
- regex: ^[A-Za-z\s]{2,50}$ # Letters and spaces, 2-50 characters
68
- - name: Family Name
69
- regex: ^[A-Za-z\s'-]{2,50}$ # Letters, spaces, hyphens and apostrophes
70
- - name: Age
71
- format: positive_integer # Using predefined format instead of custom regex
72
- max: 120
73
- - name: Salary
74
- format: integer # Allows negative integers too
75
- min: 20000
76
- - name: Email
77
- regex: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ # Standard email format
78
- - name: Phone
79
- regex: ^\+?[0-9]{10,15}$ # International phone format with optional +
80
- - name: Status
81
- values: [active, inactive, pending, suspended] # Only these values are allowed
82
- - name: Gender
83
- values: [M, F, NB, O] # M: Male, F: Female, NB: Non-Binary, O: Other
84
- ```
85
-
86
- ## Validation Types
87
-
88
- 1. **Regular Expression** (`regex`)
89
- - Validate fields against custom regex patterns
90
-
91
- 2. **Format** (`format`)
92
- - Predefined formats for common validations (This is the recommended way to validate numeric fields)
93
- - In the background, the library uses regex patterns to validate formats.
94
- - Available formats:
95
- - `integer`: Validates any integer number (positive or negative)
96
- - `positive integer`: Validates positive integer numbers
97
- - `decimal`: Validates any decimal number (positive or negative) using point as decimal separator
98
- - `positive decimal point/positive decimal`: Validates positive decimal numbers using point as decimal separator
99
- - `positive decimal comma`: Validates positive decimal numbers using comma as decimal separator
100
- - `decimal scientific`: Validates decimal numbers in scientific notation (e.g. 23.02e-12)
101
- - `non_empty`: Validates that field contains at least one character
102
- - More formats will be added in upcoming versions
103
-
104
- 3. **Minimum Value** (`min`)
105
- - Check if numeric fields are greater than or equal to a specified value
106
-
107
- 4. **Maximum Value** (`max`)
108
- - Check if numeric fields are less than or equal to a specified value
109
-
110
- 5. **Value Set** (`values`)
111
- - Ensure fields only contain values from a predefined set
112
-
113
- You can add as many validations as you want for the same column, but take into account that the
114
- column only will be considered correct if all the validations are OK.
115
-
116
- ## Global Validations
117
-
118
- ### empty_not_ok
119
-
120
- Empty values (empty string '') are considered correct and accepted by default.
121
- However, if your data must always have some content,
122
- you can add a global `empty_not_ok` flag at the root level of your YAML definition to automatically
123
- add the `format: non_empty` validation to all columns:
124
-
125
- ```yaml
126
- empty_not_ok: true
127
- columns:
128
- - name: Column1
129
- # other validations...
130
- ```
131
-
132
- ### Column Separator
133
-
134
- By default, the library uses comma (,) as the column separator. You can change this using the `set_separator` method:
135
-
136
- ```python
137
- validator = CSVValidator.from_file("rules.yml")
138
- validator.set_separator(";") # Use semicolon as separator
139
- validator.set_separator("\t") # Use tab as separator
140
- ```
141
-
142
- ### Decimal Separator
143
-
144
- By default, the library uses point (.) as the decimal separator. You can change this using the `set_decimal_separator` method:
145
-
146
- ```python
147
- validator = CSVValidator.from_file("rules.yml")
148
- validator.set_decimal_separator(",") # Use comma
149
- ```
150
-
151
- ## Error Handling
152
-
153
- The library provides detailed validation reports through Python's logging system. When a validation fails, you'll get information about:
154
- - Which columns failed validation
155
- - What type of validation failed
156
- - Sample of invalid values
157
- - Number of rows that failed each validation
158
-
159
- ## Development
160
-
161
- ### Prerequisites
162
-
163
- - Rust 2021 edition or later
164
- - Python 3.6+ (for Python bindings)
165
- - Cargo and standard Rust tooling
166
-
167
- ### Building from Source (Rust version)
168
-
169
- ```bash
170
- # Clone the repository
171
- git clone https://github.com/charro/csv_validation
172
- cd csv_validation
173
-
174
- # Build the project
175
- cargo build --release
176
-
177
- # Run tests
178
- cargo test
179
- ```
180
-
181
- ## Dependencies
182
-
183
- - `csv`: CSV parsing
184
- - `flate2`: Compression support
185
- - `pyo3`: Python bindings
186
- - `regex`: Regular expression support
187
- - `yaml-rust2`: YAML parsing
188
- - Various utilities for logging and serialization
189
-
190
- ## License
191
-
192
- MIT License
193
-
194
- Copyright (c) 2024 CSV Validation Contributors
195
-
196
- Permission is hereby granted, free of charge, to any person obtaining a copy
197
- of this software and associated documentation files (the "Software"), to deal
198
- in the Software without restriction, including without limitation the rights
199
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
200
- copies of the Software, and to permit persons to whom the Software is
201
- furnished to do so, subject to the following conditions:
202
-
203
- The above copyright notice and this permission notice shall be included in all
204
- copies or substantial portions of the Software.
205
-
206
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
207
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
209
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
210
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
212
- SOFTWARE.
213
-
214
- ## Contributing
215
-
216
- Contributions are welcome! Please feel free to submit a Pull Request.
File without changes