model applies defaults, typed external values, environment values, and validation rules to Go structs. A reusable Binding[T] compiles the schema once and can process many values of the same type.
go get github.com/ygrebnov/modelRequires Go 1.23.12 or later.
package main
import (
"context"
"fmt"
"github.com/ygrebnov/model"
)
type Config struct {
Name string `default:"api" validate:"min(1)"`
Port int `default:"8080" validate:"min(1),max(65535)"`
}
func main() {
binding, err := model.NewBinding[Config]()
if err != nil {
panic(err)
}
cfg := Config{}
if err := binding.ValidateWithDefaults(context.Background(), &cfg); err != nil {
panic(err)
}
fmt.Printf("%+v\n", cfg)
}ValidateWithDefaults applies literal defaults, then environment values captured when the binding was created, then validation. Use ApplyDefaults, ApplyValues, ApplyEnv, Validate, and WriteValues separately when your application needs a different flow. One-off wrappers are available for defaults and validation.
The model guide has practical examples for each operation, custom rules, error handling, and hierarchical CLI configuration. See the API reference for exported types and methods.
BSD 3-Clause. See LICENSE.