github.com/gogf/gf/v2@v2.7.4/util/gvalid/gvalid_register.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gvalid
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"reflect"
    13  	"runtime"
    14  
    15  	"github.com/gogf/gf/v2/container/gvar"
    16  	"github.com/gogf/gf/v2/internal/intlog"
    17  )
    18  
    19  // RuleFunc is the custom function for data validation.
    20  type RuleFunc func(ctx context.Context, in RuleFuncInput) error
    21  
    22  // RuleFuncInput holds the input parameters that passed to custom rule function RuleFunc.
    23  type RuleFuncInput struct {
    24  	// Rule specifies the validation rule string, like "required", "between:1,100", etc.
    25  	Rule string
    26  
    27  	// Message specifies the custom error message or configured i18n message for this rule.
    28  	Message string
    29  
    30  	// Field specifies the field for this rule to validate.
    31  	Field string
    32  
    33  	// ValueType specifies the type of the value, which might be nil.
    34  	ValueType reflect.Type
    35  
    36  	// Value specifies the value for this rule to validate.
    37  	Value *gvar.Var
    38  
    39  	// Data specifies the `data` which is passed to the Validator. It might be a type of map/struct or a nil value.
    40  	// You can ignore the parameter `Data` if you do not really need it in your custom validation rule.
    41  	Data *gvar.Var
    42  }
    43  
    44  var (
    45  	// customRuleFuncMap stores the custom rule functions.
    46  	// map[Rule]RuleFunc
    47  	customRuleFuncMap = make(map[string]RuleFunc)
    48  )
    49  
    50  // RegisterRule registers custom validation rule and function for package.
    51  func RegisterRule(rule string, f RuleFunc) {
    52  	if customRuleFuncMap[rule] != nil {
    53  		intlog.PrintFunc(context.TODO(), func() string {
    54  			return fmt.Sprintf(
    55  				`rule "%s" is overwritten by function "%s"`,
    56  				rule, runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(),
    57  			)
    58  		})
    59  	}
    60  	customRuleFuncMap[rule] = f
    61  }
    62  
    63  // RegisterRuleByMap registers custom validation rules using map for package.
    64  func RegisterRuleByMap(m map[string]RuleFunc) {
    65  	for k, v := range m {
    66  		customRuleFuncMap[k] = v
    67  	}
    68  }
    69  
    70  // GetRegisteredRuleMap returns all the custom registered rules and associated functions.
    71  func GetRegisteredRuleMap() map[string]RuleFunc {
    72  	if len(customRuleFuncMap) == 0 {
    73  		return nil
    74  	}
    75  	ruleMap := make(map[string]RuleFunc)
    76  	for k, v := range customRuleFuncMap {
    77  		ruleMap[k] = v
    78  	}
    79  	return ruleMap
    80  }
    81  
    82  // DeleteRule deletes custom defined validation one or more rules and associated functions from global package.
    83  func DeleteRule(rules ...string) {
    84  	for _, rule := range rules {
    85  		delete(customRuleFuncMap, rule)
    86  	}
    87  }