Getting Started
This content is for v0.7. Switch to the latest version for up-to-date documentation.
Introduction
Section titled “Introduction”Valgo is a type-safe, expressive, and extensible validator for Go with built-in i18n support.
Unlike validation libraries that rely on struct tags, Valgo defines validation rules as functions. This gives you greater flexibility to validate any value, compose rules programmatically, and decide where validation belongs within your application.
Valgo can be customized to fit your application’s needs, from overriding validation messages to localizing them for different languages and contexts.
Install
Section titled “Install”go get github.com/cohesivestack/valgoGo 1.18+ is required (Valgo uses generics).
Your first validation
Section titled “Your first validation”Is(...) creates a validation session and short-circuits per field: once a rule fails for a value, later rules for that value are skipped.
import ( "encoding/json" "fmt" v "github.com/cohesivestack/valgo")
val := v.Is( v.String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20), v.Number(17, "age").GreaterThan(18),)
if err := val.ToError(); err != nil { out, _ := json.MarshalIndent(err, "", " ") fmt.Println(string(out))}When to use Is vs Check
Section titled “When to use Is vs Check”Is(...): short-circuits rules per value (usually what you want for UX and performance).Check(...): evaluates every rule even if earlier rules fail (useful when you want all messages at once).
val := v.Check( v.String("", "full_name").Not().Blank().OfLengthBetween(4, 20),)
_ = val.Valid() // false, with 2 messages for full_nameNested models
Section titled “Nested models”Use namespaces to build structured paths:
In("ns", ...)for nested structsInRow("list", i, ...)for slices of structsInCell("list", i, ...)for slices of scalar values
See Using Valgo -> Namespaces.