github.com/gogf/gf@v1.16.9/util/gvalid/gvalid_validator.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 "github.com/gogf/gf/i18n/gi18n" 12 ) 13 14 // Validator is the validation manager for chaining operations. 15 type Validator struct { 16 ctx context.Context // Context containing custom context variables. 17 i18nManager *gi18n.Manager // I18n manager for error message translation. 18 key string // Single validation key. 19 value interface{} // Single validation value. 20 data interface{} // Validation data, which is usually a map. 21 rules interface{} // Custom validation data. 22 messages interface{} // Custom validation error messages, which can be string or type of CustomMsg. 23 ruleFuncMap map[string]RuleFunc // ruleFuncMap stores custom rule functions for current Validator. 24 useDataInsteadOfObjectAttributes bool // Using `data` as its validation source instead of attribute values from `Object`. 25 bail bool // Stop validation after the first validation error. 26 } 27 28 // New creates and returns a new Validator. 29 func New() *Validator { 30 return &Validator{ 31 ctx: context.TODO(), // Initialize an empty context. 32 i18nManager: gi18n.Instance(), // Use default i18n manager. 33 ruleFuncMap: make(map[string]RuleFunc), // Custom rule function storing map. 34 } 35 } 36 37 // Clone creates and returns a new Validator which is a shallow copy of current one. 38 func (v *Validator) Clone() *Validator { 39 newValidator := New() 40 *newValidator = *v 41 return newValidator 42 } 43 44 // I18n sets the i18n manager for the validator. 45 func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator { 46 newValidator := v.Clone() 47 newValidator.i18nManager = i18nManager 48 return newValidator 49 } 50 51 // Ctx is a chaining operation function, which sets the context for next validation. 52 func (v *Validator) Ctx(ctx context.Context) *Validator { 53 newValidator := v.Clone() 54 newValidator.ctx = ctx 55 return newValidator 56 } 57 58 // Bail sets the mark for stopping validation after the first validation error. 59 func (v *Validator) Bail() *Validator { 60 newValidator := v.Clone() 61 newValidator.bail = true 62 return newValidator 63 } 64 65 // Data is a chaining operation function, which sets validation data for current operation. 66 // The parameter `data` usually be type of map, which specifies the parameter map used in validation. 67 // Calling this function also sets `useDataInsteadOfObjectAttributes` true no mather the `data` is nil or not. 68 func (v *Validator) Data(data interface{}) *Validator { 69 newValidator := v.Clone() 70 newValidator.data = data 71 newValidator.useDataInsteadOfObjectAttributes = true 72 return newValidator 73 } 74 75 // Rules is a chaining operation function, which sets custom validation rules for current operation. 76 func (v *Validator) Rules(rules interface{}) *Validator { 77 newValidator := v.Clone() 78 newValidator.rules = rules 79 return newValidator 80 } 81 82 // Messages is a chaining operation function, which sets custom error messages for current operation. 83 // The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result 84 // if `rules` is type of []string. 85 func (v *Validator) Messages(messages interface{}) *Validator { 86 newValidator := v.Clone() 87 newValidator.messages = messages 88 return newValidator 89 } 90 91 // RuleFunc registers one custom rule function to current Validator. 92 func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator { 93 newValidator := v.Clone() 94 newValidator.ruleFuncMap[rule] = f 95 return newValidator 96 } 97 98 // RuleFuncMap registers multiple custom rule functions to current Validator. 99 func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator { 100 newValidator := v.Clone() 101 for k, v := range m { 102 newValidator.ruleFuncMap[k] = v 103 } 104 return newValidator 105 } 106 107 // getRuleFunc retrieves and returns the custom rule function for specified rule. 108 func (v *Validator) getRuleFunc(rule string) RuleFunc { 109 ruleFunc := v.ruleFuncMap[rule] 110 if ruleFunc == nil { 111 ruleFunc = customRuleFuncMap[rule] 112 } 113 return ruleFunc 114 }