Documentation ¶
Index ¶
- func RegisterCustomRuleFunc(option RegisterCustomRuleOption) error
- func RegisterCustomRuleFuncWithParse(ruleName string, ...) error
- func Struct(a any) error
- func StructCtx(ctx context.Context, a any) error
- func StructNotCache(a any) error
- type BasicFieldError
- type DetailError
- type ErrorHandler
- type Errors
- type FieldKind
- type FieldRules
- type MapFieldError
- type ParseRuleOption
- type RegisterCustomRuleOption
- type SliceFieldError
- type StructCache
- type StructRule
- type TranslationOption
- type ValidRuleOption
- type ValidationError
- func (v *ValidationError) AddFieldError(name string, fieldErr error)
- func (v *ValidationError) DetailError() string
- func (v *ValidationError) Error() string
- func (v *ValidationError) Errors() Errors
- func (v ValidationError) GetFieldError(name string) BasicFieldError
- func (v ValidationError) GetMapFieldError(name string) MapFieldError
- func (v ValidationError) GetSliceFieldError(name string) SliceFieldError
- func (v ValidationError) GetStructFieldError(name string) *ValidationError
- type Validator
- func (v *Validator) I18nSetLanguage(language string) *Validator
- func (v *Validator) I18nSetPath(path string) *Validator
- func (v *Validator) ParseStruct(a any, cache *StructCache) *StructRule
- func (v *Validator) SetFieldNameFunc(f func(structType reflect.Type, field reflect.StructField) string)
- func (v *Validator) SetFilterFieldFunc(f func(structType reflect.Type, field reflect.StructField) bool)
- func (v *Validator) SetI18n(path string, language string)
- func (v *Validator) StopOnFirstError(b bool)
- func (v *Validator) Struct(a any) error
- func (v *Validator) StructCtx(ctx context.Context, a any) error
- func (v *Validator) StructNotCache(a any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterCustomRuleFunc ¶
func RegisterCustomRuleFunc(option RegisterCustomRuleOption) error
func RegisterCustomRuleFuncWithParse ¶
func RegisterCustomRuleFuncWithParse(ruleName string, fn func(structRule StructRule, fieldRule FieldRules, args []string) ruleimpl.ValidFunc) error
让用户自定义规则的解析函数
func StructNotCache ¶
Types ¶
type BasicFieldError ¶
type BasicFieldError struct {
// contains filtered or unexported fields
}
func (BasicFieldError) DetailError ¶
func (e BasicFieldError) DetailError() string
func (BasicFieldError) Error ¶
func (e BasicFieldError) Error() string
func (BasicFieldError) Errors ¶
func (e BasicFieldError) Errors() []error
func (BasicFieldError) GetRuleError ¶
func (e BasicFieldError) GetRuleError(rule string) error
type DetailError ¶
type DetailError interface {
DetailError() string
}
type ErrorHandler ¶ added in v1.0.3
type FieldRules ¶
type FieldRules struct { // v:"[LongName@]RuleArray[#msg]" Name string // 按 | 解析的rule和msg // rule=>[value1,value2] example: length:6,16 => length=>{6,16} RuleArray map[string][]string // rule=> msg MsgArray map[string]string // 字段类型 Type reflect.Type FieldName string // 字段在结构体中的索引,反射的时候直接根据索引获取 FieldIndex int // fields 如果kind 是struct才有效,其他的无效 StructRule *StructRule // 指针类型 requiredPtr // slice map array requiredLen // func(ctx context.Context,in fieldValidFuncInput) Funcs map[string]ruleimpl.ValidFunc // contains filtered or unexported fields }
func (*FieldRules) CheckStructField ¶
func (f *FieldRules) CheckStructField(ctx context.Context, fieldVal reflect.Value, structPtr reflect.Value, verr *ValidationError, validOption ValidRuleOption) bool
fieldVal 字段的值 structPtr 结构体指针 verr 错误 validOption 校验选项 返回一个bool值,表示是否继续下个字段的校验 field: basic | struct | slice | map
func (*FieldRules) FieldType ¶
func (f *FieldRules) FieldType() reflect.Type
type MapFieldError ¶
type MapFieldError struct {
// contains filtered or unexported fields
}
func (MapFieldError) DetailError ¶
func (e MapFieldError) DetailError() string
func (MapFieldError) Error ¶
func (e MapFieldError) Error() string
func (MapFieldError) Errors ¶
func (e MapFieldError) Errors() []error
func (MapFieldError) GetError ¶
func (e MapFieldError) GetError(key string) ValidationError
type ParseRuleOption ¶
type ParseRuleOption struct { // 过滤字段 FilterFieldFunc func(structType reflect.Type, field reflect.StructField) bool FieldNameFunc func(structType reflect.Type, field reflect.StructField) string }
type RegisterCustomRuleOption ¶
type RegisterCustomRuleOption struct { // 规则名称 RuleName string // 自定义规则的验证函数 Fn ruleimpl.CustomValidRuleFunc }
校验时rule
type SliceFieldError ¶
type SliceFieldError struct {
// contains filtered or unexported fields
}
func (SliceFieldError) DetailError ¶
func (e SliceFieldError) DetailError() string
func (SliceFieldError) Error ¶
func (e SliceFieldError) Error() string
func (SliceFieldError) Errors ¶
func (e SliceFieldError) Errors() []error
func (SliceFieldError) GetError ¶
func (e SliceFieldError) GetError(index int) ValidationError
type StructCache ¶
type StructCache struct {
// contains filtered or unexported fields
}
func (*StructCache) AddStructRule ¶
func (s *StructCache) AddStructRule(v *StructRule)
func (*StructCache) GetStructRule ¶
func (s *StructCache) GetStructRule(typ reflect.Type) *StructRule
func (*StructCache) GetStructRuleOrCreate ¶
func (s *StructCache) GetStructRuleOrCreate(typ reflect.Type, v *Validator) *StructRule
type StructRule ¶
type StructRule struct { RuleFields []*FieldRules // LongName = pkgpath + structName LongName string // ShortName = structName ShortName string Type reflect.Type }
func ParseStruct ¶
func ParseStruct(a any) *StructRule
func (*StructRule) CheckStruct ¶
func (s *StructRule) CheckStruct(ctx context.Context, structPtr reflect.Value, verr *ValidationError, validOption ValidRuleOption, )
struct: field*
func (*StructRule) Valid ¶
func (s *StructRule) Valid(ctx context.Context, val reflect.Value, validOption ValidRuleOption) error
type TranslationOption ¶
type ValidRuleOption ¶
type ValidRuleOption struct { // 遇到第一个错误时是否停止 StopOnFirstError bool }
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
func NewValidationError ¶
func NewValidationError(structName string) *ValidationError
func (*ValidationError) AddFieldError ¶
func (v *ValidationError) AddFieldError(name string, fieldErr error)
func (*ValidationError) DetailError ¶
func (v *ValidationError) DetailError() string
func (*ValidationError) Error ¶
func (v *ValidationError) Error() string
func (*ValidationError) Errors ¶
func (v *ValidationError) Errors() Errors
func (ValidationError) GetFieldError ¶
func (v ValidationError) GetFieldError(name string) BasicFieldError
func (ValidationError) GetMapFieldError ¶
func (v ValidationError) GetMapFieldError(name string) MapFieldError
func (ValidationError) GetSliceFieldError ¶
func (v ValidationError) GetSliceFieldError(name string) SliceFieldError
func (ValidationError) GetStructFieldError ¶
func (v ValidationError) GetStructFieldError(name string) *ValidationError
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func (*Validator) I18nSetLanguage ¶
func (*Validator) I18nSetPath ¶
func (*Validator) ParseStruct ¶
func (v *Validator) ParseStruct(a any, cache *StructCache) *StructRule
func (*Validator) SetFieldNameFunc ¶
func (*Validator) SetFilterFieldFunc ¶
func (v *Validator) SetFilterFieldFunc(f func(structType reflect.Type, field reflect.StructField) bool)
过滤那些字段,主要是带有v规则的字段
func (*Validator) StopOnFirstError ¶
func (*Validator) StructNotCache ¶
Source Files ¶
- err_util.go
- field_convert.go
- field_parse_rule_msg.go
- field_rule.go
- field_util.go
- parser_struct_field_rule.go
- parser_tag.go
- rule_errorhandler.go
- rule_get.go
- rule_get_between.go
- rule_get_cmp.go
- rule_get_custom.go
- rule_get_format.go
- rule_get_max_min.go
- rule_get_regex.go
- rule_get_required.go
- rule_get_string.go
- rule_get_time.go
- rule_replace_msg.go
- struct_cache.go
- struct_rule.go
- util.go
- valid.go
- valid_check.go
- valid_check2.go
- valid_i18n.go
- valid_options.go
- valid_register_custom_valid_rule.go
- valid_struct.go
- validation_error.go
Click to show internal directories.
Click to hide internal directories.