Skip to main content

Bind

Zinc’s binding story is centered around three pieces:

  • RequestBinder
  • Validator
  • the Bind helper returned by c.Bind()

RequestBinder interface​

type RequestBinder interface {
Bind(*Context, any) error
BindBody(*Context, any) error
BindQuery(*Context, any) error
BindForm(*Context, any) error
BindHeader(*Context, any) error
BindPath(*Context, any) error
}

Provide a custom request binder in Config if you need different decoding behavior.

c.Bind() helper​

c.Bind() is Zinc's primary binding API.

if err := c.Bind().All(&input); err != nil {
return err
}

Use it when you want either:

  • the inferred all-source bind path with All(...)
  • an explicit source path like JSON(...) or Query(...)
MethodPurpose
AllUse the configured request binder across supported request sources
BodyDecode request body only
JSON, XML, YAML, TOML, TextDecode a specific body format
FormBind form or multipart form data
QueryBind query values
HeaderBind request headers
PathBind route params

Common struct tags:

  • path:"id"
  • query:"page"
  • header:"x-request-id"
  • form:"name"
  • json:"name"
  • xml:"name"
  • yaml:"name"
  • toml:"name"

Validation​

If Config.Validator is set, Zinc validates after binding.

This lets you centralize struct validation without changing individual handlers.

BindError​

BindError includes:

  • Source
  • Field
  • Err

This is especially useful for APIs that want structured bad-request responses.