binding

package
v0.0.0-...-aca55d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(c *gin.Context, obj any, bindFuncs ...BindFunc) error

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

func Form(c *gin.Context, obj any) error

Form binds form data to the given object. Uses Gin's ShouldBindWith with Form binding but without validation.

func FormMultipart

func FormMultipart(c *gin.Context, obj any) error

FormMultipart binds form multipart data to the given object. Uses Gin's ShouldBindWith with FormMultipart binding but without validation.

func JSON

func JSON(c *gin.Context, obj any) error

JSON binds JSON body to the given object. Uses Gin's ShouldBindJSON but without validation.

func Query

func Query(c *gin.Context, obj any) error

Query binds query parameters to the given object. Uses Gin's ShouldBindQuery but without validation.

func URI

func URI(c *gin.Context, obj any) error

URI binds URI parameters to the given object. Uses Gin's ShouldBindUri but without validation.

func ValidateStruct

func ValidateStruct(obj any) error

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.

Types

type BindFunc

type BindFunc func(*gin.Context, any) error

func Func

func Func(b ginbinding.Binding) BindFunc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL