Skip to content

Commit 8d3c252

Browse files
committed
chore: Add README.
1 parent ab63453 commit 8d3c252

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/serde_valid/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,40 @@
256256
//! assert!(s.validate().is_ok());
257257
//! ```
258258
//!
259+
//! You can use **closure**, so you can access other fields of the struct by using the `self` keyword.
260+
//!
261+
//! ```rust
262+
//! use serde_valid::Validate;
263+
//!
264+
//! #[derive(Validate)]
265+
//! struct Data {
266+
//! val1: i32,
267+
//! #[validate(custom = |val2: &i32| {
268+
//! if self.val1 < *val2 {
269+
//! Ok(())
270+
//! } else {
271+
//! Err(serde_valid::validation::Error::Custom("val2 must be greater than val1".to_owned()))
272+
//! }
273+
//! })]
274+
//! val2: i32,
275+
//! }
276+
//!
277+
//! let s = Data { val1: 2, val2: 1 };
278+
//!
279+
//! assert_eq!(
280+
//! s.validate().unwrap_err().to_string(),
281+
//! serde_json::json!({
282+
//! "errors": [],
283+
//! "properties": {
284+
//! "val2": {
285+
//! "errors": ["val2 must be greater than val1"]
286+
//! }
287+
//! }
288+
//! })
289+
//! .to_string()
290+
//! );
291+
//! ```
292+
//!
259293
//! Custom validation is suitable for handling convenience validations not defined in JSON Schema.
260294
//! `serde_valid::utils::*` provides convenience functions for specific types.
261295
//!

0 commit comments

Comments
 (0)