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

Related Posts

  • Go Engineer System Course 013 [Study Notes]

    Order transactions, whether deducting inventory first or later, will both affect inventory and orders. Therefore, distributed transactions must be used to address business issues (e.g., unpaid orders). One approach is to deduct inventory only after successful payment (e.g., an order was placed, but there was no inventory at the time of payment). Another common method is to deduct inventory when the order is placed, but if payment isn't made, the order is returned/released upon timeout.

    Transactions and Distributed Transactions
    1. What is a transaction?
    A transaction is an important concept in database management systems. It is a collection of database operations, which either all execute successfully, or all...

    Personal Nov 25, 2025
    39800
  • Deep Dive into ES6 010 [Study Notes]

    Improved array functionality. The peculiar behavior of `new Array()`: when a single numeric value is passed to the constructor, the array's `length` property is set to that value; if multiple values are passed, regardless of whether they are numeric or not, they all become elements of the array. This behavior is confusing, as it's not always possible to pay attention to the type of data passed in, thus posing a certain risk. `Array.of()`, regardless of how many arguments are passed, has no special case for a single numeric value (one argument and numeric type); it always returns an array containing all arguments...

    Personal Mar 8, 2025
    1.3K00
  • Love sports, challenge limits, embrace nature.

    Passion. In this fast-paced era, we are surrounded by the pressures of work and life, often neglecting our body's needs. However, exercise is not just a way to keep fit; it's a lifestyle that allows us to unleash ourselves, challenge our limits, and dance with nature. Whether it's skiing, rock climbing, surfing, or running, cycling, yoga, every sport allows us to find our inner passion and feel the vibrancy of life. Sport is a self-challenge. Challenging limits is not exclusive to professional athletes; it's a goal that everyone who loves sports can pursue. It can...

    Personal Feb 26, 2025
    1.5K00
  • Go Engineer 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
    32800
  • Nuxt3: Beginner's Guide and Principles Introduction [Learning Notes]

    Nuxt 3: Getting Started and Principles 💡 What is Nuxt 3? Nuxt 3 is a full-stack frontend framework built on Vue 3 and Vite, supporting: Server-Side Rendering (SSR) Static Site Generation (SSG) Single-Page Applications (SPA) Building full-stack applications (with API support) Nuxt 3 is an "enhanced version" of Vue, helping you simplify project structure and development workflow. 🔧 Core Principles Feature How Nuxt Handles It ✅ Page Routing Automatic root...

    Personal Apr 6, 2025
    2.2K00
EN
简体中文 繁體中文 English