Documentation ¶
Index ¶
- Constants
- Variables
- 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 Dir(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 File(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 GenValidKV(key string, values ...string) string
- func GetDumpStructStr(v interface{}) string
- func GetDumpStructStrForJson(v interface{}) string
- func GetJoinFieldErr(objName, fieldName string, err interface{}) string
- func GetJoinValidErrStr(objName, fieldName, inputVal string, others ...string) string
- func GetOnlyExplainErr(errMsg string) string
- func GetTimeFmt(fmtType int8, splits ...string) 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 Ints(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Ip(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Ipv4(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Ipv6(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func IsExported(fieldName string) bool
- func JoinTag2Val(key string, values ...string) stringdeprecated
- func Json(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 Map(src interface{}, ruleObj RM) error
- func MapFn(src interface{}, ruleObj RM, fnMap Name2FnMap) error
- func NestedStructForRule(src interface{}, ruleMap map[interface{}]RM) error
- func NewDumpStruct() *dumpStruct
- func NoEq(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func OTo(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ParseValidNameKV(validName string) (key, value, cusMsg string)
- func Phone(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Prefix(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Re(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ReflectKindIsNum(kind reflect.Kind, isCanFloat ...bool) (is bool)
- func RemoveTypePtr(t reflect.Type) reflect.Type
- func RemoveValuePtr(t reflect.Value) reflect.Value
- func SetCustomerValidFn(validName string, fn CommonValidFn)
- func SetStructTypeCache(cacheEr CacheEr)
- func StrEscape(val string) string
- func Struct(src interface{}, ruleObj ...RM) error
- func StructForFn(src interface{}, ruleObj RM, targetTag ...string) error
- func StructForFns(src interface{}, ruleObj RM, fnMap Name2FnMap, targetTag ...string) error
- func Suffix(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func To(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ToStr(src interface{}) string
- func Unique(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Url(src interface{}, ruleObj RM) error
- func UrlForFn(src interface{}, validName string, validFn CommonValidFn) error
- func ValidNamesSplit(s string, sep ...byte) []string
- func ValidStructForMyValidFn(src interface{}, validName string, validFn CommonValidFn, targetTag ...string) errordeprecated
- func ValidStructForRule(ruleObj RM, src interface{}, targetTag ...string) errordeprecated
- func ValidateStruct(src interface{}, targetTag ...string) errordeprecated
- func Var(src interface{}, rules ...string) error
- func VarForFn(src interface{}, validFn CommonValidFn) error
- func Year(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Year2Month(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- type CacheEr
- type CommonValidFn
- type LRUCache
- type Name2FnMap
- type RM
- type VMap
- type VStruct
- type VUrl
- type VVar
Examples ¶
Constants ¶
const ( ExplainEn = "explain:" ExplainZh = "说明:" )
err msg
const ( YearFmt int8 = 1 << iota MonthFmt DayFmt HourFmt MinFmt SecFmt DateFmt = YearFmt | MonthFmt | DayFmt DateTimeFmt = DateFmt | HourFmt | MinFmt | SecFmt )
时间格式
const ( Required = "required" // 必填 Exist = "exist" // 有值才验证 Either = "either" // 多个必须一个 BothEq = "botheq" // 两者相等 VTo = "to" // 两者之间, 闭区间 VGe = "ge" // 大于或等于 VLe = "le" // 小于或等于 VOTo = "oto" // 两者之间, 开区间 VGt = "gt" // 大于 VLt = "lt" // 小于 VEq = "eq" // 等于 VNoEq = "noeq" // 不等于 VIn = "in" // 指定输入选项 VInclude = "include" // 指定输入包含选项 VPhone = "phone" // 手机号 VEmail = "email" // 邮箱 VIDCard = "idcard" // 身份证号码 VYear = "year" // 年 VYear2Month = "year2month" // 年月 VDate = "date" // 日 VDatetime = "datetime" // 日期+时间点 VInt = "int" // 整数 VInts = "ints" // 多个数字验证 VFloat = "float" // 浮动数 VRe = "re" // 正则 VIp = "ip" // ip VIpv4 = "ipv4" // ipv4 VIpv6 = "ipv6" // ipv6 VUnique = "unique" // 唯一验证 VJson = "json" // json 格式验证 VPrefix = "prefix" // 包含前缀 VSuffix = "suffix" // 包含后缀 VFile = "file" // 文件 VDir = "dir" // 目录 )
验证 tag
Variables ¶
var ( IncludeZhRe = regexp.MustCompile("[\u4e00-\u9fa5]") // 中文 PhoneRe = regexp.MustCompile(`^1[3,4,5,6,7,8,9]\d{9}$`) // 手机号 Ipv4Re = regexp.MustCompile(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`) EmailRe = regexp.MustCompile(`^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$`) IdCardRe = regexp.MustCompile(`(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)`) IntRe = regexp.MustCompile(`^\d+$`) FloatRe = regexp.MustCompile(`^\d+.\d+$`) // Deprecated YearRe = regexp.MustCompile(`^\d{4}$`) // Deprecated Year2MonthRe = regexp.MustCompile(`^\d{4}-\d{2}$`) // Deprecated Year2MonthRe2 = regexp.MustCompile(`^\d{4}/\d{2}$`) // Deprecated DateRe = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`) // Deprecated DateRe2 = regexp.MustCompile(`^\d{4}/\d{2}/\d{2}$`) // Deprecated DatetimeRe = regexp.MustCompile(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`) // Deprecated DatetimeRe2 = regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}$`) )
正则
var (
ErrEndFlag = "; " // 错误结束符号(每个自定义 err 都需要将这个追加在后面, 用于分句)
)
标记
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=/"` Date2 string `valid:"date=''"` Datetime string `valid:"datetime|应该为 xxxx-xx-xx xx:xx:xx 的时间格式"` } v := &Tmp{Year: "2001", Year2Month: "2000/01", Date: "2021/01/22", Date2: "20210122", Datetime: "2021-01-11 23:22"} fmt.Println(Struct(v))
Output: "Tmp.Datetime" input "2021-01-11 23:22", 说明: 应该为 xxxx-xx-xx xx:xx:xx 的时间格式
func Datetime ¶
Datetime 验证时间 默认匹配 xxxx-xx-xx xx:xx:xx, 可以指定分割符
Example ¶
type Tmp struct { Datetime1 string `valid:"required,datetime|应该为 xxxx-xx-xx xx:xx:xx 的时间格式"` Datetime2 string `valid:"required,datetime='/'|应该为 xxxx/xx/xx xx:xx:xx 的时间格式"` Datetime3 string `valid:"required,datetime='/, ,/'|应该为 xxxx/xx/xx xx/xx/xx 的时间格式"` Datetime4 string `valid:"required,datetime=', ,'|应该为 xxxxxxxx xxxxxx 的时间格式"` Datetime5 string `valid:"required,datetime=',,'|应该为 xxxxxxxxxxxxxx 的时间格式"` } v := &Tmp{ Datetime1: "2022/11-09 10:05:00", Datetime2: "2022/11/09 10:05:00", Datetime3: "2022/11/09 10/05/00", Datetime4: "20221109 100500", Datetime5: "20221109100500", } fmt.Println(Struct(v))
Output: "Tmp.Datetime1" input "2022/11-09 10:05:00", 说明: 应该为 xxxx-xx-xx xx:xx:xx 的时间格式
func Email ¶
Email 验证邮箱
Example ¶
type Tmp struct { Email string `valid:"email"` } v := &Tmp{Email: "xuesongtao512qq.com"} fmt.Println(Struct(v))
Output: "Tmp.Email" input "xuesongtao512qq.com", explain: it 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|年龄应该等于 20"` Score float64 `valid:"eq=80"` Phone string `valid:"eq=11"` } v := &Tmp{ Name: "xue", Age: 21, Score: 80, Phone: "1354004261", } fmt.Println(Struct(v))
Output: "Tmp.Age" input "21", 说明: 年龄应该等于 20; "Tmp.Phone" input "1354004261", explain: it should equal 11 str-length
func Float ¶
Float 验证浮动数
Example ¶
type Tmp struct { FloatString string `valid:"float|请输入浮点数"` FloatNum32 float32 `valid:"float"` FloatNum64 float64 `valid:"float"` } v := &Tmp{ FloatString: "1", FloatNum32: 12.5, FloatNum64: 1.0, } fmt.Println(Struct(v))
Output: "Tmp.FloatString" input "1", 说明: 请输入浮点数
func Ge ¶
Ge 大于或等于验证 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"ge=1"` Age int32 `valid:"ge=0|应该大于 0"` } v := &Tmp{Name: "测试调", Age: -1} fmt.Println(Struct(v))
Output: "Tmp.Age" input "-1", 说明: 应该大于 0
func GenValidKV ¶ added in v1.5.23
GenValidKV 生成 defaultTargetTag 的值 说明: 函数名主要用于生成(如: `valid:"xxx"`) 中 "xxx" 的部分 key 为验证规则 values[0] 会被解析为值 values[1] 会被解析为自定义错误信息 如1.: GenValidKV(VTo, "1~10", "需要在 1-10 的区间") => to=1~10|需要在 1-10 的区间
如2: GenValidKV(VRe, "'\\d+'", "必须为纯数字") => re='\\d+'|必须为纯数字
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 GetJoinFieldErr ¶ added in v1.5.23
GetJoinFieldErr 拼接字段错误
func GetJoinValidErrStr ¶ added in v1.3.5
GetJoinValidErrStr 获取拼接验证的错误消息, 内容直接通过空格隔开, 最后会拼接 ErrEndFlag
func GetOnlyExplainErr ¶ added in v1.6.6
GetOnlyExplainErr 获取所有的说明错误(不包含错误的字段信息) 使用场景: 在自定义错误信息时, 返回给非开发人员看的结果
Example ¶
type Tmp struct { Name string `valid:"to=2~4|姓名长度应该在2-4个长度"` IntString string `valid:"int|IntString请输入整数类"` IntNum string `valid:"int|IntNum请输入整数"` } v := &Tmp{ Name: "xuesongtao", IntString: "11.121", IntNum: "1a", } err := Struct(&v) fmt.Println(GetOnlyExplainErr(err.Error()))
Output: 姓名长度应该在2-4个长度; IntString请输入整数类; IntNum请输入整数
func GetTimeFmt ¶ added in v1.6.9
GetTimeFmt 获取时间格式化 splits 为分隔符 splits[0] 为 [年月日] 的分割符, 默认为 "-" splits[1] 为 [年月日] 和 [时分秒] 的分割符, 默认为 " " splits[2] 为 [时分秒] 的分割符, 默认为 ":"
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(Struct(v))
Output: "Tmp.Name" input "测试", explain: it is less than or equal 2 str-length; "Tmp.Age" input "-1", explain: it is less than or equal 0 num-size
func IDCard ¶
IDCard 验证身份证
Example ¶
type Tmp struct { IDCard string `valid:"idcard"` } v := &Tmp{IDCard: "511321"} fmt.Println(Struct(v))
Output: "Tmp.IDCard" input "511321", explain: it is not idcard
func In ¶
In 指定输入选项(精准匹配)
Example ¶
type Tmp struct { SelectNum int32 `valid:"in=(1/2/3/4)"` SelectStr string `valid:"in=(a/b/c/d)|应该在 a/b/c/d 里选择"` SelectStr1 string `valid:"in=(a/'/d')|应该在 a/'/d' 里选择"` // 转义 } v := &Tmp{SelectNum: 1, SelectStr: "ac", SelectStr1: "/d"} fmt.Println(Struct(v))
Output: "Tmp.SelectStr" input "ac", 说明: 应该在 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(Struct(v))
Output: "Tmp.SelectStr" input "hel", explain: it should include (hello/test)
func Int ¶
Int 验证整数
Example ¶
type Tmp struct { IntString string `valid:"int|请输入整数类"` IntNum int `valid:"int"` } v := &Tmp{ IntString: "11.121", IntNum: 1, } fmt.Println(Struct(&v))
Output: "Tmp.IntString" input "11.121", 说明: 请输入整数类
func Ints ¶ added in v1.5.23
Ints 验证是否为多个数字 1. 如果输入为 string, 默认按逗号拼接进行处理 2. 如果为 slice/array, 会将每个值进行匹配判断
Example ¶
type Tmp struct { IntStr string `valid:"ints"` IntStr1 string `valid:"ints=-"` IntSlice []int `valid:"ints"` IntSliceStr []string `valid:"ints"` } v := &Tmp{ IntStr: "1,2,3", IntStr1: "1,2,3", IntSliceStr: []string{"1", "hello"}, IntSlice: []int{1, 2}, } fmt.Println(Struct(&v))
Output: "Tmp.IntStr1" input "1,2,3", explain: it is not separated by "-" num; "Tmp.IntSliceStr" input "[1, hello]", explain: slice/array element is not all num
func Ip ¶ added in v1.6.9
Ip ip 验证
Example ¶
type Tmp struct { Ip string `valid:"required,ip|ip不正确"` Ipv4 string `valid:"required,ipv4|ipv4不正确"` Ipv6 string `valid:"required,ipv6|ipv6不正确"` } v := &Tmp{ Ip: "192.168.1.0", Ipv4: "192.168.0.113", Ipv6: "2001:3CA1:10F:1A:121B:0:0:", } fmt.Println(Struct(&v))
Output: "Tmp.Ipv6" input "2001:3CA1:10F:1A:121B:0:0:", 说明: ipv6不正确
func JoinTag2Val
deprecated
added in
v1.5.23
func Json ¶ added in v1.6.10
Json 验证是否为 json
Example ¶
type Tmp struct { Name string `valid:"required"` Json1 string `valid:"required,json|json格式不正确"` Json2 string `valid:"required,json"` } tmp := &Tmp{ Name: "测试json", Json1: `[{"id":1,"name":"test","age":10,"cls_name":"初一","addr":"四川成都"},{"id":2,"name":"test","age":10,"cls_name":"初二","addr":"四川成都"}]`, Json2: `{"name":"A-Tao","hobby":["play basketball","write golang code"],"class_name":"community university","addr":"ChengDu"}`, } err := Struct(tmp) fmt.Println(err)
Output: <nil>
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(Struct(v))
Output: "Tmp.Name" input "测试调", explain: it is more than 2 str-length; "Tmp.Age" input "1", explain: it is more than 0 num-size
func Lt ¶
Lt 小于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小
Example ¶
type Tmp struct { Name string `valid:"lt=2"` Age int32 `valid:"lt=40"` } v := &Tmp{Name: "测试", Age: 99} fmt.Println(Struct(v))
Output: "Tmp.Name" input "测试", explain: it is more than or equal 2 str-length; "Tmp.Age" input "99", explain: it is more than or equal 40 num-size
func MapFn ¶ added in v1.6.10
func MapFn(src interface{}, ruleObj RM, fnMap Name2FnMap) error
MapFn 验证 map
func NestedStructForRule ¶ added in v1.6.10
NestedStructForRule 结构嵌套多个设置多个结构体规则 ruleMap key: 结构体指针, value: RM 注: ruleMap 的 key 必须为指针, 不然会报错 "hash of unhashable type"
Example ¶
type Tmp1 struct { Name string } type Tmp struct { Ip string T []Tmp1 } rmap := map[interface{}]RM{ // key 必须为 指针 &Tmp{}: NewRule().Set("Ip,T", Required).Set("Ip", GenValidKV(VIp, "", "ip 格式不正确")), &Tmp1{}: NewRule().Set("Name", GenValidKV(Required, "", "姓名必填")), } v := &Tmp{ Ip: "256.12.22.400", T: []Tmp1{{Name: ""}, {Name: "2"}}, } err := NestedStructForRule(v, rmap) fmt.Println(err)
Output: "Tmp.Ip" input "256.12.22.400", 说明: ip 格式不正确; "Tmp.T[0].Name" input "", 说明: 姓名必填
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|年龄不应该等于 20"` Score float64 `valid:"noeq=80"` Phone string `valid:"noeq=11"` } v := &Tmp{ Name: "xue", Age: 20, Score: 80, Phone: "1354004261", } fmt.Println(Struct(v))
Output: "Tmp.Name" input "xue", explain: it is not equal 3 str-length; "Tmp.Age" input "20", 说明: 年龄不应该等于 20; "Tmp.Score" input "80", explain: it is not equal 80 num-size
func OTo ¶
OTo 验证输入的大小区间, 注: 左右都为开区间 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"oto=1~3"` Age int32 `valid:"oto=1~100"` NickName string `valid:"oto=0~10"` Addr string `valid:"oto=1~3|家庭地址长度应该在 大于 1 且小于 3"` } v := &Tmp{Name: "测试", Age: 0, NickName: "h1", Addr: "tet"} fmt.Println(Struct(v))
Output: "Tmp.Addr" input "tet", 说明: 家庭地址长度应该在 大于 1 且小于 3
func ParseValidNameKV ¶
ParseValidNameKV 解析 validName 中的 key, value 和 cusMsg, 如: "required|必填", key 为 "required", value 为 "", cusMsg 为 "必填" 如: "to=1~2|大于等于 1 且小于等于 2", key 为 "to", value 为 "1~2", cusMsg 为 "大于等于 1 且小于等于 2"
func Phone ¶
Phone 验证手机号
Example ¶
type Tmp struct { Phone string `valid:"phone"` } v := &Tmp{Phone: "1"} fmt.Println(Struct(v))
Output: "Tmp.Phone" input "1", explain: it is not phone
func Prefix ¶ added in v1.6.10
Prefix 验证字符串包含前缀
Example ¶
type Tmp struct { Name string `valid:"required,prefix=test|前缀必须为test"` } tmp := &Tmp{ Name: "ceshi", } err := Struct(tmp) fmt.Println(err)
Output: "Tmp.Name" input "ceshi", 说明: 前缀必须为test
func Re ¶ added in v1.5.17
Re 正则表达式 使用格式如: re='\\d+'|匹配错误
Example ¶
type Tmp struct { Name string `valid:"required|必填,re='[a-z]+'|姓名必须为英文"` Age string `valid:"re='\\d{2}'|年龄必须为 2 位数"` Addr string `valid:"required|地址必须,re='[\u4e00-\u9fa5]'|地址必须为中文"` Pwd string `valid:"required,re='^[A-Za-z0-9]{8,16}$'|密码必须为8-16为字母和数字组合"` } v := &Tmp{ Name: "测试", Age: "1", Addr: "四川成都", Pwd: "1234567", } fmt.Println(Struct(v))
Output: "Tmp.Name" input "测试", 说明: 姓名必须为英文; "Tmp.Age" input "1", 说明: 年龄必须为 2 位数; "Tmp.Pwd" input "1234567", 说明: 密码必须为8-16为字母和数字组合
func ReflectKindIsNum ¶ added in v1.5.23
ReflectKindIsNum 值是否为数字
func RemoveTypePtr ¶ added in v1.5.17
RemoveTypePtr 移除多指针
func RemoveValuePtr ¶ added in v1.5.17
RemoveValuePtr 移除多指针
func SetCustomerValidFn ¶
func SetCustomerValidFn(validName string, fn CommonValidFn)
SetCustomerValidFn 自定义验证函数 用于全局添加验证方法, 如果不想定义全局, 可根据验证对象分别调用 SetValidFn, 如: *VStruct.SetValidFn
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(Struct(&v))
Output: "Tmp.Age" is not num
func SetStructTypeCache ¶ added in v1.6.5
func SetStructTypeCache(cacheEr CacheEr)
SetStructTypeCache 设置 structType 缓存类型
func Struct ¶ added in v1.5.27
Struct 验证结构体 1. 支持单结构体验证 2. 支持切片/数组类型结构体验证 3. 支持map: key为普通类型, value为结构体 验证
func StructForFn ¶ added in v1.5.29
StructForFn 验证结构体, 同时设置自定义参数
func StructForFns ¶ added in v1.6.10
func StructForFns(src interface{}, ruleObj RM, fnMap Name2FnMap, targetTag ...string) error
StructForFns 验证结构体, 可以设置自定义验证函数和规则
Example ¶
type Tmp struct { Name string Addr string } isLower := func(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value) { if err := CheckFieldIsStr(objName, fieldName, tv); err != nil { errBuf.WriteString(err.Error()) return } if strings.ToLower(tv.String()) != tv.String() { errBuf.WriteString(GetJoinValidErrStr(objName, fieldName, tv.String(), ExplainEn, "it is not lower")) } } isZh := func(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value) { if err := CheckFieldIsStr(objName, fieldName, tv); err != nil { errBuf.WriteString(err.Error()) return } if match, _ := regexp.MatchString("^[\u4e00-\u9fa5]+$", tv.String()); !match { errBuf.WriteString(GetJoinValidErrStr(objName, fieldName, tv.String(), ExplainEn, "it should is zh")) } } fnMap := Name2FnMap{"islower": isLower, "iszh": isZh} rm := RM{"Name": "islower", "Addr": "iszh"} v := &Tmp{ Name: "Test", Addr: "四川", } err := StructForFns(v, rm, fnMap) fmt.Println(err)
Output: "Tmp.Name" input "Test", explain: it is not lower
func Suffix ¶ added in v1.6.10
Suffix 验证字符串包含后缀
Example ¶
type Tmp struct { Name string `valid:"required,suffix=test|后缀必须为test"` } tmp := &Tmp{ Name: "mytest", } err := Struct(tmp) fmt.Println(err)
Output: <nil>
func To ¶
To 验证输入的大小区间, 注: 左右都为闭区间 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 3. 如果是切片的话就验证的长度
Example ¶
type Tmp struct { Name string `valid:"to=1~3|姓名长度为 1-3 个字符"` Age int32 `valid:"to=0~99|年龄应该在 0-99 之间"` Addr string `valid:"to=3~10"` } v := &Tmp{Name: "测试调1", Age: 100, Addr: "tets"} fmt.Println(Struct(v))
Output: "Tmp.Name" input "测试调1", 说明: 姓名长度为 1-3 个字符; "Tmp.Age" input "100", 说明: 年龄应该在 0-99 之间
func Unique ¶ added in v1.5.27
Unique 对集合字段进行唯一验证 1. 对以逗号隔开的字符串进行唯一验证 2. 对切片/数组元素[int 系列, float系列, bool系列, string系列]进行唯一验证
Example ¶
type Tmp struct { Name string `valid:"required|必填,re='[a-z]+'|姓名必须为英文"` Hobby string `valid:"required|必填,unique|爱好唯一"` ClassName []string `valid:"required|必填,unique|班级名唯一"` LikeNum []int `valid:"required|必填,unique|幸运数唯一"` } v := &Tmp{ Name: "测试", Hobby: "打篮球,踢足球,打篮球", ClassName: []string{"美术班", "算术班", "足球班"}, LikeNum: []int{1, 2, 3, 1}, } fmt.Println(Struct(v))
Output: "Tmp.Name" input "测试", 说明: 姓名必须为英文; "Tmp.Hobby" input "打篮球,踢足球,打篮球", 说明: 爱好唯一; "Tmp.LikeNum" input "[1,2,3,1]", 说明: 幸运数唯一
func Url ¶ added in v1.5.29
Url 验证变量
Example ¶
url := "http://test.com?name=test&age=10&nickname=test1" ruleObj := NewRule() ruleObj.Set("name", Required, GenValidKV(VTo, "5~10|姓名需在5-10之间"), GenValidKV(BothEq, "botheq=0")) ruleObj.Set("nickname", Required, GenValidKV(BothEq, "botheq=0")) err := Url(url, ruleObj) fmt.Println(err)
Output: "name" input "test", 说明: 姓名需在5-10之间; "name", "nickname" explain: they should be equal
func UrlForFn ¶ added in v1.5.29
func UrlForFn(src interface{}, validName string, validFn CommonValidFn) error
UrlForFn 验证 url, 同时设置自定义函数
func ValidNamesSplit ¶ added in v1.6.1
ValidNamesSplit 验证点进行分割 会按给定 sep(默认 ",") 进行分割, 如果遇到被 ” 进行包裹的内容会跳过
func ValidStructForMyValidFn
deprecated
added in
v1.4.4
func ValidStructForMyValidFn(src interface{}, validName string, validFn CommonValidFn, targetTag ...string) error
Deprecated: 使用 StructForFns 替换 ValidStructForMyValidFn 自定义单个验证函数
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 } } v := Tmp{Name: "12", Age: "1ha"} fmt.Println(ValidStructForMyValidFn(v, "num", isNumFn))
Output: "Tmp.Age" is not num
func ValidStructForRule
deprecated
Deprecated: 使用 StructForFn 替换 ValidStructForRule 自定义验证规则并验证 注: 通过字段名来匹配规则, 如果嵌套中如果有相同的名的都会走这个规则, 因此建议这种方式推荐使用非嵌套结构体
Example ¶
type Tmp struct { Name string Age int } v := Tmp{Name: "xue", Age: 101} ruleObj := NewRule() if v.Name == "xue" { // "required|必填,le=100|年龄最大为 100" ruleObj.Set("Age", JoinTag2Val(Required, "", "必填"), JoinTag2Val(VLe, "100", "年龄最大为 100")) } if err := ValidStructForRule(ruleObj, &v); err != nil { fmt.Println(err) }
Output: "Tmp.Age" input "101", 说明: 年龄最大为 100
func ValidateStruct
deprecated
func Var ¶ added in v1.5.27
Var 验证变量 支持 单个 [int,float,bool,string] 验证 支持 切片/数组 [int,float,bool,string] 验证时会对对象中的每个值进行验证
Example ¶
err := Var(101, Required, GenValidKV(VTo, "1~100", "年龄1~100")) fmt.Println(err)
Output: input "101", 说明: 年龄1~100
func VarForFn ¶ added in v1.5.29
func VarForFn(src interface{}, validFn CommonValidFn) error
VarForFn 验证变量, 同时设置自定义函数
Types ¶
type CacheEr ¶ added in v1.6.5
type CacheEr interface { Load(key interface{}) (interface{}, bool) Store(key, value interface{}) }
CacheEr 缓存接口
type CommonValidFn ¶ added in v1.4.4
type CommonValidFn func(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)
CommonValidFn 通用验证函数, 主要用于回调 注: 在写 errBuf 的时候建议用 GetJoinValidErrStr 包裹下, 这样产生的结果易读.
否则需要再 errBuf.WriteString 最后要加上 ErrEndFlag 分割, 工具是通过 ErrEndFlag 进行分句
type LRUCache ¶ added in v1.6.5
type LRUCache struct {
// contains filtered or unexported fields
}
func (*LRUCache) SetDelCallBackFn ¶ added in v1.6.15
func (l *LRUCache) SetDelCallBackFn(f func(key, value interface{}))
type Name2FnMap ¶ added in v1.6.10
type Name2FnMap map[string]CommonValidFn
Name2FnMap 自定义验证名对应自定义验证函数
type VMap ¶ added in v1.6.10
type VMap struct {
// contains filtered or unexported fields
}
VMap 验证 map
func (*VMap) SetValidFn ¶ added in v1.6.10
func (v *VMap) SetValidFn(validName string, fn CommonValidFn) *VMap
SetValidFn 自定义设置验证函数
type VStruct ¶ added in v1.5.17
type VStruct struct {
// contains filtered or unexported fields
}
VStruct 验证结构体
func NewVStruct ¶
NewVStruct 验证结构体, 默认目标 tagName 为 "valid"
func (*VStruct) SetRule ¶ added in v1.5.17
SetRule 指定结构体设置验证规则, 不传则验证最外层的结构体 obj 只支持一个参数, 多个无效, 此参数 待验证结构体
func (*VStruct) SetValidFn ¶ added in v1.5.17
func (v *VStruct) SetValidFn(validName string, fn CommonValidFn) *VStruct
SetValidFn 自定义设置验证函数
type VUrl ¶ added in v1.5.29
type VUrl struct {
// contains filtered or unexported fields
}
VUrl 验证 url
func (*VUrl) SetValidFn ¶ added in v1.5.29
func (v *VUrl) SetValidFn(validName string, fn CommonValidFn) *VUrl
SetValidFn 自定义设置验证函数
type VVar ¶ added in v1.5.27
type VVar struct {
// contains filtered or unexported fields
}
VVar 验证单字段
func (*VVar) SetValidFn ¶ added in v1.5.29
func (v *VVar) SetValidFn(validName string, fn CommonValidFn) *VVar
SetValidFn 自定义设置验证函数