新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame 數(shù)據(jù)校驗-校驗規(guī)則
框架校驗組件內(nèi)置了數(shù)十項常用的校驗規(guī)則。

醴陵ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
校驗規(guī)則涉及到聯(lián)合校驗的場景時,規(guī)則中關(guān)聯(lián)的參數(shù)名稱會自動按照不區(qū)分大小寫且忽略特殊字符的形式進(jìn)行智能匹配。
required
- 格式: ?
required? - 說明:必需參數(shù),除了支持常見的字符串,也支持?
Slice/Map?類型。 - 示例:姓名字段?
Name?為必需參數(shù)必需不能為空。
func Example_Rule_Required() {
type BizReq struct {
ID uint `v:"required"`
Name string `v:"required"`
}
var (
ctx = context.Background()
req = BizReq{
ID: 1,
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The Name field is required
}required-if
- 格式: ?
required-if:field,value,...? - 說明:必需參數(shù)(當(dāng)任意所給定字段值與所給值相等時,即:當(dāng)?
field?字段的值為?value?時,當(dāng)前驗證字段為必須參數(shù))。 - 示例:當(dāng)?
Gender?字段為?1?時?WifeName?字段必須不為空, 當(dāng)?Gender?字段為?2?時?HusbandName?字段必須不為空。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The WifeName field is required
}required-unless
- 格式: ?
required-unless:field,value,...? - 說明:必需參數(shù)(當(dāng)所給定字段值與所給值都不相等時,即:當(dāng)?
field?字段的值不為?value?時,當(dāng)前驗證字段為必須參數(shù))。 - 示例:當(dāng)?
Gender?不等于?0?且?Gender?不等于?2?時,?WifeName?必須不為空;當(dāng)?Id?不等于?0?且?Gender?不等于?2?時,?HusbandName?必須不為空。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The WifeName field is required; The HusbandName field is required
}required-with
- 格式: ?
required-with:field1,field2,...? - 說明:必需參數(shù)(當(dāng)所給定任意字段值不為空時)。
- 示例:當(dāng)?
WifeName?不為空時,?HusbandName?必須不為空。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The HusbandName field is required
}required-with-all
- 格式: ?
required-with-all:field1,field2,...? - 說明:必須參數(shù)(當(dāng)所給定所有字段值都不為空時)。
- 示例:當(dāng)?
Id?,?Name?,?Gender?,?WifeName?全部不為空時,?HusbandName?必須不為空。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The HusbandName field is required
}required-without
- 格式: ?
required-without:field1,field2,...? - 說明:必需參數(shù)(當(dāng)所給定任意字段值為空時)。
- 示例:當(dāng)?
Id?或?WifeName?為空時,?HusbandName?必須不為空。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The HusbandName field is required
}required-without-all
- 格式: ?
required-without-all:field1,field2,...? - 說明:必須參數(shù)(當(dāng)所給定所有字段值都為空時)。
- 示例:當(dāng)?
Id?和?WifeName?都為空時,?HusbandName?必須不為空。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The HusbandName field is required
}bail
- 格式:?
bail? - 說明:只要后續(xù)的多個校驗中有一個規(guī)則校驗失敗則停止校驗立即返回錯誤結(jié)果。
- 示例:
func Example_Rule_Bail() {
type BizReq struct {
Account string `v:"bail|required|length:6,16|same:QQ"`
QQ string
Password string `v:"required|same:Password2"`
Password2 string `v:"required"`
}
var (
ctx = context.Background()
req = BizReq{
Account: "gf",
QQ: "123456",
Password: "GOframe.org",
Password2: "GoFrame.org",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// output:
// The Account value `gf` length must be between 6 and 16
}ci
- 格式: ?
ci? - 說明:對于需要比較值的規(guī)則字段, 不區(qū)分大小寫。如:same, different, in, not-in等。
- 示例:
func Example_Rule_CaseInsensitive() {
type BizReq struct {
Account string `v:"required"`
Password string `v:"required|ci|same:Password2"`
Password2 string `v:"required"`
}
var (
ctx = context.Background()
req = BizReq{
Account: "gf",
Password: "Goframe.org", // Diff from Password2, but because of "ci", rule check passed
Password2: "goframe.org",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// output:
}date
- 格式: ?
date? - 說明:參數(shù)為常用日期類型,日期之間支持的連接符號?
-?或?/?或?.?,也支持不帶連接符號的?8?位長度日期,格式如: ?2006-01-02?, ?2006/01/02?, ?2006.01.02?, ?20060102? - 示例:
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Date3 value `2021-Oct-31` is not a valid date
// The Date4 value `2021 Octa 31` is not a valid date
// The Date5 value `2021/Oct/31` is not a valid date
}datetime
- 格式: ?
datetime? - 說明:參數(shù)為常用日期時間類型,其中日期之間支持的連接符號只支持?
-?,格式如: ?2006-01-02 12:00:00? - 示例:
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Date2 value `2021-11-01 23:00` is not a valid datetime
// The Date3 value `2021/11/01 23:00:00` is not a valid datetime
// The Date4 value `2021/Dec/01 23:00:00` is not a valid datetime
}date-format
- 格式: ?
date-format:format? - 說明:判斷日期是否為指定的日期格式,?
format?參數(shù)格式為?gtime?日期格式(可以包含日期及時間)。 - 示例:?
date-format:Y-m-d H:i:s?
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Date2 value `2021-11-01 23:00` does not match the format: Y-m-d
// The Date4 value `2021-11-01 23:00` does not match the format: Y-m-d H:i:s
}- 格式:email
- 說明:EMAIL郵箱地址
func Example_Rule_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: "[email protected]",
MailAddr2: "gf@goframe", // error
MailAddr3: "[email protected]",
MailAddr4: "gf#goframe.org", // error
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The MailAddr2 value `gf@goframe` is not a valid email address
// The MailAddr4 value `gf#goframe.org` is not a valid email address
}phone
- 格式:?
phone? - 說明:手機(jī)號
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The PhoneNumber2 value `11578912345` is not a valid phone number
// The PhoneNumber3 value `17178912345` is not a valid phone number
// The PhoneNumber4 value `1357891234` is not a valid phone number
}phone-loose
- 格式: ?
phone? - 說明:寬松的手機(jī)號驗證,只要滿足 ?
13、14、15、16、17、18、19?開頭的11位數(shù)字都可以通過驗證。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The PhoneNumber2 value `11578912345` is invalid
// The PhoneNumber4 value `1357891234` is invalid
}telephone
- 格式: ?
telephone? - 說明:國內(nèi)座機(jī)電話號碼,”XXXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”、”XXXXXXXX”
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Telephone3 value `20-77542145` is not a valid telephone number
// The Telephone4 value `775421451` is not a valid telephone number
}passport
- 格式: ?
passport? - 說明:通用帳號規(guī)則(字母開頭,只能包含字母、數(shù)字和下劃線,長度在6~18之間)。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Passport2 value `1356666` is not a valid passport format
// The Passport3 value `goframe#` is not a valid passport format
// The Passport4 value `gf` is not a valid passport format
}password
- 格式: ?
password? - 說明:通用密碼規(guī)則(任意可見字符,長度在6~18之間)。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The Password2 value `gofra` is not a valid password format
}password2
- 格式: ?
password2? - 說明:中等強(qiáng)度密碼(在弱密碼的基礎(chǔ)上,必須包含大小寫字母和數(shù)字)。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Password2 value `gofra` is not a valid password format
// The Password3 value `Goframe` is not a valid password format
// The Password4 value `goframe123` is not a valid password format
}password3
- 格式: ?
password3? - 說明:強(qiáng)等強(qiáng)度密碼(在弱密碼的基礎(chǔ)上,必須包含大小寫字母、數(shù)字和特殊字符)。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Password2 value `gofra` is not a valid password format
// The Password3 value `Goframe123` is not a valid password format
}postcode
- 格式: ?
postcode? - 說明:中國郵政編碼
func Example_Rule_Postcode() {
type BizReq struct {
Postcode1 string `v:"postcode"`
Postcode2 string `v:"postcode"`
Postcode3 string `v:"postcode"`
}
var (
ctx = context.Background()
req = BizReq{
Postcode1: "100000",
Postcode2: "10000", // error length must be 6
Postcode3: "1000000", // error length must be 6
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Postcode2 value `10000` is not a valid postcode format
// The Postcode3 value `1000000` is not a valid postcode format
}resident-id
- 格式: ?
resident-id? - 說明:公民身份證號碼
func Example_Rule_ResidentId() {
type BizReq struct {
ResidentID1 string `v:"resident-id"`
}
var (
ctx = context.Background()
req = BizReq{
ResidentID1: "320107199506285482",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The ResidentID1 value `320107199506285482` is not a valid resident id number
}bank-card
- 格式: ?
bank-card? - 說明:銀行卡號校驗
func Example_Rule_BankCard() {
type BizReq struct {
BankCard1 string `v:"bank-card"`
}
var (
ctx = context.Background()
req = BizReq{
BankCard1: "6225760079930218",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The BankCard1 value `6225760079930218` is not a valid bank card number
}- 格式: ?
qq? - 說明:騰訊QQ號碼
func Example_Rule_QQ() {
type BizReq struct {
QQ1 string `v:"qq"`
QQ2 string `v:"qq"`
QQ3 string `v:"qq"`
}
var (
ctx = context.Background()
req = BizReq{
QQ1: "389961817",
QQ2: "9999", // error >= 10000
QQ3: "514258412a", // error all number
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The QQ2 value `9999` is not a valid QQ number
// The QQ3 value `514258412a` is not a valid QQ number
}ip
- 格式: ?
ip? - 說明:?
IPv4/IPv6?地址
func Example_Rule_IP() {
type BizReq struct {
IP1 string `v:"ip"`
IP2 string `v:"ip"`
IP3 string `v:"ip"`
IP4 string `v:"ip"`
}
var (
ctx = context.Background()
req = BizReq{
IP1: "127.0.0.1",
IP2: "fe80::812b:1158:1f43:f0d1",
IP3: "520.255.255.255", // error >= 10000
IP4: "ze80::812b:1158:1f43:f0d1",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The IP3 value `520.255.255.255` is not a valid IP address
// The IP4 value `ze80::812b:1158:1f43:f0d1` is not a valid IP address
}ipv4
- 格式: ?
ipv4? - 說明:?
IPv4?地址
func Example_Rule_IPV4() {
type BizReq struct {
IP1 string `v:"ipv4"`
IP2 string `v:"ipv4"`
}
var (
ctx = context.Background()
req = BizReq{
IP1: "127.0.0.1",
IP2: "520.255.255.255",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The IP2 value `520.255.255.255` is not a valid IPv4 address
}ipv6
- 格式: ?
ipv6? - 說明:?
IPv6?地址
func Example_Rule_IPV6() {
type BizReq struct {
IP1 string `v:"ipv6"`
IP2 string `v:"ipv6"`
}
var (
ctx = context.Background()
req = BizReq{
IP1: "fe80::812b:1158:1f43:f0d1",
IP2: "ze80::812b:1158:1f43:f0d1",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The IP2 value `ze80::812b:1158:1f43:f0d1` is not a valid IPv6 address
}mac
- 格式: ?
mac? - 說明:?
MAC?地址
func Example_Rule_Mac() {
type BizReq struct {
Mac1 string `v:"mac"`
Mac2 string `v:"mac"`
}
var (
ctx = context.Background()
req = BizReq{
Mac1: "4C-CC-6A-D6-B1-1A",
Mac2: "Z0-CC-6A-D6-B1-1A",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The Mac2 value `Z0-CC-6A-D6-B1-1A` is not a valid MAC address
}url
- 格式: ?
url? - 說明:URL
- 示例:支持以?
http,https,ftp,file?開頭的地址。
func Example_Rule_Url() {
type BizReq struct {
URL1 string `v:"url"`
URL2 string `v:"url"`
URL3 string `v:"url"`
}
var (
ctx = context.Background()
req = BizReq{
URL1: "http://goframe.org",
URL2: "ftp://goframe.org",
URL3: "ws://goframe.org",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The URL3 value `ws://goframe.org` is not a valid URL address
}domain
- 格式: ?
domain? - 說明:域名
- 示例:?
xxx.yyy?(首位必須為字母)
func Example_Rule_Domain() {
type BizReq struct {
Domain1 string `v:"domain"`
Domain2 string `v:"domain"`
Domain3 string `v:"domain"`
Domain4 string `v:"domain"`
}
var (
ctx = context.Background()
req = BizReq{
Domain1: "goframe.org",
Domain2: "a.b",
Domain3: "goframe#org",
Domain4: "1a.2b",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Domain3 value `goframe#org` is not a valid domain format
// The Domain4 value `1a.2b` is not a valid domain format
}size
- 格式: ?
size:size? - 說明:參數(shù)長度為 ?
size(長度參數(shù)為整形),注意底層使用?Unicode?計算長度,因此中文一個漢字占?1?個長度單位。
func Example_Rule_Size() {
type BizReq struct {
Size1 string `v:"size:10"`
Size2 string `v:"size:5"`
}
var (
ctx = context.Background()
req = BizReq{
Size1: "goframe歡迎你",
Size2: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The Size2 value `goframe` length must be 5
}length
- 格式: ?
length:min,max? - 說明:參數(shù)長度為?
min?到?max?(長度參數(shù)為整形),注意底層使用?Unicode?計算長度,因此中文一個漢字占?1?個長度單位。
func Example_Rule_Length() {
type BizReq struct {
Length1 string `v:"length:5,10"`
Length2 string `v:"length:10,15"`
}
var (
ctx = context.Background()
req = BizReq{
Length1: "goframe歡迎你",
Length2: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The Length2 value `goframe` length must be between 10 and 15
}min-length
- 格式: ?
min-length:min? - 說明:參數(shù)長度最小為?
min?(長度參數(shù)為整形),注意底層使用?Unicode?計算長度,因此中文一個漢字占?1?個長度單位。
func Example_Rule_MinLength() {
type BizReq struct {
MinLength1 string `v:"min-length:10"`
MinLength2 string `v:"min-length:8"`
}
var (
ctx = context.Background()
req = BizReq{
MinLength1: "goframe歡迎你",
MinLength2: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The MinLength2 value `goframe` length must be equal or greater than 8
}max-length
- 格式: ?
max-length:max? - 說明:參數(shù)長度最大為?
max?(長度參數(shù)為整形),注意底層使用?Unicode?計算長度,因此中文一個漢字占?1?個長度單位。
func Example_Rule_MaxLength() {
type BizReq struct {
MaxLength1 string `v:"max-length:10"`
MaxLength2 string `v:"max-length:5"`
}
var (
ctx = context.Background()
req = BizReq{
MaxLength1: "goframe歡迎你",
MaxLength2: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The MaxLength2 value `goframe` length must be equal or lesser than 5
}between
- 格式: ?
between:min,max? - 說明:參數(shù)大小為?
min?到?max?(支持整形和浮點類型參數(shù))。
func Example_Rule_Between() {
type BizReq struct {
Age1 int `v:"between:1,100"`
Age2 int `v:"between:1,100"`
Score1 float32 `v:"between:0.0,10.0"`
Score2 float32 `v:"between:0.0,10.0"`
}
var (
ctx = context.Background()
req = BizReq{
Age1: 50,
Age2: 101,
Score1: 9.8,
Score2: -0.5,
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Age2 value `101` must be between 1 and 100
// The Score2 value `-0.5` must be between 0 and 10
}min
- 格式: ?
min:min? - 說明:參數(shù)大小最小為?
min?(支持整形和浮點類型參數(shù))。
func Example_Rule_Min() {
type BizReq struct {
Age1 int `v:"min:100"`
Age2 int `v:"min:100"`
Score1 float32 `v:"min:10.0"`
Score2 float32 `v:"min:10.0"`
}
var (
ctx = context.Background()
req = BizReq{
Age1: 50,
Age2: 101,
Score1: 9.8,
Score2: 10.1,
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Age1 value `50` must be equal or greater than 100
// The Score1 value `9.8` must be equal or greater than 10
}max
- 格式: ?
max:max? - 說明:參數(shù)大小最大為?
max?(支持整形和浮點類型參數(shù))。
func Example_Rule_Max() {
type BizReq struct {
Age1 int `v:"max:100"`
Age2 int `v:"max:100"`
Score1 float32 `v:"max:10.0"`
Score2 float32 `v:"max:10.0"`
}
var (
ctx = context.Background()
req = BizReq{
Age1: 99,
Age2: 101,
Score1: 9.9,
Score2: 10.1,
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Age2 value `101` must be equal or lesser than 100
// The Score2 value `10.1` must be equal or lesser than 10
}json
- 格式: ?
json? - 說明:判斷數(shù)據(jù)格式為?
JSON?
func Example_Rule_Json() {
type BizReq struct {
JSON1 string `v:"json"`
JSON2 string `v:"json"`
}
var (
ctx = context.Background()
req = BizReq{
JSON1: "{\"name\":\"goframe\",\"author\":\"郭強(qiáng)\"}",
JSON2: "{\"name\":\"goframe\",\"author\":\"郭強(qiáng)\",\"test\"}",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The JSON2 value `{"name":"goframe","author":"郭強(qiáng)","test"}` is not a valid JSON string
}integer
- 格式: ?
integer? - 說明:整數(shù)(正整數(shù)或者負(fù)整數(shù))。
func Example_Rule_Integer() {
type BizReq struct {
Integer string `v:"integer"`
Float string `v:"integer"`
Str string `v:"integer"`
}
var (
ctx = context.Background()
req = BizReq{
Integer: "100",
Float: "10.0",
Str: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Float value `10.0` is not an integer
// The Str value `goframe` is not an integer
}float
- 格式: ?
float? - 說明:浮點數(shù)
func Example_Rule_Float() {
type BizReq struct {
Integer string `v:"float"`
Float string `v:"float"`
Str string `v:"float"`
}
var (
ctx = context.Background()
req = BizReq{
Integer: "100",
Float: "10.0",
Str: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(err)
}
// Output:
// The Str value `goframe` is invalid
}boolean
- 格式: ?
boolean? - 說明:布爾值(?
1?,?true?,?on?,?yes?為?true?| ?0?,?false?,?off?,?no?,?""?為?false?)。
func Example_Rule_Boolean() {
type BizReq struct {
Boolean bool `v:"boolean"`
Integer int `v:"boolean"`
Float float32 `v:"boolean"`
Str1 string `v:"boolean"`
Str2 string `v:"boolean"`
Str3 string `v:"boolean"`
}
var (
ctx = context.Background()
req = BizReq{
Boolean: true,
Integer: 1,
Float: 10.0,
Str1: "on",
Str2: "",
Str3: "goframe",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Float value `10` field must be true or false
// The Str3 value `goframe` field must be true or false
}same
- 格式: ?
same:field? - 說明:參數(shù)值必需與?
field?參數(shù)的值相同 - 示例:在用戶注冊時,提交密碼?
Password?和確認(rèn)密碼?Password2?必須相等(服務(wù)端校驗)。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The Password value `goframe.org` must be the same as field Password2
}different
- 格式: ?
different:field? - 說明:參數(shù)值不能與?
field?參數(shù)的值相同 - 示例:備用郵箱?
OtherMailAddr?和郵箱地址?MailAddr?必須不相同。
func Example_Rule_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: "[email protected]",
OtherMailAddr: "[email protected]",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The OtherMailAddr value `[email protected]` must be different from field MailAddr
}in
- 格式: ?
in:value1,value2,...? - 說明:參數(shù)值應(yīng)該在?
?value1?,?value2?,...?中(字符串匹配) - 示例:性別字段?
Gender?的值必須在?0/1/2?中。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The Gender value `3` is not in acceptable range: 0,1,2
}not-in
- 格式: ?
not-in:value1,value2,...? - 說明:參數(shù)值不應(yīng)該在?
value1,value2,...?中(字符串匹配) - 示例:無效索引?
InvalidIndex?的值必須不在?-1/0/1?中。
func Example_Rule_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().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// Output:
// The InvalidIndex value `1` must not be in range: -1,0,1
}regex
- 格式: ?
regex:pattern? - 說明:參數(shù)值應(yīng)當(dāng)滿足正則匹配規(guī)則?
pattern?
func Example_Rule_Regex() {
type BizReq struct {
Regex1 string `v:"regex:[1-9][0-9]{4,14}"`
Regex2 string `v:"regex:[1-9][0-9]{4,14}"`
Regex3 string `v:"regex:[1-9][0-9]{4,14}"`
}
var (
ctx = context.Background()
req = BizReq{
Regex1: "1234",
Regex2: "01234",
Regex3: "10000",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Print(gstr.Join(err.Strings(), "\n"))
}
// Output:
// The Regex1 value `1234` must be in regex of: [1-9][0-9]{4,14}
// The Regex2 value `01234` must be in regex of: [1-9][0-9]{4,14}
} 網(wǎng)頁名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFrame 數(shù)據(jù)校驗-校驗規(guī)則
URL鏈接:http://www.fisionsoft.com.cn/article/cdjjjeo.html


咨詢
建站咨詢
