Documentation ¶
Index ¶
- Constants
- func ChineseOnly(data interface{}, fieldName string) (bool, string, error)
- func IsFloat(in string) bool
- func IsInt(in string) bool
- func RemovePrefix(s string, prefix string) string
- func SmartPrint(i interface{})
- func ToString(arg interface{}, timeFormat ...string) string
- type Checker
- func (checker *Checker) AddDefaultRegex(key string, regex string) error
- func (checker *Checker) AddFunc(f func(in interface{}, fieldName string) (bool, string, error), ...) error
- func (checker *Checker) AddRegex(key string, regex string) error
- func (checker *Checker) Check(input string, regex string) (bool, error)
- func (checker *Checker) CheckFromPool(input string, key string) (bool, error)
- func (checker *Checker) ContainFunc(key string) bool
- func (checker *Checker) FormatCheck(input interface{}) (bool, string, error)
- func (checker *Checker) GetDefaultBuilder() map[string]*regexp.Regexp
- func (checker *Checker) GetFunc(key string) Func
- func (checker *Checker) GetRule(key string) string
- func (checker *Checker) IsBuilderContainKey(key string) bool
- func (checker *Checker) IsContainKey(key string) bool
- func (checker *Checker) ListAll()
- func (checker *Checker) ListDefault()
- func (checker *Checker) ListRegexBuilder()
- func (checker *Checker) RemoveRegex(key string)
- func (checker *Checker) SetMode(mode int)
- func (checker *Checker) SuperCheck(input interface{}) (bool, string, error)
- func (checker *Checker) Validate(input interface{}) (bool, string, error)
- func (checker *Checker) ValidateByTagKeyAndMapValue(dest interface{}, tagKey string, tags map[string]string) (bool, string, error)
- func (checker *Checker) ValidateMethods(input interface{}, typ ...string) (bool, string, error)
- func (checker *Checker) ValidateOne(typeName string, value interface{}, tagValue string) (bool, string, error)
- type Func
- type Ruler
- type T
- type T2
Constants ¶
const ( DEBUG = 1 RELEASE = 2 )
Variables ¶
This section is empty.
Functions ¶
func ChineseOnly ¶
only accept 'data' to be chinese use it like:
type User struct{ ChineseName string `validate:"func,chineseOnly"` }
func main(){ user := User{ChineseName: "ft"} checker := superChecker.GetChecker() checker.AddFunc(superChecker.ChineseOnly, "chineseOnly") ok,msg,er :=checker.Validate(user) if er!=nil { panic(er) } if !ok { fmt.Println(msg) return } fmt.Println("success") }
func RemovePrefix ¶
func SmartPrint ¶
func SmartPrint(i interface{})
SmartPrint pretty format print an input value,which should be a struct
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
global checker object a checker contains its rule which stores the regex rule pool of the default pool and the added pool
func GetChecker ¶
func GetChecker() *Checker
get a checker object which has contained regex rule below: username : ^[\u4E00-\u9FA5a-zA-Z0-9_.]{0,40}$ number : "^[0-9]+$" decimal : "^\\d+\\.[0-9]+$" mobile phone : "^1[0-9]{10}$" telephone : "^[0-9]{8}$" notnull: "^[\\s\\S]+$"
func (*Checker) AddDefaultRegex ¶
add default regex rule into default pool , when the key is already existed, then it will be replaced by the new one
func (*Checker) AddFunc ¶
func (checker *Checker) AddFunc(f func(in interface{}, fieldName string) (bool, string, error), keyAndPath ...string) error
add a func into func pool keyAndPath stands for the func's key and func's define path. key must specific and must be keyAndPath[0], path is optional. when the length of keyAndPath is 0 or >2 , then throws error. when the length of keyAndPath is 1, key is keyAndPath[0], path is the caller stack depth 1. when the length of keyAndPath is 2, key is keyAndPath[0], path is keyAndPath[1].
func (*Checker) CheckFromPool ¶
check an input string value by the compiled regex object from the checker's default and added pool
func (*Checker) ContainFunc ¶
whether the func pool contains the func key
func (*Checker) FormatCheck ¶
validate if an input value is correct or not notice:
- some ignored cases: `validate:""`, `validate:"-"` will be ignored struct{name string}{name:"undefine"}, struct{name string}{name:"undefined"} will be ignored
support int types ,float types, string, time
func (*Checker) GetDefaultBuilder ¶
get the default pool
func (*Checker) IsBuilderContainKey ¶
whether the added pool contains the rule key
func (*Checker) IsContainKey ¶
whether the key is contained
func (*Checker) ListAll ¶
func (checker *Checker) ListAll()
list all regex compiled in both the default and the added pool.
func (*Checker) RemoveRegex ¶
remove regex kv from the added pool.
func (*Checker) SetMode ¶
set its mode of superChecker.DEBUG,superChecker.RELEASE DEBUG =1 RELEASE = 2
func (*Checker) SuperCheck ¶
Latest version has stop this's developing replaced by functions like 'FormatCheck' 'Validate' 'ValidateMethods' which alias with tag 'validate' However the old usages are remained, function is access if you insist on using it
support for string input or type that can be transfer to a string or an object which has function String(). notice:
- the value of tag 'superCheck' can be either upper or lower or mixed, `superChecker:"username"`,`superChecker:"usERName"` are ok
- some cases will be ignored: `superChecker:""`, `superChecker:"-"` will be ignored struct{name string}{name:"undefined"}, struct{name string}{name:"undefine"} will be ignored
- make sure the not-ignored fields is string-able, these types can be well stringed: [int,int8,int16,int32,int64,float32,float64,] || <object'function String()>
func (*Checker) Validate ¶
validate the tag whose key named 'validate'
the same as FormatCheck,but sounds more specific
func (*Checker) ValidateByTagKeyAndMapValue ¶
func (checker *Checker) ValidateByTagKeyAndMapValue(dest interface{}, tagKey string, tags map[string]string) (bool, string, error)
This method is used for the case when dest struct has no 'validate' tag and tag value. Tag name can be specific,like 'json'.
tagKey value should be single `json:""username` or 'username' as its first word splitting by ',' these tags are ok `json:"username"`, `json:"username,x"`, `json:"username,x,y,z,n,m,q"
type User struct{ Username string `json:"username"` }
user := User{ Username:"LiLei" } checker := SuperChecker.GetChecker() ok,msg,e:=checker.ValidateByTagKeyAndMapValue(user, "json", map[string]string{ "username": "regex,^[\u4E00-\u9FA5a-zA-Z0-9_.]{0,40}$")} fmt.Println(ok, msg, e)
func (*Checker) ValidateMethods ¶
validate a struct methods whose method name ends with '"SVValidate"+<typ flags>'. typ is short for 'type' to avoid the built-in word type. what the string 'typ' stands for? assume you are going to validate a struct User{Username string , Age int}, when creating a new user, 'username' field is required, however when updating a user,'username' is optional. typ helps to tell which method validates on which case. For example: func (o O) UserSVValidateSVBCreate()(bool,string,error){ } SV means 'Super Validate' ,which tells the checker this method will be checked when call ValidateMethods. SVB means 'Super Validate Begin', which tells the checker to identify the typ flags. in the example above, 'typ' is 'Create',and this method only works for 'checker.ValidateMethods(o, 'Create')' func (o O) UserSVValidateSVBCreateSVSUpdate()(bool,string,error){ } SV means 'Super Validate' ,which tells the checker this method will be checked when call ValidateMethods. SVB means 'Super Validate Begin', which tells the checker to identify the typ flags. SVS meas 'Super Validate Separate', which takes typ flags apart. in the example above, this method only works for 'checker.ValidateMethods(o, 'Create', 'Update')'. if typ is not set, all methods which end with 'Validate' or 'SVValidate' will be well checked
type Func ¶
Func has a value and its desgin path.
value serves for a self design function that deals with the input data 'in interface{}', and returns its result 'ok bool', 'message string', 'e error'. path serves for logging where the function is design
for example:
value: func ValideMoney(in interface{}) (bool,string,error){ v, ok :=in.(float64) if !ok{ return false, fmt.Sprintf( want float64 type, got '%v'", in), errors.New(fmt.Sprintf(" want float64 type, got '%v'", in)) } return true,"success",nil } path: xxx/xxx/xx/main.go: 90
type Ruler ¶
type Ruler struct { RegexBuilder map[string]*regexp.Regexp Funcs map[string]Func // contains filtered or unexported fields }
rule object contained in a checker,it consists of a default pool and an added pool
type T2 ¶
type T2 struct { CreatedAt time.Time `validate:"time.time"` UpdatedAt string `validate:"time.time,2006/01/02 15:04:05"` Count int `validate:"int,0:200"` MaxCount int `validate:"int,:200"` MinCount int `validate:"int,10:"` Count2 int `validate:"int64,0:200"` RewardRate float64 `validate:"float,0:0.4"` MaxRewardRate float64 `validate:"float,:0.4"` MinRewradRate float64 `validate:"float,0:"` RewardRate2 float64 `validate:"float64,0:0.4"` RewardRate3 decimal.Decimal `validate:"decimal,0:0.4"` OrderUsername string `validate:"regex,^[\u4E00-\u9FA5a-zA-Z0-9_.]{0,40}$"` OrderUsername2 string `validate:"regex,username"` OrderStatus int `validate:"range,[1,2,3,4]"` OrderStatusName string `validate:"in,[unpaid,paid,closed]"` MailTypeCheckBox string `validate:"func,inAndLength"` MailTypeCheckBox2 string `validate:"function,inAndLength"` }