Documentation
¶
Overview ¶
Package validatorh 提供基于 Hertz 的请求参数绑定与验证。
HTTP 框架绑定层(自 pkg/validator 分离的先例延续)。 底层验证逻辑在 github.com/oekit/kitx/validator(自 pkg v0.10.0 拆出)。
用法:
var req CreateReq
if err := validatorh.BindAndValidate(c, &req); err != nil {
mw.Fail(ctx, c, err)
return
}
Package validatorh 提供基于 Hertz 的请求参数绑定与验证。
将 HTTP 框架绑定从 pkg/validator 分离,保持 pkg 层框架无关。 底层验证逻辑(Validate/ValidationError)在 github.com/oekit/kitx/validator(自 pkg v0.10.0 拆出)。
Index ¶
- func BindAndValidate(c *app.RequestContext, obj any) error
- func BindHeader(c *app.RequestContext, obj any) error
- func BindJSON(c *app.RequestContext, obj any) error
- func BindPage(c *app.RequestContext) *pagination.PageRequest
- func BindPageWithDefault(c *app.RequestContext, defaultPage, defaultPageSize int) *pagination.PageRequest
- func BindPath(c *app.RequestContext, obj any) error
- func BindQuery(c *app.RequestContext, obj any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindAndValidate ¶
func BindAndValidate(c *app.RequestContext, obj any) error
BindAndValidate 绑定请求参数并验证。 自动根据 Content-Type 选择绑定方式(JSON/query/form)。 使用 c.Bind(仅绑定)+ validator.Validate(统一验证),避免重复验证。
func BindJSON ¶
func BindJSON(c *app.RequestContext, obj any) error
BindJSON 绑定 JSON body 并验证。 使用 c.Bind(仅绑定)+ validator.Validate(统一验证),避免重复验证。 注意:hertz 泛型 c.Bind 只在 Content-Type 为 application/json 时解析 body,否则静默跳过(零值对象 + nil error)。这里对"带 body 但声明了 其他 Content-Type"显式报错,避免漏头请求被当成空参数成功。 body 判断用实际字节而非 Content-Length:chunked 请求(无 Content-Length, hertz 中为 -1)同样携带 body,按长度判断会漏防。
func BindPage ¶
func BindPage(c *app.RequestContext) *pagination.PageRequest
BindPage 从 Hertz RequestContext 绑定分页参数。 支持 query 参数: page, page_size
func BindPageWithDefault ¶
func BindPageWithDefault(c *app.RequestContext, defaultPage, defaultPageSize int) *pagination.PageRequest
BindPageWithDefault 从 Hertz RequestContext 绑定分页参数,使用自定义默认值。
Types ¶
This section is empty.