Documentation
¶
Overview ¶
Package binding provides a type-safe, reflect-free, and expression-oriented way to bind data from HTTP requests to Go structs.
Index ¶
- func Join(errs ...error) error
- func One[T any](b *Binding, dest *T, source Source, key string, parse Parser[T], ...) error
- func OnePtr[T any](b *Binding, dest **T, source Source, key string, parse Parser[T], ...) error
- func Slice[T any](b *Binding, dest *[]T, source Source, key string, parse Parser[T], ...) error
- func SlicePtr[T any](b *Binding, dest *[]*T, source Source, key string, parse Parser[T], ...) error
- type Binding
- type Error
- type Parser
- type Requirement
- type Source
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Join ¶ added in v0.0.4
Join collects binding errors into a single ValidationErrors instance. It filters out nil errors. If no errors are found, it returns nil.
func One ¶
func One[T any](b *Binding, dest *T, source Source, key string, parse Parser[T], req Requirement) error
One binds a single value of a non-pointer type (e.g., int, string).
func OnePtr ¶
func OnePtr[T any](b *Binding, dest **T, source Source, key string, parse Parser[T], req Requirement) error
OnePtr binds a single value of a pointer type (e.g., *int, *string).
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding holds the context for a binding operation, including the HTTP request and a function to retrieve path parameters.
type Error ¶ added in v0.0.4
type Error struct {
Source Source `json:"source"` // e.g., "query", "header"
Key string `json:"key"` // The parameter name (e.g., "id", "sort")
Value any `json:"value"` // The invalid value that was provided
Err error `json:"-"` // The underlying error (not exposed in JSON)
}
Error represents a single validation error, providing structured details.
func (*Error) MarshalJSON ¶ added in v0.0.4
MarshalJSON customizes the JSON output to include a user-friendly message.
type Parser ¶
Parser is a generic function that parses a string into a value of type T. It returns an error if parsing fails.
type Requirement ¶
type Requirement bool
Requirement specifies whether a value is required or optional.
const ( Required Requirement = true Optional Requirement = false )
type ValidationErrors ¶ added in v0.0.4
type ValidationErrors struct {
Errors []*Error `json:"errors"`
}
ValidationErrors collects multiple binding errors.
func (*ValidationErrors) Error ¶ added in v0.0.4
func (e *ValidationErrors) Error() string
func (*ValidationErrors) StatusCode ¶ added in v0.0.4
func (e *ValidationErrors) StatusCode() int
StatusCode returns 400 Bad Request, allowing it to work with the lift handler.