Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom validation. #75

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions serde_valid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ Serde Valid support standard validation based JSON Schema.

In addition, [serde_valid::utils][module@crate::utils] provides a type of validation not described in the JSON schema specification.

| Type | Serde Valid (validate derive) | Serde Valid (validation function) |
| :------------------------------------------------------------------: | :-------------------------------------------------------- | :----------------------------------------------------------------------- |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_maximum(SECOND)))]` | [duration_maximum][`crate::utils::duration_maximum`] |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_minimum(ZERO)))]` | [duration_minimum][`crate::utils::duration_minimum`] |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_exclusive_maximum(SECOND)))]` | [duration_exclusive_maximum][`crate::utils::duration_exclusive_maximum`] |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_exclusive_minimum(ZERO)))]` | [duration_exclusive_minimum][`crate::utils::duration_exclusive_minimum`] |
| Type | Serde Valid (validate derive) | Serde Valid (validation function) |
| :------------------------------------------------------------------: | :--------------------------------------------------------- | :----------------------------------------------------------------------- |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_maximum(SECOND))]` | [duration_maximum][`crate::utils::duration_maximum`] |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_minimum(ZERO))]` | [duration_minimum][`crate::utils::duration_minimum`] |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_exclusive_maximum(SECOND))]` | [duration_exclusive_maximum][`crate::utils::duration_exclusive_maximum`] |
| [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_exclusive_minimum(ZERO))]` | [duration_exclusive_minimum][`crate::utils::duration_exclusive_minimum`] |

## Complete Constructor (Deserialization)

