github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_max.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 "strconv" 12 13 "github.com/wangyougui/gf/v2/text/gstr" 14 ) 15 16 // RuleMax implements `max` rule: 17 // Equal or lesser than :max. It supports both integer and float. 18 // 19 // Format: max:max 20 type RuleMax struct{} 21 22 func init() { 23 Register(RuleMax{}) 24 } 25 26 func (r RuleMax) Name() string { 27 return "max" 28 } 29 30 func (r RuleMax) Message() string { 31 return "The {field} value `{value}` must be equal or lesser than {max}" 32 } 33 34 func (r RuleMax) Run(in RunInput) error { 35 var ( 36 max, err1 = strconv.ParseFloat(in.RulePattern, 10) 37 valueN, err2 = strconv.ParseFloat(in.Value.String(), 10) 38 ) 39 if valueN > max || err1 != nil || err2 != nil { 40 return errors.New(gstr.Replace(in.Message, "{max}", strconv.FormatFloat(max, 'f', -1, 64))) 41 } 42 return nil 43 }