binding

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package binding turns a request into a validated DTO, or into an error that already knows how to render itself.

The generic form means a controller never declares a variable just to pass its address somewhere:

req, err := binding.Body[dto.CreateProduct](c)
if err != nil {
    return response.Fail(c, err)
}

Three conventions the package enforces, each because its absence has cost somebody a production incident:

  • A DTO with no `validate` tag on a required field is accepted silently by most validators. Use `required` and mean it; Strict mode below makes the omission visible.
  • A PATCH field that may legitimately be false or zero is a POINTER. A plain bool cannot distinguish "set it to false" from "do not touch it", and the second is what a rename request means.
  • Validation failures come back as field-scoped details, never as a single prose sentence a form cannot attach to an input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body[T any](c fiber.Ctx) (T, error)

Body binds and validates a JSON request body.

func Header[T any](c fiber.Ctx) (T, error)

Header binds and validates request headers.

func Into

func Into(c fiber.Ctx, dst any) error

Into binds and validates into an existing pointer, for the rare case where the DTO type is not known statically.

func Params

func Params[T any](c fiber.Ctx) (T, error)

Params binds and validates the path parameters.

func Query

func Query[T any](c fiber.Ctx) (T, error)

Query binds and validates the query string.

func RegisterMessage

func RegisterMessage(tag string, fn func(validator.FieldError) string)

RegisterMessage sets the human sentence produced for a tag. Without one, a custom rule renders as "<field> is invalid", which tells a user nothing about how to fix it.

func RegisterRule

func RegisterRule(tag string, fn validator.Func, callEvenIfNull ...bool) error

RegisterRule adds a custom `validate` tag, usable as validate:"<tag>".

binding.RegisterRule("cidr4", func(fl validator.FieldLevel) bool { ... })

func Validate

func Validate(v any) []errs.Detail

Validate runs the rules on v and returns one Detail per failing field. A nil return means valid.

func Validator

func Validator() *validator.Validate

Validator exposes the underlying instance so an application can register struct-level rules or custom types.

Types

type Defaulter

type Defaulter interface{ SetDefaults() }

Defaulter is implemented by a DTO that fills its own optional fields before validation. It runs after binding and before the rules, so a default can satisfy a `required` rule.

type Normalizer

type Normalizer interface{ Normalize() }

Normalizer is implemented by a DTO that cleans its input — trimming whitespace, lowercasing a hostname, stripping a trailing dot. It runs before validation, so the rules see the value that will actually be stored.

type SelfValidator

type SelfValidator interface{ Validate() error }

SelfValidator is implemented by a DTO with a rule that tags cannot express: "end must be after start", "exactly one of these three". It runs AFTER tag validation, so it can assume the individual fields are well-formed.

Return an *errs.Error with details to attach the failure to a field; a plain error becomes a form-level message.

Jump to

Keyboard shortcuts

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