Bind
Zinc’s binding story is centered around three pieces:
RequestBinderValidator- the
Bindhelper returned byc.Bind()
RequestBinder interface
Section titled “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
Section titled “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(...)orQuery(...)
| Method | Purpose |
|---|---|
All |
Use the configured request binder across supported request sources |
Body |
Decode request body only |
JSON, XML, YAML, TOML, Text |
Decode a specific body format |
Form |
Bind form or multipart form data |
Query |
Bind query values |
Header |
Bind request headers |
Path |
Bind route params |
Common struct tags:
path:"id"query:"page"header:"x-request-id"form:"name"json:"name"xml:"name"yaml:"name"toml:"name"
Validation
Section titled “Validation”If Config.Validator is set, Zinc validates after binding.
This lets you centralize struct validation without changing individual handlers.
BindError
Section titled “BindError”BindError includes:
SourceFieldErr
This is especially useful for APIs that want structured bad-request responses.
