Skip to content
Open
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
2 changes: 1 addition & 1 deletion validator_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl ToTokens for ValidateField {

let nested = if let Some(n) = self.nested {
if n {
wrapper_closure(nested_tokens(&actual_field, &field_name_str))
wrapper_closure(nested_tokens(&actual_field, &field_name_str, &self.crate_name))
} else {
quote!()
}
Expand Down
4 changes: 4 additions & 0 deletions validator_derive/src/tokens/nested.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use quote::quote;

use crate::CrateName;

pub fn nested_tokens(
field_name: &proc_macro2::TokenStream,
field_name_str: &str,
crate_name: &CrateName,
) -> proc_macro2::TokenStream {
quote! {
if let std::collections::hash_map::Entry::Vacant(entry) = errors.0.entry(::std::borrow::Cow::Borrowed(#field_name_str)) {
use #crate_name::Validate;
errors.merge_self(#field_name_str, (&#field_name).validate());
}
}
Expand Down
27 changes: 27 additions & 0 deletions validator_derive_tests/tests/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,3 +910,30 @@ where
let errors = errors.clone();
f(errors.errors().clone());
}

/// Sometimes users may want to use absolute path on struct, and validate in other module
pub mod test_nest_field_with_absolute_path {
#[derive(validator::Validate)]
struct Parent {
#[validate(nested)]
child: Child,
#[validate(nested)]
child2: Child,
}

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

#[test]
fn test() {
use validator::Validate;
let instance = Parent {
child: Child { value: String::from("1") },
child2: Child { value: String::from("2") },
};
assert!(instance.validate().is_ok());
}
}
Loading