diff --git a/util/gvalid/gvalid_check.go b/util/gvalid/gvalid_check.go index 0db27f0df..eb30a292a 100644 --- a/util/gvalid/gvalid_check.go +++ b/util/gvalid/gvalid_check.go @@ -120,6 +120,7 @@ func Check(value interface{}, rules string, messages interface{}, params ...inte // doCheck does the really rules validation for single key-value. func doCheck(key string, value interface{}, rules string, messages interface{}, params ...interface{}) *Error { + // If there's no validation rules, it does nothing and returns quickly. if rules == "" { return nil } diff --git a/util/gvalid/gvalid_check_map.go b/util/gvalid/gvalid_check_map.go index f29c87975..ca5a754f8 100644 --- a/util/gvalid/gvalid_check_map.go +++ b/util/gvalid/gvalid_check_map.go @@ -18,12 +18,9 @@ import ( // if is type of []string. // The optional parameter specifies the custom error messages for specified keys and rules. func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error { - data := gconv.Map(params) - if data == nil { - return newErrorStr( - "invalid_params", - "invalid params type: convert to map failed", - ) + // If there's no validation rules, it does nothing and returns quickly. + if params == nil || rules == nil { + return nil } var ( checkRules = make(map[string]string) @@ -69,6 +66,17 @@ func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Err case map[string]string: checkRules = v } + // If there's no validation rules, it does nothing and returns quickly. + if len(checkRules) == 0 { + return nil + } + data := gconv.Map(params) + if data == nil { + return newErrorStr( + "invalid_params", + "invalid params type: convert to map failed", + ) + } if len(messages) > 0 && len(messages[0]) > 0 { if len(customMsgs) > 0 { for k, v := range messages[0] { diff --git a/util/gvalid/gvalid_check_struct.go b/util/gvalid/gvalid_check_struct.go index 528d6213b..69fee0430 100644 --- a/util/gvalid/gvalid_check_struct.go +++ b/util/gvalid/gvalid_check_struct.go @@ -25,6 +25,15 @@ var ( // if is type of []string. // The optional parameter specifies the custom error messages for specified keys and rules. func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error { + // It here must use structs.TagFields not structs.MapField to ensure error sequence. + tagField, err := structs.TagFields(object, structTagPriority) + if err != nil { + return newErrorStr("invalid_object", err.Error()) + } + // If there's no struct tag and validation rules, it does nothing and returns quickly. + if len(tagField) == 0 && rules == nil { + return nil + } var ( params = make(map[string]interface{}) checkRules = make(map[string]string) @@ -72,6 +81,10 @@ func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) * case map[string]string: checkRules = v } + // If there's no struct tag and validation rules, it does nothing and returns quickly. + if len(tagField) == 0 && len(checkRules) == 0 { + return nil + } // Checks and extends the parameters map with struct alias tag. mapField, err := structs.MapField(object, aliasNameTagPriority) if err != nil { @@ -81,11 +94,6 @@ func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) * params[nameOrTag] = field.Value() params[field.Name()] = field.Value() } - // It here must use structs.TagFields not structs.MapField to ensure error sequence. - tagField, err := structs.TagFields(object, structTagPriority) - if err != nil { - return newErrorStr("invalid_object", err.Error()) - } for _, field := range tagField { fieldName := field.Name() // sequence tag == struct tag diff --git a/util/gvalid/gvalid_unit_checkmap_test.go b/util/gvalid/gvalid_unit_checkmap_test.go index 24f4102fb..e29d9104a 100755 --- a/util/gvalid/gvalid_unit_checkmap_test.go +++ b/util/gvalid/gvalid_unit_checkmap_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -36,9 +36,11 @@ func Test_CheckMap1(t *testing.T) { func Test_CheckMap2(t *testing.T) { var params interface{} - if m := gvalid.CheckMap(params, nil, nil); m == nil { - t.Error("CheckMap校验失败") - } + gtest.C(t, func(t *gtest.T) { + if err := gvalid.CheckMap(params, nil, nil); err == nil { + t.Assert(err, nil) + } + }) kvmap := map[string]interface{}{ "id": "0", diff --git a/util/gvalid/gvalid_unit_checkstruct_test.go b/util/gvalid/gvalid_unit_checkstruct_test.go index f1f6f0ec8..3e9f3aae5 100755 --- a/util/gvalid/gvalid_unit_checkstruct_test.go +++ b/util/gvalid/gvalid_unit_checkstruct_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -293,3 +293,19 @@ func Test_CheckStruct_Optional(t *testing.T) { t.Assert(err.String(), "project id must between 1, 10000") }) } + +func Test_CheckStruct_NoTag(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + type Params struct { + Page int + Size int + ProjectId string + } + obj := &Params{ + Page: 1, + Size: 10, + } + err := gvalid.CheckStruct(obj, nil) + t.Assert(err, nil) + }) +}