mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
fix issue in rules of gmeta.Meta for package gvalid
This commit is contained in:
parent
9345eb5e94
commit
033e2c1d78
@ -147,8 +147,11 @@ func (entry *Entry) check(ctx context.Context) {
|
||||
fallthrough
|
||||
case StatusRunning:
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
entry.logErrorf(ctx, "[gcron] %s %s end with error: %+v", entry.schedule.pattern, entry.jobName, err)
|
||||
if exception := recover(); exception != nil {
|
||||
entry.logErrorf(ctx,
|
||||
"[gcron] %s %s end with error: %+v",
|
||||
entry.schedule.pattern, entry.jobName, exception,
|
||||
)
|
||||
} else {
|
||||
entry.logDebugf(ctx, "[gcron] %s %s end", entry.schedule.pattern, entry.jobName)
|
||||
}
|
||||
|
@ -20,8 +20,9 @@ type CustomMsg = map[string]interface{}
|
||||
|
||||
// fieldRule defined the alias name and rule string for specified field.
|
||||
type fieldRule struct {
|
||||
Name string // Alias name for the field.
|
||||
Rule string // Rule string like: "max:6"
|
||||
Name string // Alias name for the field.
|
||||
Rule string // Rule string like: "max:6"
|
||||
IsMeta bool // Is this rule is from gmeta.Meta, which marks it as whole struct rule.
|
||||
}
|
||||
|
||||
// iNoValidation is an interface that marks current struct not validated by package `gvalid`.
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/os/gstructs"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
@ -124,6 +125,7 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
|
||||
// The custom rules has the most high priority that can overwrite the struct tag rules.
|
||||
for _, field := range tagFields {
|
||||
var (
|
||||
isMeta bool
|
||||
fieldName = field.Name() // Attribute name.
|
||||
name, rule, msg = parseSequenceTag(field.TagValue) // The `name` is different from `attribute alias`, which is used for validation only.
|
||||
)
|
||||
@ -168,9 +170,13 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
|
||||
}
|
||||
} else {
|
||||
nameToRuleMap[name] = rule
|
||||
if fieldValue := field.Value.Interface(); fieldValue != nil {
|
||||
_, isMeta = fieldValue.(gmeta.Meta)
|
||||
}
|
||||
checkRules = append(checkRules, fieldRule{
|
||||
Name: name,
|
||||
Rule: rule,
|
||||
Name: name,
|
||||
Rule: rule,
|
||||
IsMeta: isMeta,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
@ -263,10 +269,12 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
|
||||
|
||||
// The following logic is the same as some of CheckMap but with sequence support.
|
||||
for _, checkRuleItem := range checkRules {
|
||||
_, value = gutil.MapPossibleItemByKey(inputParamMap, checkRuleItem.Name)
|
||||
if value == nil {
|
||||
if aliasName := fieldToAliasNameMap[checkRuleItem.Name]; aliasName != "" {
|
||||
_, value = gutil.MapPossibleItemByKey(inputParamMap, aliasName)
|
||||
if !checkRuleItem.IsMeta {
|
||||
_, value = gutil.MapPossibleItemByKey(inputParamMap, checkRuleItem.Name)
|
||||
if value == nil {
|
||||
if aliasName := fieldToAliasNameMap[checkRuleItem.Name]; aliasName != "" {
|
||||
_, value = gutil.MapPossibleItemByKey(inputParamMap, aliasName)
|
||||
}
|
||||
}
|
||||
}
|
||||
// It checks each rule and its value in loop.
|
||||
@ -284,7 +292,7 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
|
||||
// If value is nil or empty string and has no required* rules,
|
||||
// it clears the error message.
|
||||
// ============================================================
|
||||
if value == nil || gconv.String(value) == "" {
|
||||
if !checkRuleItem.IsMeta && (value == nil || gconv.String(value) == "") {
|
||||
required := false
|
||||
// rule => error
|
||||
for ruleKey := range errorItem {
|
||||
|
47
util/gvalid/gvalid_z_unit_feature_meta_test.go
Normal file
47
util/gvalid/gvalid_z_unit_feature_meta_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gvalid"
|
||||
)
|
||||
|
||||
type UserCreateReq struct {
|
||||
g.Meta `v:"UserCreateReq"`
|
||||
Name string
|
||||
Pass string
|
||||
}
|
||||
|
||||
func RuleUserCreateReq(ctx context.Context, in gvalid.RuleFuncInput) error {
|
||||
var req *UserCreateReq
|
||||
if err := in.Data.Scan(&req); err != nil {
|
||||
return gerror.Wrap(err, `Scan data to UserCreateReq failed`)
|
||||
}
|
||||
return gerror.Newf(`The name "%s" is already token by others`, req.Name)
|
||||
}
|
||||
|
||||
func Test_Meta(t *testing.T) {
|
||||
var user = &UserCreateReq{
|
||||
Name: "john",
|
||||
Pass: "123456",
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
err := g.Validator().RuleFunc("UserCreateReq", RuleUserCreateReq).
|
||||
Data(user).
|
||||
Assoc(g.Map{
|
||||
"Name": "john smith",
|
||||
}).Run(ctx)
|
||||
t.Assert(err.String(), `The name "john smith" is already token by others`)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user