Skip to content

Opt-in split of valid types into Input and Output by (google.api.field_behavior) #1259

@tony998244353

Description

@tony998244353

I have been using the valid types feature and found it very convenient.
On the server side, it is nice that I can handle a validated message as a narrowed type.

Then I thought that the same kind of narrowing would also be useful on the client side, for validated response messages.
However, fields that have (google.api.field_behavior) = OUTPUT_ONLY are difficult to deal with.
If I set (buf.validate.field).required = true, the request validation fails, so I cannot make them required, and because of that the client cannot know that those fields always exist in responses.

If there was an option to generate two valid types based on (google.api.field_behavior), it might solve this problem.

For example:

  • FooValidInput: excludes fields with (google.api.field_behavior) = OUTPUT_ONLY
  • FooValidOutput: includes those fields, and maybe treats (google.api.field_behavior) = INPUT_ONLY in the opposite way

Then both sides could validate and use narrowed types without duplicating .proto messages.

If this change happens, protovalidate would also need a related feature to generate two kinds of validators for input and output.

I understand that (google.api.field_behavior) is a Google annotation, and maybe Buf does not want to handle it directly.
But if there is any other way to solve this problem, it would be very helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions