Documentation ¶
Index ¶
- Constants
- func CheckFieldIsStr(objName, fieldName string, tv reflect.Value) (err error)
- func Date(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Datetime(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Email(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Eq(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Float(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Ge(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func GetDumpStructStr(v interface{}) string
- func GetDumpStructStrForJson(v interface{}) string
- func GetJoinValidErrStr(objName, fieldName, inputVal string, others ...string) (res string)
- func Gt(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func IDCard(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func In(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Include(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Int(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Le(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Lt(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func NewDumpStruct() *dumpStruct
- func NewVStruct(targetTag ...string) *vStruct
- func NoEq(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func OTo(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ParseValidNameKV(validName string) (key, value string)
- func Phone(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func SetCustomerValidFn(validName string, fn CommonValidFn)
- func To(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ValidStructForMyValidFn(src interface{}, validName string, validFn CommonValidFn, targetTag ...string) error
- func ValidStructForRule(ruleObj RM, src interface{}, targetTag ...string) error
- func ValidateStruct(src interface{}, targetTag ...string) error
- func Year(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Year2Month(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- type CommonValidFn
- type RM
Examples ¶
Constants ¶
const ( Required = "required" // 必填 Exist = "exist" // 有值才验证 Either = "either" // 多个必须一个 BothEq = "botheq" // 两者相等 )
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 等于验证 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
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" strLength 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 Ge ¶
Ge 大于或等于验证 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"ge=1"` Age int32 `valid:"ge=0"` } v := &Tmp{Name: "测试调", Age: -1} fmt.Println(ValidateStruct(v))
Output: "Tmp.Age" input "-1" numSize less than 0
func GetDumpStructStr ¶ added in v1.3.9
func GetDumpStructStr(v interface{}) string
GetDumpStructStr 获取待 dump 的结构体字符串, 支持 json 格式化 只会把字段名解析成 key, 值为 value GetDumpStructStrForJson 性能较好, 只是在有 json_tag 的时候会用 json_tag 作为 key
func GetDumpStructStrForJson ¶ added in v1.3.9
func GetDumpStructStrForJson(v interface{}) string
GetDumpStructStrForJson 先 json 序列化, 再获取
func GetJoinValidErrStr ¶ added in v1.3.5
GetJoinValidErrStr 获取拼接验证的错误消息, 内容直接通过空格隔开, 最后会拼接 errEndFlag
func Gt ¶
Gt 大于验证 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"gt=2"` Age int32 `valid:"gt=0"` } v := &Tmp{Name: "测试", Age: -1} fmt.Println(ValidateStruct(v))
Output: "Tmp.Name" input "测试" strLength less than or equal 2; "Tmp.Age" input "-1" numSize less than or equal 0
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 Le ¶
Le 小于或等于验证 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"le=2"` Age int32 `valid:"le=0"` } v := &Tmp{Name: "测试调", Age: 1} fmt.Println(ValidateStruct(v))
Output: "Tmp.Name" input "测试调" strLength more than 2; "Tmp.Age" input "1" numSize more than 0
func Lt ¶
Lt 小于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小
Example ¶
type Tmp struct { Name string `valid:"lt=2"` Age int32 `valid:"lt=40"` } v := &Tmp{Name: "测试", Age: 99} fmt.Println(ValidateStruct(v))
Output: "Tmp.Name" input "测试" strLength more than or equal 2; "Tmp.Age" input "99" numSize more than or equal 40
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 不等于验证 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
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" strLength should no equal 3; "Tmp.Age" input "20" numSize should no equal 20; "Tmp.Score" input "80" numSize 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, fieldName string, tv reflect.Value) { ok, _ := regexp.MatchString("^\\d+$", tv.String()) if !ok { errBuf.WriteString(fmt.Sprintf("%q is not num", structName+"."+fieldName)) return } } // 弃用 SetCustomerValidFn("num", isNumFn) v := Tmp{Name: "12", Age: "1ha"} fmt.Println(ValidateStruct(&v))
Output: "Tmp.Age" is not num
func To ¶
To 验证输入的大小区间, 注: 左右都为闭区间 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"to=1~3"` Age int32 `valid:"to=0~99"` Addr string `valid:"to=3~10"` } v := &Tmp{Name: "测试调", Age: 100, Addr: "tets"} fmt.Println(ValidateStruct(v))
Output: "Tmp.Age" input "100" numSize more than 99
func ValidStructForMyValidFn ¶ added in v1.4.4
func ValidStructForMyValidFn(src interface{}, validName string, validFn CommonValidFn, targetTag ...string) error
ValidStructForMyValidFn 自定义单个验证函数
func ValidStructForRule ¶
ValidStructForRule 自定义验证规则并验证 注: 通过字段名来匹配规则, 如果嵌套中如果有相同的名的都会走这个规则, 因此建议这种方式推荐使用非嵌套结构体
func ValidateStruct ¶
ValidateStruct 验证结构体