This commit is contained in:
John
2020-06-15 16:48:17 +08:00
8 changed files with 16 additions and 14 deletions

View File

@ -42,7 +42,7 @@ golang version >= 1.11
# Architecture
<div align=center>
<img src="https://goframe.org/images/arch.png?v=11"/>
<img src="https://goframe.org/images/arch.png?v=12"/>
</div>
# Performance

View File

@ -58,7 +58,7 @@ golang版本 >= 1.11
# 架构
<div align=center>
<img src="https://goframe.org/images/arch.png?v=11"/>
<img src="https://goframe.org/images/arch.png?v=12"/>
</div>
# 性能

View File

@ -183,8 +183,8 @@ func Test_Params_Struct(t *testing.T) {
t.Assert(client.PostContent("/struct1", `id=1&name=john&password1=123&password2=456`), `1john123456`)
t.Assert(client.PostContent("/struct2", `id=1&name=john&password1=123&password2=456`), `1john123456`)
t.Assert(client.PostContent("/struct2", ``), ``)
t.Assert(client.PostContent("/struct-valid", `id=1&name=john&password1=123&password2=0`), `The value length must be between 2 and 20; 密码强度不足`)
t.Assert(client.PostContent("/parse", `id=1&name=john&password1=123&password2=0`), `The value length must be between 2 and 20; 密码强度不足`)
t.Assert(client.PostContent("/struct-valid", `id=1&name=john&password1=123&password2=0`), `The passwd1 value length must be between 2 and 20; 密码强度不足`)
t.Assert(client.PostContent("/parse", `id=1&name=john&password1=123&password2=0`), `The passwd1 value length must be between 2 and 20; 密码强度不足`)
t.Assert(client.GetContent("/parse", `id=1&name=john&password1=123&password2=456`), `密码强度不足`)
t.Assert(client.GetContent("/parse", `id=1&name=john&password1=123Abc!@#&password2=123Abc!@#`), `1john123Abc!@#123Abc!@#`)
t.Assert(client.PostContent("/parse", `{"id":1,"name":"john","password1":"123Abc!@#","password2":"123Abc!@#"}`), `1john123Abc!@#123Abc!@#`)

View File

@ -103,7 +103,7 @@ var (
)
// Check checks single value with specified rules.
// It returns nil if with successful validation.
// It returns nil if successful validation.
//
// The parameter <value> can be any type of variable, which will be converted to string
// for validation.
@ -113,6 +113,11 @@ var (
// The optional parameter <params> specifies the extra validation parameters for some rules
// like: required-*、same、different, etc.
func Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
return doCheck("", value, rules, messages, params...)
}
// doCheck does the really rules validation for single key-value.
func doCheck(key string, value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
if rules == "" {
return nil
}
@ -453,8 +458,7 @@ func Check(value interface{}, rules string, messages interface{}, params ...inte
}
if len(errorMsgs) > 0 {
return newError([]string{rules}, ErrorMap{
// Single rule validation has no key.
"": errorMsgs,
key: errorMsgs,
})
}
return nil

View File

@ -87,11 +87,11 @@ func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Err
if v, ok := data[key]; ok {
value = v
}
if e := Check(value, rule, customMsgs[key], data); e != nil {
if e := doCheck(key, value, rule, customMsgs[key], data); e != nil {
_, item := e.FirstItem()
// If value is nil or empty string and has no required* rules,
// clear the error message.
if value == nil || gconv.String(value) == "" {
if gconv.String(value) == "" {
required := false
// rule => error
for k := range item {

View File

@ -149,7 +149,7 @@ func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *
if v, ok := params[key]; ok {
value = v
}
if e := Check(value, rule, customMessage[key], params); e != nil {
if e := doCheck(key, value, rule, customMessage[key], params); e != nil {
_, item := e.FirstItem()
// If value is nil or empty string and has no required* rules,
// clear the error message.

View File

@ -27,9 +27,7 @@ type ErrorMap map[string]map[string]string
func newError(rules []string, errors map[string]map[string]string) *Error {
for field, m := range errors {
for k, v := range m {
if field != "" {
v = strings.Replace(v, ":attribute", field, -1)
}
v = strings.Replace(v, ":attribute", field, -1)
m[k], _ = gregex.ReplaceString(`\s{2,}`, ` `, v)
}
errors[field] = m

View File

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