Skip to content

Commit

Permalink
feat: add readme and add test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jun 20, 2024
1 parent e9f899b commit 2fa3ddf
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
41 changes: 41 additions & 0 deletions serde_valid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,47 @@ let s = Data {
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.

```rust
use serde_valid::Validate;

fn user_validation(_val: &i32) -> Result<(), Vec<serde_valid::validation::Error>> { // <-- Just change the return type from `Result<(), Error>` to `Result<(), Vec<Error>>` !!
Ok(())
}

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

let s = Data { val: 1 };

assert!(s.validate().is_ok());
```

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 Data {
#[validate(custom(|v| user_validation(v, true)))]
val: i32,
}

let s = Data { val: 1 };

assert!(s.validate().is_ok());
```

## Multi Fields Validation
### Custom Validation
Now, you can use `#[validate(custom)]` for multi fields validation.
Expand Down
41 changes: 41 additions & 0 deletions serde_valid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,47 @@
//! 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.
//!
//! ```rust
//! use serde_valid::Validate;
//!
//! fn user_validation(_val: &i32) -> Result<(), Vec<serde_valid::validation::Error>> { // <-- Just change the return type from `Result<(), Error>` to `Result<(), Vec<Error>>` !!
//! Ok(())
//! }
//!
//! #[derive(Validate)]
//! struct Data {
//! #[validate(custom(user_validation))]
//! val: i32,
//! }
//!
//! let s = Data { val: 1 };
//!
//! assert!(s.validate().is_ok());
//! ```
//!
//! 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 Data {
//! #[validate(custom(|v| user_validation(v, true)))]
//! val: i32,
//! }
//!
//! let s = Data { val: 1 };
//!
//! assert!(s.validate().is_ok());
//! ```
//!
//! ## Multi Fields Validation
//! ### Custom Validation
//! Now, you can use `#[validate(custom)]` for multi fields validation.
Expand Down
13 changes: 10 additions & 3 deletions serde_valid/src/validation/custom.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/// 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, M: IntoVecErrors>(
data: T,
f: impl FnOnce(T) -> Result<(), M>,
pub fn wrap_closure_validation<T: ?Sized, M: IntoVecErrors>(
data: &T,
f: impl FnOnce(&T) -> Result<(), M>,
) -> Result<(), Vec<crate::validation::Error>> {
f(data).map_err(|e| e.into_vec_errors())
}

#[inline]
pub fn wrap_into_vec_errors<M: IntoVecErrors>(
result: Result<(), M>,
) -> Result<(), Vec<crate::validation::Error>> {
result.map_err(|e| e.into_vec_errors())
}

pub trait IntoVecErrors {
fn into_vec_errors(self) -> Vec<crate::validation::Error>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub fn extract_generic_custom_validator(
}?;

Ok(quote!(
if let Err(__error) = #custom_fn_name(#field_ident) {
if let Err(__errors) = serde_valid::validation::custom::wrap_into_vec_errors(#custom_fn_name(#field_ident)) {
#errors
.entry(#rename)
.or_default()
.push(__error);
.extend(__errors);
};
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ fn extract_variant_custom_from_closure(
closure: &syn::ExprClosure,
) -> Result<Validator, crate::Errors> {
Ok(quote!(
if let Err(__error) = serde_valid::helpers::wrap_closure_validation(self, #closure) {
__rule_vec_errors.push(__error);
if let Err(__errors) = serde_valid::validation::custom::wrap_closure_validation(self, #closure) {
__rule_vec_errors.extend(__errors);
};
))
}

0 comments on commit 2fa3ddf

Please sign in to comment.