protoc-gen-validatex

command module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2025 License: MIT Imports: 3 Imported by: 0

README

protoc-gen-validatex


protoc-gen-validatex is a plugin for protoc designed to simplify and automate the generation of common parameter validation rules. By using parameter options and extensions, users can easily configure validation rules and automatically generate the corresponding validators. The plugin also supports integration with internationalization (i18n), allowing the generation of multilingual error messages based on different language environments, thereby enhancing the user experience.

Key Features

  • Automated Rule Generation: This is a fundamental feature of the protoc plugin, enabling automatic generation of common validation rules.
  • Multilingual Support: Naturally supports multiple languages, allowing quick adaptation to various projects and business scenarios.
  • MIT License: Users can freely fork and modify the project without concerns, under the MIT license.

Quick Start

You can refer to the example for a quick start. Below are some code snippets for reference:

Definition

syntax = "proto3";

option go_package = "github.com/protoc-gen/protoc-gen-validatex/example;main";

import "validatex/validatex.proto";

message SignInRequest {
  string email = 1 [(validatex.rules).string.email = true];
  string password = 2 [(validatex.rules).string = {min_len: 5, max_len: 50}];
}

Generation

go install github.com/protoc-gen/protoc-gen-validatex
protoc --proto_path=. \
       --go_out=paths=source_relative:. \
       --validatex_out=paths=source_relative:. \
       --validatex_opt=i18n_dir=./example/i18n \
       ./example/*.proto

Usage:

package main

import (
    "context"
    "github.com/protoc-gen/protoc-gen-validatex/pkg/validatex"
    "log"
)

func main() {
    // Create a SignInRequest instance
    req := &SignInRequest{
        Email:    "test.com",
        Password: "password123",
    }

    ctx := context.WithValue(context.Background(), validatex.KeyXLang, "zh")

    // Validate the SignInRequest instance
    if err := req.Validate(ctx); err != nil {
        log.Fatalf("validation failed: %v", err)
    }

    log.Println("validation passed")
}

License

This project is licensed under the MIT License. See LICENSE for the full license text.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL