Protobuf3: String validation with regex

Adam Matan picture Adam Matan · Oct 5, 2016 · Viewed 12k times · Source

I have been using Protobuf3 to define a PB message:

syntax = "proto3";
package vioozer_protobuf;

message Update
{
  string sensor_id = 1;
  ...
}

In my system, sensors have a unique id format (a-la SENSOR-1342r43) that can easily be validated with a regex.

Is there a way to add a regex validator to a protobuf field, so that only strings that adhere to the regex would be accepted into that field?

Answer

Kostiantyn picture Kostiantyn · Oct 8, 2016

Protobuf does not support message validation out of the box, but it is possible to add it using plugin (that's the only way, however, it is not simple).

You can try to find existing plugin, or create your own (if there is no existing plugin for your language).

If you decide to write your own plugin, then first step is to define a custom option for fields:

package yourcompany;

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
    optional string validator = 51234;
}

This option allows you to specify regular expression for a concrete field. Then you apply your new custom option:

message Update {
    string sensor_id = 1 [(yourcompany.validator) = "SENSOR-???????"];
    // ...
}

Second, and more challenging step is to write your own plugin in order to add validation logic to generated code:

Additionally, plugins are able to insert code into the files generated by other code generators. See the comments about "insertion points" in plugin.proto for more on this. This could be used, for example, to write a plugin which generates RPC service code that is tailored for a particular RPC system. See the documentation for the generated code in each language to find out what insertion points they provide.

Your plugin must check value of your custom option and generate additional validation code for fields.