MCPcopy Index your code
hub / github.com/RussellLuo/validating

github.com/RussellLuo/validating @v3.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.0.0 ↗ · + Follow
113 symbols 329 edges 15 files 46 documented · 41%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

validating

A Go library for validating structs, maps and slices.

Features

  1. Simple

    Simple and stupid, no magic involved.

  2. Type-safe

    Schema is defined in Go, which is type-safer (and more powerful) than traditional struct tags.

  3. Flexible

    • Validators are composable.
    • Nested struct validation is well supported.
    • Schema can be defined inside or outside struct.
    • Validator customizations are made easy.
  4. No reflection

Installation

$ go get github.com/RussellLuo/validating/v3@latest

Quick Start

package main

import (
    "fmt"

    v "github.com/RussellLuo/validating/v3"
)

type Address struct {
    Country string
    City    string
}

func (a Address) Schema() v.Schema {
    return v.Schema{
        v.F("country", a.Country): v.Nonzero[string]().Msg("empty country"),
        v.F("city", a.City):       v.In("A", "B", "C").Msg("must be A or B or C"),
    }
}

type Person struct {
    Name    string
    Age     int
    Address Address
}

func (p Person) Schema() v.Schema {
    return v.Schema{
        v.F("name", p.Name):       v.LenString(1, 5).Msg("bad name"),
        v.F("age", p.Age):         v.Gte(10).Msg("must be older than 10 years old"),
        v.F("address", p.Address): p.Address.Schema(),
    }
}

func main() {
    p := Person{}
    errs := v.Validate(p.Schema())
    for _, err := range errs {
        fmt.Println(err)
    }
}
$ go run main.go
name: INVALID(bad name)
age: INVALID(must be older than 10 years old)
address.country: INVALID(empty country)
address.city: INVALID(must be A or B or C)

Validator factories and validators

To be strict, this library has a conceptual distinction between validator factory and validator.

A validator factory is a function used to create a validator, which will do the actual validation.

Built-in validator factories

Extension validator factories

Validator customizations

Examples

Documentation

Check out the Godoc.

Thanks

This library borrows some ideas from the following libraries:

License

MIT

Extension points exported contracts — how you extend this code

Validator (Interface)
Validator is an interface for representing a validating's validator. [3 implementers]
validating.go
Func (FuncType)
Func is an adapter to allow the use of ordinary functions as validators. If f is a function with the appropriate signatu
builtin.go
Error (Interface)
(no doc) [1 implementers]
errors.go

Core symbols most depended-on inside this repo

Validate
called by 39
validating.go
NewUnsupportedErrors
called by 21
errors.go
NewInvalidErrors
called by 17
errors.go
Msg
called by 10
builtin.go
Append
called by 7
errors.go
validateSchema
called by 5
builtin.go
Map
called by 5
errors.go
toSchema
called by 4
builtin.go

Shape

Function 70
Method 20
Struct 18
Interface 2
TypeAlias 2
FuncType 1

Languages

Go100%

Modules by API surface

builtin.go39 symbols
builtin_test.go20 symbols
errors.go17 symbols
validating.go5 symbols
example_nested_struct_slice_test.go5 symbols
example_nested_struct_schema_inside_test.go5 symbols
example_nested_struct_map_test.go5 symbols
example_nested_struct_pointer_test.go4 symbols
example_customizations_test.go4 symbols
example_nested_struct_test.go3 symbols
example_simple_struct_test.go2 symbols
example_simple_value_test.go1 symbols

For agents

$ claude mcp add validating \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact