github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf. 6 7 package builtin 8 9 import ( 10 "errors" 11 "strings" 12 13 "github.com/wangyougui/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 ok bool 36 for _, rulePattern := range gstr.SplitAndTrim(in.RulePattern, ",") { 37 if in.Option.CaseInsensitive { 38 ok = strings.EqualFold(in.Value.String(), strings.TrimSpace(rulePattern)) 39 } else { 40 ok = strings.Compare(in.Value.String(), strings.TrimSpace(rulePattern)) == 0 41 } 42 if ok { 43 return nil 44 } 45 } 46 return errors.New(in.Message) 47 }