Documentation ¶
Index ¶
- func CheckFieldIsStr(objName, filedName string, tv reflect.Value) (err error)
- func Date(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Datetime(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Email(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Eq(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Float(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Ge(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func GetDumpStructStr(v interface{}) string
- func GetJoinValidErrStr(objName, filedName, inputVal string, others ...string) (res string)
- func Gt(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func IDCard(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func In(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Include(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Int(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Le(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Lt(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func NewDumpStruct() *dumpStruct
- func NewVStruct(targetTag ...string) *vStruct
- func NoEq(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func OTo(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func ParseValidNameKV(validName string) (key, value string)
- func Phone(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func SetCustomerValidFn(validName string, fn CommonValidFn)
- func To(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func ValidStructForMyValidFn(in interface{}, validName string, validFn CommonValidFn, targetTag ...string) error
- func ValidStructForRule(ruleObj RM, in interface{}, targetTag ...string) error
- func ValidateStruct(in interface{}, targetTag ...string) error
- func Year(errBuf *strings.Builder, validName, objName, filedName string, ...)
- func Year2Month(errBuf *strings.Builder, validName, objName, filedName string, ...)
- type CommonValidFn
- type RM
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckFieldIsStr ¶ added in v1.4.8
CheckFieldIsStr 验证字段类型是否为字符串
func Date ¶
Date 验证日期 默认匹配 xxxx-xx-xx, 可以指定分割符
Example ¶
type Tmp struct { Year string `valid:"year"` Year2Month string `valid:"year2month=/"` Date string `valid:"date=/"` Datetime string `valid:"datetime"` } v := &Tmp{Year: "2001", Year2Month: "2000/01", Date: "2021/01/22", Datetime: "2021-01-11 23:22"} fmt.Println(ValidateStruct(v))
Output: "Tmp.Datetime" input "2021-01-11 23:22" is not datetime, eg: 1996-09-28 23:00:00
func Email ¶
Email 验证邮箱
Example ¶
type Tmp struct { Email string `valid:"email"` } v := &Tmp{Email: "xuesongtao512qq.com"} fmt.Println(ValidateStruct(v))
Output: "Tmp.Email" input "xuesongtao512qq.com" is not email
func Eq ¶
Eq 等于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小
Example ¶
type Tmp struct { Name string `valid:"required,eq=3"` Age int32 `valid:"required,eq=20"` Score float64 `valid:"eq=80"` Phone string `valid:"eq=11"` } v := &Tmp{ Name: "xue", Age: 20, Score: 80, Phone: "1354004261", } fmt.Println(ValidateStruct(v))
Output: "Tmp.Phone" input "1354004261" length should equal 11
func Float ¶
Float 验证浮动数
Example ¶
type Tmp struct { FloatString string `valid:"float"` IntNum int `valid:"float"` FloatNum32 float32 `valid:"float"` FloatNum64 float64 `valid:"float"` } v := &Tmp{ FloatString: "1.1", IntNum: 10, FloatNum32: 12.5, FloatNum64: 1.0, } fmt.Println(ValidateStruct(v))
Output: "Tmp.IntNum" input "10" is not float
func GetDumpStructStr ¶ added in v1.3.9
func GetDumpStructStr(v interface{}) string
GetDumpStructStr 获取待 dump 的结构体字符串, 支持json格式化
func GetJoinValidErrStr ¶ added in v1.3.5
GetJoinValidErrStr 获取拼接验证的错误消息, 内容直接通过空格隔开, 最后会拼接 errEndFlag
func In ¶
In 指定输入选项(精准匹配)
Example ¶
type Tmp struct { SelectNum int32 `valid:"in=(1/2/3/4)"` SelectStr string `valid:"in=(a/b/c/d)"` } v := &Tmp{SelectNum: 1, SelectStr: "ac"} fmt.Println(ValidateStruct(v))
Output: "Tmp.SelectStr" input "ac" should in (a/b/c/d)
func Include ¶ added in v1.3.2
Include 指定包含什么字符串(模糊匹配)
Example ¶
type Tmp struct { SelectStr string `valid:"include=(hello/test)"` } v := &Tmp{SelectStr: "hel"} fmt.Println(ValidateStruct(v))
Output: "Tmp.SelectStr" input "hel" should include (hello/test)
func Int ¶
Int 验证整数
Example ¶
type Tmp struct { IntString string `valid:"int"` IntNum int `valid:"int"` FloatNum float32 `valid:"int"` } v := &Tmp{ IntString: "11", IntNum: 1, FloatNum: 1.2, } fmt.Println(ValidateStruct(&v))
Output: "Tmp.FloatNum" input "1.2" is not integer
func NewDumpStruct ¶ added in v1.3.9
func NewDumpStruct() *dumpStruct
func NewVStruct ¶
func NewVStruct(targetTag ...string) *vStruct
NewVStruct 验证结构体, 默认目标 tagName 为 "valid"
func NoEq ¶ added in v1.4.2
NoEq 不等于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小
Example ¶
type Tmp struct { Name string `valid:"required,noeq=3"` Age int32 `valid:"required,noeq=20"` Score float64 `valid:"noeq=80"` Phone string `valid:"noeq=11"` } v := &Tmp{ Name: "xue", Age: 20, Score: 80, Phone: "1354004261", } fmt.Println(ValidateStruct(v))
Output: "Tmp.Name" input "xue" length should no equal 3; "Tmp.Age" input "20" size should no equal 20; "Tmp.Score" input "80" size should no equal 80
func ParseValidNameKV ¶
ParseValidNameKV 解析 validName 中的 key 和 value, 如: "required", key 为 "required", value 为 "" 如: "to=1~2", key 为 "to", value 为 "1~2"
func Phone ¶
Phone 验证手机号
Example ¶
type Tmp struct { Phone string `valid:"phone"` } v := &Tmp{Phone: "1"} fmt.Println(ValidateStruct(v))
Output: "Tmp.Phone" input "1" is not phone
func SetCustomerValidFn ¶
func SetCustomerValidFn(validName string, fn CommonValidFn)
Deprecated 此函数会修改全局变量, 会导致内存释放不了, 此推荐 ValidStructForMyValidFn SetCustomerValidFn 自定义验证函数
Example ¶
type Tmp struct { Name string `valid:"required"` Age string `valid:"num"` } isNumFn := func(errBuf *strings.Builder, validName, structName, filedName string, tv reflect.Value) { ok, _ := regexp.MatchString("^\\d+$", tv.String()) if !ok { errBuf.WriteString(fmt.Sprintf("%q is not num", structName+"."+filedName)) return } } // 弃用 SetCustomerValidFn("num", isNumFn) v := Tmp{Name: "12", Age: "1ha"} fmt.Println(ValidateStruct(&v))
Output: "Tmp.Age" is not num
func To ¶
To 验证输入的大小区间, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小, 注: 左右都为闭区间
Example ¶
type Tmp struct { Name string `valid:"to=1~3"` Age int32 `valid:"to=0~99"` Addr string `valid:"le=3.2"` } v := &Tmp{Name: "测试调", Age: 100, Addr: "tets"} fmt.Println(ValidateStruct(v))
Output: "Tmp.Age" input "100" size more than or equal 99
func ValidStructForMyValidFn ¶ added in v1.4.4
func ValidStructForMyValidFn(in interface{}, validName string, validFn CommonValidFn, targetTag ...string) error
ValidStructForMyValidFn 自定义验证规则
func ValidStructForRule ¶
ValidStructForRule 自定义验证规则并验证 注: 通过字段名来匹配规则, 如果嵌套中如果有相同的名的都会走这个规则, 因此建议这种方式推荐使用非嵌套结构体
func ValidateStruct ¶
ValidateStruct 验证结构体