Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orsinium-labs/valdo
Go package for validating JSON. It's the first Go package that can generate JSON Schema (OpeanAPI-compatible), produces user-friendly errors, and supports translations.
https://github.com/orsinium-labs/valdo
go golang json jsonschema openapi openapi3 validation validator
Last synced: about 1 month ago
JSON representation
Go package for validating JSON. It's the first Go package that can generate JSON Schema (OpeanAPI-compatible), produces user-friendly errors, and supports translations.
- Host: GitHub
- URL: https://github.com/orsinium-labs/valdo
- Owner: orsinium-labs
- License: mit
- Created: 2024-06-01T12:09:15.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-11-21T17:23:21.000Z (about 1 month ago)
- Last Synced: 2024-11-21T18:25:14.368Z (about 1 month ago)
- Topics: go, golang, json, jsonschema, openapi, openapi3, validation, validator
- Language: Go
- Homepage:
- Size: 88.9 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# valdo
[ [📚 docs](https://pkg.go.dev/github.com/orsinium-labs/valdo/valdo) ] [ [🐙 github](https://github.com/orsinium-labs/valdo) ]
Go package for validating JSON. Can generate [JSON Schema](https://json-schema.org/overview/what-is-jsonschema) (100% compatible with [OpenAPI](https://swagger.io/specification/)), produces user-friendly errors, supports translations.
You could write OpenAPI documentation by hand (which is very painfull) and then use it to validate user input in your HTTP service, but then error messages are very confusing, not user-friendly, and only in English. Or you could write input validaion by hand and then maintain the OpenAPI documentation separately but then the two will eventually drift and your documentaiton will be a lie. Valdo solves all these problems: write validation once using a real programming language, use it everywhere.
Features:
* Mechanism to translate error messages.
* Out-of-the-box translations for some languages.
* Supports the latest JSON Schema specification (2020-12).
* Pure Go.
* No code generation, no reflection, no unsafe code.
* User-freindly error messages.
* Concurrency-safe, no global state.
* Strict by default, without imlicit type casting.
* Type-safe, thanks to generics.## Installation
```bash
go get github.com/orsinium-labs/valdo
```## Usage
```go
validator := valdo.Object(
valdo.Property("name", valdo.String(valdo.MinLen(1))),
valdo.Property("admin", valdo.Bool()),
)// validate JSON
input := []byte(`{"name": "aragorn", "admin": true}`)
err := valdo.Validate(validator, raw)// validate and unmarshal JSON
type User struct {
Name string `json:"name"`
Admin bool `json:"admin"`
}
user, err := valdo.Unmarshal[User](validator, input)// generate JSON Schema
schema := valdo.Schema(validator)
```See [documentation](https://pkg.go.dev/github.com/orsinium-labs/valdo/valdo) for more.