bind

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package bind 提供 HTTP 请求参数绑定与校验能力,集成 validator 校验与中文翻译。 适用于 gin 框架,将 ShouldBindJSON 与 struct validate 合并为一步调用。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTrans

func NewTrans(v *validator.Validate) (ut.Translator, error)

NewTrans 创建中文翻译器,注册默认翻译规则。

func NewValidate

func NewValidate() *validator.Validate

NewValidate 创建 validator 实例。

Types

type Binder

type Binder struct {
	// contains filtered or unexported fields
}

Binder 请求参数绑定器,组合 JSON 绑定与 struct 校验。

func New

func New(validate *validator.Validate, trans ut.Translator, errCode apperror.Code) *Binder

New 创建绑定器,需注入 validator、翻译器及业务错误码。

func (*Binder) ShouldBindJSON

func (b *Binder) ShouldBindJSON(ctx *gin.Context, data any) error

ShouldBindJSON 将请求体 JSON 绑定到 data 并执行 struct 校验,返回首条翻译后的校验错误。

func (*Binder) ShouldBindJSONArray

func (b *Binder) ShouldBindJSONArray(ctx *gin.Context, data any) error

ShouldBindJSONArray 将请求体 JSON 数组绑定到 data 并逐个校验元素。 data 必须是指向切片的指针,如 &[]SomeDto{} 或 &[]*SomeDto{}。

Jump to

Keyboard shortcuts

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