-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from yassun7010/add_struct_custom_validator
feat: add struct custom validator.
- Loading branch information
Showing
18 changed files
with
569 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use serde_valid::Validate; | ||
|
||
#[test] | ||
fn named_struct_custom_is_ok() { | ||
fn sample_struct_validation(_val: &TestStruct) -> Result<(), serde_valid::validation::Error> { | ||
Ok(()) | ||
} | ||
|
||
#[derive(Validate)] | ||
#[validate(custom(sample_struct_validation))] | ||
struct TestStruct { | ||
val: i32, | ||
} | ||
|
||
let s = TestStruct { val: 5 }; | ||
assert_eq!(s.val, 5); | ||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn named_struct_custom_closure_is_ok() { | ||
fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { | ||
Ok(()) | ||
} | ||
|
||
#[derive(Validate)] | ||
#[validate(custom(|s| sample_struct_validation(s.val)))] | ||
struct TestStruct { | ||
val: i32, | ||
} | ||
|
||
let s = TestStruct { val: 5 }; | ||
assert_eq!(s.val, 5); | ||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn unnamed_struct_custom_is_ok() { | ||
fn sample_struct_validation(_val: &TestStruct) -> Result<(), serde_valid::validation::Error> { | ||
Ok(()) | ||
} | ||
|
||
#[derive(Validate)] | ||
#[validate(custom(sample_struct_validation))] | ||
struct TestStruct(i32); | ||
|
||
let s = TestStruct(5); | ||
assert_eq!(s.0, 5); | ||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn unnamed_struct_custom_closure_is_ok() { | ||
fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { | ||
Ok(()) | ||
} | ||
|
||
#[derive(Validate)] | ||
#[validate(custom(|s| sample_struct_validation(s.0)))] | ||
struct TestStruct(i32); | ||
|
||
let s = TestStruct(5); | ||
assert_eq!(s.0, 5); | ||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn unnamed_struct_custom_closure_is_err() { | ||
fn sample_struct_validation(_val: i32) -> Result<(), serde_valid::validation::Error> { | ||
Err(serde_valid::validation::Error::Custom( | ||
"Struct Validation Error.".to_owned(), | ||
)) | ||
} | ||
|
||
#[derive(Validate)] | ||
#[validate(custom(|s| sample_struct_validation(s.0)))] | ||
struct TestStruct(i32); | ||
|
||
let s = TestStruct(5); | ||
assert_eq!(s.0, 5); | ||
assert!(s.validate().is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod named_struct_rule; | ||
mod struct_custom; | ||
mod unnamed_struct_rule; | ||
|
||
pub use named_struct_rule::collect_rules_from_named_struct; | ||
pub use struct_custom::collect_struct_custom_from_named_struct; | ||
pub use unnamed_struct_rule::collect_rules_from_unnamed_struct; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.