Skip to content

Add lint rule for redundant protovalidate rules #4177

Description

@timostamm

Feature

There are situations where a protovalidate rule is redundant. For example, both rules in the following example express the same requirement: The field hello cannot be "":

syntax="proto3";
import "buf/validate/validate.proto";
message Message {
  string hello = 1 [
    (buf.validate.field).required = true,
    (buf.validate.field).string.min_len = 1
  ];
}
// error raised for an empty field:
// hello: value is required [required]

It's similar for other standard rules:

syntax="proto3";
import "buf/validate/validate.proto";
message Message {
  string email = 1 [
    (buf.validate.field).required = true,
    (buf.validate.field).string.email = true
  ];
}
// error raised for an empty field:
// email: value is required [required]

In this case, removing the required rule would actually produce a more helpful error:

email: value is empty, which is not a valid email address [string.email]

This applies to many of the standard rules on fields with implicit presence when combined with the required rule, but it depends on rule values.

buf lint could detect such situations, and alert the user about redundant rules. This leads to a more simple schema, and better error messages.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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