FieldRules encapsulates the rules for each type of field. Depending on the field, the correct set should be used to ensure proper validations.
FieldRules encapsulates the rules for each type of field. Depending on the field, the correct set should be used to ensure proper validations.
Optional[customReturns a safe value for JSON logs.
Returns a safe value for JSON logs.
Optional[unknownPreserves protobuf fields that this SDK version does not recognize.
Contains the fully qualified protobuf type name.
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",
}];
}
cel_expression is a repeated field CEL expressions. Each expression specifies a validation
rule to be applied to this message. These rules are written in Common Expression Language (CEL) syntax.
This is a simplified form of the cel Rule field, where only expression is set. This allows for
simpler syntax when defining CEL Rules where id and message derived from the expression. id will
be same as the expression.
For more information, see our documentation.
message MyMessage {
// The field `value` must be greater than 42.
optional int32 value = 1 [(buf.validate.field).cel_expression = "this > 42"];
}
OptionalignoreIgnore 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
];
}
OptionalrequiredIf 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. It
can be added to indicate that the field cannot be the zero value.
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 = 2 [
(buf.validate.field).repeated.min_items = 1
];
// `required`, for fields that don't track presence, indicates
// the value of the field can't be the zero value.
int32 zero_value_not_allowed = 3 [
(buf.validate.field).required = true
];
}
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.
OptionaltypeContains the selected value for the type protobuf oneof.
Contains the case.
Contains the float.
Contains the case.
Contains the double.
Contains the case.
Contains the int32.
Contains the case.
Contains the int64.
Contains the case.
Contains the uint32.
Contains the case.
Contains the uint64.
Contains the case.
Contains the sint32.
Contains the case.
Contains the sint64.
Contains the case.
Contains the fixed32.
Contains the case.
Contains the fixed64.
Contains the case.
Contains the sfixed32.
Contains the case.
Contains the sfixed64.
Contains the case.
Contains the bool.
Contains the case.
Contains the string.
Contains the case.
Contains the bytes.
Contains the case.
Contains the enum.
Contains the case.
Contains the repeated.
Contains the case.
Contains the map.
Contains the case.
Contains the any.
Contains the case.
Contains the duration.
Contains the case.
Contains the field mask.
Contains the case.
Contains the timestamp.
FieldRules encapsulates the rules for each type of field. Depending on the field, the correct set should be used to ensure proper validations.