Skip to content

Valid types not generated for containing messages that don't have annotations #1224

@anuraaga

Description

@anuraaga

I have a schema like this

message Plan {
  // The date of the plan. The timestamp will begin on the date in UTC.
  google.protobuf.Timestamp date = 1 [(buf.validate.field).required = true];
}

message GetPlansRequest {}

message GetPlansResponse {
  // The users current plans.
  repeated Plan plans = 1;
}

The generated code does not generate GetPlansResponseValid with plans: PlanValid[]

export type GetPlansResponse = Message<"frontendapi.GetPlansResponse"> & {
  /**
   * The users current plans.
   *
   * @generated from field: repeated frontendapi.Plan plans = 1;
   */
  plans: Plan[];
};

export type GetPlansResponseValid = GetPlansResponse; // !!! Needs to be a valid type

/**
 * Describes the message frontendapi.GetPlansResponse.
 * Use `create(GetPlansResponseSchema)` to create a new message.
 */
export const GetPlansResponseSchema: GenMessage<GetPlansResponse, {validType: GetPlansResponseValid}> = /*@__PURE__*/
  messageDesc(file_frontendapi_frontend, 22);

I suspect this is because GetPlansResponse itself has no annotations so is getting ignored by the valid type generation. All of a message's message-type fields would need to be checked to see if a valid type should be generated.

My workaround is to validate the field

const plans = plansRes.plans
    .map((p) => validator.validate(PlanSchema, p))
    .filter((r) => r.kind === "valid")
    .map((r) => r.message);

but I'd like to be able to validate the whole response as one, notably it makes handling an invalid result (which I don't do here since it becomes too awkward) much easier.

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