Skip to content

Commit

Permalink
add validate! macro to be able to validate messages that do not imple…
Browse files Browse the repository at this point in the history
…ment the Validator trait

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
  • Loading branch information
Adphi committed Oct 14, 2024
1 parent 1fe82e7 commit c444f4d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2,804 deletions.
5 changes: 3 additions & 2 deletions prost-validate-derive-core/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ impl ToValidationTokens for RepeatedRules {
if ctx.boxed {
quote! {
for (i, item) in #name.iter.enumerate() {
::prost_validate::Validator::validate(item.as_ref()).map_err(|e| ::prost_validate::Error::new(format!("{}[{}]", #field, i), ::prost_validate::errors::list::Error::Item(Box::new(e))))?;
let item = item.as_ref();
::prost_validate::validate!(item).map_err(|e| ::prost_validate::Error::new(format!("{}[{}]", #field, i), ::prost_validate::errors::list::Error::Item(Box::new(e))))?;
}
}
} else {
quote! {
for (i, item) in #name.iter().enumerate() {
::prost_validate::Validator::validate(item).map_err(|e| ::prost_validate::Error::new(format!("{}[{}]", #field, i), ::prost_validate::errors::list::Error::Item(Box::new(e))))?;
::prost_validate::validate!(item).map_err(|e| ::prost_validate::Error::new(format!("{}[{}]", #field, i), ::prost_validate::errors::list::Error::Item(Box::new(e))))?;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions prost-validate-derive-core/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ impl ToValidationTokens for MessageRules {
let field = &ctx.name;
if ctx.boxed {
quote! {
::prost_validate::Validator::validate(#name.as_ref()).map_err(|e| ::prost_validate::Error::new(#field, ::prost_validate::errors::message::Error::Message(Box::new(e))))?;
let #name = #name.as_ref();
::prost_validate::validate!(#name).map_err(|e| ::prost_validate::Error::new(#field, ::prost_validate::errors::message::Error::Message(Box::new(e))))?;
}
} else {
quote! {
::prost_validate::Validator::validate(#name).map_err(|e| ::prost_validate::Error::new(#field, ::prost_validate::errors::message::Error::Message(Box::new(e))))?;
::prost_validate::validate!(#name).map_err(|e| ::prost_validate::Error::new(#field, ::prost_validate::errors::message::Error::Message(Box::new(e))))?;
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion prost-validate-derive-core/src/oneof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct OneOfRules {
impl ToValidationTokens for OneOfRules {
fn to_validation_tokens(&self, _: &Context, name: &Ident) -> TokenStream {
quote! {
::prost_validate::Validator::validate(#name)?;
::prost_validate::validate!(#name)?;
}
}
}
Loading

0 comments on commit c444f4d

Please sign in to comment.