mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 04:07:47 +08:00
fix Strings/String function for package gvalid, issue:1077
This commit is contained in:
parent
a7f19e9e45
commit
9f5a51e854
@ -21,13 +21,22 @@ import (
|
||||
|
||||
const (
|
||||
// regular expression pattern for single validation rule.
|
||||
gSINGLE_RULE_PATTERN = `^([\w-]+):{0,1}(.*)`
|
||||
singleRulePattern = `^([\w-]+):{0,1}(.*)`
|
||||
invalidRulesErrKey = "invalid_rules"
|
||||
invalidParamsErrKey = "invalid_params"
|
||||
invalidObjectErrKey = "invalid_object"
|
||||
)
|
||||
|
||||
var (
|
||||
// all internal error keys.
|
||||
internalErrKeyMap = map[string]string{
|
||||
invalidRulesErrKey: invalidRulesErrKey,
|
||||
invalidParamsErrKey: invalidParamsErrKey,
|
||||
invalidObjectErrKey: invalidObjectErrKey,
|
||||
}
|
||||
// regular expression object for single rule
|
||||
// which is compiled just once and of repeatable usage.
|
||||
ruleRegex, _ = regexp.Compile(gSINGLE_RULE_PATTERN)
|
||||
ruleRegex, _ = regexp.Compile(singleRulePattern)
|
||||
|
||||
// mustCheckRulesEvenValueEmpty specifies some rules that must be validated
|
||||
// even the value is empty (nil or empty).
|
||||
@ -160,8 +169,8 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
|
||||
ruleItems = append(ruleItems[:i], ruleItems[i+1:]...)
|
||||
} else {
|
||||
return newErrorStr(
|
||||
"invalid_rules",
|
||||
"invalid rules: "+rules,
|
||||
invalidRulesErrKey,
|
||||
invalidRulesErrKey+": "+rules,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@ -273,7 +282,7 @@ func doCheckBuildInRules(
|
||||
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]) {
|
||||
if !gregex.IsMatchString(singleRulePattern, ruleItems[i]) {
|
||||
rulePattern += "|" + ruleItems[i]
|
||||
index++
|
||||
}
|
||||
|
@ -168,13 +168,19 @@ func (e *Error) Strings() (errs []string) {
|
||||
for _, v := range e.rules {
|
||||
name, rule, _ := parseSequenceTag(v)
|
||||
if m, ok := e.errors[name]; ok {
|
||||
// validation error checks.
|
||||
for _, rule := range strings.Split(rule, "|") {
|
||||
array := strings.Split(rule, ":")
|
||||
rule = strings.TrimSpace(array[0])
|
||||
rule = strings.TrimSpace(strings.Split(rule, ":")[0])
|
||||
if err, ok := m[rule]; ok {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
// internal error checks.
|
||||
for k, _ := range internalErrKeyMap {
|
||||
if err, ok := m[k]; ok {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return errs
|
||||
|
@ -7,6 +7,7 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
@ -23,9 +24,9 @@ func Test_Check(t *testing.T) {
|
||||
err1 := gvalid.Check(val1, rule, nil)
|
||||
err2 := gvalid.Check(val2, rule, nil)
|
||||
err3 := gvalid.Check(val3, rule, nil)
|
||||
t.Assert(err1, "invalid rules: abc:6,16")
|
||||
t.Assert(err2, "invalid rules: abc:6,16")
|
||||
t.Assert(err3, "invalid rules: abc:6,16")
|
||||
t.Assert(err1, "invalid_rules: abc:6,16")
|
||||
t.Assert(err2, "invalid_rules: abc:6,16")
|
||||
t.Assert(err3, "invalid_rules: abc:6,16")
|
||||
})
|
||||
}
|
||||
|
||||
@ -907,3 +908,19 @@ func Test_Regex2(t *testing.T) {
|
||||
t.AssertNE(err2.Map()["min-length"], nil)
|
||||
})
|
||||
}
|
||||
|
||||
// issue: https://github.com/gogf/gf/issues/1077
|
||||
func Test_InternalError_String(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type a struct {
|
||||
Name string `v:"hh"`
|
||||
}
|
||||
aa := a{Name: "2"}
|
||||
err := gvalid.CheckStruct(&aa, nil)
|
||||
|
||||
t.Assert(err.String(), "invalid_rules: hh")
|
||||
t.Assert(err.Strings(), g.Slice{"invalid_rules: hh"})
|
||||
t.Assert(err.FirstString(), "invalid_rules: hh")
|
||||
t.Assert(gerror.Current(err), "invalid_rules: hh")
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user