Documentation
¶
Overview ¶
Package validator 提供统一参数校验(对标 Spring Validation): 结构体 tag 校验 + 中文错误信息 + 业务错误码(A0400 参数错误)集成。 底层基于 go-playground/validator(Go 生态主流,iris 同款)。
用法:
type CreateOrderReq struct {
OrderNo string `validate:"required,min=6,max=32" label:"订单号"`
Qty int `validate:"required,gt=0" label:"数量"`
Email string `validate:"omitempty,email" label:"邮箱"`
}
if err := validator.Struct(&req); err != nil {
// err 为 *apperr.Error(CodeA0400),错误信息含字段名与规则
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterCustom ¶
func RegisterCustom(tag, message string, fn playground.Func)
RegisterCustom 注册自定义校验规则(启动时调用一次)。 规则函数签名:func(fl playground.FieldLevel) bool。
func Struct ¶
func Struct(s interface{}) error
Struct 校验结构体:通过返回 nil; 失败返回 *apperr.Error(CodeA0400 参数错误),Message 为第一条错误(字段名+规则)。 校验多字段时只取首错(接口风格);业务需要全部错误用 StructAll。
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.