improve custom rule function type for package gvalid

This commit is contained in:
John
2020-09-29 23:25:20 +08:00
parent 9943966a86
commit a585a26c39
4 changed files with 8 additions and 8 deletions

View File

@ -172,9 +172,8 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
for index := 0; index < len(ruleItems); {
var (
err error
item = ruleItems[index]
match = false
results = ruleRegex.FindStringSubmatch(item)
results = ruleRegex.FindStringSubmatch(ruleItems[index])
ruleKey = strings.TrimSpace(results[1])
rulePattern = strings.TrimSpace(results[2])
)
@ -191,7 +190,7 @@ func doCheck(key string, value interface{}, rules string, messages interface{},
if len(params) > 0 {
dataMap = gconv.Map(params[0])
}
if err := f(value, message, dataMap); err != nil {
if err := f(ruleItems[index], value, message, dataMap); err != nil {
match = false
errorMsgArray[ruleKey] = err.Error()
} else {

View File

@ -7,11 +7,12 @@
package gvalid
// RuleFunc is the custom function for data validation.
// The parameter <rule> specifies the validation rule string, like "required", "between:1,100", etc.
// The parameter <value> specifies the value for this rule to validate.
// The parameter <message> specifies the custom error message or configured i18n message for this rule.
// The parameter <params> specifies all the parameters that needs. You can ignore parameter <params> if
// you do not really need it in your custom validation rule.
type RuleFunc func(value interface{}, message string, params map[string]interface{}) error
type RuleFunc func(rule string, value interface{}, message string, params map[string]interface{}) error
var (
// customRuleFuncMap stores the custom rule functions.

View File

@ -18,7 +18,7 @@ import (
func Test_CustomRule1(t *testing.T) {
rule := "custom"
err := gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
err := gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error {
pass := gconv.String(value)
if len(pass) != 6 {
return errors.New(message)
@ -65,7 +65,7 @@ func Test_CustomRule1(t *testing.T) {
func Test_CustomRule2(t *testing.T) {
rule := "required-map"
err := gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
err := gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error {
m := gconv.Map(value)
if len(m) == 0 {
return errors.New(message)

View File

@ -114,7 +114,7 @@ func ExampleCheckStruct3() {
func ExampleRegisterRule() {
rule := "unique-name"
gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error {
var (
id = gconv.Int(params["Id"])
name = gconv.String(value)
@ -146,7 +146,7 @@ func ExampleRegisterRule() {
func ExampleRegisterRule_OverwriteRequired() {
rule := "required"
gvalid.RegisterRule(rule, func(value interface{}, message string, params map[string]interface{}) error {
gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error {
reflectValue := reflect.ValueOf(value)
if reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()