github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_before.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 12 "github.com/gogf/gf/v2/text/gstr" 13 "github.com/gogf/gf/v2/util/gconv" 14 "github.com/gogf/gf/v2/util/gutil" 15 ) 16 17 // RuleBefore implements `before` rule: 18 // The datetime value should be after the value of field `field`. 19 // 20 // Format: before:field 21 type RuleBefore struct{} 22 23 func init() { 24 Register(RuleBefore{}) 25 } 26 27 func (r RuleBefore) Name() string { 28 return "before" 29 } 30 31 func (r RuleBefore) Message() string { 32 return "The {field} value `{value}` must be before field {field1} value `{value1}`" 33 } 34 35 func (r RuleBefore) Run(in RunInput) error { 36 var ( 37 fieldName, fieldValue = gutil.MapPossibleItemByKey(in.Data.Map(), in.RulePattern) 38 valueDatetime = in.Value.Time() 39 fieldDatetime = gconv.Time(fieldValue) 40 ) 41 if valueDatetime.Before(fieldDatetime) { 42 return nil 43 } 44 return errors.New(gstr.ReplaceByMap(in.Message, map[string]string{ 45 "{field1}": fieldName, 46 "{value1}": gconv.String(fieldValue), 47 })) 48 }