Overview
In our Go architecture, we usego-playground/validator for data validation, which is similar to Zod in TypeScript. Both provide declarative, type-safe validation with excellent error messages.
📊 Comparison: Zod vs Go Validator
| Feature | Zod (TypeScript) | go-playground/validator (Go) |
|---|---|---|
| Syntax | Fluent API | Struct tags |
| Type Safety | ✅ Yes | ✅ Yes (compile-time) |
| Validation | Runtime | Runtime |
| Error Messages | Customizable | Customizable |
| Ecosystem | npm package | Go module |
| Performance | Good | Excellent (compiled) |
🎯 How It Works in Our Architecture
1. Define Request DTOs with Validation Tags
2. Validation Flow in HTTP Handler
🏗️ Architecture Layers
📝 Common Validation Tags
Basic Validations
Advanced Validations
🎨 Custom Validation
1. Register Custom Validator
2. Use Custom Validator
🚨 Error Handling
Current Implementation
Enhanced Error Messages (Recommended)
🔄 Comparison with Your TypeScript Code
TypeScript (Hono + Zod)
Go (Echo + Validator)
🎯 Best Practices
1. Separate DTOs from Domain Models
2. Layer-Specific Validation
3. Validation Groups (Optional)
📚 Available Validation Tags
| Tag | Description | Example |
|---|---|---|
required | Field must be present | validate:"required" |
email | Valid email format | validate:"email" |
min | Minimum length/value | validate:"min=8" |
max | Maximum length/value | validate:"max=100" |
len | Exact length | validate:"len=10" |
gt | Greater than | validate:"gt=0" |
gte | Greater than or equal | validate:"gte=18" |
lt | Less than | validate:"lt=100" |
lte | Less than or equal | validate:"lte=120" |
oneof | One of values | validate:"oneof=red blue green" |
url | Valid URL | validate:"url" |
uuid | Valid UUID | validate:"uuid" |
numeric | Numeric string | validate:"numeric" |
alpha | Alphabetic only | validate:"alpha" |
alphanum | Alphanumeric only | validate:"alphanum" |
e164 | E.164 phone format | validate:"e164" |
omitempty | Skip if empty | validate:"omitempty,email" |