ProtoValidate is a fluent data validation library for Go.
Project goals:
- Human readable easy to understand and write validation rules.
- Easily extensible / composable to support any data type and rules.
- Detailed, actionable, and customizable errors.
Features:
- Type checking at compile time.
- Declarative rule syntax.
- Works on deeply nested objects and slices/arrays.
- Easy to extend with custom validation rules.
- Easy to support additional data types.
- Structured error responses make it easy to format, display, and correct errors.
- Support for Internationalization (i18n) for error messages.
- Able to take unstructured input (such as Json) and output structured data.
Common use cases:
- API input validation.
- Command line flag validation.
- Unit testing.
- File validating.
Supported data types out of the box:
interface
string
int
/int8
/int16
/int32
/int64
uint
/uint8
/uint16
/uint32
/uint64
float32
/float64
struct
/map
/[]
time.Time
- Email addresses
- Domains
Easily customize to support your own data types.
This package follows conventional Go versioning. Any version up to version 1.0.0 is considered "unstable" and the API may change.
When possible, any backwards incompatible changes in the unstable releases will follow the procedure:
- Mark old methods as deprecated.
- Update deprecated methods to call newer methods.
- Document the changes in the release notes.
go get proto.zip/studio/validate
Simple usage:
package main
import (
"fmt"
"os"
"proto.zip/studio/validate"
"proto.zip/studio/validate/pkg/rules"
)
var ruleSet rules.RuleSet[string] = rules.String().
WithMinLen(3).
WithMaxLen(7)
// Try changing the string to see different results
func main() {
str := "a"
if _, err := ruleSet.Validate(str); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
See the examples folder for more samples.
Here are some best practices to help you get the most out of ProtoValidate:
- Break custom rules out into their own testable functions.
- If you need the whole object for your rule you can add it to the object
RuleSet
instead of the key. - Don't redefine your rules sets every use. Rule sets are immutable so you can use/reuse the same instance in multiple threads.
- Impossible rules will often
panic
at runtime. Defining your rule sets at the top level or in module init will let you catch them at launch instead of later.
- ProtoStudio - Build app backends fast without writing code.
- Curioso Industries - Expert product development and consulting services.
ProtoValidate is built for mission critical code. We want you to get all the support you need.
For community support join the ProtoStudio Discord Community. If you require commercial support please contact our premium support partner Curioso Industries.