valid

package
v1.5.20 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2022 License: MulanPSL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	Required = "required" // 必填
	Exist    = "exist"    // 有值才验证
	Either   = "either"   // 多个必须一个
	BothEq   = "botheq"   // 两者相等
)

Variables

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=/"`
	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 Datetime

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

Datetime 验证时间 默认匹配 xxxx-xx-xx xx:xx:xx, 可以指定分割符

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(ValidateStruct(v))
Output:

"Tmp.Email" input "xuesongtao512qq.com" 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(ValidateStruct(v))
Output:

"Tmp.Age" input "21" numSize 说明: 年龄应该等于 20; "Tmp.Phone" input "1354004261" strLength should equal 11

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(ValidateStruct(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(ValidateStruct(v))
Output:

"Tmp.Age" input "-1" 说明: 应该大于 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

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

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

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(ValidateStruct(v))
Output:

"Tmp.Name" input "测试" strLength less than or equal 2; "Tmp.Age" input "-1" numSize less than or equal 0

func IDCard

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

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 里选择"`
}
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

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

Include 指定包含什么字符串(模糊匹配)

Example
type Tmp struct {
	SelectStr string `valid:"include=(hello/test)|应该包含 hello/test"`
}
v := &Tmp{SelectStr: "hel"}
fmt.Println(ValidateStruct(v))
Output:

"Tmp.SelectStr" input "hel" 说明: 应该包含 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(ValidateStruct(&v))
Output:

"Tmp.IntString" input "11.121" 说明: 请输入整数类

func IsExported added in v1.5.17

func IsExported(fieldName string) bool

IsExported 是可导出

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(ValidateStruct(v))
Output:

"Tmp.Name" input "测试调" strLength more than 2; "Tmp.Age" input "1" numSize more than 0

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(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

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(ValidateStruct(v))
Output:

"Tmp.Name" input "xue" strLength should no equal 3; "Tmp.Age" input "20" 说明: 年龄不应该等于 20; "Tmp.Score" input "80" numSize should no equal 80

func OTo

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

OTo 验证输入的大小区间, 注: 左右都为开区间 1. 如果为字符串则是验证字符个数 2. 如果是数字的话就验证数字的大小 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(ValidateStruct(v))
Output:

"Tmp.Phone" input "1" 说明: 不是正确的手机号码

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]'|地址必须为中文"`
}

v := &Tmp{
	Name: "测试",
	Age:  "1",
	Addr: "四川成都",
}

fmt.Println(ValidateStruct(v))
Output:

"Tmp.Name" input "测试" 说明: 姓名必须为英文; "Tmp.Age" input "1" 说明: 年龄必须为 2 位数

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)

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

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(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

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

ValidStructForRule 自定义验证规则并验证 注: 通过字段名来匹配规则, 如果嵌套中如果有相同的名的都会走这个规则, 因此建议这种方式推荐使用非嵌套结构体

Example
type Tmp struct {
	Name string
	Age  int
}
v := Tmp{Name: "xue", Age: 101}
ruleObj := NewRule()
if v.Name == "xue" {
	ruleObj.Set("Age", "required|必填,le=100|年龄最大为 100")
}
if err := ValidStructForRule(ruleObj, &v); err != nil {
	fmt.Println(err)
}
Output:

"Tmp.Age" input "101" 说明: 年龄最大为 100

func ValidateStruct

func ValidateStruct(src interface{}, targetTag ...string) error

ValidateStruct 验证结构体

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 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 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 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(ruleObj RM) *VStruct

SetRule 添加验证规则

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 验证

Jump to

Keyboard shortcuts

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