Compare commits

..

5 Commits

Author SHA1 Message Date
262f27748c improve custom validation rule feature for package gvalid 2020-09-22 08:45:22 +08:00
a29aef7e1c improve package gvalid for custom rule 2020-09-21 23:51:30 +08:00
1671391195 comment updates 2020-09-21 22:56:48 +08:00
28b0d59c61 improve package gcfg/gjson for data loading 2020-09-21 21:30:58 +08:00
86433cef25 improve gtime.New, gconv.String 2020-09-18 23:59:49 +08:00
15 changed files with 573 additions and 335 deletions

View File

@ -207,6 +207,21 @@ func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, er
return doLoadContent(dataType, content, safe...)
}
// IsValidDataType checks and returns whether given <dataType> a valid data type for loading.
func IsValidDataType(dataType string) bool {
if dataType == "" {
return false
}
if dataType[0] == '.' {
dataType = dataType[1:]
}
switch dataType {
case "json", "js", "xml", "yaml", "yml", "toml", "ini":
return true
}
return false
}
// checkDataType automatically checks and returns the data type for <content>.
// Note that it uses regular expression for loose checking, you can use LoadXXX/LoadContentType
// functions to load the content for certain content type.
@ -215,8 +230,9 @@ func checkDataType(content []byte) string {
return "json"
} else if gregex.IsMatch(`^<.+>[\S\s]+<.+>$`, content) {
return "xml"
} else if (gregex.IsMatch(`^[\n\r]*[\w\-\s\t]+\s*:\s*".+"`, content) || gregex.IsMatch(`^[\n\r]*[\w\-\s\t]+\s*:\s*\w+`, content)) ||
(gregex.IsMatch(`[\n\r]+[\w\-\s\t]+\s*:\s*".+"`, content) || gregex.IsMatch(`[\n\r]+[\w\-\s\t]+\s*:\s*\w+`, content)) {
} else if !gregex.IsMatch(`[\n\r]*[\s\t\w\-\."]+\s*=\s*"""[\s\S]+"""`, content) && !gregex.IsMatch(`[\n\r]*[\s\t\w\-\."]+\s*=\s*'''[\s\S]+'''`, content) &&
((gregex.IsMatch(`^[\n\r]*[\w\-\s\t]+\s*:\s*".+"`, content) || gregex.IsMatch(`^[\n\r]*[\w\-\s\t]+\s*:\s*\w+`, content)) ||
(gregex.IsMatch(`[\n\r]+[\w\-\s\t]+\s*:\s*".+"`, content) || gregex.IsMatch(`[\n\r]+[\w\-\s\t]+\s*:\s*\w+`, content))) {
return "yml"
} else if !gregex.IsMatch(`^[\s\t\n\r]*;.+`, content) &&
!gregex.IsMatch(`[\s\t\n\r]+;.+`, content) &&

View File

