Documentation
¶
Overview ¶
Package binding provides enhanced binding utilities that extend Gin's binding capabilities. It solves the common problem of binding data from multiple sources (URI, JSON, etc.) without validation errors caused by partially filled structs.
Index ¶
- func Bind(c *gin.Context, obj any, bindFuncs ...BindFunc) error
- func Form(c *gin.Context, obj any) error
- func FormMultipart(c *gin.Context, obj any) error
- func JSON(c *gin.Context, obj any) error
- func Query(c *gin.Context, obj any) error
- func URI(c *gin.Context, obj any) error
- func ValidateStruct(obj any) error
- type BindFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
Bind processes data from multiple sources without validating between each step, then performs a single validation at the end.
This solves the problem where binding from one source (e.g., URI) fails validation because fields from another source (e.g., JSON) haven't been bound yet.
Example usage:
var req UserUpdateRequest
if err := binding.Bind(c, &req, binding.URI, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Message: err.Error()})
return
}
func Form ¶
Form binds form data to the given object. Uses Gin's ShouldBindWith with Form binding but without validation.
func FormMultipart ¶
FormMultipart binds form multipart data to the given object. Uses Gin's ShouldBindWith with FormMultipart binding but without validation.
func JSON ¶
JSON binds JSON body to the given object. Uses Gin's ShouldBindJSON but without validation.
func Query ¶
Query binds query parameters to the given object. Uses Gin's ShouldBindQuery but without validation.
func URI ¶
URI binds URI parameters to the given object. Uses Gin's ShouldBindUri but without validation.
func ValidateStruct ¶
ValidateStruct provides a way to manually validate a struct using the original validator. This is useful if you need to validate a struct outside the Bind function.