Help us shape the future of Go! Take the 2026 Go Developer Survey

validatorh

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validatorh 提供基于 Hertz 的请求参数绑定与验证。

将 HTTP 框架绑定逻辑从 pkg/validator 分离,保持 pkg 层框架无关。 底层验证逻辑(Validate/ValidationError)仍在 pkg/validator 中。

用法:

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)仍在 pkg/validator 中。

Index

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 BindHeader

func BindHeader(c *app.RequestContext, obj any) error

BindHeader 绑定请求头参数并验证。

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

BindPage 从 Hertz RequestContext 绑定分页参数。 支持 query 参数: page, page_size

func BindPageWithDefault

func BindPageWithDefault(c *app.RequestContext, defaultPage, defaultPageSize int) *pagination.PageRequest

BindPageWithDefault 从 Hertz RequestContext 绑定分页参数,使用自定义默认值。

func BindPath

func BindPath(c *app.RequestContext, obj any) error

BindPath 绑定路径参数并验证。

func BindQuery

func BindQuery(c *app.RequestContext, obj any) error

BindQuery 绑定 query 参数并验证。

Types

This section is empty.

Jump to

Keyboard shortcuts

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