@ -112,4 +112,22 @@ app_conf = ./config/app.ini
`)
t.Assert(checkDataType(data), "ini")
})
gtest.C(t, func(t *gtest.T) {
data := []byte(`
# API Server
[server]
address = ":8199"
# Jenkins
[jenkins]
url = "https://jenkins-swimlane.com"
nodeJsStaticBuildCmdTpl = """
npm i --registry=https://registry.npm.taobao.org
wget http://consul.infra:8500/v1/kv/app_{{.SwimlaneName}}/{{.RepoName}}/.env.qa?raw=true -O ./env.qa
npm run build:qa
"""
`)
t.Assert(checkDataType(data), "toml")
})
}

View File

@ -334,7 +334,10 @@ func (c *Config) getJson(file ...string) *gjson.Json {
content = ""
filePath = ""
)
// The configured content can be any kind of data type different from its file type.
isFromConfigContent := true
if content = GetContent(name); content == "" {
isFromConfigContent = false
filePath = c.filePath(name)
if filePath == "" {
return nil
@ -346,7 +349,17 @@ func (c *Config) getJson(file ...string) *gjson.Json {
}
}
// Note that the underlying configuration json object operations are concurrent safe.
if j, err := gjson.LoadContent(content, true); err == nil {
var (
j *gjson.Json
err error
)
dataType := gfile.ExtName(name)
if gjson.IsValidDataType(dataType) && !isFromConfigContent {
j, err = gjson.LoadContentType(dataType, content, true)
} else {
j, err = gjson.LoadContent(content, true)
}
if err == nil {
j.SetViolenceCheck(c.vc)
// Add monitor for this configuration file,
// any changes of this file will refresh its cache in Config object.

View File

@ -231,21 +231,16 @@ func Test_SetFileName(t *testing.T) {
func Test_Instance(t *testing.T) {
config := `
{
"array": [
1,
2,
3
],
"redis": {
"cache": "127.0.0.1:6379,1",
"disk": "127.0.0.1:6379,0"
},
"v1": 1,
"v2": "true",
"v3": "off",
"v4": "1.234"
}
array = [1.0, 2.0, 3.0]
v1 = 1.0
v2 = "true"
v3 = "off"
v4 = "1.234"
[redis]
cache = "127.0.0.1:6379,1"
disk = "127.0.0.1:6379,0"
`
gtest.C(t, func(t *gtest.T) {
path := gcfg.DEFAULT_CONFIG_FILE

View File

@ -17,13 +17,21 @@ type Time struct {
TimeWrapper
}
// apiUnixNano is an interface definition commonly for custom time.Time wrapper.
type apiUnixNano interface {
UnixNano() int64
}
// New creates and returns a Time object with given parameter.
// The optional parameter can be type of: time.Time, string or integer.
// The optional parameter can be type of: time.Time/*time.Time, string or integer.
func New(param ...interface{}) *Time {
if len(param) > 0 {
switch r := param[0].(type) {
case time.Time:
r.Nanosecond()
return NewFromTime(r)
case *time.Time:
return NewFromTime(*r)
case string:
return NewFromStr(r)
case []byte:
@ -32,6 +40,10 @@ func New(param ...interface{}) *Time {
return NewFromTimeStamp(int64(r))
case int64:
return NewFromTimeStamp(r)
default:
if v, ok := r.(apiUnixNano); ok {
return NewFromTimeStamp(v.UnixNano())
}
}
}
return &Time{
@ -84,6 +96,7 @@ func NewFromStrLayout(str string, layout string) *Time {
// NewFromTimeStamp creates and returns a Time object with given timestamp,
// which can be in seconds to nanoseconds.
// Eg: 1600443866 and 1600443866199266000 are both considered as valid timestamp number.
func NewFromTimeStamp(timestamp int64) *Time {
if timestamp == 0 {
return &Time{}

View File

@ -381,8 +381,10 @@ func String(i interface{}) string {
return f.Error()
}
// Reflect checks.
rv := reflect.ValueOf(value)
kind := rv.Kind()
var (
rv = reflect.ValueOf(value)
kind = rv.Kind()
)
switch kind {
case reflect.Chan,
reflect.Map,
@ -394,6 +396,8 @@ func String(i interface{}) string {
if rv.IsNil() {
return ""
}
case reflect.String:
return rv.String()
}
if kind == reflect.Ptr {
return String(rv.Elem().Interface())

View File

@ -7,6 +7,7 @@
package gvalid
import (
"errors"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/net/gipv4"
"github.com/gogf/gf/net/gipv6"
@ -124,9 +125,8 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
// It converts value to string and then does the validation.
var (
// Do not trim it as the space is also part of the value.
val = gconv.String(value)
data = make(map[string]string)
errorMsgs = make(map[string]string)
data = make(map[string]string)
errorMsgArray = make(map[string]string)
)
if len(params) > 0 {
for k, v := range gconv.Map(params[0]) {
@ -151,7 +151,8 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
ruleItems := strings.Split(strings.TrimSpace(rules), "|")
for i := 0; ; {
array := strings.Split(ruleItems[i], ":")
if _, ok := allSupportedRules[array[0]]; !ok {
_, ok := allSupportedRules[array[0]]
if !ok && customRuleFuncMap[array[0]] == nil {
if i > 0 && ruleItems[i-1][:5] == "regex" {
ruleItems[i-1] += "|" + ruleItems[i]
ruleItems = append(ruleItems[:i], ruleItems[i+1:]...)
@ -170,303 +171,37 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
}
for index := 0; index < len(ruleItems); {
var (
item = ruleItems[index]
match = false
results = ruleRegex.FindStringSubmatch(item)
ruleKey = strings.TrimSpace(results[1])
ruleVal = strings.TrimSpace(results[2])
err error
item = ruleItems[index]
match = false
results = ruleRegex.FindStringSubmatch(item)
ruleKey = strings.TrimSpace(results[1])
rulePattern = strings.TrimSpace(results[2])
)
if len(msgArray) > index {
customMsgMap[ruleKey] = strings.TrimSpace(msgArray[index])
}
switch ruleKey {
// Required rules.
case
"required",
"required-if",
"required-unless",
"required-with",
"required-with-all",
"required-without",
"required-without-all":
match = checkRequired(val, ruleKey, ruleVal, data)
// Length rules.
// It also supports length of unicode string.
case
"length",
"min-length",
"max-length":
if msg := checkLength(val, ruleKey, ruleVal, customMsgMap); msg != "" {
errorMsgs[ruleKey] = msg
if f, ok := customRuleFuncMap[ruleKey]; ok {
// It checks custom validation rules with most priority.
var (
dataMap map[string]interface{}
message = getErrorMessageByRule(ruleKey, customMsgMap)
)
if len(params) > 0 {
dataMap = gconv.Map(params[0])
}
if err := f(value, message, dataMap); err != nil {
match = false
errorMsgArray[ruleKey] = err.Error()
} else {
match = true
}
// Range rules.
case
"min",
"max",
"between":
if msg := checkRange(val, ruleKey, ruleVal, customMsgMap); msg != "" {
errorMsgs[ruleKey] = msg
} else {
match = true
}
// Custom regular expression.
case "regex":
// It here should check the rule as there might be special char '|' in it.
for i := index + 1; i < len(ruleItems); i++ {
if !gregex.IsMatchString(gSINGLE_RULE_PATTERN, ruleItems[i]) {
ruleVal += "|" + ruleItems[i]
index++
}
}
match = gregex.IsMatchString(ruleVal, val)
// Date rules.
case "date":
// Standard date string, which must contain char '-' or '.'.
if _, err := gtime.StrToTime(val); err == nil {
match = true
break
}
// Date that not contains char '-' or '.'.
if _, err := gtime.StrToTime(val, "Ymd"); err == nil {
match = true
break
}
// Date rule with specified format.
case "date-format":
if _, err := gtime.StrToTimeFormat(val, ruleVal); err == nil {
match = true
} else {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":format", ruleVal, -1)
errorMsgs[ruleKey] = msg
}
// Values of two fields should be equal as string.
case "same":
if v, ok := data[ruleVal]; ok {
if strings.Compare(val, v) == 0 {
match = true
}
}
if !match {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":field", ruleVal, -1)
errorMsgs[ruleKey] = msg
}
// Values of two fields should not be equal as string.
case "different":
match = true
if v, ok := data[ruleVal]; ok {
if strings.Compare(val, v) == 0 {
match = false
}
}
if !match {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":field", ruleVal, -1)
errorMsgs[ruleKey] = msg
}
// Field value should be in range of.
case "in":
array := strings.Split(ruleVal, ",")
for _, v := range array {
if strings.Compare(val, strings.TrimSpace(v)) == 0 {
match = true
break
}
}
// Field value should not be in range of.
case "not-in":
match = true
array := strings.Split(ruleVal, ",")
for _, v := range array {
if strings.Compare(val, strings.TrimSpace(v)) == 0 {
match = false
break
}
}
// Phone format validation.
// 1. China Mobile:
// 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188,
// 178(4G), 147(Net)
//
// 2. China Unicom:
// 130, 131, 132, 155, 156, 185, 186 ,176(4G), 145(Net), 175
//
// 3. China Telecom:
// 133, 153, 180, 181, 189, 177(4G)
//
// 4. Satelite:
// 1349
//
// 5. Virtual:
// 170, 173
//
// 6. 2018:
// 16x, 19x
case "phone":
match = gregex.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`, val)
// Telephone number:
// "XXXX-XXXXXXX"
// "XXXX-XXXXXXXX"
// "XXX-XXXXXXX"
// "XXX-XXXXXXXX"
// "XXXXXXX"
// "XXXXXXXX"
case "telephone":
match = gregex.IsMatchString(`^((\d{3,4})|\d{3,4}-)?\d{7,8}$`, val)
// QQ number: from 10000.
case "qq":
match = gregex.IsMatchString(`^[1-9][0-9]{4,}$`, val)
// Postcode number.
case "postcode":
match = gregex.IsMatchString(`^\d{6}$`, val)
// China resident id number.
//
// xxxxxx yyyy MM dd 375 0 十八位
// xxxxxx yy MM dd 75 0 十五位
//
// 地区: [1-9]\d{5}
// 年的前两位:(18|19|([23]\d)) 1800-2399
// 年的后两位:\d{2}
// 月份: ((0[1-9])|(10|11|12))
// 天数: (([0-2][1-9])|10|20|30|31) 闰年不能禁止29+
//
// 三位顺序码:\d{3}
// 两位顺序码:\d{2}
// 校验码: [0-9Xx]
//
// 十八位:^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
// 十五位:^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$
//
// 总:
// (^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)
case "resident-id":
match = checkResidentId(val)
// Bank card number using LUHN algorithm.
case "bank-card":
match = checkLuHn(val)
// Universal passport format rule:
// Starting with letter, containing only numbers or underscores, length between 6 and 18.
case "passport":
match = gregex.IsMatchString(`^[a-zA-Z]{1}\w{5,17}$`, val)
// Universal password format rule1:
// Containing any visible chars, length between 6 and 18.
case "password":
match = gregex.IsMatchString(`^[\w\S]{6,18}$`, val)
// Universal password format rule2:
// Must meet password rule1, must contain lower and upper letters and numbers.
case "password2":
if gregex.IsMatchString(`^[\w\S]{6,18}$`, val) &&
gregex.IsMatchString(`[a-z]+`, val) &&
gregex.IsMatchString(`[A-Z]+`, val) &&
gregex.IsMatchString(`\d+`, val) {
match = true
}
// Universal password format rule3:
// Must meet password rule1, must contain lower and upper letters, numbers and special chars.
case "password3":
if gregex.IsMatchString(`^[\w\S]{6,18}$`, val) &&
gregex.IsMatchString(`[a-z]+`, val) &&
gregex.IsMatchString(`[A-Z]+`, val) &&
gregex.IsMatchString(`\d+`, val) &&
gregex.IsMatchString(`[^a-zA-Z0-9]+`, val) {
match = true
}
// Json.
case "json":
if _, err := gjson.Decode([]byte(val)); err == nil {
match = true
}
// Integer.
case "integer":
if _, err := strconv.Atoi(val); err == nil {
match = true
}
// Float.
case "float":
if _, err := strconv.ParseFloat(val, 10); err == nil {
match = true
}
// Boolean(1,true,on,yes:true | 0,false,off,no,"":false).
case "boolean":
match = false
if _, ok := boolMap[strings.ToLower(val)]; ok {
match = true
}
// Email.
case "email":
match = gregex.IsMatchString(`^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+$`, val)
// URL
case "url":
match = gregex.IsMatchString(`(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`, val)
// Domain
case "domain":
match = gregex.IsMatchString(`^([0-9a-zA-Z][0-9a-zA-Z\-]{0,62}\.)+([a-zA-Z]{0,62})$`, val)
// IP(IPv4/IPv6).
case "ip":
match = gipv4.Validate(val) || gipv6.Validate(val)
// IPv4.
case "ipv4":
match = gipv4.Validate(val)
// IPv6.
case "ipv6":
match = gipv6.Validate(val)
// MAC.
case "mac":
match = gregex.IsMatchString(`^([0-9A-Fa-f]{2}[\-:]){5}[0-9A-Fa-f]{2}$`, val)
default:
if f, ok := customRuleFuncMap[ruleKey]; ok {
var (
dataMap map[string]interface{}
message = getErrorMessageByRule(ruleKey, customMsgMap)
)
if len(params) > 0 {
dataMap = gconv.Map(params[0])
}
if err := f(value, message, dataMap); err != nil {
match = false
errorMsgs[ruleKey] = err.Error()
} else {
match = true
}
} else {
errorMsgs[ruleKey] = "Invalid rule name: " + ruleKey
} else {
// It checks build-in validation rules if there's no custom rule.
match, err = doCheckBuildInRules(index, value, ruleKey, rulePattern, ruleItems, data, customMsgMap)
if !match && err != nil {
errorMsgArray[ruleKey] = err.Error()
}
}
@ -474,16 +209,303 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
if !match {
// It does nothing if the error message for this rule
// is already set in previous validation.
if _, ok := errorMsgs[ruleKey]; !ok {
errorMsgs[ruleKey] = getErrorMessageByRule(ruleKey, customMsgMap)
if _, ok := errorMsgArray[ruleKey]; !ok {
errorMsgArray[ruleKey] = getErrorMessageByRule(ruleKey, customMsgMap)
}
}
index++
}
if len(errorMsgs) > 0 {
if len(errorMsgArray) > 0 {
return newError([]string{rules}, ErrorMap{
key: errorMsgs,
key: errorMsgArray,
})
}
return nil
}
func doCheckBuildInRules(
index int,
value interface{},
ruleKey string,
rulePattern string,
ruleItems []string,
dataMap map[string]string,
customMsgMap map[string]string,
) (match bool, err error) {
valueStr := gconv.String(value)
switch ruleKey {
// Required rules.
case
"required",
"required-if",
"required-unless",
"required-with",
"required-with-all",
"required-without",
"required-without-all":
match = checkRequired(valueStr, ruleKey, rulePattern, dataMap)
// Length rules.
// It also supports length of unicode string.
case
"length",
"min-length",
"max-length":
if msg := checkLength(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
return match, errors.New(msg)
} else {
match = true
}
// Range rules.
case
"min",
"max",
"between":
if msg := checkRange(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
return match, errors.New(msg)
} else {
match = true
}
// Custom regular expression.
case "regex":
// It here should check the rule as there might be special char '|' in it.
for i := index + 1; i < len(ruleItems); i++ {
if !gregex.IsMatchString(gSINGLE_RULE_PATTERN, ruleItems[i]) {
rulePattern += "|" + ruleItems[i]
index++
}
}
match = gregex.IsMatchString(rulePattern, valueStr)
// Date rules.
case "date":
// Standard date string, which must contain char '-' or '.'.
if _, err := gtime.StrToTime(valueStr); err == nil {
match = true
break
}
// Date that not contains char '-' or '.'.
if _, err := gtime.StrToTime(valueStr, "Ymd"); err == nil {
match = true
break
}
// Date rule with specified format.
case "date-format":
if _, err := gtime.StrToTimeFormat(valueStr, rulePattern); err == nil {
match = true
} else {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":format", rulePattern, -1)
return match, errors.New(msg)
}
// Values of two fields should be equal as string.
case "same":
if v, ok := dataMap[rulePattern]; ok {
if strings.Compare(valueStr, v) == 0 {
match = true
}
}
if !match {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":field", rulePattern, -1)
return match, errors.New(msg)
}
// Values of two fields should not be equal as string.
case "different":
match = true
if v, ok := dataMap[rulePattern]; ok {
if strings.Compare(valueStr, v) == 0 {
match = false
}
}
if !match {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":field", rulePattern, -1)
return match, errors.New(msg)
}
// Field value should be in range of.
case "in":
array := strings.Split(rulePattern, ",")
for _, v := range array {
if strings.Compare(valueStr, strings.TrimSpace(v)) == 0 {
match = true
break
}
}
// Field value should not be in range of.
case "not-in":
match = true
array := strings.Split(rulePattern, ",")
for _, v := range array {
if strings.Compare(valueStr, strings.TrimSpace(v)) == 0 {
match = false
break
}
}
// Phone format validation.
// 1. China Mobile:
// 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188,
// 178(4G), 147(Net)
//
// 2. China Unicom:
// 130, 131, 132, 155, 156, 185, 186 ,176(4G), 145(Net), 175
//
// 3. China Telecom:
// 133, 153, 180, 181, 189, 177(4G)
//
// 4. Satelite:
// 1349
//
// 5. Virtual:
// 170, 173
//
// 6. 2018:
// 16x, 19x
case "phone":
match = gregex.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`, valueStr)
// Telephone number:
// "XXXX-XXXXXXX"
// "XXXX-XXXXXXXX"
// "XXX-XXXXXXX"
// "XXX-XXXXXXXX"
// "XXXXXXX"
// "XXXXXXXX"
case "telephone":
match = gregex.IsMatchString(`^((\d{3,4})|\d{3,4}-)?\d{7,8}$`, valueStr)
// QQ number: from 10000.
case "qq":
match = gregex.IsMatchString(`^[1-9][0-9]{4,}$`, valueStr)
// Postcode number.
case "postcode":
match = gregex.IsMatchString(`^\d{6}$`, valueStr)
// China resident id number.
//
// xxxxxx yyyy MM dd 375 0 十八位
// xxxxxx yy MM dd 75 0 十五位
//
// 地区: [1-9]\d{5}
// 年的前两位:(18|19|([23]\d)) 1800-2399
// 年的后两位:\d{2}
// 月份: ((0[1-9])|(10|11|12))
// 天数: (([0-2][1-9])|10|20|30|31) 闰年不能禁止29+
//
// 三位顺序码:\d{3}
// 两位顺序码:\d{2}
// 校验码: [0-9Xx]
//
// 十八位:^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
// 十五位:^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$
//
// 总:
// (^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)
case "resident-id":
match = checkResidentId(valueStr)
// Bank card number using LUHN algorithm.
case "bank-card":
match = checkLuHn(valueStr)
// Universal passport format rule:
// Starting with letter, containing only numbers or underscores, length between 6 and 18.
case "passport":
match = gregex.IsMatchString(`^[a-zA-Z]{1}\w{5,17}$`, valueStr)
// Universal password format rule1:
// Containing any visible chars, length between 6 and 18.
case "password":
match = gregex.IsMatchString(`^[\w\S]{6,18}$`, valueStr)
// Universal password format rule2:
// Must meet password rule1, must contain lower and upper letters and numbers.
case "password2":
if gregex.IsMatchString(`^[\w\S]{6,18}$`, valueStr) &&
gregex.IsMatchString(`[a-z]+`, valueStr) &&
gregex.IsMatchString(`[A-Z]+`, valueStr) &&
gregex.IsMatchString(`\d+`, valueStr) {
match = true
}
// Universal password format rule3:
// Must meet password rule1, must contain lower and upper letters, numbers and special chars.
case "password3":
if gregex.IsMatchString(`^[\w\S]{6,18}$`, valueStr) &&
gregex.IsMatchString(`[a-z]+`, valueStr) &&
gregex.IsMatchString(`[A-Z]+`, valueStr) &&
gregex.IsMatchString(`\d+`, valueStr) &&
gregex.IsMatchString(`[^a-zA-Z0-9]+`, valueStr) {
match = true
}
// Json.
case "json":
if _, err := gjson.Decode([]byte(valueStr)); err == nil {
match = true
}
// Integer.
case "integer":
if _, err := strconv.Atoi(valueStr); err == nil {
match = true
}
// Float.
case "float":
if _, err := strconv.ParseFloat(valueStr, 10); err == nil {
match = true
}
// Boolean(1,true,on,yes:true | 0,false,off,no,"":false).
case "boolean":
match = false
if _, ok := boolMap[strings.ToLower(valueStr)]; ok {
match = true
}
// Email.
case "email":
match = gregex.IsMatchString(`^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+$`, valueStr)
// URL
case "url":
match = gregex.IsMatchString(`(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`, valueStr)
// Domain
case "domain":
match = gregex.IsMatchString(`^([0-9a-zA-Z][0-9a-zA-Z\-]{0,62}\.)+([a-zA-Z]{0,62})$`, valueStr)
// IP(IPv4/IPv6).
case "ip":
match = gipv4.Validate(valueStr) || gipv6.Validate(valueStr)
// IPv4.
case "ipv4":
match = gipv4.Validate(valueStr)
// IPv6.
case "ipv6":
match = gipv6.Validate(valueStr)
// MAC.
case "mac":
match = gregex.IsMatchString(`^([0-9A-Fa-f]{2}[\-:]){5}[0-9A-Fa-f]{2}$`, valueStr)
default:
return match, errors.New("Invalid rule name: " + ruleKey)
}
return match, nil
}

View File

@ -6,14 +6,11 @@
package gvalid
import (
"fmt"
)
// RuleFunc is the custom function for data validation.
// The parameter <value> specifies the value for this rule to validate.
// The parameter <message> specifies the custom error message or configured i18n message for this rule.
// The parameter <params> specifies all the parameters that needs .
// The parameter <params> specifies all the parameters that needs. You can ignore parameter <params> if
// you do not really need it in your custom validation rule.
type RuleFunc func(value interface{}, message string, params map[string]interface{}) error
var (
@ -24,10 +21,11 @@ var (
// RegisterRule registers custom validation rule and function for package.
// It returns error if there's already the same rule registered previously.
func RegisterRule(rule string, f RuleFunc) error {
if _, ok := allSupportedRules[rule]; ok {
return fmt.Errorf(`validation rule "%s" is already registered`, rule)
}
allSupportedRules[rule] = struct{}{}
customRuleFuncMap[rule] = f
return nil
}
// DeleteRule deletes custom defined validation rule and its function from global package.
func DeleteRule(rule string) {
delete(customRuleFuncMap, rule)
}

View File

@ -49,17 +49,26 @@ func newErrorStr(key, err string) *Error {
// Map returns the first error message as map.
func (e *Error) Map() map[string]string {
if e == nil {
return map[string]string{}
}
_, m := e.FirstItem()
return m
}
// Maps returns all error messages as map.
func (e *Error) Maps() ErrorMap {
if e == nil {
return nil
}
return e.errors
}
// FirstItem returns the field name and error messages for the first validation rule error.
func (e *Error) FirstItem() (key string, messages map[string]string) {
if e == nil {
return "", map[string]string{}
}
if e.firstItem != nil {
return e.firstKey, e.firstItem
}
@ -85,6 +94,9 @@ func (e *Error) FirstItem() (key string, messages map[string]string) {
// FirstRule returns the first error rule and message string.
func (e *Error) FirstRule() (rule string, err string) {
if e == nil {
return "", ""
}
// By sequence.
if len(e.rules) > 0 {
for _, v := range e.rules {
@ -112,22 +124,34 @@ func (e *Error) FirstRule() (rule string, err string) {
// FirstString returns the first error message as string.
// Note that the returned message might be different if it has no sequence.
func (e *Error) FirstString() (err string) {
if e == nil {
return ""
}
_, err = e.FirstRule()
return
}
// String returns all error messages as string, multiple error messages joined using char ';'.
func (e *Error) String() string {
if e == nil {
return ""
}
return strings.Join(e.Strings(), "; ")
}
// Error implements interface of error.Error.
func (e *Error) Error() string {
if e == nil {
return ""
}
return e.String()
}
// Strings returns all error messages as string array.
func (e *Error) Strings() (errs []string) {
if e == nil {
return []string{}
}
errs = make([]string, 0)
// By sequence.
if len(e.rules) > 0 {

View File

@ -57,6 +57,7 @@ var defaultMessages = map[string]string{
"in": "The :attribute value is not in acceptable range",
"not-in": "The :attribute value is not in acceptable range",
"regex": "The :attribute value is invalid",
"__default__": "The :attribute value is invalid",
}
// getErrorMessageByRule retrieves and returns the error message for specified rule.
@ -71,5 +72,12 @@ func getErrorMessageByRule(ruleKey string, customMsgMap map[string]string) strin
if content == "" {
content = defaultMessages[ruleKey]
}
// If there's no configured rule message, it uses default one.
if content == "" {
content = gi18n.GetContent(`gf.gvalid.rule.__default__`)
if content == "" {
content = defaultMessages["__default__"]
}
}
return content
}

View File

@ -16,7 +16,7 @@ import (
"github.com/gogf/gf/util/gvalid"
)
func Test_CustomRule(t *testing.T) {
func Test_CustomRule1(t *testing.T) {
rule := "custom"
err := gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
pass := gconv.String(value)
@ -62,3 +62,47 @@ func Test_CustomRule(t *testing.T) {
t.Assert(err, nil)
})
}
func Test_CustomRule2(t *testing.T) {
rule := "required-map"
err := gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
m := gconv.Map(value)
if len(m) == 0 {
return errors.New(message)
}
return nil
})
gtest.Assert(err, nil)
// Check.
gtest.C(t, func(t *gtest.T) {
errStr := "data map should not be empty"
t.Assert(gvalid.Check(g.Map{}, rule, errStr).String(), errStr)
t.Assert(gvalid.Check(g.Map{"k": "v"}, rule, errStr).String(), nil)
})
// Error with struct validation.
gtest.C(t, func(t *gtest.T) {
type T struct {
Value map[string]string `v:"uid@required-map#自定义错误"`
Data string `p:"data"`
}
st := &T{
Value: map[string]string{},
Data: "123456",
}
err := gvalid.CheckStruct(st, nil)
t.Assert(err.String(), "自定义错误")
})
// No error with struct validation.
gtest.C(t, func(t *gtest.T) {
type T struct {
Value map[string]string `v:"uid@required-map#自定义错误"`
Data string `p:"data"`
}
st := &T{
Value: map[string]string{"k": "v"},
Data: "123456",
}
err := gvalid.CheckStruct(st, nil)
t.Assert(err, nil)
})
}

View File

@ -7,9 +7,14 @@
package gvalid_test
import (
"errors"
"fmt"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gvalid"
"math"
"reflect"
)
func ExampleCheckMap() {
@ -68,9 +73,9 @@ func ExampleCheckStruct() {
Size: 10,
}
err := gvalid.CheckStruct(obj, nil)
fmt.Println(err)
fmt.Println(err == nil)
// Output:
// <nil>
// true
}
// Empty pointer attribute.
@ -85,9 +90,9 @@ func ExampleCheckStruct2() {
Size: 10,
}
err := gvalid.CheckStruct(obj, nil)
fmt.Println(err)
fmt.Println(err == nil)
// Output:
// <nil>
// true
}
// Empty integer attribute.
@ -106,3 +111,79 @@ func ExampleCheckStruct3() {
// Output:
// project id must between 1, 10000
}
func ExampleRegisterRule() {
rule := "unique-name"
gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
var (
id = gconv.Int(params["Id"])
name = gconv.String(value)
)
n, err := g.Table("user").Where("id != ? and name = ?", id, name).Count()
if err != nil {
return err
}
if n > 0 {
return errors.New(message)
}
return nil
})
type User struct {
Id int
Name string `v:"required|unique-name # 请输入用户名称|用户名称已被占用"`
Pass string `v:"required|length:6,18"`
}
user := &User{
Id: 1,
Name: "john",
Pass: "123456",
}
err := gvalid.CheckStruct(user, nil)
fmt.Println(err.Error())
// May Output:
// 用户名称已被占用
}
func ExampleRegisterRule_OverwriteRequired() {
rule := "required"
gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
reflectValue := reflect.ValueOf(value)
if reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
isEmpty := false
switch reflectValue.Kind() {
case reflect.Bool:
isEmpty = !reflectValue.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
isEmpty = reflectValue.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
isEmpty = reflectValue.Uint() == 0
case reflect.Float32, reflect.Float64:
isEmpty = math.Float64bits(reflectValue.Float()) == 0
case reflect.Complex64, reflect.Complex128:
c := reflectValue.Complex()
isEmpty = math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
case reflect.String, reflect.Map, reflect.Array, reflect.Slice:
isEmpty = reflectValue.Len() == 0
}
if isEmpty {
return errors.New(message)
}
return nil
})
fmt.Println(gvalid.Check("", "required", "It's required"))
fmt.Println(gvalid.Check([]string{}, "required", "It's required"))
fmt.Println(gvalid.Check(map[string]int{}, "required", "It's required"))
gvalid.DeleteRule(rule)
fmt.Println("rule deleted")
fmt.Println(gvalid.Check("", "required", "It's required"))
fmt.Println(gvalid.Check([]string{}, "required", "It's required"))
fmt.Println(gvalid.Check(map[string]int{}, "required", "It's required"))
// Output:
// It's required
// It's required
// It's required
// rule deleted
// It's required
}

View File

@ -40,4 +40,5 @@
"gf.gvalid.rule.different" = ":attribute 字段值不能与:field相同"
"gf.gvalid.rule.in" = ":attribute 字段值不合法"
"gf.gvalid.rule.not-in" = ":attribute 字段值不合法"
"gf.gvalid.rule.regex" = ":attribute 字段值不合法"
"gf.gvalid.rule.regex" = ":attribute 字段值不合法"
"gf.gvalid.rule.__default__" = ":attribute 字段值不合法"

View File

@ -40,4 +40,5 @@
"gf.gvalid.rule.different" = "The :attribute value must be different from field :field"
"gf.gvalid.rule.in" = "The :attribute value is not in acceptable range"
"gf.gvalid.rule.not-in" = "The :attribute value is not in acceptable range"
"gf.gvalid.rule.regex" = "The :attribute value is invalid"
"gf.gvalid.rule.regex" = "The :attribute value is invalid"
"gf.gvalid.rule.__default__" = "The :attribute value is invalid"

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v1.13.6"
const VERSION = "v1.13.7"
const AUTHORS = "john<john@goframe.org>"