Go Engineer Comprehensive Course: protoc-gen-validate Study Notes

protoc-gen-validate: Introduction and Usage Guide ✅ What is protoc-gen-validate? protoc-gen-validate (PGV for short) is a Protocol Buffers plugin used to add validation logic for struct fields in generated Go code. It automatically generates validation code for each field by adding validation rules in .proto files, saving you the trouble of manually...

protoc-gen-validate Introduction and Usage Guide

✅ What is protoc-gen-validate

protoc-gen-validate (PGV for short) is a Protocol Buffers plugin used to add validation logic for struct fields in generated Go code.

It automatically generates validation code for each field by adding validate rules in .proto files, saving you from writing validation logic manually.


🌟 Example

syntax = "proto3";

import "validate/validate.proto";

message User {
  string name = 1 [(validate.rules).string.min_len = 3];
  int32 age = 2 [(validate.rules).int32.gte = 18];
}

The generated Go code will automatically include:

user := &User{Name: "Tom", Age: 16}
if err := user.Validate(); err != nil {
    // err 会指出字段不符合要求
}

✅ Supported Validation Rule Types

  • string: min_len, max_len, email, ip, uri, pattern etc.
  • number: gte, lte, lt, gt
  • repeated: min_items, unique, items rules
  • map: min_pairs, keys rules, values rules
  • message: nested validation
  • oneof: sub-field validation
  • required: field requiredness

✅ Supported Languages

  • Go (strongest official support)
  • C++
  • Java
  • Python
  • C#
  • Rust (via community plugin)

✅ Maturity and Usage Recommendations

Item Description
Maturity Very mature, widely used in many microservice projects (e.g., buf.build ecosystem)
Community Support Has an active community, maintenance has been transferred from the original author to bufbuild
Compatibility with other tools Perfectly supports protoc-gen-go, can be used with buf, grpc-gateway, govalidator, etc.
Applicable Scenarios Server-side field validation, API request validation, microservice interface parameter validation

🛠 Usage Steps (Brief)

  1. Install the plugin:
go install github.com/bufbuild/protoc-gen-validate@latest
  1. Download the proto file definitions required by PGV:
git clone https://github.com/bufbuild/protoc-gen-validate

Or use buf registry reference: buf.build/buf/validate

  1. Compile command:
protoc \
  --proto_path=./proto \
  --go_out=. \
  --go-grpc_out=. \
  --validate_out=lang=go:. \
  your_file.proto

📦 Common Alternatives

Tool Introduction
cel An expression language from Google, can be used for proto validation, but slightly higher complexity
Manual validation Flexible but repetitive and prone to errors
go-playground/validator General Go struct validation tool, not dependent on proto

✅ Summary

protoc-gen-validate is a mature, stable, and easy-to-use protobuf field validation solution, especially suitable for:

  • Parameter validation in microservice architectures
  • Automatic generation of validation logic
  • Project teams looking to reduce manual, repetitive validation

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/4461

(0)
Walker的头像Walker
上一篇 Mar 10, 2026 00:00
下一篇 Mar 8, 2026 15:40

Related Posts

  • Go Engineer Systematic Course 002 [Study Notes]

    Differences between GOPATH and Go Modules 1. Concept GOPATH was Go's early dependency management mechanism. All Go projects and dependency packages must be placed within the GOPATH directory (default is ~/go). GO111MODULE=off must be set. Project paths must be organized according to the src/package_name structure. Version control is not supported, and dependency management needs to be handled manually (e.g., go get). The order for finding dependency packages is g...

    Nov 25, 2025
    29600
  • Go Engineer's Comprehensive Course 017: Learning Notes

    Introduction to Rate Limiting, Circuit Breaking, and Degradation (with Sentinel Practical Application)
    Based on the key video points from Chapter 3 (3-1 to 3-9) of the courseware, this guide compiles a service protection introduction for beginners, helping them understand "why rate limiting, circuit breaking, and degradation are needed," and how to quickly get started with Sentinel.
    Learning Path at a Glance
    3-1 Understanding Service Avalanche and the Background of Rate Limiting, Circuit Breaking, and Degradation
    3-2 Comparing Sentinel and Hystrix to clarify technology selection
    3-3 Sen...

    Personal Nov 25, 2025
    24200
  • [Opening]

    I am Walker, born in the early 1980s, a journeyer through code and life. A full-stack development engineer, I navigate the boundaries between front-end and back-end, dedicated to the intersection of technology and art. Code is the language with which I weave dreams; projects are the canvas on which I paint the future. Amidst the rhythmic tapping of the keyboard, I explore the endless possibilities of technology, allowing inspiration to bloom eternally within the code. An avid coffee enthusiast, I am captivated by the poetry and ritual of every pour-over. In the rich aroma and subtle bitterness of coffee, I find focus and inspiration, mirroring my pursuit of excellence and balance in the world of development. Cycling...

    Feb 6, 2025 Personal
    2.3K00
  • Fearless forward, fist power unleashed.

    Striving is an attitude. Life is like a competition with no shortcuts; only through continuous training, breaking through, and surpassing oneself can one stand on their own stage. This is not merely a contest, but rather a self-awakening—daring to face the fight, daring to challenge, and daring to become a stronger version of oneself. The Spirit of Striving in Sports. Whether it's boxing, running, or strength training, every punch thrown, every bead of sweat, every moment of gritting one's teeth and persevering, is a tempering of both body and mind. Striving is not merely a confrontation, but an attitude—facing challenges without backing down; facing failure, ...

    Personal Feb 26, 2025
    1.4K00
  • Node: Demystified (Shengsi Garden Education) 001 [Learning Notes]

    Node.js: Understanding it from an Asynchronous Programming Paradigm
    Node.js's Positioning and Core Philosophy
    Based on the V8 engine + libuv event-driven library, it brings JavaScript from the browser to the server side. It uses a single-threaded event loop to handle I/O, maximizing CPU time slices while waiting for I/O, making it particularly suitable for high-concurrency, I/O-intensive scenarios. "Don't block the main thread" is its design philosophy: try to offload time-consuming operations to the kernel or a thread pool, and callback results...

    Personal Nov 24, 2025
    34500
EN
简体中文 繁體中文 English