Expand Down Expand Up @@ -202,7 +202,7 @@ assert_eq!(

## Custom Validation
### Single Error Validation
You can use your custom validation using by `#[validate(custom)]`.
You can use your custom validation using by `#[validate(custom = ...)]`.

```rust
use serde_valid::Validate;
Expand All @@ -213,7 +213,7 @@ fn user_validation(_val: &i32) -> Result<(), serde_valid::validation::Error> {

#[derive(Validate)]
struct Data {
#[validate(custom(user_validation))]
#[validate(custom = user_validation)]
val: i32,
}

Expand All @@ -233,7 +233,7 @@ fn user_validation(_val: &i32, param1: bool) -> Result<(), serde_valid::validati

#[derive(Validate)]
struct Data {
#[validate(custom(|v| user_validation(v, true)))]
#[validate(custom = |v| user_validation(v, true))]
val: i32,
}

Expand All @@ -253,8 +253,8 @@ use serde_valid::utils::{duration_maximum, duration_minimum};

#[derive(Validate)]
struct Data {
#[validate(custom(duration_maximum(std::time::Duration::from_micros(5))))]
#[validate(custom(duration_minimum(std::time::Duration::from_micros(0))))]
#[validate(custom = duration_maximum(std::time::Duration::from_micros(5)))]
#[validate(custom = duration_minimum(std::time::Duration::from_micros(0)))]
val1: std::time::Duration,
}

Expand All @@ -266,7 +266,7 @@ assert!(s.validate().is_ok());
```

### Multi Errors Validation
If you want to return multiple errors in the use custom validation method, you can use `#[validate(custom)]` same as single error.
If you want to return multiple errors in the use custom validation method, you can use `#[validate(custom = ...)]` same as single error.

```rust
use serde_valid::Validate;
Expand All @@ -278,7 +278,7 @@ fn user_validation(_val: &i32) -> Result<(), Vec<serde_valid::validation::Error>

#[derive(Validate)]
struct Data {
#[validate(custom(user_validation))]
#[validate(custom = user_validation)]
val: i32,
}

Expand All @@ -288,7 +288,7 @@ assert!(s.validate().is_ok());
```

### Multi Fields Validation
Now, you can use `#[validate(custom)]` for multi fields validation.
Now, you can use `#[validate(custom = ...)]` for multi fields validation.

```rust
use serde_json::json;
Expand All @@ -299,7 +299,7 @@ fn sample_validation(val1: i32, val2: &str) -> Result<(), serde_valid::validatio
}

#[derive(Validate)]
#[validate(custom(|s| sample_validation(s.val1, &s.val2)))]
#[validate(custom = |s| sample_validation(s.val1, &s.val2))]
struct Data {
val1: i32,
val2: String,
Expand Down
30 changes: 15 additions & 15 deletions serde_valid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
//!
//! In addition, [serde_valid::utils][module@crate::utils] provides a type of validation not described in the JSON schema specification.
//!
//! | Type | Serde Valid (validate derive) | Serde Valid (validation function) |
//! | :------------------------------------------------------------------: | :-------------------------------------------------------- | :----------------------------------------------------------------------- |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_maximum(SECOND)))]` | [duration_maximum][`crate::utils::duration_maximum`] |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_minimum(ZERO)))]` | [duration_minimum][`crate::utils::duration_minimum`] |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_exclusive_maximum(SECOND)))]` | [duration_exclusive_maximum][`crate::utils::duration_exclusive_maximum`] |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom(duration_exclusive_minimum(ZERO)))]` | [duration_exclusive_minimum][`crate::utils::duration_exclusive_minimum`] |
//! | Type | Serde Valid (validate derive) | Serde Valid (validation function) |
//! | :------------------------------------------------------------------: | :--------------------------------------------------------- | :----------------------------------------------------------------------- |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_maximum(SECOND))]` | [duration_maximum][`crate::utils::duration_maximum`] |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_minimum(ZERO))]` | [duration_minimum][`crate::utils::duration_minimum`] |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_exclusive_maximum(SECOND))]` | [duration_exclusive_maximum][`crate::utils::duration_exclusive_maximum`] |
//! | [Duration](https://doc.rust-lang.org/core/time/struct.Duration.html) | `#[validate(custom = duration_exclusive_minimum(ZERO))]` | [duration_exclusive_minimum][`crate::utils::duration_exclusive_minimum`] |
//!
//! ## Complete Constructor (Deserialization)
//!
Expand Down Expand Up @@ -212,7 +212,7 @@
//!
//! ## Custom Validation
//! ### Single Error Validation
//! You can use your custom validation using by `#[validate(custom)]`.
//! You can use your custom validation using by `#[validate(custom = ...)]`.
//!
//! ```rust
//! use serde_valid::Validate;
Expand All @@ -223,7 +223,7 @@
//!
//! #[derive(Validate)]
//! struct Data {
//! #[validate(custom(user_validation))]
//! #[validate(custom = user_validation)]
//! val: i32,
//! }
//!
Expand All @@ -243,7 +243,7 @@
//!
//! #[derive(Validate)]
//! struct Data {
//! #[validate(custom(|v| user_validation(v, true)))]
//! #[validate(custom = |v| user_validation(v, true))]
//! val: i32,
//! }
//!
Expand All @@ -263,8 +263,8 @@
//!
//! #[derive(Validate)]
//! struct Data {
//! #[validate(custom(duration_maximum(std::time::Duration::from_micros(5))))]
//! #[validate(custom(duration_minimum(std::time::Duration::from_micros(0))))]
//! #[validate(custom = duration_maximum(std::time::Duration::from_micros(5)))]
//! #[validate(custom = duration_minimum(std::time::Duration::from_micros(0)))]
//! val1: std::time::Duration,
//! }
//!
Expand All @@ -276,7 +276,7 @@
//! ```
//!
//! ### Multi Errors Validation
//! If you want to return multiple errors in the use custom validation method, you can use `#[validate(custom)]` same as single error.
//! If you want to return multiple errors in the use custom validation method, you can use `#[validate(custom = ...)]` same as single error.
//!
//! ```rust
//! use serde_valid::Validate;
Expand All @@ -288,7 +288,7 @@
//!
//! #[derive(Validate)]
//! struct Data {
//! #[validate(custom(user_validation))]
//! #[validate(custom = user_validation)]
//! val: i32,
//! }
//!
Expand All @@ -298,7 +298,7 @@
//! ```
//!
//! ### Multi Fields Validation
//! Now, you can use `#[validate(custom)]` for multi fields validation.
//! Now, you can use `#[validate(custom = ...)]` for multi fields validation.
//!
//! ```rust
//! use serde_json::json;
Expand All @@ -309,7 +309,7 @@
//! }
//!
//! #[derive(Validate)]
//! #[validate(custom(|s| sample_validation(s.val1, &s.val2)))]
//! #[validate(custom = |s| sample_validation(s.val1, &s.val2))]
//! struct Data {
//! val1: i32,
//! val2: String,
Expand Down
8 changes: 4 additions & 4 deletions serde_valid/src/utils/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::time::Duration;
///
/// #[derive(Validate)]
/// struct TestStruct {
/// #[validate(custom(duration_maximum(Duration::from_micros(5))))]
/// #[validate(custom = duration_maximum(Duration::from_micros(5)))]
/// val: Duration,
/// }
///
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn duration_maximum(
///
/// #[derive(Validate)]
/// struct TestStruct {
/// #[validate(custom(duration_minimum(Duration::from_micros(5))))]
/// #[validate(custom = duration_minimum(Duration::from_micros(5)))]
/// val: Duration,
/// }
///
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn duration_minimum(
///
/// #[derive(Validate)]
/// struct TestStruct {
/// #[validate(custom(duration_exclusive_maximum(Duration::from_micros(5))))]
/// #[validate(custom = duration_exclusive_maximum(Duration::from_micros(5)))]
/// val: Duration,
/// }
///
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn duration_exclusive_maximum(
///
/// #[derive(Validate)]
/// struct TestStruct {
/// #[validate(custom(duration_exclusive_minimum(Duration::from_micros(5))))]
/// #[validate(custom = duration_exclusive_minimum(Duration::from_micros(5)))]
/// val: Duration,
/// }
///
Expand Down
2 changes: 1 addition & 1 deletion serde_valid/src/validation/custom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This function is used to avoid [rustc(E0282)](https://doc.rust-lang.org/error_codes/E0282.html) error in `#[validate(custom)]` validator on the struct.
/// This function is used to avoid [rustc(E0282)](https://doc.rust-lang.org/error_codes/E0282.html) error in `#[validate(custom = ...)]` validator on the struct.
#[inline]
pub fn wrap_closure_validation<T: ?Sized, M: IntoVecErrors>(
data: &T,
Expand Down
2 changes: 1 addition & 1 deletion serde_valid/tests/array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn items_err_message() {
}

#[derive(Validate)]
#[validate(custom(|s| rule_sample(s.val)))]
#[validate(custom = |s| rule_sample(s.val))]
struct TestChildStruct {
#[validate(minimum = 1)]
#[validate(maximum = 10)]
Expand Down
2 changes: 1 addition & 1 deletion serde_valid/tests/complex_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn sample_rule(_val: i32) -> Result<(), serde_valid::validation::Error> {
}

#[derive(Debug, Validate)]
#[validate(custom(|s| sample_rule(s.int_value)))]
#[validate(custom = |s| sample_rule(s.int_value))]
struct TestStruct<'a> {
// Generic validator
#[validate(enumerate = [5, 10, 15])]
Expand Down
8 changes: 4 additions & 4 deletions serde_valid/tests/custom_duration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde_valid::Validate;
fn duration_maximum_is_ok() {
#[derive(Validate)]
struct TestStruct {
#[validate(custom(duration_maximum(Duration::from_micros(5))))]
#[validate(custom = duration_maximum(Duration::from_micros(5)))]
val: Duration,
}

Expand All @@ -23,7 +23,7 @@ fn duration_maximum_is_ok() {
fn duration_minimum_is_ok() {
#[derive(Validate)]
struct TestStruct {
#[validate(custom(duration_minimum(Duration::from_micros(5))))]
#[validate(custom = duration_minimum(Duration::from_micros(5)))]
val: Duration,
}

Expand All @@ -38,7 +38,7 @@ fn duration_minimum_is_ok() {
fn duration_maximum_is_err() {
#[derive(Validate)]
struct TestStruct {
#[validate(custom(duration_maximum(Duration::from_micros(5))))]
#[validate(custom = duration_maximum(Duration::from_micros(5)))]
val: Duration,
}

Expand Down Expand Up @@ -66,7 +66,7 @@ fn duration_maximum_is_err() {
fn duration_minimum_is_err() {
#[derive(Validate)]
struct TestStruct {
#[validate(custom(duration_minimum(Duration::from_micros(5))))]
#[validate(custom = duration_minimum(Duration::from_micros(5)))]
val: Duration,
}

Expand Down
Loading
Loading