Skip to content

Commit

Permalink
docs: update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 8, 2024
1 parent 3d38128 commit 91247fe
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
31 changes: 30 additions & 1 deletion serde_valid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`.

Expand Down
93 changes: 92 additions & 1 deletion serde_valid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 218 in serde_valid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

type annotations needed
//! 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]`.
//!
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 91247fe

Please sign in to comment.