ElementsProject/peerswap

[Enhancement]Introducing protovalidate for Protobuf Validation

Open

#358 opened on 2025年2月18日

GitHub で見る
 (1 comment) (0 reactions) (0 assignees)Go (81 forks)auto 404
enhancementgood first issue

Repository metrics

Stars
 (125 stars)
PR merge metrics
 (PR metrics pending)

説明

Overview

This project uses Go and gRPC with Protocol Buffers (Protobuf). We already have buf set up for code generation and linting. Now, I propose introducing protovalidate to define and enforce validation rules directly in .proto files. This approach reduces duplicated validation logic and ensures data integrity.

Key Steps

  1. Integrate protovalidate
  • Add (buf.validate.field) annotations to .proto files for field-level validation (e.g., numeric ranges, string lengths).
  • Install protovalidate in your Go module (go get github.com/bufbuild/protovalidate-go).
  • In your gRPC handlers or interceptors, call validator.Validate(ctx, message) to check inputs before processing.
  1. Add Unit Tests for Validation
  • Write test cases covering expected valid and invalid inputs.
  • Ensure that fields fail when violating constraints (e.g., out-of-range numeric values, overly long strings).
  • Verify that the server rejects invalid requests and returns appropriate error messages.

Benefits

  • Centralized Validation: Keep validation rules in Protobuf definitions.
  • Consistent Enforcements: Rely on Protobuf’s type system for predictable checks.
  • Efficiency: Streamline code generation and reduce manual validation code.

Request for Feedback

Please share any concerns or suggestions about integrating protovalidate—particularly regarding unit test strategies and the effect on existing logic. Thank you for your input!

コントリビューターガイド