protobuf-es icon indicating copy to clipboard operation
protobuf-es copied to clipboard

Support buf.validate.oneof for Valid Types

Open ryancerf opened this issue 7 months ago • 1 comments

If (buf.validate.oneof).required = true; is used should the union for the valid msg exclude the undefined variant?

message MyOneOf {
  oneof variant {
    option (buf.validate.oneof).required = true;
    string value1 = 1;
    string value2 = 2;
  }
}
/**
 * @generated from message inputs.model.v1.MyOneOf
 */
export type MyOneOf = Message<"inputs.model.v1.MyOneOf"> & {
  /**
   * @generated from oneof inputs.model.v1.MyOneOf.variant
   */
  variant: {
    /**
     * @generated from field: string value1 = 1;
     */
    value: string;
    case: "value1";
  } | {
    /**
     * @generated from field: string value2 = 2;
     */
    value: string;
    case: "value2";
  } | { case: undefined; value?: undefined };
};

export type MyOneOfValid = MyOneOf;

ryancerf avatar Jul 16 '25 18:07 ryancerf

Valid types is an experimental feature. For a start, we only honor the required rule on message fields.

But we'd definitely love to support more rules in the future, and oneof.required is probably one of the first ones we want to add. 👍

timostamm avatar Jul 17 '25 14:07 timostamm