github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_boolean.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 14 // RuleBoolean implements `boolean` rule: 15 // Boolean(1,true,on,yes:true | 0,false,off,no,"":false) 16 // 17 // Format: boolean 18 type RuleBoolean struct{} 19 20 // boolMap defines the boolean values. 21 var boolMap = map[string]struct{}{ 22 "1": {}, 23 "true": {}, 24 "on": {}, 25 "yes": {}, 26 "": {}, 27 "0": {}, 28 "false": {}, 29 "off": {}, 30 "no": {}, 31 } 32 33 func init() { 34 Register(RuleBoolean{}) 35 } 36 37 func (r RuleBoolean) Name() string { 38 return "boolean" 39 } 40 41 func (r RuleBoolean) Message() string { 42 return "The {field} value `{value}` field must be true or false" 43 } 44 45 func (r RuleBoolean) Run(in RunInput) error { 46 if _, ok := boolMap[strings.ToLower(in.Value.String())]; ok { 47 return nil 48 } 49 return errors.New(in.Message) 50 }