Protobuf3: regex string check

I used Protobuf3 to define the PB message:

syntax = "proto3";
package vioozer_protobuf;

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

On my system, the sensors have a unique id (a-la SENSOR-1342r43) format , which can be easily checked using a regular expression.

Is there a way to add a regex validator to the protobuf field, so that only lines that are bound to the regular expression will be accepted into this field?

+4
source share
1 answer

Protobuf does not support checking messages out of the box, but it can be added using a plugin (this is the only way, however, it is not easy).

( ).

, :

package yourcompany;

import "google/protobuf/descriptor.proto";

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

. :

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

-, , :

, , . . " " plugin.proto. , , , RPC, RPC. . , , .

.

+5

Source: https://habr.com/ru/post/1656801/


All Articles