csv-schema-validator
In the roadmap: version 0.2.0
with cross-column validations.
Version 0.1.3
A Rust library for validating CSV record data based on rules defined directly in your structs using the #[derive(ValidateCsv)]
macro.
Installation
Add the following to your Cargo.toml
:
[]
= "0.1.3"
= { = "1.0", = ["derive"] }
= "1.3"
= "1.11"
= "1.21"
Quick Start
use Deserialize;
use Reader;
use ;
// Custom validator: comments must be at most 50 characters
Usage
Range Validation (since 0.1.0, changed in 0.1.3)
: f64,
grade
Ensures that grade
is between 0.0 and 100.0 (inclusive).
If using version 0.1.3 you can specify just min
or just max
to check greater-or-equal-to
and less-or-equal-to
.
Literal type must match field type. You can have int
or float
fields but literals must match field type. Only for numeric fields.
Regex Validation (since 0.1.0)
: String,
code
Validates the field against a regular expression. Only for String
.
Required Validation (since 0.1.0)
: ,
name
Ensures that the Option
is not None
. If using required
the field must be Option<T>
.
Custom Validation (since 0.1.0)
: String,
comments
Calls your custom function fn(&T) -> Result<(), String>
for additional checks. Only for String
fields.
Length (since 0.1.1)
: ,
name
Only for String
fields.
Not Blank (since 0.1.2)
Checks for all spaces or all whitespaces field (Strings):
: ,
name
Only for String
fields.
One of (since 0.1.2)
Checks if the string has one of the allowed values:
: ,
more_comments
Only for String
fields.
Not in (since 0.1.2)
Checks if the string has one of the not allowed values:
: ,
tag
Only for String
fields.
Struct check
The macro validates the type it is annotating, only strucs with named fields are allowed:
use Deserialize;
use ValidateCsv;
;
Trying to compile this code will result in errors:
cargo run
error: only structs with named fields (e.g., `struct S { a: T }`) are supported
--> src/main.rs:5:19
|
5 | struct TupleStruct(f64, String);
| ^^^^^^^^^^^^^
error: only structs are supported
--> src/main.rs:8:1
|
8 | / enum Status {
9 | | Success { code: f64, message: String },
10 | | Error(f64, String),
11 | | Unknown,
12 | | }
| |_^
Complete example
This is an example which reads a csv file:
Cargo.toml
:
[]
= "use-csv-validator"
= "0.1.1"
= "2021"
[]
= "1.1"
= { = "1.0", = ["derive"] }
= "0.1.3"
src/main.rs
:
use Error;
use ReaderBuilder;
use Deserialize;
use ;
/// Custom validator: ensure comments string isn't too long
data.csv
:
grade,code,name,comments,more,tag,level,top
85.5,XYZ1234,Alice Smith,All good,short,allowed,2,
90.0,XYZ5678,Bob Marley,Too long comment indeed,medium,allowed,0,
110.0,XYZ4567, ,ok,short,allowed,5,
95.0,xWF9101,Charlie,code,long,allowed,6,
110.0,XYZ2345,Dave Copperfield,range,short,allowed,-1,80
34.0,XYZ6789,,name,medium,allowed,5,
78.0,XYZ7890,Frank,more,invalid comment,allowed,10,
88.0,XYZ4567,Grace,All good,short,,3,
90.0,XYZ3567,Grace of All Times,All good,medium,forbidden,5,150
3.0,XYZ3456,Eve Max Smith,,short,invalid grade,2,
f34s,XYZ3456,Eve,comments,short,invalid grade,,,
Running this example will generate these messages:
Line 1: Record is valid: TestRecord { grade: 85.5, code: "XYZ1234", name: Some("Alice Smith"), comments: "All good", more_comments: Some("short"), tag: Some("allowed"), level: 2, top: None }
Line 2: Validation errors:
Field `comments`: Comments too long
Field `level`: value below min: 1
Line 3: Validation errors:
Field `grade`: value out of expected range: 0 to 100
Field `name`: length out of expected range: 10 to 50
Field `name`: must not be blank or contain only whitespace
Line 4: Validation errors:
Field `code`: does not match the expected pattern
Field `name`: length out of expected range: 10 to 50
Line 5: Validation errors:
Field `grade`: value out of expected range: 0 to 100
Field `level`: value below min: 1
Line 6: Validation errors:
Field `name`: mandatory field
Line 7: Validation errors:
Field `name`: length out of expected range: 10 to 50
Field `more_comments`: invalid value
Line 8: Validation errors:
Field `name`: length out of expected range: 10 to 50
Field `tag`: mandatory field
Line 9: Validation errors:
Field `tag`: value not allowed
Field `top`: value above max: 100
Line 10: Record is valid: TestRecord { grade: 3.0, code: "XYZ3456", name: Some("Eve Max Smith"), comments: "", more_comments: Some("short"), tag: Some("invalid grade"), level: 2, top: None }
Error: Error(UnequalLengths { pos: Some(Position { byte: 542, line: 12, record: 11 }), expected_len: 8, len: 9 })
Why Use This Crate?
- Declarative API: Define validation rules directly in your struct.
- Zero Runtime Overhead: All checks are generated at compile time.
- Seamless Serde & CSV Integration: Works directly with
serde
andcsv
crates. - Clear Error Messages: Each failure reports the field and reason.
Comparison with csv Crate Validations
While the csv
crate provides low‑level parsing and some helper methods, this derive‑based approach offers:
- Field‑Level Declarative Rules: Annotate each struct field with its own validation, rather than writing imperative checks after parsing.
- Type‑Safety & Integration: Leverages your existing
serde::Deserialize
types, so you get compile‑time guarantees on types and validations in one place. - Custom Validators: Easily plug in custom functions per field without manual looping or error‑handling boilerplate.
- Centralized Error Collection: Automatically collects all errors into a single
Vec<ValidationError>
, instead of ad‑hoc early exits. - Reusable Across Projects: Define your struct once, reuse validations in different contexts (CLI, web server, batch jobs) with the same guarantees.
By contrast, using the csv
crate directly may require manual loops over records and explicit match
/if
chains for each validation, leading to more boilerplate and potential for missing checks.
Compatibility
- This crate requires the Rust standard library (it is not compatible with
#![no_std]
environments). - Rust 1.56+
serde
1.0csv
1.3regex
1.11
Contributing
Feel free to open issues and submit pull requests. See CONTRIBUTING.md for details.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Links
- Released on crates.io: csv-schema-validator
- API Documentation: docs.rs
- Source Code: https://github.com/cleuton/rustingcrab/tree/main/code_samples/csv-schema-validator