-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_after_equal.go
47 lines (39 loc) · 1.31 KB
/
builtin_after_equal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 版权所有 GoFrame 作者(https://goframe.org)。保留所有权利。
//
// 本源代码形式遵循 MIT 许可协议条款。如果随此文件未分发 MIT 许可副本,
// 您可以在 https://github.com/gogf/gf 获取一份。
package builtin
import (
"errors"
"github.com/888go/goframe/text/gstr"
"github.com/888go/goframe/util/gconv"
"github.com/888go/goframe/util/gutil"
)
// RuleAfterEqual 实现了 `after-equal` 规则:
// 该日期时间值应当大于或等于字段 `field` 的值。
//
// 格式:after-equal:field
type RuleAfterEqual struct{}
func init() {
Register(RuleAfterEqual{})
}
func (r RuleAfterEqual) Name() string {
return "after-equal"
}
func (r RuleAfterEqual) Message() string {
return "The {field} value `{value}` must be after or equal to field {field1} value `{value1}`"
}
func (r RuleAfterEqual) Run(in RunInput) error {
var (
fieldName, fieldValue = 工具类.MapPossibleItemByKey(in.Data.X取Map(), in.RulePattern)
valueDatetime = in.Value.X取时间类()
fieldDatetime = 转换类.X取时间(fieldValue)
)
if valueDatetime.After(fieldDatetime) || valueDatetime.Equal(fieldDatetime) {
return nil
}
return errors.New(文本类.Map替换(in.Message, map[string]string{
"{field1}": fieldName,
"{value1}": 转换类.String(fieldValue),
}))
}