From 91247fed605ad8ffb95a567fba41ec26c7430a1d Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Mon, 8 Jan 2024 16:21:00 +0900 Subject: [PATCH] docs: update docs. --- serde_valid/README.md | 31 +++++++++++++- serde_valid/src/lib.rs | 93 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/serde_valid/README.md b/serde_valid/README.md index 1192d2e..dc1a51e 100644 --- a/serde_valid/README.md +++ b/serde_valid/README.md @@ -198,7 +198,36 @@ let s = SampleStruct { val: 1 }; assert!(s.validate().is_ok()); ``` -## Rules +## Multi Fields Validation +### Custom Validation +Now, you can use `#[validate(custom)]` for multi fields validation. + +```rust +use serde_json::json; +use serde_valid::Validate; + +fn sample_validation(val1: i32, val2: &str) -> Result<(), serde_valid::validation::Error> { + Ok(()) +} + +#[derive(Validate)] +struct SampleStruct { + #[validate(custom(|s| sample_validation(s.val1, &s.val2)))] + val1: i32, + val2: String, +} + +let s = SampleStruct { + val1: 1, + val2: "val2".to_owned(), +}; + +assert!(s.validate().is_ok()); +``` + +### Rules + +**Warning**: this feature is deprecated. Please use `#[validate(custom)]` instead. If you want to check multi fields validation, can use `#[rule]`. diff --git a/serde_valid/src/lib.rs b/serde_valid/src/lib.rs index 4a379e5..1420e3a 100644 --- a/serde_valid/src/lib.rs +++ b/serde_valid/src/lib.rs @@ -180,7 +180,56 @@ //! assert!(s.validate().is_ok()); //! ``` //! -//! ## Rules +//! And you can also use closure. +//! +//! ```rust +//! use serde_valid::Validate; +//! +//! fn user_validation(_val: &i32, param1: bool) -> Result<(), serde_valid::validation::Error> { +//! Ok(()) +//! } +//! +//! #[derive(Validate)] +//! struct SampleStruct { +//! #[validate(custom(|v| user_validation(v, true)))] +//! val: i32, +//! } +//! +//! let s = SampleStruct { val: 1 }; +//! +//! assert!(s.validate().is_ok()); +//! ``` +//! +//! ## Multi Fields Validation +//! ### Custom Validation +//! Now, you can use `#[validate(custom)]` for multi fields validation. +//! +//! ```rust +//! use serde_json::json; +//! use serde_valid::Validate; +//! +//! fn sample_validation(val1: i32, val2: &str) -> Result<(), serde_valid::validation::Error> { +//! Ok(()) +//! } +//! +//! #[derive(Validate)] +//! struct SampleStruct { +//! #[validate(custom(|s| sample_validation(s.val1, &s.val2)))] +//! val1: i32, +//! val2: String, +//! } +//! +//! let s = SampleStruct { +//! val1: 1, +//! val2: "val2".to_owned(), +//! }; +//! +//! assert!(s.validate().is_ok()); +//! ``` +//! +//! ### Rules +//! +//! **Warning**: this feature is deprecated. Please use `#[validate(custom)]` instead. //! //! If you want to check multi fields validation, can use `#[rule]`. //! @@ -237,6 +286,48 @@ //! assert!(s.validate().is_ok()); //! ``` //! +//! And you can also use closure. +//! +//! ```rust +//! use serde_json::json; +//! use serde_valid::Validate; +//! +//! fn sample_rule(_val1: &i32, _val2: &str) -> Result<(), serde_valid::validation::Error> { +//! Ok(()) +//! } +//! +//! #[derive(Validate)] +//! #[rule(|val1, val2| sample_rule(val2, val1))] +//! struct SampleStruct { +//! val1: String, +//! val2: i32, +//! } +//! +//! let s = SampleStruct { +//! val1: "val1".to_owned(), +//! val2: 1, +//! }; +//! +//! assert!(s.validate().is_ok()); +//! ``` +//! +//! ```rust +//! use serde_json::json; +//! use serde_valid::Validate; +//! +//! fn sample_rule(_val1: &i32, _val2: &str) -> Result<(), serde_valid::validation::Error> { +//! Ok(()) +//! } +//! +//! #[derive(Validate)] +//! #[rule(|_0, _1| sample_rule(_0, _1))] +//! struct SampleStruct(i32, String); +//! +//! let s = SampleStruct(0, "1".to_owned()); +//! +//! assert!(s.validate().is_ok()); +//! ``` +//! //! ## Validate Traits //! //! By implementing the validation trait, Your original type can uses Serde Valid validations.