File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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//!
You can’t perform that action at this time.
0 commit comments