valid

package
v1.6.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ExplainEn = "explain:"
	ExplainZh = "说明:"
)

err msg

View Source
const (
	YearFmt int8 = 1 << iota
	MonthFmt
	DayFmt
	HourFmt
	MinFmt
	SecFmt

	DateFmt     = YearFmt | MonthFmt | DayFmt
	DateTimeFmt = DateFmt | HourFmt | MinFmt | SecFmt
)

时间格式

View Source
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

View Source
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}$`)
)

正则

View Source
var (
	ErrEndFlag = "; " // 错误结束符号(每个自定义 err 都需要将这个追加在后面, 用于分句)
)

标记

Functions

func CheckFieldIsStr added in v1.4.8

func CheckFieldIsStr(objName, fieldName string, tv reflect.Value) (err error)

CheckFieldIsStr 验证字段类型是否为字符串

func Date

func Date(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Datetime(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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 Dir added in v1.6.17

func Dir(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

Dir 验证是否为目录

func Email

func Email(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Eq(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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 File added in v1.6.17

func File(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

File 验证是否为文件

func Float

func Float(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Ge(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func GenValidKV(key string, values ...string) string

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

func GetJoinFieldErr(objName, fieldName string, err interface{}) string

GetJoinFieldErr 拼接字段错误

func GetJoinValidErrStr added in v1.3.5

func GetJoinValidErrStr(objName, fieldName, inputVal string, others ...string) string

GetJoinValidErrStr 获取拼接验证的错误消息, 内容直接通过空格隔开, 最后会拼接 ErrEndFlag

func GetOnlyExplainErr added in v1.6.6

func GetOnlyExplainErr(errMsg string) string

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

func GetTimeFmt(fmtType int8, splits ...string) string

GetTimeFmt 获取时间格式化 splits 为分隔符 splits[0] 为 [年月日] 的分割符, 默认为 "-" splits[1] 为 [年月日] 和 [时分秒] 的分割符, 默认为 " " splits[2] 为 [时分秒] 的分割符, 默认为 ":"

func Gt

func Gt(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func IDCard(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func In(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Include(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Int(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Ints(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Ip(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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 Ipv4 added in v1.5.24

func Ipv4(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

Ipv4 ipv4 验证

func Ipv6 added in v1.6.9

func Ipv6(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

Ipv6 ipv6 验证

func IsExported added in v1.5.17

func IsExported(fieldName string) bool

IsExported 是可导出

func JoinTag2Val deprecated added in v1.5.23

func JoinTag2Val(key string, values ...string) string

Deprecated: 名字存在歧义, 因为已上线不能删除, 特此标记, 推荐使用 GenValidKV JoinTag2Val 生成 defaultTargetTag 的值

Example
val := JoinTag2Val(VIn, "1/2/3", "必须在 1,2,3 之中")
fmt.Println(val)
Output:

in=(1/2/3)|必须在 1,2,3 之中

func Json added in v1.6.10

func Json(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Le(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Lt(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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 Map added in v1.6.10

func Map(src interface{}, ruleObj RM) error

Map 验证 map 支持:

key:   string
value: int,float,bool,string

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

func NestedStructForRule(src interface{}, ruleMap map[interface{}]RM) error

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 NewDumpStruct added in v1.3.9

func NewDumpStruct() *dumpStruct

NewDumpStruct

func NoEq added in v1.4.2

func NoEq(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func OTo(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func ParseValidNameKV(validName string) (key, value, cusMsg string)

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

func Phone(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Prefix(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Re(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func ReflectKindIsNum(kind reflect.Kind, isCanFloat ...bool) (is bool)

ReflectKindIsNum 值是否为数字

func RemoveTypePtr added in v1.5.17

func RemoveTypePtr(t reflect.Type) reflect.Type

RemoveTypePtr 移除多指针

func RemoveValuePtr added in v1.5.17

func RemoveValuePtr(t reflect.Value) reflect.Value

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 StrEscape added in v1.6.10

func StrEscape(val string) string

StrEscape 转义

func Struct added in v1.5.27

func Struct(src interface{}, ruleObj ...RM) error

Struct 验证结构体 1. 支持单结构体验证 2. 支持切片/数组类型结构体验证 3. 支持map: key为普通类型, value为结构体 验证

func StructForFn added in v1.5.29

func StructForFn(src interface{}, ruleObj RM, targetTag ...string) error

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

func Suffix(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func To(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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 ToStr added in v1.5.23

func ToStr(src interface{}) string

ToStr 将内容转为 string

func Unique added in v1.5.27

func Unique(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

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

func Url(src interface{}, ruleObj RM) error

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

func ValidNamesSplit(s string, sep ...byte) []string

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

func ValidStructForRule(ruleObj RM, src interface{}, targetTag ...string) error

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 ValidateStruct(src interface{}, targetTag ...string) error

Deprecated: 使用 Struct 替换 ValidateStruct 验证结构体

func Var added in v1.5.27

func Var(src interface{}, rules ...string) error

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 验证变量, 同时设置自定义函数

func Year added in v1.4.6

func Year(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

Year 验证年

func Year2Month added in v1.4.6

func Year2Month(errBuf *strings.Builder, validName, objName, fieldName string, tv reflect.Value)

Year2Month 验证年月 默认匹配 xxxx-xx, 可以指定分割符

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 NewLRU added in v1.6.5

func NewLRU(max ...int) *LRUCache

func (*LRUCache) Delete added in v1.6.15

func (l *LRUCache) Delete(key interface{})

func (*LRUCache) Dump added in v1.6.5

func (l *LRUCache) Dump() string

func (*LRUCache) Len added in v1.6.5

func (l *LRUCache) Len() int

Len 长度 return -1 的话, 长度不正确

func (*LRUCache) Load added in v1.6.5

func (l *LRUCache) Load(key interface{}) (data interface{}, ok bool)

func (*LRUCache) SetDelCallBackFn added in v1.6.15

func (l *LRUCache) SetDelCallBackFn(f func(key, value interface{}))

func (*LRUCache) Store added in v1.6.5

func (l *LRUCache) Store(key, value interface{})

type Name2FnMap added in v1.6.10

type Name2FnMap map[string]CommonValidFn

Name2FnMap 自定义验证名对应自定义验证函数

type RM

type RM map[string]string

RM 字段的自定义验证规则, key 为字段名, value 为验证规则

func NewRule

func NewRule() RM

func (RM) Get

func (r RM) Get(fieldName string) string

Get 获取验证规则

func (RM) Set

func (r RM) Set(filedNames string, rules ...string) RM

Set 设置验证规则 fieldName 多个字段通过逗号隔开 rules 多个字段通过逗号隔开

type VMap added in v1.6.10

type VMap struct {
	// contains filtered or unexported fields
}

VMap 验证 map

func NewVMap added in v1.6.10

func NewVMap() *VMap

NewVMap 验证 map

func (*VMap) SetRule added in v1.6.10

func (v *VMap) SetRule(ruleObj RM) *VMap

SetRule 设置规则

func (*VMap) SetValidFn added in v1.6.10

func (v *VMap) SetValidFn(validName string, fn CommonValidFn) *VMap

SetValidFn 自定义设置验证函数

func (*VMap) Valid added in v1.6.10

func (v *VMap) Valid(src interface{}) error

Valid 验证

key:   string
value: int,float,bool,string

type VStruct added in v1.5.17

type VStruct struct {
	// contains filtered or unexported fields
}

VStruct 验证结构体

func NewVStruct

func NewVStruct(targetTag ...string) *VStruct

NewVStruct 验证结构体, 默认目标 tagName 为 "valid"

func (*VStruct) SetRule added in v1.5.17

func (v *VStruct) SetRule(rule RM, obj ...interface{}) *VStruct

SetRule 指定结构体设置验证规则, 不传则验证最外层的结构体 obj 只支持一个参数, 多个无效, 此参数 待验证结构体

func (*VStruct) SetValidFn added in v1.5.17

func (v *VStruct) SetValidFn(validName string, fn CommonValidFn) *VStruct

SetValidFn 自定义设置验证函数

func (*VStruct) Valid added in v1.5.17

func (v *VStruct) Valid(src interface{}) error

Valid 验证 1. 支持单结构体验证 2. 支持切片/数组类型结构体验证 3. 支持map: key为普通类型, value为结构体 验证

type VUrl added in v1.5.29

type VUrl struct {
	// contains filtered or unexported fields
}

VUrl 验证 url

func NewVUrl added in v1.5.29

func NewVUrl() *VUrl

NewVUrl 验证 url

func (*VUrl) SetRule added in v1.5.29

func (v *VUrl) SetRule(ruleObj RM) *VUrl

SetRule 添加验证规则

func (*VUrl) SetValidFn added in v1.5.29

func (v *VUrl) SetValidFn(validName string, fn CommonValidFn) *VUrl

SetValidFn 自定义设置验证函数

func (*VUrl) Valid added in v1.5.29

func (v *VUrl) Valid(src interface{}) error

Valid 验证

type VVar added in v1.5.27

type VVar struct {
	// contains filtered or unexported fields
}

VVar 验证单字段

func NewVVar added in v1.5.27

func NewVVar() *VVar

NewVVar 单值校验

func (*VVar) SetRules added in v1.5.27

func (v *VVar) SetRules(rules ...string) *VVar

SetRules 设置规则

func (*VVar) SetValidFn added in v1.5.29

func (v *VVar) SetValidFn(validName string, fn CommonValidFn) *VVar

SetValidFn 自定义设置验证函数

func (*VVar) Valid added in v1.5.27

func (v *VVar) Valid(src interface{}) error

Valid 验证 支持 单个 [int,float,bool,string] 验证 支持 切片/数组 [int,float,bool,string] 验证(在使用时, 建议看下 README.md 中对应的验证名所验证的内容)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL