validation

package
v1.0.0-alpha.25 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RecordRule

func RecordRule(fieldPtr unsafe.Pointer, ruleType RuleType, params ...interface{})

RecordRule 供 v 包的基础类型调用,记录规则

func Register

func Register[T any](fn func(*T))

Register 注册类型的验证规则(默认快速失败) T 必须是结构体

func RegisterAll

func RegisterAll[T any](fn func(*T))

RegisterAll 注册类型的验证规则(全量验证) T 必须是结构体

func SetGroupMsg

func SetGroupMsg(fieldPtr unsafe.Pointer, msg string)

SetGroupMsg 设置一组规则的自定义消息 它会从最后一条规则开始向前遍历,直到遇到已经有自定义消息的规则为止 将这期间的所有规则的 CustomMsg 设置为 msg

func SetLastRuleMsg

func SetLastRuleMsg(fieldPtr unsafe.Pointer, msg string)

SetLastRuleMsg 设置最后一条规则的自定义消息

Types

type Rule

type Rule struct {
	Type      RuleType
	Params    []interface{}
	CustomMsg string // 自定义错误消息
}

Rule 表示单个验证规则

type RuleType

type RuleType string

RuleType 定义规则类型

type Schema

type Schema struct {
	// Offset -> Rules
	FieldRules map[uintptr][]Rule
	// Offset -> Field Name (JSON tag or Struct Field Name)
	FieldNames map[uintptr]string
	// Offset -> Field Kind (用于 Required 等通用规则判断类型)
	FieldKinds map[uintptr]reflect.Kind
	// Offset -> Field Type (用于获取字段详细类型信息,如 Time, Slice 元素类型)
	FieldTypes map[uintptr]reflect.Type
	// OrderedOffsets 预先排序的偏移量列表,用于保证验证顺序一致性
	OrderedOffsets []uintptr
	// BaseType
	Type reflect.Type
	// FailFast 是否快速失败(遇到第一个错误就返回)
	FailFast bool
}

Schema 存储类型的验证模式

func GetSchema

func GetSchema(t reflect.Type) *Schema

GetSchema 获取类型的 Schema

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`   // 字段路径,优先使用 JSON tag,如 "email" 或嵌套 "address.city"
	Message string `json:"message"` // 错误消息
	Code    string `json:"code"`    // 错误码,如 "VALIDATION.REQUIRED"
}

ValidationError 表示单个字段的验证错误

func (ValidationError) Error

func (e ValidationError) Error() string

Error 实现 error 接口

type ValidationErrors

type ValidationErrors []ValidationError

ValidationErrors 验证错误集合

func Validate

func Validate[T any](obj *T) ValidationErrors

Validate 验证对象

func (*ValidationErrors) Add

func (e *ValidationErrors) Add(field, message, code string)

Add 添加验证错误(需要提供错误码)

func (ValidationErrors) Error

func (e ValidationErrors) Error() string

Error 实现 error 接口

func (ValidationErrors) Fields

func (e ValidationErrors) Fields() []string

Fields 返回所有错误字段名列表(去重)

func (ValidationErrors) FirstError

func (e ValidationErrors) FirstError() *ValidationError

FirstError 返回第一个错误(如果存在)

func (ValidationErrors) GetByField

func (e ValidationErrors) GetByField(field string) []ValidationError

GetByField 获取指定字段的所有错误

func (ValidationErrors) HasErrors

func (e ValidationErrors) HasErrors() bool

HasErrors 检查是否有错误

func (ValidationErrors) ToFieldMap

func (e ValidationErrors) ToFieldMap() map[string][]string

ToFieldMap 转换为 map[字段名][]错误消息

type ValidationResult

type ValidationResult struct {
	IsValid bool
	Errors  ValidationErrors
}

ValidationResult 验证结果

func NewValidationResult

func NewValidationResult(errors ValidationErrors) ValidationResult

NewValidationResult 创建验证结果

func SuccessResult

func SuccessResult() ValidationResult

SuccessResult 创建成功的验证结果

func (ValidationResult) ToError

func (r ValidationResult) ToError() error

ToError 转换为 error(如果有错误)

Directories

Path Synopsis
v

Jump to

Keyboard shortcuts

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