Skip to content
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

Implement Filter interface #185

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Implement Filter interface #185

wants to merge 4 commits into from

Conversation

jchadwick-buf
Copy link
Member

@jchadwick-buf jchadwick-buf commented Feb 7, 2025

Adds ValidationOption for options passed to the Validate function rather than on creation of a validator.

Adds WithFilter option for setting a filter to control which fields are validated.

Adds Filter interface:

type Filter interface {
	ShouldValidate(message protoreflect.Message, descriptor protoreflect.Descriptor) bool
}
  • ShouldValidate is called once for a message, once for each of its oneofs, and once for each of its fields. The message value is the message itself, and the descriptor value is the descriptor of the element being operated on.
  • ShouldValidate will be called for every oneof and field even if it returns false for any message, field or oneof.
  • Sub-messages nested under fields will not be checked if ShouldValidate returns false.

This is to satisfy the following usecases

  • For buf lint, we want to be able to single out an individual field to check examples. This can be done by using descriptor == fieldDescriptor as a filter. This can also filter oneof and message constraints, which is desirable.
  • For field masking, it is possible for a stateful filter to perform field masking using this interface. The only important behavioral detail for that is that we nest depth-first into submessages. This would allow a user to implement out-of-band field masks.

Copy link

github-actions bot commented Feb 7, 2025

The latest Buf updates on your PR. Results from workflow Buf / validate-protos (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedFeb 11, 2025, 6:25 PM

@jchadwick-buf jchadwick-buf marked this pull request as ready for review February 11, 2025 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant