github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_in.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 builtin 8 9 import ( 10 "errors" 11 "strings" 12 13 "github.com/gogf/gf/v2/text/gstr" 14 ) 15 16 // RuleIn implements `in` rule: 17 // Value should be in: value1,value2,... 18 // 19 // Format: in:value1,value2,... 20 type RuleIn struct{} 21 22 func init() { 23 Register(RuleIn{}) 24 } 25 26 func (r RuleIn) Name() string { 27 return "in" 28 } 29 30 func (r RuleIn) Message() string { 31 return "The {field} value `{value}` is not in acceptable range: {pattern}" 32 } 33 34 func (r RuleIn) Run(in RunInput) error { 35 var ( 36 ok bool 37 inputValueString = in.Value.String() 38 ) 39 40 for _, rulePattern := range gstr.SplitAndTrim(in.RulePattern, ",") { 41 if in.Option.CaseInsensitive { 42 ok = strings.EqualFold(inputValueString, strings.TrimSpace(rulePattern)) 43 } else { 44 ok = strings.Compare(inputValueString, strings.TrimSpace(rulePattern)) == 0 45 } 46 if ok { 47 return nil 48 } 49 } 50 return errors.New(in.Message) 51 }