mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
// 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"
|
|
)
|
|
|
|
// RuleLT implements `lt` rule:
|
|
// Lesser than `field`.
|
|
// It supports both integer and float.
|
|
//
|
|
// Format: lt:field
|
|
type RuleLT struct{}
|
|
|
|
func init() {
|
|
Register(RuleLT{})
|
|
}
|
|
|
|
func (r RuleLT) Name() string {
|
|
return "lt"
|
|
}
|
|
|
|
func (r RuleLT) Message() string {
|
|
return "The {field} value `{value}` must be lesser than field {field1} value `{value1}`"
|
|
}
|
|
|
|
func (r RuleLT) 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
|
|
}
|