-
Notifications
You must be signed in to change notification settings - Fork 53
Add field_mask rules const/in/not_in
#429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1a19a4b to
3d47abb
Compare
|
Ready for review. |
80dbb4d to
51abb66
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch! Few comments to get it consistent with other rules, but there's a detail we might want to address:
A field_mask.in rule with a path of a would hypothetically imply that any sub-path also would be valid, such as a.b and a.c. As written, this limits the field mask to only target all of a and not just some of its sub-paths.
Changing the expression to support matching subpaths isn't terribly difficult (though polynomial time complexity). The expression for in would be:
!this.paths.all(p, p in getField(rules, 'in') || getField(rules, 'in').exists(f, p.startsWith(f+"."))) ? ... : ...
@timostamm, any feelings on this?
|
Thank you for the detailled review. I agree on the requested changes and Regarding your suggestion in #429 (review):
|
Agreed. I expect a mask IMO it makes sense to apply the same logic for |
in/not_in
in/not_inin/not_in
in/not_inconst/in/not_in
|
Are CI errors related to license headers removed when using edit: well, and I did not regenerate.. |
|
Looks like bazel is unhappy now. You'll need to add the field mask dep to proto/protovalidate/buf/validate/BUILD.bazel. Gazelle should add it when you run generate, but it looks like you may need to do it manually. |
This adds support for the well-known type google.protobuf.FieldMask:
- const: exact matching of paths
- in: accepted paths (violation if unlisted paths or subpaths are encountered)
- not_in: rejected paths (violation if any specified path or subpath is encountered)
Usage examples:
const:
```proto
message MyFieldMask {
// The `value` FieldMask must be equal to `const`.
google.protobuf.FieldMask value = 1 [(buf.validate.field).field_mask.const = {
paths: ["foo", "bar"]
}];
}
```
in:
```proto
message MyFieldMask {
// The `value` FieldMask must only contain paths listed in `in`.
google.protobuf.FieldMask value = 1 [(buf.validate.field).field_mask = {
in: ["a", "b", "c.a"]
}];
}
```
not_in:
```proto
message MyFieldMask {
// The `value` FieldMask must not contain paths listed in `not_in`.
google.protobuf.FieldMask value = 1 [(buf.validate.field).field_mask = {
not_in: ["forbidden", "immutable", "c.a"]
}];
}
```
Closes bufbuild#428.
Signed-off-by: Christoph Hoopmann <[email protected]>
|
Done, run generate and pushed. Here is what I'm referring to when I mentioned license headers being removed by generate:
@@ -1,17 +1,3 @@
-// Copyright 2023-2025 Buf Technologies, Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-Should I push these changes aswell? |
Nope, there's likely a bug in our Makefile. I'll look into it. |
rodaine
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @choopm!
This implements #428.
I might require some help regarding the test suites. Let me know what you think.