/
builtin_lte.go
51 lines (42 loc) · 1.27 KB
/
builtin_lte.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
48
49
50
51
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package builtin
import (
"errors"
"strconv"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
)
// RuleLTE implements `lte` rule:
// Lesser than or equal to `field`.
// It supports both integer and float.
//
// Format: lte:field
type RuleLTE struct{}
func init() {
Register(RuleLTE{})
}
func (r RuleLTE) Name() string {
return "lte"
}
func (r RuleLTE) Message() string {
return "The {field} value `{value}` must be lesser than or equal to field {field1} value `{value1}`"
}
func (r RuleLTE) Run(in RunInput) error {
var (
fieldName, fieldValue = gutil.MapPossibleItemByKey(in.Data.Map(), in.RulePattern)
fieldValueN, err1 = strconv.ParseFloat(gconv.String(fieldValue), 10)
valueN, err2 = strconv.ParseFloat(in.Value.String(), 10)
)
if valueN > fieldValueN || err1 != nil || err2 != nil {
return errors.New(gstr.ReplaceByMap(in.Message, map[string]string{
"{field1}": fieldName,
"{value1}": gconv.String(fieldValue),
}))
}
return nil
}