pub struct FieldRules {
pub cel: Vec<Rule>,
pub required: Option<bool>,
pub ignore: Option<i32>,
pub type: Option<Type>,
}Expand description
FieldRules encapsulates the rules for each type of field. Depending on the field, the correct set should be used to ensure proper validations.
Fields§
§cel: Vec<Rule>cel is a repeated field used to represent a textual expression
in the Common Expression Language (CEL) syntax. For more information,
see our documentation.
message MyMessage {
// The field `value` must be greater than 42.
optional int32 value = 1 [(buf.validate.field).cel = {
id: "my_message.value",
message: "value must be greater than 42",
expression: "this > 42",
}];
}required: Option<bool>If required is true, the field must be set. A validation error is returned
if the field is not set.
syntax="proto3";
message FieldsWithPresence {
// Requires any string to be set, including the empty string.
optional string link = 1 [
(buf.validate.field).required = true
];
// Requires true or false to be set.
optional bool disabled = 2 [
(buf.validate.field).required = true
];
// Requires a message to be set, including the empty message.
SomeMessage msg = 4 [
(buf.validate.field).required = true
];
}All fields in the example above track presence. By default, Protovalidate
ignores rules on those fields if no value is set. required ensures that
the fields are set and valid.
Fields that don’t track presence are always validated by Protovalidate,
whether they are set or not. It is not necessary to add required:
syntax="proto3";
message FieldsWithoutPresence {
// `string.email` always applies, even to an empty string.
string link = 1 [
(buf.validate.field).string.email = true
];
// `repeated.min_items` always applies, even to an empty list.
repeated string labels = 4 [
(buf.validate.field).repeated.min_items = 1
];
}To learn which fields track presence, see the Field Presence cheat sheet.
Note: While field rules can be applied to repeated items, map keys, and map
values, the elements are always considered to be set. Consequently,
specifying repeated.items.required is redundant.
ignore: Option<i32>Ignore validation rules on the field if its value matches the specified
criteria. See the Ignore enum for details.
message UpdateRequest {
// The uri rule only applies if the field is not an empty string.
string url = 1 [
(buf.validate.field).ignore = IGNORE_IF_ZERO_VALUE,
(buf.validate.field).string.uri = true
];
}type: Option<Type>Implementations§
Source§impl FieldRules
impl FieldRules
Sourcepub fn required(&self) -> bool
pub fn required(&self) -> bool
Returns the value of required, or the default value if required is unset.
Sourcepub fn ignore(&self) -> Ignore
pub fn ignore(&self) -> Ignore
Returns the enum value of ignore, or the default if the field is unset or set to an invalid enum value.
Sourcepub fn set_ignore(&mut self, value: Ignore)
pub fn set_ignore(&mut self, value: Ignore)
Sets ignore to the provided enum value.
Trait Implementations§
Source§impl Clone for FieldRules
impl Clone for FieldRules
Source§fn clone(&self) -> FieldRules
fn clone(&self) -> FieldRules
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for FieldRules
impl Debug for FieldRules
Source§impl Default for FieldRules
impl Default for FieldRules
Source§impl Message for FieldRules
impl Message for FieldRules
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 FieldRules
impl PartialEq for FieldRules
impl StructuralPartialEq for FieldRules
Auto Trait Implementations§
impl !Freeze for FieldRules
impl RefUnwindSafe for FieldRules
impl Send for FieldRules
impl Sync for FieldRules
impl Unpin for FieldRules
impl UnwindSafe for FieldRules
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