go-joi





A schema description & validation library for Go, inspired by hapi/joi.
Contents
Getting started
- Install the package:
go get github.com/leandroluk/go-joi
- Import it in your project:
import "github.com/leandroluk/go-joi"
- Create and validate schemas:
schema := joi.Object(map[string]joi.Schema{
"username": joi.String().Min(3).Max(20).Required(),
"age": joi.Number().Min(18).Required(),
"email": joi.String().Regex(regexp.MustCompile(`.+@.+\..+`)),
})
value := map[string]any{
"username": "john_doe",
"age": 25,
"email": "john@example.com",
}
parsed, errs := schema.Validate("user", value)
if len(errs) > 0 {
fmt.Println("Validation failed:", errs)
} else {
fmt.Println("Validation passed:", parsed)
}
Usage
- Basic types:
String, Number, Boolean, Object
- Rules:
- String:
.Min(), .Max(), .Regex(), .Trim(), .Lowercase(), .Uppercase()
- Number:
.Min(), .Max(), .Integer(), .Positive(), .Negative()
- Boolean:
.True(), .False(), .Truthy(), .Falsy()
- Object:
.Min(), .Max(), .Length(), .Unknown()
Available Schemas
- ✅ String
- ✅ Number
- ✅ Boolean
- ✅ Object
- ⬜ Array (coming soon)
Examples
String Validation
joi.String().Min(5).Max(10).Trim()
Number Validation
joi.Number().Integer().Positive()
Boolean Validation
joi.Boolean().Truthy("yes", "1").Falsy("no", "0")
Object Validation
joi.Object(map[string]joi.Schema{
"id": joi.Number().Required(),
"name": joi.String().Min(3),
}).Unknown(false)
Implementation Status
- String rules
- Number rules
- Boolean rules
- Object rules
- Array rules
- Custom extensions
About the Project
This project was inspired by joi for Node.js and aims to bring a similar developer experience to Go.
Contributors
Thanks to all the people who contribute! [Contribute]
License
MIT License – see LICENSE file for details.