diff --git a/util/gvalid/gvalid_custom_rule.go b/util/gvalid/gvalid_custom_rule.go index 63b1020b4..11fe6da87 100644 --- a/util/gvalid/gvalid_custom_rule.go +++ b/util/gvalid/gvalid_custom_rule.go @@ -37,17 +37,15 @@ var ( ) // RegisterRule registers custom validation rule and function for package. -func RegisterRule(rule string, f RuleFunc) error { +func RegisterRule(rule string, f RuleFunc) { customRuleFuncMap[rule] = f - return nil } // RegisterRuleByMap registers custom validation rules using map for package. -func RegisterRuleByMap(m map[string]RuleFunc) error { +func RegisterRuleByMap(m map[string]RuleFunc) { for k, v := range m { customRuleFuncMap[k] = v } - return nil } // DeleteRule deletes custom defined validation one or more rules and associated functions from global package. diff --git a/util/gvalid/gvalid_z_unit_custom_rule_test.go b/util/gvalid/gvalid_z_unit_custom_rule_test.go index cd27fb46c..21ad7dcce 100644 --- a/util/gvalid/gvalid_z_unit_custom_rule_test.go +++ b/util/gvalid/gvalid_z_unit_custom_rule_test.go @@ -18,7 +18,7 @@ import ( func Test_CustomRule1(t *testing.T) { rule := "custom" - err := gvalid.RegisterRule( + gvalid.RegisterRule( rule, func(ctx context.Context, in gvalid.RuleFuncInput) error { pass := in.Value.String() @@ -32,7 +32,7 @@ func Test_CustomRule1(t *testing.T) { return nil }, ) - gtest.Assert(err, nil) + gtest.C(t, func(t *gtest.T) { err := gvalid.CheckValue(context.TODO(), "123456", rule, "custom message") t.Assert(err.String(), "custom message") @@ -69,14 +69,13 @@ func Test_CustomRule1(t *testing.T) { func Test_CustomRule2(t *testing.T) { rule := "required-map" - err := gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error { + gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error { m := in.Value.Map() if len(m) == 0 { return errors.New(in.Message) } return nil }) - gtest.Assert(err, nil) // Check. gtest.C(t, func(t *gtest.T) { errStr := "data map should not be empty" @@ -113,14 +112,13 @@ func Test_CustomRule2(t *testing.T) { func Test_CustomRule_AllowEmpty(t *testing.T) { rule := "allow-empty-str" - err := gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error { + gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error { s := in.Value.String() if len(s) == 0 || s == "gf" { return nil } return errors.New(in.Message) }) - gtest.Assert(err, nil) // Check. gtest.C(t, func(t *gtest.T) { errStr := "error"