Struct Validator
The codec/validator package provides struct validation inspired by the OpenAPI Specification (OAS). It uses constraints struct tags to declaratively validate field values.
Installation
go get oss.nandlabs.io/golly/codec/validatorQuick Start
Add constraints tags to your struct fields and use the validator to check values:
package main
import (
"fmt"
validator "oss.nandlabs.io/golly/codec/validator"
)
type TestStruct struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:"min=21"`
Description string `json:"description" constraints:"max-length=50"`
Cost float64 `json:"cost" constraints:"exclusiveMin=200"`
ItemCount int `json:"itemCount" constraints:"multipleOf=5"`
}
func main() {
sv := validator.NewStructValidator()
msg := TestStruct{
Name: "Test",
Age: 25,
Description: "this is bench testing",
Cost: 299.9,
ItemCount: 2000,
}
if err := sv.Validate(msg); err != nil {
fmt.Println(err)
}
}Fields without constraints tags are skipped during validation.
Supported Validations
| Name | Data Type | Description | Status |
|---|---|---|---|
min | numeric | Minimum value (inclusive) | โ |
max | numeric | Maximum value (inclusive) | โ |
exclusiveMin | numeric | Minimum value (exclusive) | โ |
exclusiveMax | numeric | Maximum value (exclusive) | โ |
multipleOf | numeric | Value must be a multiple of | โ |
max-length | string | Maximum string length | โ |
min-length | string | Minimum string length | โ |
pattern | string | Regex pattern match | โ |
notnull | string | Must not be empty | โ |
enum | all | Value must be one of listed values | โ |
Usage
Multiple Constraints
Combine multiple constraints on a single field:
type Config struct {
Port int `constraints:"min=1,max=65535"`
Host string `constraints:"min-length=1,pattern=^[a-z0-9.-]+$"`
Environment string `constraints:"enum=dev|staging|prod"`
}Skipping Validation
Fields without constraints tags are not validated:
type User struct {
Name string `constraints:"min-length=1"` // validated
Email string `constraints:"pattern=.+@.+"` // validated
Notes string // skipped
}