valid

package
v1.4.6 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MulanPSL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Date

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

Date 验证日期

Example
type Tmp struct {
	Year       string `valid:"year"`
	Year2Month string `valid:"year2month"`
	Date       string `valid:"date"`
	Datetime   string `valid:"datetime"`
}
v := &Tmp{Year: "2000", Year2Month: "2000-01", Date: "2021-1", Datetime: "2021-01-11"}
fmt.Println(ValidateStruct(v))
Output:

"Tmp.Date" input "2021-1" is not date, eg: 1996-09-28; "Tmp.Datetime" input "2021-01-11" is not datetime, eg: 1996-09-28 23:00:00

func Datetime

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

Datetime 验证时间

func Email

func Email(errBuf *strings.Builder, validName, objName, filedName 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, filedName string, tv reflect.Value)

Eq 等于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小

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" length should equal 11

func Float

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

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

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

Ge 大于或等于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小

func GetDumpStructStr added in v1.3.9

func GetDumpStructStr(v interface{}) string

GetDumpStructStr 获取待 dump 的结构体字符串, 支持json格式化

func GetJoinValidErrStr added in v1.3.5

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

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

func Gt

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

Gt 大于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小

func IDCard

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

IDCard 验证身份证

func In

func In(errBuf *strings.Builder, validName, objName, filedName 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)"`
}
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

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

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

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

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

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

Le 小于或等于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小

func Lt

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

Lt 小于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小

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

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

NoEq 不等于验证, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小

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" length should no equal 3; "Tmp.Age" input "20" size should no equal 20; "Tmp.Score" input "80" size should no equal 80

func OTo

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

OTo 验证输入的大小区间, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小, 注: 左右都为开区间

func ParseValidNameKV

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

ParseValidNameKV 解析 validName 中的 key 和 value, 如: "required", key 为 "required", value 为 "" 如: "to=1~2", key 为 "to", value 为 "1~2"

func Phone

func Phone(errBuf *strings.Builder, validName, objName, filedName 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" 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, filedName string, tv reflect.Value) {
	ok, _ := regexp.MatchString("^\\d+$", tv.String())
	if !ok {
		errBuf.WriteString(fmt.Sprintf("%q is not num", structName+"."+filedName))
		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, filedName string, tv reflect.Value)

To 验证输入的大小区间, 如果为字符串则是验证字符个数, 如果是数字的话就验证数字的大小, 注: 左右都为闭区间

Example
type Tmp struct {
	Name string `valid:"to=1~3"`
	Age  int32  `valid:"to=0~99"`
	Addr string `valid:"le=3.2"`
}
v := &Tmp{Name: "测试调", Age: 100, Addr: "tets"}
fmt.Println(ValidateStruct(v))
Output:

"Tmp.Age" input "100" size more than or equal 99

func ValidStructForMyValidFn added in v1.4.4

func ValidStructForMyValidFn(in interface{}, validName string, validFn CommonValidFn, targetTag ...string) error

ValidStructForMyValidFn 自定义验证规则

func ValidStructForRule

func ValidStructForRule(ruleMap RM, in interface{}, targetTag ...string) error

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

func ValidateStruct

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

ValidateStruct 验证结构体

func Year added in v1.4.6

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

Year 验证年

func Year2Month added in v1.4.6

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

Year2Month 验证年月

Types

type CommonValidFn added in v1.4.4

type CommonValidFn func(errBuf *strings.Builder, validName, objName, filedName string, tv reflect.Value)

type RM

type RM map[string]string

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

func NewRule

func NewRule() RM

func (RM) Get

func (r RM) Get(filedName string) string

Get 获取验证规则

func (RM) Set

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

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

Jump to

Keyboard shortcuts

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