Skip to content

Commit 0f679e0

Browse files
committed
wip: more complex test case.
1 parent 057afd5 commit 0f679e0

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

crates/serde_valid/tests/custom_test.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,16 @@ fn named_struct_custom_closure_vec_errors_is_err() {
283283
}
284284

285285
#[test]
286-
fn filed_custom_validation_using_self() {
286+
fn filed_custom_validation_complex_closure() {
287+
fn kind_validation(kind: &str) -> Result<(), serde_valid::validation::Error> {
288+
match kind {
289+
"Cat" | "Dog" => Ok(()),
290+
_ => Err(serde_valid::validation::Error::Custom(
291+
"Kind should be Cat or Dog.".to_string(),
292+
)),
293+
}
294+
}
295+
287296
fn food_validation(kind: &str, food: &str) -> Result<(), serde_valid::validation::Error> {
288297
match kind {
289298
"Cat" => {
@@ -310,10 +319,16 @@ fn filed_custom_validation_using_self() {
310319

311320
#[derive(Validate)]
312321
struct Pet {
313-
#[validate(enumerate = ["Cat", "Dog"])]
322+
#[validate(custom = kind_validation)]
314323
kind: String,
315324

316-
#[validate(custom = |food| food_validation(&self.kind, food))]
325+
#[validate(custom = |food| {
326+
if kind_validation(&self.kind).is_ok() {
327+
food_validation(&self.kind, food)
328+
} else {
329+
Ok(())
330+
}
331+
})]
317332
food: String,
318333
}
319334

0 commit comments

Comments
 (0)