validator

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	STRUCT_EMPTY          = "struct %v is empty"
	VALIDATOR_VALUE_SIGN  = "="
	VALIDATOR_RANGE_SPLIT = ","
	VALIDATOR_IGNORE_SIGN = "_"

	//邮箱验证正则
	MAIL_REG = `\A[\w+\-.]+@[code-z\d\-]+(\.[code-z]+)*\.[code-z]+\z`
	//密码正则
	PASSWORD_REG = `^.{6,30}$`
	//^[\w_-]{6,30}$
	//url验证正则
	URL_REG = `^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$`
	//是否为整数正则
	INTEGER_REG = `^(-)?[0-9]+$`
	//是否为float正则
	FLOAT_REG = `^(-)?[0-9]+(.[0-9]+)$`
	//年正则
	YEAR_REG = `(19|2[0-4])\d{2}`
	//月正则
	MONTH_REF = `(10|11|12|0[1-9])`
	//日正则
	DAY_REF = `(30|31|0[1-9]|[1-2][0-9])`
	//小时正则
	HOUR_REF = `(20|21|22|23|[0-1]\d)`
	//分钟正则
	MINUTE_REF = `([0-5]\d)`
	//秒正则
	SECOND_REF = `([0-5]\d)`
)

Variables

View Source
var ReqValidator *validator.Validate

Functions

func InArray

func InArray(val interface{}, listArr interface{}) (re bool)

func Init

func Init()

替换gin的binding

func InitValidator

func InitValidator() *validator.Validate

func New

func New() *myValidator

Types

type ArrayValidator

type ArrayValidator struct {
	EMsg string
	Range
}

*

  • 当只有 Min 或者 Max 的值,另一个值为 nil 时,验证器为等于有值的对应值
  • 当只有 Min 或者 Max 的值,另一个值为 _ 时,验证器为忽略带 _ 的值
  • 栗子
  • string=1,2 表示 Min=1,Max=2,就是说 1 <= len(array) <= 2
  • string=1 表示 Min=1,Max=nil,就是说 len(array) = 1
  • string=1,_ 表示 Min=1,Max=_,就是说 1 <= len(array)

func (*ArrayValidator) Validate

func (self *ArrayValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type DateTimeValidator

type DateTimeValidator struct {
	EMsg   string
	FmtStr string
}

func (*DateTimeValidator) Validate

func (self *DateTimeValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type DefaultValidator

type DefaultValidator struct {
	// contains filtered or unexported fields
}

func (*DefaultValidator) Engine

func (v *DefaultValidator) Engine() interface{}

func (*DefaultValidator) ValidateStruct

func (v *DefaultValidator) ValidateStruct(obj interface{}) error

type EmailValidator

type EmailValidator struct {
	EMsg string
	Reg  string
}

func (*EmailValidator) Validate

func (self *EmailValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type InValidator

type InValidator struct {
	EMsg     string
	TypeEMsg string
}

*

  • 仅支持 string、float、int、bool 类型
  • 或值类型为 string、float、int、bool 类型的array、slice、map

func (*InValidator) Validate

func (self *InValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type IntegerValidator

type IntegerValidator struct {
	EMsg string
	Range
}

*

  • 当只有 Min 或者 Max 的值,另一个值为 nil 时,验证器为等于有值的对应值
  • 当只有 Min 或者 Max 的值,另一个值为 _ 时,验证器为忽略带 _ 的值
  • 栗子
  • int=1,2 表示 Min=1,Max=2,就是说 1 <= num <= 2
  • int=1 表示 Min=1,Max=nil,就是说 num = 1
  • int=1,_ 表示 Min=1,Max=_,就是说 1 <= num

func (*IntegerValidator) Validate

func (self *IntegerValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type Kind

type Kind uint
const (
	ALL_KIND Kind = iota
	ARRAY_KIND
	SLICE_KIND
	MAP_KIND
	STRING_KIND
	INTEGER_KIND
	FLOAT_KIND
	BOOL_KIND
)

type PasswordValidator

type PasswordValidator struct {
	EMsg string
	Reg  string
}

func (*PasswordValidator) Validate

func (self *PasswordValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type Range

type Range struct {
	Min string
	Max string

	RangeEMsg map[string]string //keys: lessThan,equal,atLeast,between
	// contains filtered or unexported fields
}

func (*Range) CompareFloat

func (self *Range) CompareFloat(valNum float64, eParamsMap map[string]string, errorMap map[string]string) error

func (*Range) CompareInteger

func (self *Range) CompareInteger(valNum int64, eParamsMap map[string]string, errorMap map[string]string) error

func (*Range) InitRangeNum

func (self *Range) InitRangeNum(eParamsMap map[string]string, args ...string) error

将structTag中的min和max解析到结构体中

type RequiredValidator

type RequiredValidator struct {
	EMsg string
}

func (*RequiredValidator) Validate

func (self *RequiredValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type StringValidator

type StringValidator struct {
	EMsg string
	Range
}

*

  • 当只有 Min 或者 Max 的值,另一个值为 nil 时,验证器为等于有值的对应值
  • 当只有 Min 或者 Max 的值,另一个值为 _ 时,验证器为忽略带 _ 的值
  • 栗子
  • string=1,2 表示 Min=1,Max=2,就是说 1 <= len(str) <= 2
  • string=1 表示 Min=1,Max=nil,就是说 len(str) = 1
  • string=1,_ 表示 Min=1,Max=_,就是说 1 <= len(str)

func (*StringValidator) Validate

func (self *StringValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type UniqueValidator

type UniqueValidator struct {
	EMsg string
}

*

  • 仅支持 string、float、int、bool 类型
  • 或值类型为 string、float、int、bool 类型的array、slice、map

func (*UniqueValidator) Validate

func (self *UniqueValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type UrlValidator

type UrlValidator struct {
	EMsg string
	Reg  string
}

func (*UrlValidator) Validate

func (self *UrlValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

type Validator

type Validator interface {
	Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)
}

验证接口

type ValidatorF

type ValidatorF func(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

验证函数

func (ValidatorF) Validate

func (f ValidatorF) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)

Jump to

Keyboard shortcuts

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