syntax = "proto3";
package vioozer_protobuf ;
message Update
{
string sensor_id = 1;
...
}
In my system, The sensor has a unique id format (a-la SENSOR-1342r43), which can be easily verified using regular expressions.
Is there a way to add a regular expression validator to the protobuf field so that only the regular expression is met The type string will be accepted into this field?
You can try to find an existing plugin, or you can create your own plugin (if you don’t have an existing plugin in your language).
If you decide to write your own plugin, the first step is to define a custom option for the field:
package yourcompany;
import " google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional string validator = 51234;
}
This option allows you Specify regular expressions for specific fields. Then, you apply new custom options:
message Update {
string sensor_id = 1 [(yourcompany.validator) = "SENSOR-???????"];
// ...
}
Secondly, the more challenging step is to write your own plugin in order to Add verification logic to the generated code:
Additionally, plugins are able to insert code into the files generated by other code generators. See the comments about “insertion points” in 07002 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 the value of the custom option and generate additional verification code for the field.
I have been using Protobuf3 to define PB messages:
syntax = "proto3";
package vioozer_protobuf ;
message Update
{
string sensor_id = 1;
...
}
In my system, The sensor has a unique id format (a-la SENSOR-1342r43), which can be easily verified using regular expressions.
Is there a way to add a regular expression validator to the protobuf field so that only the regular expression is met The type string will be accepted into this field?
Protobuf does not support out-of-the-box message verification, but you can add it using plugins (this is the only way, but it’s not simple).
You can try to find existing plugins, or you can create your own plugins (if you don’t have existing plugins in your language).
If you decide to write your own plugins, then The first step is to define a custom option for the field:
package yourcompany;
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional string validator = 51234;
}
This option allows you to specify regular expressions for specific fields. Then, you apply New customization options:
message Update {
string sensor_id = 1 [(yourcompany.validator) = "SENSOR-???????"] ;
// ...
}
Secondly, the more challenging step is to write your own plugin to add verification logic to the generated code:
< p>
Additionally, plugins are able to insert code into the files generated by other code generators. See the comments about “insertion points” in 07002 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 inser tion points they provide.
Your plugin must check the value of the custom option and generate other verification codes for the field.