Skip to content

[Feature Request] Support standard field rules on nested message fields #427

@obeattie

Description

@obeattie

Feature description:

As discussed on Slack, add syntax sugar to apply standard field rules (like string.min_len) to nested message fields without requiring CEL expressions.

Problem it solves or use case:
Currently, if you want to validate a nested field in a context-specific way (e.g., require a field only in certain request messages), you must use CEL expressions. This is verbose and less discoverable than the standard field rules, and requires a manually-written message each time:

For example, to validate that user.id is not empty only in CreateUserRequest:

message User {
  string id = 1;
  string name = 2;
}

message CreateUserRequest {
  User user = 1 [
    (buf.validate.field).cel = {
      id: "user.id"
      message: "user.id must not be empty"
      expression: "this.id.size() > 0"
    }
  ];
}

This works, but requires developers to write CEL expressions that essentially duplicate existing standard rules.

Proposed implementation or solution:

Allow dot notation or nested syntax to apply standard rules to nested fields. Something like this, perhaps:

message CreateUserRequest {
  User user = 1 [
    (buf.validate.field).nested.id.string.min_len = 1
  ];
}

Contribution

Happy to provide feedback during design, but not able to implement at this time.

Examples or references

JSON Schema allows similar nested validation. I believe GraphQL input validation also supports nested field constraints.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions