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 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 GenValidKV(key string, values ...string) string
- func GetDumpStructStr(v interface{}) string
- func GetDumpStructStrForJson(v interface{}) string
- func GetJoinFieldErr(objName, fieldName string, err error) string
- func GetJoinValidErrStr(objName, fieldName, inputVal string, others ...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 IsExported(fieldName string) bool
- func JoinTag2Val(key string, values ...string) string
- func Le(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func Lt(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- 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 Re(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ReflectKindIsNum(kind reflect.Kind) (is bool)
- func RemoveTypePtr(t reflect.Type) reflect.Type
- func RemoveValuePtr(t reflect.Value) reflect.Value
- func SetCustomerValidFn(validName string, fn CommonValidFn)
- func To(errBuf *strings.Builder, validName, objName, fieldName string, ...)
- func ToStr(src interface{}) 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
- type VStruct
Examples ¶
Constants ¶
const ( // err msg 前缀 ExplainEn = "explain:" ExplainZh = "说明:" )
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" // 正则 )
验证 tag
Variables ¶
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=/"` Datetime string `valid:"datetime|应该为 xxxx-xx-xx xx:xx:xx 的时间格式"` } 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", 说明: 应该为 xxxx-xx-xx xx:xx:xx 的时间格式
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", 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(ValidateStruct(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(ValidateStruct(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(ValidateStruct(v))
Output: "Tmp.Age" input "-1", 说明: 应该大于 0
func GenValidKV ¶ added in v1.5.23
如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 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 "测试", 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 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 里选择"` } v := &Tmp{SelectNum: 1, SelectStr: "ac"} fmt.Println(ValidateStruct(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(ValidateStruct(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(ValidateStruct(&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(ValidateStruct(&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 JoinTag2Val ¶ added in v1.5.23
JoinTag2Val 生成 defaultTargetTag 的值 Deprecated 名字存在歧义, 因为已上线不能删除, 特此标记, 推荐使用 GenValidKV
Example ¶
val := JoinTag2Val(VIn, "1/2/3", "必须在 1,2,3 之中") fmt.Println(val)
Output: in=(1/2/3)|必须在 1,2,3 之中
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 "测试调", 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(ValidateStruct(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 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(ValidateStruct(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 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(ValidateStruct(v))
Output: "Tmp.Phone" input "1", explain: it is not phone
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]'|地址必须为中文"` } v := &Tmp{ Name: "测试", Age: "1", Addr: "四川成都", } fmt.Println(ValidateStruct(v))
Output: "Tmp.Name" input "测试", 说明: 姓名必须为英文; "Tmp.Age" input "1", 说明: 年龄必须为 2 位数
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 自定义验证函数 Deprecated 此函数会修改全局变量, 会导致内存释放不了, 此推荐 *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(ValidateStruct(&v))
Output: "Tmp.Age" is not num
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(ValidateStruct(v))
Output: "Tmp.Name" input "测试调1", 说明: 姓名长度为 1-3 个字符; "Tmp.Age" input "100", 说明: 年龄应该在 0-99 之间
func ValidStructForMyValidFn ¶ added in v1.4.4
func ValidStructForMyValidFn(src interface{}, validName string, validFn CommonValidFn, targetTag ...string) error
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 ¶
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 ¶
ValidateStruct 验证结构体
Types ¶
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 VStruct ¶ added in v1.5.17
type VStruct struct {
// contains filtered or unexported fields
}
VStruct 验证结构体
func NewVStruct ¶
NewVStruct 验证结构体, 默认目标 tagName 为 "valid"
func (*VStruct) SetValidFn ¶ added in v1.5.17
func (v *VStruct) SetValidFn(validName string, fn CommonValidFn) *VStruct
SetValidFn 自定义设置验证函数