serde-validate
The serde-validate crate provides utilities for validating deserialized structs and enums in Rust.
The core of the crate is the Validate trait, which defines a method for validating instances of types and returning a custom error type if validation fails.
The crate also includes a procedural macro, validate_deser, that generates deserialization code that validates the deserialized data.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
To use the macro the macro feature needs to be enabled (enabled by default):
[]
= { = "0.1", = false, = ["macro"] }
Usage
Validate Trait
Implement the Validate trait for your struct or enum to define custom validation logic.
use Validate;
use Deserialize;
let my_struct = MyStruct ;
assert!;
validate_deser Macro
Use the validate_deser macro to automatically generate deserialization code that validates the deserialized data.
use serde-;
// Assuming you have a JSON input as below:
let json_input = r#"{ "value": 10 }"#;
// Deserialize and validate the JSON input
let my_struct: = from_str;
assert!;
// Assuming you have a JSON input as below:
let bad_json_input = r#"{ "value": -10 }"#;
// Deserialize and validate the JSON input
let my_struct: = from_str;
assert!;
License
This project is licensed under the GPL-3.0 License - see the LICENSE file for details.