Skip to content

Commit

Permalink
Merge pull request #25 from yassun7010/update_doc
Browse files Browse the repository at this point in the history
docs: update docs.
  • Loading branch information
yassun7010 authored Jan 7, 2024
2 parents 52d0f6d + bf46a03 commit e9da032
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ let s = SampleStruct { val: 1 };
assert!(s.validate().is_ok());
```

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());
```

## Rules

If you want to check multi fields validation, can use `#[rule]`.
Expand Down Expand Up @@ -235,6 +255,48 @@ let s = SampleStruct(0, "1".to_owned());
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 e9da032

Please sign in to comment.