Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

框架校验组件内置了40+左右常用的校验规则。

Note

校验规则涉及到联合校验的场景时,规则中关联的参数名称会自动按照不区分大小写且忽略特殊字符的形式进行智能匹配。

required

  • 格式: required
  • 说明:必需参数,除了支持常见的字符串,也支持Slice/Map类型。
  • 示例:姓名字段Name为必需参数必需不能为空。

    Code Block
    languagego
    func ExampleValidator_Required() {
    	type BizReq struct {
    		Id   uint   `v:"required"`
    		Name string `v:"required"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id: 1,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The Name field is required
    }


required-if

  • 格式: required-if:field,value,...
  • 说明:必需参数(当任意所给定字段值与所给值相等时,即:当field字段的值为value时,当前验证字段为必须参数)。
  • 示例:当Gender字段为1时WifeName字段必须不为空, 当Gender字段为2时HusbandName字段必须不为空

    Code Block
    languagego
    func ExampleValidator_RequiredIf()  {
    	type BizReq struct {
    		Id          uint   `v:"required" dc:"Your Id"`
    		Name        string `v:"required" dc:"Your name"`
    		Gender      uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    		WifeName    string `v:"required-if:gender,1"`
    		HusbandName string `v:"required-if:gender,2"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     1,
    			Name:   "test",
    			Gender: 1,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The WifeName field is required
    }


required-unless

  • 格式: required-unless:field,value,...
  • 说明:必需参数(当所给定字段值与所给值都不相等时,即:当field字段的值不为value时,当前验证字段为必须参数)。
  • 示例:当Gender不等于0且Gender不等于2时,WifeName必须不为空
  •            当Id不等于0且Gender不等于2时,HusbandName必须不为空

    Code Block
    languagego
    func ExampleValidator_RequiredUnless()  {
    	type BizReq struct {
    		Id       	uint   `v:"required" dc:"Your Id"`
    		Name     	string `v:"required" dc:"Your name"`
    		Gender   	uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    		WifeName 	string `v:"required-unless:gender,0,gender,2"`
    		HusbandName string `v:"required-unless:id,0,gender,2"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     1,
    			Name:   "test",
    			Gender: 1,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The WifeName field is required; The HusbandName field is required
    }


required-with

  • 格式: required-with:field1,field2,...
  • 说明:必需参数(当所给定任意字段值不为空时)。
  • 示例:当WifeName不为空时,HusbandName必须不为空


    Code Block
    languagego
    func ExampleValidator_RequiredWith()  {
    	type BizReq struct {
    		Id       	uint   `v:"required" dc:"Your Id"`
    		Name     	string `v:"required" dc:"Your name"`
    		Gender   	uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    		WifeName 	string
    		HusbandName string `v:"required-with:WifeName"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     	1,
    			Name:   	"test",
    			Gender:		1,
    			WifeName:	"Ann",
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The HusbandName field is required
    }


required-with-all

  • 格式: required-with-all:field1,field2,...
  • 说明:必须参数(当所给定所有字段值都不为空时)。
  • 示例:当Id,Name,Gender,WifeName全部不为空时,HusbandName必须不为空


    Code Block
    languagego
    func ExampleValidator_RequiredWithAll()  {
    	type BizReq struct {
    		Id       	uint   `v:"required" dc:"Your Id"`
    		Name     	string `v:"required" dc:"Your name"`
    		Gender   	uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    		WifeName 	string
    		HusbandName string `v:"required-with-all:Id,Name,Gender,WifeName"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     	1,
    			Name:   	"test",
    			Gender:		1,
    			WifeName:	"Ann",
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The HusbandName field is required
    }


required-without

  • 格式: required-without:field1,field2,...
  • 说明:必需参数(当所给定任意字段值为空时)。
  • 示例:当IdWifeName为空时,HusbandName必须不为空


    Code Block
    languagego
    func ExampleValidator_RequiredWithout()  {
    	type BizReq struct {
    		Id       	uint   `v:"required" dc:"Your Id"`
    		Name     	string `v:"required" dc:"Your name"`
    		Gender   	uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    		WifeName 	string
    		HusbandName string `v:"required-without:Id,WifeName"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     	1,
    			Name:   	"test",
    			Gender:		1,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The HusbandName field is required
    }


required-without-all

  • 格式: required-without-all:field1,field2,...
  • 说明:必须参数(当所给定所有字段值都为空时)。
  • 示例:当IdWifeName都为空时,HusbandName必须不为空


    Code Block
    languagego
    func ExampleValidator_RequiredWithoutAll()  {
    	type BizReq struct {
    		Id       	uint   `v:"required" dc:"Your Id"`
    		Name     	string `v:"required" dc:"Your name"`
    		Gender   	uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    		WifeName 	string
    		HusbandName string `v:"required-without-all:Id,WifeName"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Name:   	"test",
    			Gender:		1,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The HusbandName field is required
    }


date

  • 格式: date
  • 说明:参数为常用日期类型,日期之间支持的连接符号-/.,也支持不带连接符号的8位长度日期,格式如: 2006-01-02, 2006/01/02, 2006.01.02, 20060102
  • 示例:

    Code Block
    languagego
    func ExampleValidator_Date() {
    	type BizReq struct {
    		Date1 string `v:"date"`
    		Date2 string `v:"date"`
    		Date3 string `v:"date"`
    		Date4 string `v:"date"`
    		Date5 string `v:"date"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Date1: "2021-10-31",
    			Date2: "2021.10.31",
    			Date3: "2021-Oct-31",
    			Date4: "2021 Octa 31",
    			Date5: "2021/Oct/31",
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Date3 value is not a valid date; The Date4 value is not a valid date; The Date5 value is not a valid date
    }


datetime

  • 格式: datetime
  • 说明:参数为常用日期时间类型,其中日期之间支持的连接符号只支持-,格式如: 2006-01-02 12:00:00
  • 示例:

    Code Block
    languagego
    func ExampleValidator_Datetime() {
    	type BizReq struct {
    		Date1 string `v:"datetime"`
    		Date2 string `v:"datetime"`
    		Date3 string `v:"datetime"`
    		Date4 string `v:"datetime"`
    	}
    
     	var (
    		ctx = context.Background()
    		req = BizReq{
    			Date1: "2021-11-01 23:00:00",
    			Date2: "2021-11-01 23:00",		// error
    			Date3: "2021/11/01 23:00:00",	// error
    			Date4: "2021/Dec/01 23:00:00",	// error
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Date2 value is not a valid datetime; The Date3 value is not a valid datetime; The Date4 value is not a valid datetime
    }



date-format

  • 格式: date-format:format
  • 说明:判断日期是否为指定的日期格式,format参数格式为gtime日期格式(可以包含日期及时间),格式说明参考章节:gtime模块
  • 示例:date-format:Y-m-d H:i:s

  • Code Block
    languagego
    func ExampleValidator_DateFormat() {
    	type BizReq struct {
    		Date1 string `v:"date-format:Y-m-d"`
    		Date2 string `v:"date-format:Y-m-d"`
    		Date3 string `v:"date-format:Y-m-d H:i:s"`
    		Date4 string `v:"date-format:Y-m-d H:i:s"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Date1: "2021-11-01",
    			Date2: "2021-11-01 23:00",		// error
    			Date3: "2021-11-01 23:00:00",
    			Date4: "2021-11-01 23:00",		// error
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Date2 value does not match the format Y-m-d; The Date4 value does not match the format Y-m-d H:i:s
    }



email

  • 格式: email
  • 说明:EMAIL邮箱地址

  • Code Block
    languagego
    func ExampleValidator_Email() {
    	type BizReq struct {
    		MailAddr1 string `v:"email"`
    		MailAddr2 string `v:"email"`
    		MailAddr3 string `v:"email"`
    		MailAddr4 string `v:"email"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			MailAddr1: "gf@goframe.org",
    			MailAddr2: "gf@goframe",		// error
    			MailAddr3: "gf@goframe.org.cn",
    			MailAddr4: "gf#goframe.org",	// error
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The MailAddr2 value must be a valid email address; The MailAddr4 value must be a valid email address
    }


phone

  • 格式: phone
  • 说明:手机号

  • Code Block
    languagego
    func ExampleValidator_Phone() {
    	type BizReq struct {
    		PhoneNumber1 string `v:"phone"`
    		PhoneNumber2 string `v:"phone"`
    		PhoneNumber3 string `v:"phone"`
    		PhoneNumber4 string `v:"phone"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			PhoneNumber1: "13578912345",
    			PhoneNumber2: "11578912345",		// error 11x not exist
    			PhoneNumber3: "17178912345",		// error 171 not exit
    			PhoneNumber4: "1357891234",			// error len must be 11
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The PhoneNumber2 value must be a valid phone number; The PhoneNumber3 value must be a valid phone number; The PhoneNumber4 value must be a valid phone number
    }


phone-loose

  • 格式: phone
  • 说明:宽松的手机号验证,只要满足 13、14、15、16、17、18、19开头的11位数字都可以通过验证。

  • Code Block
    languagego
    func ExampleValidator_PhoneLoose() {
    	type BizReq struct {
    		PhoneNumber1 string `v:"phone-loose"`
    		PhoneNumber2 string `v:"phone-loose"`
    		PhoneNumber3 string `v:"phone-loose"`
    		PhoneNumber4 string `v:"phone-loose"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			PhoneNumber1: "13578912345",
    			PhoneNumber2: "11578912345",		// error 11x not exist
    			PhoneNumber3: "17178912345",
    			PhoneNumber4: "1357891234",			// error len must be 11
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The PhoneNumber2 value must be a valid phone number; The PhoneNumber4 value must be a valid phone number
    }


telephone

  • 格式: telephone
  • 说明:国内座机电话号码,”XXXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”、”XXXXXXXX”

  • Code Block
    languagego
    func ExampleValidator_Telephone() {
    	type BizReq struct {
    		Telephone1 string `v:"telephone"`
    		Telephone2 string `v:"telephone"`
    		Telephone3 string `v:"telephone"`
    		Telephone4 string `v:"telephone"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Telephone1: "010-77542145",
    			Telephone2: "0571-77542145",
    			Telephone3: "20-77542145",			// error
    			Telephone4: "775421451",			// error len must be 7 or 8
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Telephone3 value must be a valid telephone number; The Telephone4 value must be a valid telephone number
    }


passport

  • 格式: passport
  • 说明:通用帐号规则(字母开头,只能包含字母、数字和下划线,长度在6~18之间)

password

  • 格式: password
  • 说明:通用密码(任意可见字符,长度在6~18之间)

password2

  • 格式: password2
  • 说明:中等强度密码(在弱密码的基础上,必须包含大小写字母和数字)

password3


  • Code Block
    languagego
    func ExampleValidator_Passport() {
    	type BizReq struct {
    		Passport1 string `v:"passport"`
    		Passport2 string `v:"passport"`
    		Passport3 string `v:"passport"`
    		Passport4 string `v:"passport"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Passport1: "goframe",
    			Passport2: "1356666",		// error starting with letter
    			Passport3: "goframe#", 		// error containing only numbers or underscores
    			Passport4: "gf",   			// error length between 6 and 18
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Passport2 value is not a valid passport format; The Passport3 value is not a valid passport format; The Passport4 value is not a valid passport format
    }


password

  • 格式: password
  • 说明:通用密码(任意可见字符,长度在6~18之间)

  • Code Block
    languagego
    func ExampleValidator_Password() {
    	type BizReq struct {
    		Password1 string `v:"password"`
    		Password2 string `v:"password"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Password1: "goframe",
    			Password2: "gofra",   			// error length between 6 and 18
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Password2 value is not a valid passport format
    }


password2

  • 格式: password2
  • 说明:中等强度密码(在弱密码的基础上,必须包含大小写字母和数字)

  • Code Block
    languagego
    func ExampleValidator_Password2() {
    	type BizReq struct {
    		Password1 string `v:"password2"`
    		Password2 string `v:"password2"`
    		Password3 string `v:"password2"`
    		Password4 string `v:"password2"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Password1: "Goframe123",
    			Password2: "gofra",   			// error length between 6 and 18
    			Password3: "Goframe",			// error must contain lower and upper letters and numbers.
    			Password4: "goframe123",		// error must contain lower and upper letters and numbers.
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Password2 value is not a valid passport format; The Password3 value is not a valid passport format; The Password4 value is not a valid passport format
    }


password3

  • 格式: password3
  • 说明:强等强度密码(在弱密码的基础上,必须包含大小写字母、数字和特殊字符)

  • Code Block
    languagego
    func ExampleValidator_Password3() {
    	type BizReq struct {
    		Password1 string `v:"password3"`
    		Password2 string `v:"password3"`
    		Password3 string `v:"password3"`
    	}
    
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Password1: "Goframe123#",
    			Password2: "gofra",   			// error length between 6 and 18
    			Password3: "Goframe123",			// error must contain lower and upper letters, numbers and special chars.
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Print(err)
    	}
    
    	// Output:
    	// The Password2 value is not a valid passport format; The Password3 value is not a valid passport format
    }
  • 格式: password3
  • 说明:强等强度密码(在弱密码的基础上,必须包含大小写字母、数字和特殊字符)


postcode

  • 格式: postcode
  • 说明:中国邮政编码

resident-id

  • 格式:  resident-id
  • 说明:公民身份证号码

bank-card

  • 格式:   bank-card
  • 说明:银行卡号校验

qq

  • 格式: qq
  • 说明:腾讯QQ号码

ip

  • 格式: ip
  • 说明:IPv4/IPv6地址

ipv4

  • 格式: ipv4
  • 说明:IPv4地址

ipv6

  • 格式: ipv6
  • 说明:IPv6地址

mac

  • 格式: mac
  • 说明:MAC地址

url

  • 格式: url
  • 说明:URL

domain

  • 格式: domain
  • 说明:域名

size

  • 格式: size:size
  • 说明:参数长度 size (长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位

length

  • 格式: length:min,max
  • 说明:参数长度minmax(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位

min-length

  • 格式: min-length:min
  • 说明:参数长度最小为min(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位

max-length

  • 格式: max-length:max
  • 说明:参数长度最大为max(长度参数为整形),注意底层使用Unicode计算长度,因此中文一个汉字占1个长度单位

between

  • 格式: between:min,max
  • 说明:参数大小minmax(支持整形和浮点类型参数)

min

  • 格式: min:min
  • 说明:参数大小最小为min(支持整形和浮点类型参数)

max

  • 格式: max:max
  • 说明:参数大小最大为max(支持整形和浮点类型参数)

json

  • 格式: json
  • 说明:判断数据格式为JSON

integer

  • 格式: integer
  • 说明:整数

float

  • 格式: float
  • 说明:浮点数

boolean

  • 格式: boolean
  • 说明:布尔值(1,true,on,yestrue | 0,false,off,no,""false)

same

  • 格式: same:field
  • 说明:参数值必需与field参数的值相同
  • 示例:在用户注册时,提交密码Password和确认密码Password2必须相等(服务端校验)。

    Code Block
    languagego
    func ExampleValidator_Same()  {
    	type BizReq struct {
    		Name      string `v:"required"`
    		Password  string `v:"required|same:Password2"`
    		Password2 string `v:"required"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Name:      "gf",
    			Password:  "goframe.org",
    			Password2: "goframe.net",
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The Password value must be the same as field Password2
    }


different

  • 格式: different:field
  • 说明:参数值不能与field参数的值相同
  • 示例:备用邮箱OtherMailAddr和邮箱地址MailAddr必须不相同。

    Code Block
    languagego
    func ExampleValidator_Different()  {
    	type BizReq struct {
    		Name        	string 	`v:"required"`
    		MailAddr		string 	`v:"required"`
    		OtherMailAddr	string 	`v:"required|different:MailAddr"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Name:   		"gf",
    			MailAddr:		"gf@goframe.org",
    			OtherMailAddr:	"gf@goframe.org",
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The OtherMailAddr value must be different from field MailAddr
    }


in

  • 格式: in:value1,value2,...
  • 说明:参数值应该在value1,value2,...中(字符串匹配)
  • 示例:性别字段Gender的值必须在0/1/2中。

    Code Block
    languagego
    func ExampleValidator_In()  {
    	type BizReq struct {
    		Id          uint   `v:"required" dc:"Your Id"`
    		Name        string `v:"required" dc:"Your name"`
    		Gender      uint   `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     1,
    			Name:   "test",
    			Gender: 3,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The Gender value is not in acceptable range
    }


not-in

  • 格式: not-in:value1,value2,...
  • 说明:参数值不应该在value1,value2,...中(字符串匹配)
  • 示例:无效索引InvalidIndex的值必须不在-1/0/1


    Code Block
    languagego
    func ExampleValidator_NotIn()  {
    	type BizReq struct {
    		Id          	uint   `v:"required" dc:"Your Id"`
    		Name        	string `v:"required" dc:"Your name"`
    		InvalidIndex	uint   `v:"not-in:-1,0,1"`
    	}
    	var (
    		ctx = context.Background()
    		req = BizReq{
    			Id:     1,
    			Name:   "test",
    			InvalidIndex: 1,
    		}
    	)
    	if err := g.Validator().CheckStruct(ctx, req); err != nil {
    		fmt.Println(err)
    	}
    
    	// Output:
    	// The InvalidIndex value is not in acceptable range
    }


regex

  • 格式: regex:pattern
  • 说明:参数值应当满足正则匹配规则pattern





Panel
titleContent Menu

Table of Contents