bufbuild / buf

The best way of working with Protocol Buffers.

Home Page:https://buf.build

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issues with the PROTOVALIDATE rules when linting a proto file with optional fields and protovalidate constraints

mivanovcpd opened this issue · comments

The linting rules for protovalidate do not support optional fields with field constraints.

To reproduce:

In a proto file:
optional string abc = 1 [(buf.validate.field).required = true];

And then use as rules for running buf:

version: v1
breaking:
  use:
    - FILE
lint:
  use:
    - DEFAULT
deps:
  - buf.build/bufbuild/protovalidate

Run the linter(I use npx @bufbuild/buf lint).

You will get something similar to:

file.proto:33:39:Field "abc" has (buf.validate.field).required but is in a oneof (_abc). Oneof fields must not have (buf.validate.field).required.

Workaround is to disable the PROTOVALIDATE rules, but I think that's a bad and temporary solution as you lose out on validation.

Please ask if additional context is needed.

As for why optional string abc = 1 [(buf.validate.field).required = true]; is meaningful => it allows you to differentiate between the field abc not being set at all and being set to "", basically allowing explicit field presence.

This is similar to how in OpenAPI terms you could have a JSON field of type string that's required, and you could set it to an empty string and that's still valid, but if you remove the field from the JSON, it will then complain it's missing.

This is definitely a bug - we'll fix.

Thanks for reporting this bug! This is fixed in #2590 and will go out in the next release. Sorry for the inconvenience

This has been fixed in v1.28.1, sorry for the troubles.

Thanks for the kind comments and quick fix. Really appreciate it 👍