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?
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:
This option allows you to specify regular expression for a concrete field. Then you apply your new custom option:
Second, and more challenging step is to write your own plugin in order to add validation logic to generated code:
Your plugin must check value of your custom option and generate additional validation code for fields.