Documentation
¶
Overview ¶
Example (Api_payload_validation) ¶
Example of validating a real-world API payload with nested structs and slices
payload := User{
Name: "",
Email: "",
Address: Address{Street: "", City: "", Zip: "12345"},
Orders: []Order{{ID: "", Amount: 0}, {ID: "A123", Amount: 100}},
Password: "abc",
}
err := payload.Validate()
if err != nil {
b, _ := json.MarshalIndent(err, "", " ")
fmt.Println(string(b))
} else {
fmt.Println("<nil>")
}
Output: { "address": { "city": "required", "street": "required" }, "email": "required", "name": "required", "orders[0]": { "amount": "required", "id": "required", "product": { "code": "required", "price": "price must be at least 1" } }, "orders[1]": { "product": { "code": "required", "price": "price must be at least 1" } }, "password": "must be at least 8 characters, must contain a capital letter, must contain a number" }
Example (Nested_validation) ¶
Example of nested struct validation and error aggregation
p := Parent{
Name: "",
Age: 0,
Children: []Child{{Name: ""}, {Name: "Alice"}},
}
err := p.Validate()
fmt.Println(err)
Output: invalid fields: age, children[0], name
Example (Required_optional) ¶
Example of using Required and Optional
fmt.Println(Required(nil))
fmt.Println(Required("foo"))
fmt.Println(Optional(nil, errors.New("fail")))
fmt.Println(Optional("foo", errors.New("fail")))
Output: required <nil> <nil> fail
Index ¶
- Variables
- func Assert(valid bool, msg string, args ...any) error
- func AssertMap(conds map[string]bool) error
- func Map(m map[string]error) error
- func Optional(value any, errs ...error) error
- func Required(value any, errs ...error) error
- func Validate[T validatable](v T) error
- func ValidateMany[T validatable](value []T) error
- func ValidateManyFunc[T any](items []T, fn func(T) error) error
- func When(valid bool, msg string, args ...any) error
- func WhenMap(conds map[string]bool) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRequired = errors.New("required")
ErrRequired is returned when a required value is missing or zero.
Functions ¶
func Assert ¶
Assert returns an error with the given message if valid is false, otherwise returns nil.
func Map ¶
Map converts a map of field errors into a structured errorMap, handling nested errors.
Example ¶
Example of Map for field errors
m := map[string]error{"field": errors.New("fail")}
err := Map(m)
fmt.Println(err)
Output: invalid fields: field
func Required ¶
Required returns ErrRequired if the value is zero, otherwise joins additional errors.
func Validate ¶
func Validate[T validatable](v T) error
Validate calls Validate on a validatable type if it is not zero.
func ValidateMany ¶
func ValidateMany[T validatable](value []T) error
ValidateMany validates each item in a slice of validatable types and collects errors. Returns nil if no errors are found, otherwise returns an errorSlice.
Example ¶
Example of ValidateMany with validatable
items := []validatableItem{{myType{true}}, {myType{false}}, {myType{true}}}
err := ValidateMany(items)
fmt.Println(err)
Output: invalid slice
func ValidateManyFunc ¶
ValidateManyFunc applies a validation function to each item in a slice and collects errors. Returns nil if no errors are found, otherwise returns an errorSlice.
Example ¶
Example of ValidateManyFunc
items := []int{1, 2, 3}
err := ValidateManyFunc(items, func(i int) error {
if i%2 == 0 {
return fmt.Errorf("even: %d", i)
}
return nil
})
fmt.Println(err)
Output: invalid slice
Types ¶
This section is empty.