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
上一篇 Nov 25, 2025 17:00
下一篇 Nov 24, 2025 00:00

Related Posts

  • Go Engineering System Course 007 [Study Notes]

    Goods Microservice Entity Structure Description This module contains the following core entities: Goods (Goods) Goods Category (Category) Brand (Brands) Carousel (Banner) Goods Category Brand (GoodsCategoryBrand) 1. Goods (Goods) Describes the product information actually displayed and sold on the platform. Field Description Field Name Type Description name String Product name, required brand Pointer …

    Personal Nov 25, 2025
    21500
  • In-depth Understanding of ES6 002 [Study Notes]

    Strings and Regular Expressions. Strings and Regular Expressions. JavaScript strings have always been built based on 16-bit character encoding (UTF-16). Each 16-bit sequence is a code unit, representing a character. String properties and methods like `length` and `charAt()` are all constructed based on these code units. The goal of Unicode is to provide a globally unique identifier for every character in the world. If we limit the character length to 16 bits, the number of code points will not...

    Personal Mar 8, 2025
    1.8K00
  • Node: In-depth Yet Accessible (Sheng Siyuan Education) 001 [Study 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
    28100
  • Go Engineer Comprehensive Course 011 [Study Notes]

    Inverted Index for Queries
    1. What is an Inverted Index?
    An Inverted Index is a data structure used to quickly find documents containing specific terms. It is one of the core technologies of search engines.
    1.1 Basic Concepts
    Forward Index: Document ID → Document Content (list of terms)
    Inverted Index: Term → List of Document IDs containing the term
    1.2 Why is it called "Inverted"?
    An inverted index reverses the traditional relationship of "which terms a document contains" to "in which documents a term appears...

    Personal Nov 25, 2025
    20100
  • In-depth Understanding of ES6 007 [Study Notes]

    Set and Map Collections. In JS, there is an `in` operator that can determine if a property exists in an object without needing to read the object's value, returning true if it exists. However, the `in` operator also checks the object's prototype chain, so using this method is only relatively safe when the object's prototype is null. Set Collection: `let set = new Set()` `set.add(5)` `set.add("5")` `console.log(s…`

    Personal Mar 8, 2025
    1.2K00
EN
简体中文 繁體中文 English
欢迎🌹 Coding never stops, keep learning! 💡💻 光临🌹