Documentation
¶
Index ¶
- func RecordRule(fieldPtr unsafe.Pointer, ruleType RuleType, params ...interface{})
- func Register[T any](fn func(*T))
- func RegisterAll[T any](fn func(*T))
- func SetGroupMsg(fieldPtr unsafe.Pointer, msg string)
- func SetLastRuleMsg(fieldPtr unsafe.Pointer, msg string)
- type Rule
- type RuleType
- type Schema
- type ValidationError
- type ValidationErrors
- func (e *ValidationErrors) Add(field, message, code string)
- func (e ValidationErrors) Error() string
- func (e ValidationErrors) Fields() []string
- func (e ValidationErrors) FirstError() *ValidationError
- func (e ValidationErrors) GetByField(field string) []ValidationError
- func (e ValidationErrors) HasErrors() bool
- func (e ValidationErrors) ToFieldMap() map[string][]string
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RecordRule ¶
RecordRule 供 v 包的基础类型调用,记录规则
func SetGroupMsg ¶
SetGroupMsg 设置一组规则的自定义消息 它会从最后一条规则开始向前遍历,直到遇到已经有自定义消息的规则为止 将这期间的所有规则的 CustomMsg 设置为 msg
func SetLastRuleMsg ¶
SetLastRuleMsg 设置最后一条规则的自定义消息
Types ¶
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 存储类型的验证模式
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 表示单个字段的验证错误
type ValidationErrors ¶
type ValidationErrors []ValidationError
ValidationErrors 验证错误集合
func (*ValidationErrors) Add ¶
func (e *ValidationErrors) Add(field, message, code string)
Add 添加验证错误(需要提供错误码)
func (ValidationErrors) FirstError ¶
func (e ValidationErrors) FirstError() *ValidationError
FirstError 返回第一个错误(如果存在)
func (ValidationErrors) GetByField ¶
func (e ValidationErrors) GetByField(field string) []ValidationError
GetByField 获取指定字段的所有错误
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 (ValidationResult) ToError ¶
func (r ValidationResult) ToError() error
ToError 转换为 error(如果有错误)
Click to show internal directories.
Click to hide internal directories.