github.com/gogf/gf@v1.16.9/util/gvalid/gvalid_custom_rule.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 "context" 10 11 // RuleFunc is the custom function for data validation. 12 // The parameter `rule` specifies the validation rule string, like "required", "between:1,100", etc. 13 // The parameter `value` specifies the value for this rule to validate. 14 // The parameter `message` specifies the custom error message or configured i18n message for this rule. 15 // The parameter `data` specifies the `data` which is passed to the Validator. It might be type of map/struct or a nil value. 16 // You can ignore the parameter `data` if you do not really need it in your custom validation rule. 17 type RuleFunc func(ctx context.Context, rule string, value interface{}, message string, data interface{}) error 18 19 var ( 20 // customRuleFuncMap stores the custom rule functions. 21 // map[Rule]RuleFunc 22 customRuleFuncMap = make(map[string]RuleFunc) 23 ) 24 25 // RegisterRule registers custom validation rule and function for package. 26 // It returns error if there's already the same rule registered previously. 27 func RegisterRule(rule string, f RuleFunc) error { 28 customRuleFuncMap[rule] = f 29 return nil 30 } 31 32 // DeleteRule deletes custom defined validation rule and its function from global package. 33 func DeleteRule(rule string) { 34 delete(customRuleFuncMap, rule) 35 }