binders

package
v1.3.24 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package binders parses HTTP request bodies and coerces values into typed request structs.

Responsibility boundary

The binder handles:

  • Parsing JSON and multipart/form-data request bodies
  • Coercing raw values (string, float64, bool) to Go struct field types

Typical handler flow

// Step 1: binder parses body
request := ctx.MustGet("request").(*CreateCustomerRequest)

// Step 2: validator runs stateful rules (only when needed)
v := validator.New(ctx, db)
if err := v.Validate(request); err != nil {
    _ = ctx.Error(err)
    return
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindRaw

func BindRaw[T any](v *T, raw map[string]any, isMultipart bool) error

BindRaw is the testable core of BindRequest. Call this in unit tests to avoid needing an HTTP server or gin context.

raw := map[string]any{"vrsta": float64(1), "prezime": "Doe"}
var req UpdateCustomerRequest
err := binders.BindRaw(&req, raw, false)

func BindRequest

func BindRequest[T any](ctx *gin.Context, v *T) error

BindRequest parses and validates the request body into v. Supports Content-Type: application/json and multipart/form-data.

Returns nil on success. Returns ErrValidation with field-level messages on validation failure. Returns ErrBadRequest if the body is unreadable or malformed JSON.

Types

This section is empty.

Jump to

Keyboard shortcuts

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