0

I'm sending the following POST request to my gRPC application:

curl \
--request POST \
--header 'Content-Type: application/json' \
--data-raw '{
 "mandatory-key1": "value1",
 "mandatory-key2": {
    "arbitrary-optional-key1": [
        "b",
        "c"
    ],
    "arbitrary-optional-key2": [
        "e"
    ]
  }
}' \
'http://localhost:11000/MyEndpoint'

The value associated with mandatory-key-1 must be a non-empty string. The value associated with mandatory-key-2 must be a map where all keys are strings and all values are lists of strings.

Now I have to model this request's data structure in the gRPC proto file.

I am thinking of doing something like this:

message MyRequestData {
  // pairs represents that map that the user will send in to the MyEndpoint.
  map<string, string> pairs = 1;
}

But this specification is not general enough. I need to know how to write this specification correctly.

Question 1: How can I write this specification so it accepts strings in the values and also lists of strings?

Question 2: How can I do validation such that I ensure pairs has keys mandatory-key1 and mandatory-key2 and nothing else?

Question 3: How can I do validation such that I ensure:

  1. pairs has keys mandatory-key1 and mandatory-key2 and nothing else?
  2. pairs[mandatory-key1"] has value which is a non-empty string?
  3. pairs["mandatory-key2"] has value which is a map of <strings, list of non-empty strings>?
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

1 Answers1

0

Protobufs don't provide (the) validation (that you need).

You'd need to code your validation when you use the protoc-generated sources.

Protobuf doesn't support repeated map values directly, but you can:

message Request {
  string mandatory_key1 = 1;
  map<string, Value> mandatory_key2 = 2;
}
message Value {
  repeated string value = 1;
}
DazWilkin
  • 32,823
  • 5
  • 47
  • 88