improve package gvalid

This commit is contained in:
John Guo
2020-12-09 21:00:30 +08:00
parent 7fa09596b0
commit b7e41ec32c
5 changed files with 51 additions and 16 deletions

View File

@ -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
}

View File

@ -18,12 +18,9 @@ import (
// if <rules> is type of []string.
// The optional parameter <messages> 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] {

View File

@ -25,6 +25,15 @@ var (
// if <rules> is type of []string.
// The optional parameter <messages> 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

View File

@ -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",

View File

@ -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)
})
}