pub struct Violation {
pub field: Option<FieldPath>,
pub rule: Option<FieldPath>,
pub rule_id: Option<String>,
pub message: Option<String>,
pub for_key: Option<bool>,
}Expand description
Violation represents a single instance where a validation rule, expressed
as a Rule, was not met. It provides information about the field that
caused the violation, the specific rule that wasn’t fulfilled, and a
human-readable error message.
{
"fieldPath": "bar",
"ruleId": "foo.bar",
"message": "bar must be greater than 0"
}Fields§
§field: Option<FieldPath>field is a machine-readable path to the field that failed validation.
This could be a nested field, in which case the path will include all the parent fields leading to the actual field that caused the violation.
For example, consider the following message:
message Message {
bool a = 1 \[(buf.validate.field).required = true\];
}It could produce the following violation:
violation {
field { element { field_number: 1, field_name: "a", field_type: 8 } }
...
}rule: Option<FieldPath>rule is a machine-readable path that points to the specific rule rule that failed validation.
This will be a nested field starting from the FieldRules of the field that failed validation.
For custom rules, this will provide the path of the rule, e.g. cel\[0\].
For example, consider the following message:
message Message {
bool a = 1 \[(buf.validate.field).required = true\];
bool b = 2 [(buf.validate.field).cel = {
id: "custom_rule",
expression: "!this ? 'b must be true': ''"
}]
}It could produce the following violations:
violation {
rule { element { field_number: 25, field_name: "required", field_type: 8 } }
...
}
violation {
rule { element { field_number: 23, field_name: "cel", field_type: 11, index: 0 } }
...
}rule_id: Option<String>rule_id is the unique identifier of the Rule that was not fulfilled.
This is the same id that was specified in the Rule message, allowing easy tracing of which rule was violated.
message: Option<String>message is a human-readable error message that describes the nature of the violation.
This can be the default error message from the violated Rule, or it can be a custom message that gives more context about the violation.
for_key: Option<bool>for_key indicates whether the violation was caused by a map key, rather than a value.
Implementations§
Trait Implementations§
Source§impl Message for Violation
impl Message for Violation
Source§fn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
Source§fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>where
Self: Sized,
fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>where
Self: Sized,
Source§fn encode_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
fn encode_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
Source§fn encode_length_delimited(
&self,
buf: &mut impl BufMut,
) -> Result<(), EncodeError>where
Self: Sized,
fn encode_length_delimited(
&self,
buf: &mut impl BufMut,
) -> Result<(), EncodeError>where
Self: Sized,
Source§fn encode_length_delimited_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
fn encode_length_delimited_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
Source§fn decode(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
fn decode(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
Source§fn decode_length_delimited(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
fn decode_length_delimited(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
Source§fn merge(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
fn merge(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
self. Read moreSource§fn merge_length_delimited(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
fn merge_length_delimited(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
self.impl StructuralPartialEq for Violation
Auto Trait Implementations§
impl Freeze for Violation
impl RefUnwindSafe for Violation
impl Send for Violation
impl Sync for Violation
impl Unpin for Violation
impl UnwindSafe for Violation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request