Skip to content

Map rules

MapRules describe the constraints applied to map values.

min_pairs

Specifies the minimum number of key-value pairs allowed. If the field has fewer key-value pairs than specified, an error message is generated.

map.min_pairs example
message MyMap {
  // The field `value` must have at least 2 key-value pairs.
  map<string, string> value = 1 [(buf.validate.field).map.min_pairs = 2];
}

max_pairs

Specifies the maximum number of key-value pairs allowed. If the field has more key-value pairs than specified, an error message is generated.

map.max_pairs example
message MyMap {
  // The field `value` must have at most 3 key-value pairs.
  map<string, string> value = 1 [(buf.validate.field).map.max_pairs = 3];
}

keys

Specifies the constraints to be applied to each key in the field.

map.keys example
message MyMap {
  // The keys in the field `value` must follow the specified constraints.
  map<string, string> value = 1 [(buf.validate.field).map.keys = {
    string: {
      min_len: 3
      max_len: 10
    }
  }];
}

values

Specifies the constraints to be applied to the value of each key in the field. Message values will still have their validations evaluated unless skip is specified here.

map.values example
message MyMap {
  // The values in the field `value` must follow the specified constraints.
  map<string, string> value = 1 [(buf.validate.field).map.values = {
    string: {
      min_len: 5
      max_len: 20
    }
  }];
}