https://github.com/protoc-gen/protoc-gen-validatex
đ¨ protoc-gen-validatex is a protoc plugin that auto-generates validation rules for Protobuf messages, with easy configuration and i18n support for multilingual error messages.
https://github.com/protoc-gen/protoc-gen-validatex
i18n protoc protoc-gen protoc-gen-validate protoc-plugin protocol-buffers validation
Last synced: 5 months ago
JSON representation
đ¨ protoc-gen-validatex is a protoc plugin that auto-generates validation rules for Protobuf messages, with easy configuration and i18n support for multilingual error messages.
- Host: GitHub
- URL: https://github.com/protoc-gen/protoc-gen-validatex
- Owner: protoc-gen
- License: mit
- Created: 2025-01-24T02:04:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-04T08:00:35.000Z (about 1 year ago)
- Last Synced: 2025-04-04T09:21:36.323Z (about 1 year ago)
- Topics: i18n, protoc, protoc-gen, protoc-gen-validate, protoc-plugin, protocol-buffers, validation
- Language: Go
- Homepage:
- Size: 152 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
protoc-gen-validatex
| [English](https://github.com/protoc-gen/protoc-gen-validatex) | [䏿įŽäŊ](docs/README_zh-CN.md) |
---
`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](./example) for a quick start. Below are some code snippets for reference:
### Definition
```proto
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
```shell
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:
```go
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](./LICENSE) for the full license text.