gopantic

module
v1.5.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: MIT

README

gopantic

Go Reference CI License

Parse JSON or YAML into a Go struct, convert what can be converted exactly, and run the rules in its validate tags, in one call. You get back the value the input meant, or an error.

type User struct {
    ID    int    `json:"id" validate:"required,min=1"`
    Name  string `json:"name" validate:"required,min=2"`
    Email string `json:"email" validate:"required,email"`
    Age   int    `json:"age" validate:"min=18,max=120"`
}

raw := []byte(`{"id": "42", "name": "Alice", "email": "alice@example.com", "age": "28"}`)

user, err := model.ParseInto[User](raw)
// user.ID == 42, user.Age == 28

The same struct takes YAML with no other change. Input that doesn't fit is an error:

_, err := model.ParseInto[User]([]byte(`{"id": 1.5, "name": "A", "email": "nope", "age": 28}`))
// multiple errors: parse error on field "ID": value has a fractional part;
// validation error on field "Name": string length must be at least 2 characters;
// validation error on field "Email": invalid email address format

Who it is for

gopantic suits code that reads input it doesn't control: API payloads where numbers arrive as strings, config files that may be JSON or YAML, data imports. You write one struct and get decoding, conversion and validation together, with every problem reported at once.

If your input is already well typed and JSON only, use encoding/json with go-playground/validator. It has far more rules, and you wouldn't be using what gopantic adds. The two are within 30% of each other on correctly typed JSON, so speed won't decide it. Input that needs converting takes about 3 times as long. Figures are on the benchmarks page.

Install

go get github.com/1mb-dev/gopantic

Requires Go 1.24 or later. The one dependency is gopkg.in/yaml.v3.

Documentation

The full documentation has the validation rules, what converts and what errors, caching, and the API. Upgrading from v1.4 can reject input that used to pass, so read the v1.5.0 changelog first. Known limitations lists what gopantic doesn't do. Runnable programs are in examples/.

Contributing

See CONTRIBUTING. Report security issues as described in the security policy.

License

MIT

Directories

Path Synopsis
examples
api_validation command
cache_demo command
pointers command
postgresql_jsonb command
Package main demonstrates PostgreSQL JSONB integration with gopantic.
Package main demonstrates PostgreSQL JSONB integration with gopantic.
quickstart command
yaml command
pkg
model
Package model parses JSON or YAML into a struct, converts values that fit exactly, and runs the rules in its validate tags.
Package model parses JSON or YAML into a struct, converts values that fit exactly, and runs the rules in its validate tags.
Package tests provides comprehensive test coverage for gopantic's parsing, validation, and caching functionality.
Package tests provides comprehensive test coverage for gopantic's parsing, validation, and caching functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL