gvalid_register.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. package gvalid
  7. import (
  8. "context"
  9. "fmt"
  10. "reflect"
  11. "runtime"
  12. "github.com/gogf/gf/v2/container/gvar"
  13. "github.com/gogf/gf/v2/internal/intlog"
  14. )
  15. // RuleFunc is the custom function for data validation.
  16. type RuleFunc func(ctx context.Context, in RuleFuncInput) error
  17. // RuleFuncInput holds the input parameters that passed to custom rule function RuleFunc.
  18. type RuleFuncInput struct {
  19. // Rule specifies the validation rule string, like "required", "between:1,100", etc.
  20. Rule string
  21. // Message specifies the custom error message or configured i18n message for this rule.
  22. Message string
  23. // Field specifies the field for this rule to validate.
  24. Field string
  25. // ValueType specifies the type of the value, which might be nil.
  26. ValueType reflect.Type
  27. // Value specifies the value for this rule to validate.
  28. Value *gvar.Var
  29. // Data specifies the `data` which is passed to the Validator. It might be a type of map/struct or a nil value.
  30. // You can ignore the parameter `Data` if you do not really need it in your custom validation rule.
  31. Data *gvar.Var
  32. }
  33. var (
  34. // customRuleFuncMap stores the custom rule functions.
  35. // map[Rule]RuleFunc
  36. customRuleFuncMap = make(map[string]RuleFunc)
  37. )
  38. // RegisterRule registers custom validation rule and function for package.
  39. func RegisterRule(rule string, f RuleFunc) {
  40. if customRuleFuncMap[rule] != nil {
  41. intlog.PrintFunc(context.TODO(), func() string {
  42. return fmt.Sprintf(
  43. `rule "%s" is overwrotten by function "%s"`,
  44. rule, runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(),
  45. )
  46. })
  47. }
  48. customRuleFuncMap[rule] = f
  49. }
  50. // RegisterRuleByMap registers custom validation rules using map for package.
  51. func RegisterRuleByMap(m map[string]RuleFunc) {
  52. for k, v := range m {
  53. customRuleFuncMap[k] = v
  54. }
  55. }
  56. // GetRegisteredRuleMap returns all the custom registered rules and associated functions.
  57. func GetRegisteredRuleMap() map[string]RuleFunc {
  58. if len(customRuleFuncMap) == 0 {
  59. return nil
  60. }
  61. ruleMap := make(map[string]RuleFunc)
  62. for k, v := range customRuleFuncMap {
  63. ruleMap[k] = v
  64. }
  65. return ruleMap
  66. }
  67. // DeleteRule deletes custom defined validation one or more rules and associated functions from global package.
  68. func DeleteRule(rules ...string) {
  69. for _, rule := range rules {
  70. delete(customRuleFuncMap, rule)
  71. }
  72. }