Documentation
¶
Overview ¶
Package bind provides data binding functionality for forms, JSON, and XML.
Index ¶
- func GenerateJSONSchema(t any, components *openapi.Components) *openapi.SchemaOrRef
- func GenerateXMLSchema(t any, xmlRootName string, components *openapi.Components) *openapi.SchemaOrRef
- func IsFloatType(kind reflect.Kind) bool
- func IsIntType(kind reflect.Kind) bool
- func IsUintType(kind reflect.Kind) bool
- type BindSource
- type ValidationError
- func Bind[T any](r *http.Request, validate bool) (T, []ValidationError, error)
- func Cookie[T any](r *http.Request) (T, []ValidationError, error)
- func Form[T any](r *http.Request) (T, []ValidationError, error)
- func Header[T any](r *http.Request) (T, []ValidationError, error)
- func JSON[T any](r *http.Request, validate bool) (T, []ValidationError, error)
- func Path[T any](r *http.Request) (T, []ValidationError, error)
- func Query[T any](r *http.Request) (T, []ValidationError, error)
- func ValidateJSON[T any](data *T) []ValidationError
- func XML[T any](r *http.Request, validate bool) (T, []ValidationError, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateJSONSchema ¶
func GenerateJSONSchema(t any, components *openapi.Components) *openapi.SchemaOrRef
GenerateJSONSchema generates an OpenAPI JSON Schema for the given type. It analyzes struct fields, validation tags, and type information to produce a complete schema with properties, types, formats, and validation constraints. The components parameter is used to register reusable schema definitions. Returns a SchemaOrRef that can be used in OpenAPI documentation.
func GenerateXMLSchema ¶ added in v0.2.4
func GenerateXMLSchema(t any, xmlRootName string, components *openapi.Components) *openapi.SchemaOrRef
func IsFloatType ¶
IsFloatType returns true if the given reflect.Kind represents a floating-point type. Includes float32 and float64.
func IsIntType ¶
IsIntType returns true if the given reflect.Kind represents an integer type. Includes signed and unsigned integers of all sizes (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64).
func IsUintType ¶
IsUintType returns true if the given reflect.Kind represents an unsigned integer type.
Types ¶
type BindSource ¶
type BindSource string
BindSource represents the source from which to bind data.
const ( // BindSourceAuto automatically determines the binding source based on precedence rules. BindSourceAuto BindSource = "auto" // BindSourcePath binds from URL path parameters. BindSourcePath BindSource = "path" // BindSourceQuery binds from URL query parameters. BindSourceQuery BindSource = "query" // BindSourceHeader binds from HTTP headers. BindSourceHeader BindSource = "header" // BindSourceCookie binds from HTTP cookies. BindSourceCookie BindSource = "cookie" // BindSourceBody binds from the request body (JSON, XML, or Form). BindSourceBody BindSource = "body" // BindSourceForm binds from form data (query + body). BindSourceForm BindSource = "form" )
type ValidationError ¶
type ValidationError struct {
XMLName xml.Name `json:"-" xml:"validationError" form:"-"`
Field string `json:"field" xml:"field" form:"field"`
Error string `json:"error" xml:"error" form:"error"`
}
ValidationError represents a field validation error.
func Bind ¶
Bind is a unified method that binds data from multiple sources to a struct of type T. It supports binding from path parameters, query parameters, headers, cookies, and request body. The bindFrom tag on struct fields determines the source of binding. If no tag is present, a precedence rule is applied: path > query > header > cookie > body. The validate parameter controls whether validation is performed after binding. Returns the populated struct, validation errors (if any), and an error if binding fails.
func Cookie ¶
func Cookie[T any](r *http.Request) (T, []ValidationError, error)
Cookie binds HTTP cookies to a struct of type T. Cookie values are extracted from r.Cookies(). Struct fields should use the "form" tag to specify cookie names. Returns the populated struct, validation errors (if any), and an error if binding fails.
func Form ¶
func Form[T any](r *http.Request) (T, []ValidationError, error)
Form parses form data from an HTTP request and binds it to a struct of type T. It extracts values from both URL query parameters and POST form data, performs type conversion, and validates the data according to struct tags. Returns the populated struct, validation errors (if any), and a decoding error (if parsing fails).
func Header ¶
func Header[T any](r *http.Request) (T, []ValidationError, error)
Header binds HTTP headers to a struct of type T. Header values are extracted from r.Header. Struct fields should use the "form" tag to specify header names. Header names are case-insensitive per HTTP specification. Supports slices for multi-value headers. Returns the populated struct, validation errors (if any), and an error if binding fails.
func JSON ¶
JSON parses JSON from an HTTP request body and binds it to a struct of type T. If validate is true, performs validation according to struct tags after decoding. Returns the populated struct, validation errors (if validation is enabled), and a decoding error (if parsing fails).
func Path ¶
func Path[T any](r *http.Request) (T, []ValidationError, error)
Path binds URL path parameters to a struct of type T. Path parameters are extracted from the request using PathValue method. Struct fields should use the "form" tag to specify parameter names. Returns the populated struct, validation errors (if any), and an error if binding fails.
func Query ¶
func Query[T any](r *http.Request) (T, []ValidationError, error)
Query binds URL query parameters to a struct of type T. Query parameters are extracted from r.URL.Query(). Struct fields should use the "form" tag to specify parameter names. Supports slices for multi-value parameters. Returns the populated struct, validation errors (if any), and an error if binding fails.
func ValidateJSON ¶
func ValidateJSON[T any](data *T) []ValidationError
ValidateJSON validates a struct according to its validation tags. It recursively checks all fields and nested structs for compliance with constraints such as required, min, max, pattern, format, etc. Returns a slice of validation errors, empty if validation passes.