validator

package
v1.57.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 6 Imported by: 0

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。

func StructAll

func StructAll(s interface{}) []error

StructAll 校验结构体并返回全部错误(空列表 = 通过)。 错误信息格式:"订单号 长度/数值过小"(label 优先,其次字段名)。

func Var

func Var(value interface{}, tag string) error

Var 校验单个变量(如 url 参数):tag 如 "required,email"。

Types

This section is empty.

Jump to

Keyboard shortcuts

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