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 ¶
- func Body[T any](c fiber.Ctx) (T, error)
- func Header[T any](c fiber.Ctx) (T, error)
- func Into(c fiber.Ctx, dst any) error
- func Params[T any](c fiber.Ctx) (T, error)
- func Query[T any](c fiber.Ctx) (T, error)
- func RegisterMessage(tag string, fn func(validator.FieldError) string)
- func RegisterRule(tag string, fn validator.Func, callEvenIfNull ...bool) error
- func Validate(v any) []errs.Detail
- func Validator() *validator.Validate
- type Defaulter
- type Normalizer
- type SelfValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Into ¶
Into binds and validates into an existing pointer, for the rare case where the DTO type is not known statically.
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 ¶
RegisterRule adds a custom `validate` tag, usable as validate:"<tag>".
binding.RegisterRule("cidr4", func(fl validator.FieldLevel) bool { ... })
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.