pub struct MessageRules {
pub cel: Vec<Rule>,
pub oneof: Vec<MessageOneofRule>,
}Expand description
MessageRules represents validation rules that are applied to the entire message. It includes disabling options and a list of Rule messages representing Common Expression Language (CEL) validation rules.
Fields§
§cel: Vec<Rule>cel is a repeated field of type Rule. Each Rule specifies a validation rule to be applied to this message.
These rules are written in Common Expression Language (CEL) syntax. For more information,
see our documentation.
message MyMessage {
// The field `foo` must be greater than 42.
option (buf.validate.message).cel = {
id: "my_message.value",
message: "value must be greater than 42",
expression: "this.foo > 42",
};
optional int32 foo = 1;
}oneof: Vec<MessageOneofRule>oneof is a repeated field of type MessageOneofRule that specifies a list of fields
of which at most one can be present. If required is also specified, then exactly one
of the specified fields must be present.
This will enforce oneof-like constraints with a few features not provided by actual Protobuf oneof declarations:
- Repeated and map fields are allowed in this validation. In a Protobuf oneof, only scalar fields are allowed.
- Fields with implicit presence are allowed. In a Protobuf oneof, all member fields have explicit presence. This means that, for the purpose of determining how many fields are set, explicitly setting such a field to its zero value is effectively the same as not setting it at all.
- This will always generate validation errors for a message unmarshalled from serialized data that sets more than one field. With a Protobuf oneof, when multiple fields are present in the serialized form, earlier values are usually silently ignored when unmarshalling, with only the last field being set when unmarshalling completes.
Note that adding a field to a oneof will also set the IGNORE_IF_ZERO_VALUE on the fields. This means
only the field that is set will be validated and the unset fields are not validated according to the field rules.
This behavior can be overridden by setting ignore against a field.
message MyMessage {
// Only one of `field1` or `field2` _can_ be present in this message.
option (buf.validate.message).oneof = { fields: \["field1", "field2"\] };
// Exactly one of `field3` or `field4` _must_ be present in this message.
option (buf.validate.message).oneof = { fields: \["field3", "field4"\], required: true };
string field1 = 1;
bytes field2 = 2;
bool field3 = 3;
int32 field4 = 4;
}Trait Implementations§
Source§impl Clone for MessageRules
impl Clone for MessageRules
Source§fn clone(&self) -> MessageRules
fn clone(&self) -> MessageRules
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MessageRules
impl Debug for MessageRules
Source§impl Default for MessageRules
impl Default for MessageRules
Source§impl Message for MessageRules
impl Message for MessageRules
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.Source§impl PartialEq for MessageRules
impl PartialEq for MessageRules
impl StructuralPartialEq for MessageRules
Auto Trait Implementations§
impl Freeze for MessageRules
impl RefUnwindSafe for MessageRules
impl Send for MessageRules
impl Sync for MessageRules
impl Unpin for MessageRules
impl UnwindSafe for MessageRules
Blanket Implementations§
Source§impl<T> AnyExt for T
impl<T> AnyExt for T
Source§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T behind referenceSource§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T behind mutable referenceSource§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T behind Rc pointerSource§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T behind Arc pointer