Documentation
¶
Overview ¶
Package validation 提供数据验证功能和自定义验证规则
Package validation 提供数据验证功能和自定义验证规则 ¶
Package validation 提供数据验证功能和自定义验证规则
Index ¶
- func GetTranslator() ut.Translator
- func GetValidator() *validator.Validate
- func InitCustomRules()
- func Initialize()
- func InitializeDomainValidation()
- func IsValidIPv4(ip string) bool
- func RegisterRule(tag string, rule Rule)
- func TranslateError(err error) []string
- func Validate(s interface{}) error
- func ValidateChinaMobile(phone string) bool
- func ValidatePasswordStrength(password string) (bool, string)
- func ValidateVar(field interface{}, tag string) error
- type DomainValidator
- type DomainValidatorImpl
- type DomainValidatorRegistry
- type FieldError
- type Rule
- type StructValidator
- type ValidationError
- type ValidationErrorWithCustomMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitializeDomainValidation ¶
func InitializeDomainValidation()
InitializeDomainValidation 初始化领域验证器
func ValidateChinaMobile ¶
ValidateChinaMobile 验证中国大陆手机号(可直接调用)
func ValidatePasswordStrength ¶
ValidatePasswordStrength 验证密码强度(可直接调用)
Types ¶
type DomainValidator ¶
type DomainValidator interface {
Validate(domain interface{}) error
}
DomainValidator 领域模型验证器接口
type DomainValidatorImpl ¶
type DomainValidatorImpl struct{}
DomainValidatorImpl 领域验证器实现
func (*DomainValidatorImpl) Validate ¶
func (v *DomainValidatorImpl) Validate(domain interface{}) error
Validate 验证领域模型
type DomainValidatorRegistry ¶
type DomainValidatorRegistry struct {
// contains filtered or unexported fields
}
DomainValidatorRegistry 领域验证器注册表
func NewDomainValidatorRegistry ¶
func NewDomainValidatorRegistry() *DomainValidatorRegistry
NewDomainValidatorRegistry 创建领域验证器注册表
func (*DomainValidatorRegistry) Get ¶
func (r *DomainValidatorRegistry) Get(name string) (DomainValidator, bool)
Get 获取领域验证器
func (*DomainValidatorRegistry) Register ¶
func (r *DomainValidatorRegistry) Register(name string, factory func() DomainValidator)
Register 注册领域验证器
type FieldError ¶
FieldError 字段错误
type Rule ¶
type Rule struct {
Validation validator.Func // 验证函数
ErrorMessage string // 默认错误消息
TranslateFunc func(ut ut.Translator) error // 翻译器注册函数
}
Rule 定义自定义验证规则
type StructValidator ¶
type StructValidator struct {
// 需要验证的领域模型
Model interface{}
// 验证标签名称,默认为"validate"
TagName string
// 自定义错误消息映射 map[字段名]错误消息
ErrorMessages map[string]string
}
StructValidator 结构体验证器
func NewStructValidator ¶
func NewStructValidator(model interface{}) *StructValidator
NewStructValidator 创建结构体验证器
func (*StructValidator) WithErrorMessage ¶
func (v *StructValidator) WithErrorMessage(field, message string) *StructValidator
WithErrorMessage 添加自定义错误消息
func (*StructValidator) WithErrorMessages ¶
func (v *StructValidator) WithErrorMessages(messages map[string]string) *StructValidator
WithErrorMessages 批量添加自定义错误消息
func (*StructValidator) WithTagName ¶
func (v *StructValidator) WithTagName(tagName string) *StructValidator
WithTagName 设置验证标签名称
type ValidationErrorWithCustomMessage ¶
type ValidationErrorWithCustomMessage struct {
validator.FieldError
// contains filtered or unexported fields
}
ValidationErrorWithCustomMessage 带有自定义消息的验证错误
func NewValidationErrorWithCustomMessage ¶
func NewValidationErrorWithCustomMessage(err validator.FieldError, message string) ValidationErrorWithCustomMessage
NewValidationErrorWithCustomMessage 创建带有自定义消息的验证错误
func (ValidationErrorWithCustomMessage) Error ¶
func (e ValidationErrorWithCustomMessage) Error() string
Error 返回错误消息
Click to show internal directories.
Click to hide internal directories.