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

Fix Merge issues between custom and nested #359

Merged
merged 4 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## 0.19.0 (2024/11/03)

Expand Down
4 changes: 3 additions & 1 deletion validator_derive/src/tokens/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub fn nested_tokens(
field_name_str: &str,
) -> proc_macro2::TokenStream {
quote! {
errors.merge_self(#field_name_str, (&#field_name).validate());
if let std::collections::hash_map::Entry::Vacant(entry) = errors.0.entry(#field_name_str) {
errors.merge_self(#field_name_str, (&#field_name).validate());
}
}
}
52 changes: 0 additions & 52 deletions validator_derive_tests/tests/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,58 +847,6 @@ fn test_field_validations_take_priority_over_nested_validations() {
}
}

#[test]
#[should_panic(expected = "Attempt to replace non-empty ValidationErrors entry")]
#[allow(unused)]
fn test_field_validation_errors_replaced_with_nested_validations_fails() {
#[derive(Debug)]
struct ParentWithOverridingStructValidations {
child: Vec<Child>,
}

#[derive(Debug, Validate, Serialize)]
struct Child {
#[validate(length(min = 1))]
value: String,
}

impl Validate for ParentWithOverridingStructValidations {
// Evaluating structs after fields validations have discovered errors should fail because
// field validations are expected to take priority over nested struct validations
#[allow(unused_mut)]
fn validate(&self) -> Result<(), ValidationErrors> {
// First validate the length of the vector:
let mut errors = ValidationErrors::new();
if !self.child.validate_length(Some(2u64), None, None) {
let mut err = ValidationError::new("length");
err.add_param(Cow::from("min"), &2u64);
err.add_param(Cow::from("value"), &&self.child);
errors.add("child", err);
}

// Then validate the nested vector of structs without checking for existing field errors:
let mut result = if errors.is_empty() { Ok(()) } else { Err(errors) };
{
let results: Vec<_> = self
.child
.iter()
.map(|child| {
let mut result = Ok(());
result = ValidationErrors::merge(result, "child", child.validate());
result
})
.collect();
result = ValidationErrors::merge_all(result, "child", results);
}
result
}
}

let instance =
ParentWithOverridingStructValidations { child: vec![Child { value: String::new() }] };
instance.validate();
}

#[test]
#[should_panic(
expected = "Attempt to add field validation to a non-Field ValidationErrorsKind instance"
Expand Down
Loading