Skip to content

Commit 629ee58

Browse files
Fix Merge issues between custom and nested (#359)
* fix by only running nested if outer is not run * nested test will no longer fail * add back else case
1 parent c38ae67 commit 629ee58

File tree

3 files changed

+4
-53
lines changed

3 files changed

+4
-53
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
- Implement `AsRegex` for `std::sync::LazyLock<Regex>`
5+
- Bug fix for nested issue with custom only running nested if outer passes
56

67
## 0.19.0 (2024/11/03)
78

validator_derive/src/tokens/nested.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub fn nested_tokens(
55
field_name_str: &str,
66
) -> proc_macro2::TokenStream {
77
quote! {
8-
errors.merge_self(#field_name_str, (&#field_name).validate());
8+
if let std::collections::hash_map::Entry::Vacant(entry) = errors.0.entry(#field_name_str) {
9+
errors.merge_self(#field_name_str, (&#field_name).validate());
10+
}
911
}
1012
}

validator_derive_tests/tests/nested.rs

-52
Original file line numberDiff line numberDiff line change
@@ -847,58 +847,6 @@ fn test_field_validations_take_priority_over_nested_validations() {
847847
}
848848
}
849849

850-
#[test]
851-
#[should_panic(expected = "Attempt to replace non-empty ValidationErrors entry")]
852-
#[allow(unused)]
853-
fn test_field_validation_errors_replaced_with_nested_validations_fails() {
854-
#[derive(Debug)]
855-
struct ParentWithOverridingStructValidations {
856-
child: Vec<Child>,
857-
}
858-
859-
#[derive(Debug, Validate, Serialize)]
860-
struct Child {
861-
#[validate(length(min = 1))]
862-
value: String,
863-
}
864-
865-
impl Validate for ParentWithOverridingStructValidations {
866-
// Evaluating structs after fields validations have discovered errors should fail because
867-
// field validations are expected to take priority over nested struct validations
868-
#[allow(unused_mut)]
869-
fn validate(&self) -> Result<(), ValidationErrors> {
870-
// First validate the length of the vector:
871-
let mut errors = ValidationErrors::new();
872-
if !self.child.validate_length(Some(2u64), None, None) {
873-
let mut err = ValidationError::new("length");
874-
err.add_param(Cow::from("min"), &2u64);
875-
err.add_param(Cow::from("value"), &&self.child);
876-
errors.add("child", err);
877-
}
878-
879-
// Then validate the nested vector of structs without checking for existing field errors:
880-
let mut result = if errors.is_empty() { Ok(()) } else { Err(errors) };
881-
{
882-
let results: Vec<_> = self
883-
.child
884-
.iter()
885-
.map(|child| {
886-
let mut result = Ok(());
887-
result = ValidationErrors::merge(result, "child", child.validate());
888-
result
889-
})
890-
.collect();
891-
result = ValidationErrors::merge_all(result, "child", results);
892-
}
893-
result
894-
}
895-
}
896-
897-
let instance =
898-
ParentWithOverridingStructValidations { child: vec![Child { value: String::new() }] };
899-
instance.validate();
900-
}
901-
902850
#[test]
903851
#[should_panic(
904852
expected = "Attempt to add field validation to a non-Field ValidationErrorsKind instance"

0 commit comments

Comments
 (0)