Skip to content

Field rules

FieldRules encapsulates the rules for each type of field. Depending on the field, the correct set should be used to ensure proper validations.

cel

cel is a repeated field used to represent a textual expression in the Common Expression Language (CEL) syntax. For more information on CEL, see our documentation.

field.cel example
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

If required is true, the field must be populated. A populated field can be described as "serialized in the wire format," which includes:

  • the following "nullable" fields must be explicitly set to be considered populated:
    • singular message fields (whose fields may be unpopulated/default values)
    • member fields of a oneof (may be their default value)
    • proto3 optional fields (may be their default value)
    • proto2 scalar fields (both optional and required)
  • proto3 scalar fields must be non-zero to be considered populated
  • repeated and map fields must be non-empty to be considered populated
field.required example
message MyMessage {
  // The field `value` must be set to a non-null value.
  optional MyOtherMessage value = 1 [(buf.validate.field).required = true];
}

ignore

Skip validation on the field if its value matches the specified criteria. See Ignore enum for details.

field.ignore example
message UpdateRequest {
  // The uri rule only applies if the field is populated and not an empty
  // string.
  optional string url = 1 [
    (buf.validate.field).ignore = IGNORE_IF_DEFAULT_VALUE,
    (buf.validate.field).string.uri = true,
  ];
}