csv-validation 0.1.4__cp314-cp314t-win32.whl
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/__init__.py +5 -0
- csv_validation/__init__.pyi +13 -0
- csv_validation/csv_validation.cp314t-win32.pyd +0 -0
- csv_validation/py.typed +0 -0
- csv_validation-0.1.4.dist-info/METADATA +340 -0
- csv_validation-0.1.4.dist-info/RECORD +9 -0
- csv_validation-0.1.4.dist-info/WHEEL +4 -0
- csv_validation-0.1.4.dist-info/licenses/LICENSE +21 -0
- csv_validation-0.1.4.dist-info/sboms/csv_validation.cyclonedx.json +2249 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class CSVValidator:
|
|
2
|
+
|
|
3
|
+
@staticmethod
|
|
4
|
+
def from_file(definition_path: str) -> CSVValidator: ...
|
|
5
|
+
|
|
6
|
+
@staticmethod
|
|
7
|
+
def from_string(definition_string: str) -> CSVValidator: ...
|
|
8
|
+
|
|
9
|
+
def set_separator(self, separator: str): ...
|
|
10
|
+
|
|
11
|
+
def set_decimal_separator(self, decimal_separator: str): ...
|
|
12
|
+
|
|
13
|
+
def validate(self, file_path: str) -> bool: ...
|
|
Binary file
|
csv_validation/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: csv_validation
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Summary: Blazing fast format validations for your CSV files
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
11
|
+
Project-URL: Homepage, https://github.com/charro/csv_validation
|
|
12
|
+
|
|
13
|
+
# CSV Validation Library
|
|
14
|
+
|
|
15
|
+
Blazing fast format validations for your CSV files
|
|
16
|
+
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- ✨ Validate both plain and gzipped CSV files
|
|
22
|
+
- 🔍 Multiple validation types supported:
|
|
23
|
+
- Correct column name and order
|
|
24
|
+
- Regular expressions
|
|
25
|
+
- Well-known formats (integer, decimal, etc.)
|
|
26
|
+
- Minimum/Maximum numerical value checks
|
|
27
|
+
- Value set validation (allowed values)
|
|
28
|
+
- 🦀 + 🐍 Rust lib with Python bindings included
|
|
29
|
+
- 📝 Detailed validation summaries with sample invalid values
|
|
30
|
+
- 🚀 High performance with optimizations like regex pre-compilation
|
|
31
|
+
- 📊 Support for large CSV files
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
### Python
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install csv_validation
|
|
39
|
+
```
|
|
40
|
+
```bash
|
|
41
|
+
poetry add csv_validation
|
|
42
|
+
```
|
|
43
|
+
```bash
|
|
44
|
+
uv add csv_validation
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### Python
|
|
50
|
+
|
|
51
|
+
#### You can provide a file with the validation rules
|
|
52
|
+
```python
|
|
53
|
+
from csv_validation import CSVValidator
|
|
54
|
+
|
|
55
|
+
validator = CSVValidator.from_file("validation_rules.yaml")
|
|
56
|
+
is_valid = validator.validate("data.csv")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### You can also create a validator from a string
|
|
60
|
+
```python
|
|
61
|
+
validation_rules = """
|
|
62
|
+
columns:
|
|
63
|
+
- name: Name
|
|
64
|
+
regex: ^[A-Za-z\s]{2,50}$
|
|
65
|
+
- name: Age
|
|
66
|
+
format: positive_integer
|
|
67
|
+
max: 120
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
validator = CSVValidator.from_string(validation_rules)
|
|
71
|
+
# Optionally set a custom column separator
|
|
72
|
+
validator.set_separator(";") # Default is comma (,)
|
|
73
|
+
|
|
74
|
+
# Validate a CSV file
|
|
75
|
+
is_valid = validator.validate("data.csv")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Validation Definition Format
|
|
79
|
+
|
|
80
|
+
Create a small, easy-to-read YAML file with your validation rules. Example:
|
|
81
|
+
|
|
82
|
+
```yaml
|
|
83
|
+
columns:
|
|
84
|
+
- name: Name
|
|
85
|
+
regex: ^[A-Za-z\s]{2,50}$ # Letters and spaces, 2-50 characters
|
|
86
|
+
- name: Family Name
|
|
87
|
+
regex: ^[A-Za-z\s'-]{2,50}$ # Letters, spaces, hyphens and apostrophes
|
|
88
|
+
- name: Age
|
|
89
|
+
format: positive_integer # Using predefined format instead of custom regex
|
|
90
|
+
max: 120
|
|
91
|
+
- name: Salary
|
|
92
|
+
format: integer # Allows negative integers too
|
|
93
|
+
min: 20000
|
|
94
|
+
- name: Email
|
|
95
|
+
regex: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ # Standard email format
|
|
96
|
+
- name: Phone
|
|
97
|
+
regex: ^\+?[0-9]{10,15}$ # International phone format with optional +
|
|
98
|
+
- name: Status
|
|
99
|
+
values: [active, inactive, pending, suspended] # Only these values are allowed
|
|
100
|
+
- name: Gender
|
|
101
|
+
values: [M, F, NB, O] # M: Male, F: Female, NB: Non-Binary, O: Other
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Validation Types
|
|
105
|
+
|
|
106
|
+
1. **Regular Expression** (`regex`)
|
|
107
|
+
- Validate fields against custom regex patterns
|
|
108
|
+
|
|
109
|
+
2. **Format** (`format`)
|
|
110
|
+
- Predefined formats for common validations (This is the recommended way to validate numeric fields)
|
|
111
|
+
- In the background, the library uses regex patterns to validate formats.
|
|
112
|
+
- Available formats:
|
|
113
|
+
- `integer`: Validates any integer number (positive or negative)
|
|
114
|
+
- `positive integer`: Validates positive integer numbers
|
|
115
|
+
- `negative integer`: Validates negative integer numbers
|
|
116
|
+
- `decimal`/`decimal point`: Validates any decimal number (positive or negative) using point as decimal separator
|
|
117
|
+
- `negative decimal point`: Validates negative decimal numbers using point as decimal separator
|
|
118
|
+
- `decimal comma`: Validates decimal numbers using comma as decimal separator
|
|
119
|
+
- `positive decimal`/`positive decimal point`: Validates positive decimal numbers using point as decimal separator
|
|
120
|
+
- `positive decimal comma`: Validates positive decimal numbers using comma as decimal separator
|
|
121
|
+
- `decimal scientific`: Validates decimal numbers in scientific notation (e.g. 23.02e-12)
|
|
122
|
+
- `decimal scientific comma`: Validates decimal numbers in scientific notation using comma as decimal separator
|
|
123
|
+
- `positive decimal scientific`: Validates positive decimal numbers in scientific notation
|
|
124
|
+
- More formats will be added in upcoming versions
|
|
125
|
+
|
|
126
|
+
3. **Minimum Value** (`min`)
|
|
127
|
+
- Check if numeric fields are greater than or equal to a specified value
|
|
128
|
+
|
|
129
|
+
4. **Maximum Value** (`max`)
|
|
130
|
+
- Check if numeric fields are less than or equal to a specified value
|
|
131
|
+
|
|
132
|
+
5. **Value Set** (`values`)
|
|
133
|
+
- Ensure fields only contain values from a predefined set
|
|
134
|
+
|
|
135
|
+
6. **Extra** (`extra`)
|
|
136
|
+
- Extra validations that normally complement one of the other types
|
|
137
|
+
- Available extras:
|
|
138
|
+
- `non_empty`: Validates that field contains at least one character
|
|
139
|
+
|
|
140
|
+
You can add as many validation types as you want for the same column, but take into account that the
|
|
141
|
+
column only will be considered correct if all the validations are OK.
|
|
142
|
+
|
|
143
|
+
## Global Validations
|
|
144
|
+
|
|
145
|
+
### empty_not_ok
|
|
146
|
+
|
|
147
|
+
Empty values (empty string '') are considered correct and accepted by default.
|
|
148
|
+
However, if your data must always have some content,
|
|
149
|
+
you can add a global `empty_not_ok` flag at the root level of your YAML definition to automatically
|
|
150
|
+
add the `extra: non_empty` validation to all columns:
|
|
151
|
+
|
|
152
|
+
```yaml
|
|
153
|
+
empty_not_ok: true
|
|
154
|
+
columns:
|
|
155
|
+
- name: Column1
|
|
156
|
+
# other validations...
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Unique Key (Duplicate Rows) Validation
|
|
160
|
+
|
|
161
|
+
You can enforce uniqueness across one or more columns by adding a root-level `unique` field in your YAML definition. This enables a memory-efficient duplicate detection that scales to very large files.
|
|
162
|
+
|
|
163
|
+
- Accepts a single column name (string) or a list of column names (array of strings).
|
|
164
|
+
- Works with both plain and gzipped CSV files.
|
|
165
|
+
- Uses a high-performant disk-backed key-value store (redb) under the hood to keep memory usage low.
|
|
166
|
+
|
|
167
|
+
Examples:
|
|
168
|
+
|
|
169
|
+
```yaml
|
|
170
|
+
# Single-column unique key
|
|
171
|
+
unique: ID
|
|
172
|
+
columns:
|
|
173
|
+
- name: ID
|
|
174
|
+
format: positive integer
|
|
175
|
+
- name: Name
|
|
176
|
+
regex: ^[A-Za-z\s]{2,50}$
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
```yaml
|
|
180
|
+
# Composite unique key across multiple columns
|
|
181
|
+
unique: [isin, date, score_provider]
|
|
182
|
+
columns:
|
|
183
|
+
- name: isin
|
|
184
|
+
regex: ^[A-Z0-9]{12}$
|
|
185
|
+
- name: date
|
|
186
|
+
regex: ^\d{4}-\d{2}-\d{2}$
|
|
187
|
+
- name: score_provider
|
|
188
|
+
values: [sp1, sp2, sp3]
|
|
189
|
+
- name: score
|
|
190
|
+
format: decimal
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
What you will see in the summary:
|
|
194
|
+
- Always shows the number of duplicate key groups found.
|
|
195
|
+
- If there are more than 100 duplicate groups, only the first 100 are displayed as a sample (with their occurrence counts), and the total number is indicated.
|
|
196
|
+
- If no duplicates are found, it reports: "No duplicates found".
|
|
197
|
+
|
|
198
|
+
Sample output (composite key):
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
UNIQUE KEY: column(s): isin, date, score_provider
|
|
202
|
+
|
|
203
|
+
DUPLICATED KEYS (groups found: 134)
|
|
204
|
+
--------------------------------------------------
|
|
205
|
+
Showing first 100 of 134 duplicate key groups:
|
|
206
|
+
- (isin='US0004026250', date='2025-10-01', score_provider='sp1') -> occurrences: 3
|
|
207
|
+
- (isin='US5949181045', date='2025-10-01', score_provider='sp2') -> occurrences: 4
|
|
208
|
+
... up to 100 lines ...
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
If none:
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
UNIQUE KEYS CHECK
|
|
215
|
+
--------------------------------------------------
|
|
216
|
+
- No duplicates found
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Performance note: Duplicate detection stores only the unique keys on disk and keeps a tiny in-memory summary used for printing the report. This allows validating huge datasets with minimal RAM usage.
|
|
220
|
+
|
|
221
|
+
### Column Separator
|
|
222
|
+
|
|
223
|
+
By default, the library uses comma (,) as the column separator. You can change this using the `set_separator` method:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
validator = CSVValidator.from_file("rules.yml")
|
|
227
|
+
validator.set_separator(";") # Use semicolon as separator
|
|
228
|
+
validator.set_separator("\t") # Use tab as separator
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Decimal Separator
|
|
232
|
+
|
|
233
|
+
By default, the library uses point (.) as the decimal separator. You can change this using the `set_decimal_separator` method:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
validator = CSVValidator.from_file("rules.yml")
|
|
237
|
+
validator.set_decimal_separator(",") # Use comma
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Error Handling
|
|
241
|
+
|
|
242
|
+
The library provides detailed validation reports through Python's logging system. When a validation fails, you'll get information about:
|
|
243
|
+
- Which columns failed validation
|
|
244
|
+
- What type of validation failed
|
|
245
|
+
- Sample of invalid values (including row numbers)
|
|
246
|
+
- Number of rows that failed each validation
|
|
247
|
+
|
|
248
|
+
### Example of validation report
|
|
249
|
+
|
|
250
|
+
```
|
|
251
|
+
VALIDATIONS SUMMARY
|
|
252
|
+
==================================================
|
|
253
|
+
FILE: /test.csv
|
|
254
|
+
Rows: 232230 | Columns: 5
|
|
255
|
+
|
|
256
|
+
CORRECT COLUMNS: 3/5
|
|
257
|
+
--------------------------------------------------
|
|
258
|
+
- County: [✔] OK
|
|
259
|
+
✔ - regex: '^.*$'
|
|
260
|
+
- City: [✔] OK
|
|
261
|
+
✔ - regex: '^.*$'
|
|
262
|
+
- Model Year: [✔] OK
|
|
263
|
+
✔ - regex: '^[0-9]+$'
|
|
264
|
+
✔ - min: 1999
|
|
265
|
+
✔ - max: 2025.78
|
|
266
|
+
|
|
267
|
+
WRONG COLUMNS: 2/5
|
|
268
|
+
--------------------------------------------------
|
|
269
|
+
- State: [✖] FAIL
|
|
270
|
+
✖ - values: 'WA', 'OR', 'NY', 'DC', 'CA', 'TX', 'FL', 'OK', 'MO', 'KS', 'VA', 'MA', 'MO', 'NC', 'IL', 'AL', 'WY', 'CO', 'PA', 'WI', 'MD', 'NV', 'AZ'
|
|
271
|
+
"Wrong Rows: 93 | Sample: [row 12: 'GA'], [row 15: 'NJ'], [row 22: 'CT'], [row 23: 'NJ'], [row 24: 'CT']"
|
|
272
|
+
- Postal Code: [✖] FAIL
|
|
273
|
+
✔ - integer
|
|
274
|
+
✖ - non_empty
|
|
275
|
+
"Wrong Rows: 4 | Sample: [row 45: ''], [row 123: ''], [row 456: ''], [row 789: '']"
|
|
276
|
+
|
|
277
|
+
VALIDATION RESULT
|
|
278
|
+
--------------------------------------------------
|
|
279
|
+
[✖] FAIL: File /test.csv DOESN'T match all validations
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Development
|
|
283
|
+
|
|
284
|
+
### Prerequisites
|
|
285
|
+
|
|
286
|
+
- Rust 2021 edition or later
|
|
287
|
+
- Python 3.6+ (for Python bindings)
|
|
288
|
+
- Cargo and standard Rust tooling
|
|
289
|
+
|
|
290
|
+
### Building from Source (Rust version)
|
|
291
|
+
|
|
292
|
+
```bash
|
|
293
|
+
# Clone the repository
|
|
294
|
+
git clone https://github.com/charro/csv_validation
|
|
295
|
+
cd csv_validation
|
|
296
|
+
|
|
297
|
+
# Build the project
|
|
298
|
+
cargo build --release
|
|
299
|
+
|
|
300
|
+
# Run tests
|
|
301
|
+
cargo test
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Dependencies
|
|
305
|
+
|
|
306
|
+
- `csv`: CSV parsing
|
|
307
|
+
- `flate2`: Compression support
|
|
308
|
+
- `pyo3`: Python bindings
|
|
309
|
+
- `regex`: Regular expression support
|
|
310
|
+
- `yaml-rust2`: YAML parsing
|
|
311
|
+
- `redb`: Disk-backed key-value storage used for memory-efficient duplicate detection
|
|
312
|
+
- Various utilities for logging and serialization
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
MIT License
|
|
317
|
+
|
|
318
|
+
Copyright (c) 2024 CSV Validation Contributors
|
|
319
|
+
|
|
320
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
321
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
322
|
+
in the Software without restriction, including without limitation the rights
|
|
323
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
324
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
325
|
+
furnished to do so, subject to the following conditions:
|
|
326
|
+
|
|
327
|
+
The above copyright notice and this permission notice shall be included in all
|
|
328
|
+
copies or substantial portions of the Software.
|
|
329
|
+
|
|
330
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
331
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
332
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
333
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
334
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
335
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
336
|
+
SOFTWARE.
|
|
337
|
+
|
|
338
|
+
## Contributing
|
|
339
|
+
|
|
340
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
csv_validation/__init__.py,sha256=_T51iFPLyvnJxi6AYK2AFzEPPm2Tc0SMW3CvMWu9zmA,139
|
|
2
|
+
csv_validation/__init__.pyi,sha256=lQtvYhG-Sg-KtwZ1mTaCCQdVTL82spay2yHUnnywnOs,366
|
|
3
|
+
csv_validation/csv_validation.cp314t-win32.pyd,sha256=vC3AzWbuXGhA_rB4IGXKyxp4hIxYwEeW-UF56Cmh6Jg,2723840
|
|
4
|
+
csv_validation/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
csv_validation-0.1.4.dist-info/METADATA,sha256=jU2_VhPJhRmerjUlAaQOEJDiq_BPbhLecJrWd9XoIxA,11553
|
|
6
|
+
csv_validation-0.1.4.dist-info/WHEEL,sha256=BQrALlmOrCaYBVfbu46ZoLBuMadabdK5JJwwmeDUPek,94
|
|
7
|
+
csv_validation-0.1.4.dist-info/licenses/LICENSE,sha256=HLI39hE89zK7gMRt-ptreJexU0sgD8mmphaeziSD6Z4,1104
|
|
8
|
+
csv_validation-0.1.4.dist-info/sboms/csv_validation.cyclonedx.json,sha256=G8VCdcSlpVVPdQozVnnFGC7lheQrutCIhOsu_M6mAp0,69899
|
|
9
|
+
csv_validation-0.1.4.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 CSV Validation Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|