gvalid_custom_rule.go 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2020 gf Author(https://github.com/gogf/gf). 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. // RuleFunc is the custom function for data validation.
  8. // The parameter <rule> specifies the validation rule string, like "required", "between:1,100", etc.
  9. // The parameter <value> specifies the value for this rule to validate.
  10. // The parameter <message> specifies the custom error message or configured i18n message for this rule.
  11. // The parameter <params> specifies all the parameters that needs. You can ignore parameter <params> if
  12. // you do not really need it in your custom validation rule.
  13. type RuleFunc func(rule string, value interface{}, message string, params map[string]interface{}) error
  14. var (
  15. // customRuleFuncMap stores the custom rule functions.
  16. // map[Rule]RuleFunc
  17. customRuleFuncMap = make(map[string]RuleFunc)
  18. )
  19. // RegisterRule registers custom validation rule and function for package.
  20. // It returns error if there's already the same rule registered previously.
  21. func RegisterRule(rule string, f RuleFunc) error {
  22. customRuleFuncMap[rule] = f
  23. return nil
  24. }
  25. // DeleteRule deletes custom defined validation rule and its function from global package.
  26. func DeleteRule(rule string) {
  27. delete(customRuleFuncMap, rule)
  28. }