Skip to main content

protovalidate_buffa/
lib.rs

1//! Runtime companion for `protoc-gen-protovalidate-buffa`.
2
3mod error;
4pub mod cel;
5pub mod rules;
6
7#[cfg(feature = "connect")]
8mod connect;
9
10pub use error::{FieldPath, FieldPathElement, FieldType, Subscript, ValidationError, Violation};
11
12// Re-export external crates referenced by plugin-generated code so that
13// downstream crates only need to depend on `protovalidate-buffa` and not on
14// `regex` / `cel-interpreter` directly.
15pub use cel_interpreter;
16pub use regex;
17
18/// `#[auto_validate]` — attribute macro applied to a service `impl` block that
19/// inserts `req.validate()?` at the top of every handler method. Guarantees
20/// protovalidate runs for every RPC without relying on per-handler discipline.
21pub use protovalidate_buffa_macros::auto_validate;
22
23pub trait Validate {
24    /// Runs every rule attached to this message (and any nested messages),
25    /// collecting violations rather than short-circuiting on the first.
26    ///
27    /// # Errors
28    ///
29    /// Returns a [`ValidationError`] containing one or more [`Violation`]s
30    /// when any rule fails. Callers typically map this to
31    /// `ConnectError::invalid_argument` via
32    /// [`ValidationError::into_connect_error`] (requires the `connect` feature).
33    fn validate(&self) -> Result<(), ValidationError>;
34}
35
36#[macro_export]
37macro_rules! field_path {
38    ( $( $part:expr ),* $(,)? ) => {{
39        let mut elements = ::std::vec::Vec::new();
40        $(
41            elements.push($crate::FieldPathElement {
42                field_number: None,
43                field_name: Some(::std::borrow::Cow::Borrowed($part)),
44                field_type: None,
45                subscript: None,
46            });
47        )*
48        $crate::FieldPath { elements }
49    }};
50}