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

Add validating interceptor #4

Merged
merged 21 commits into from
Sep 6, 2023
Merged

Add validating interceptor #4

merged 21 commits into from
Sep 6, 2023

Conversation

elliotmjackson
Copy link
Contributor

@elliotmjackson elliotmjackson commented Aug 29, 2023

Relates to #2

This PR introduces an interceptor for Connect to validate incoming protobuf messages using protovalidate-go. Supports both unary and streaming RPCs.

📝 Default Behaviors:

  • Requests are validated for adherence to the defined message structure.
  • Responses remain unvalidated; the focus is on incoming data validation.
  • Errors are raised for messages that aren't protocol buffer messages.
  • In cases of validation errors, an error detail type is attached containing the Violations to offer more context about the failure.

🧪 Testing:

  1. Test Scenarios:
    • Check that an empty request correctly reports required fields.
    • Validate that invalid values trigger the expected validation errors.
    • Ensure unrelated server errors (like internal server errors) remain unaffected by validation logic.
    • Confirm valid requests proceed without any validation issues.
  2. Mock Server Utilization: Utilizes the testserver package to create in-memory servers for testing client-server interactions without the need for actual networking.

Open Questions:

  • Tests contain a bit of duplicated code(at least enough to raise suspicion of the linter) - I've chosen to ignore this warning in favor of readability. I'm open to reducing this duplication if its desired.
  • More on linting: the exhaustruct rule is very annoying, particularly when dealing with functional option pattern as this requires an initialized and empty struct to be defined. I have turned it off but I feel we have a reason to be using it so I'm open to hearing what this reason may be.

buf.gen.yaml Outdated Show resolved Hide resolved
interceptor.go Outdated Show resolved Hide resolved
interceptor.go Outdated Show resolved Hide resolved
// See the License for the specific language governing permissions and
// limitations under the License.

package validate_test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to perform a user-centric test strategy. This is not a pattern I usually employ but I see it throughout the connectrpc org

internal/proto/connect/ping/v1/ping.proto Outdated Show resolved Hide resolved
internal/proto/connect/ping/v1/ping.proto Outdated Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pinched this from connect-go. Have we considered a connecttest sdk for users?

internal/testserver/ping_server.go Outdated Show resolved Hide resolved
@elliotmjackson elliotmjackson changed the title Implement Message Validation Interceptor Implement Message Validation Interceptor Aug 29, 2023
Rather than using whatever plugin version is latest in the BSR, build
`protoc-gen-go` and `protoc-gen-connect-go` locally. This guarantees
that we're using versions compatible with the Protobuf and Connect
runtimes in go.mod.
Switch to using a user service as our example. It offers a little more
space for complex validation, and using it in tests gives us confidence
that the README code snippet is correct.
@akshayjshah akshayjshah changed the title Implement Message Validation Interceptor Add validating interceptor Sep 6, 2023
@akshayjshah akshayjshah merged commit e34d02e into main Sep 6, 2023
5 checks passed
@akshayjshah akshayjshah deleted the ejackson/add-interceptor branch September 6, 2023 05:43
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.

2 participants