gf/util/gvalid/gvalid_rule_range.go

65 lines
1.8 KiB
Go
Raw Normal View History

2020-05-10 10:56:11 +08:00
// Copyright 2018 gf Author(https://github.com/gogf/gf). 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 gvalid
import (
"strconv"
"strings"
)
2020-05-16 13:31:24 +08:00
// checkRange checks <value> using range rules.
2020-05-10 10:56:11 +08:00
func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
msg := ""
switch ruleKey {
2020-05-10 17:49:23 +08:00
// Value range.
2020-05-10 10:56:11 +08:00
case "between":
array := strings.Split(ruleVal, ",")
min := float64(0)
max := float64(0)
if len(array) > 0 {
if v, err := strconv.ParseFloat(strings.TrimSpace(array[0]), 10); err == nil {
min = v
}
}
if len(array) > 1 {
if v, err := strconv.ParseFloat(strings.TrimSpace(array[1]), 10); err == nil {
max = v
}
}
2020-05-16 13:31:24 +08:00
v, err := strconv.ParseFloat(value, 10)
if v < min || v > max || err != nil {
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":min", strconv.FormatFloat(min, 'f', -1, 64), -1)
msg = strings.Replace(msg, ":max", strconv.FormatFloat(max, 'f', -1, 64), -1)
2020-05-10 10:56:11 +08:00
}
2020-05-10 17:49:23 +08:00
// Min value.
2020-05-10 10:56:11 +08:00
case "min":
2020-05-16 13:31:24 +08:00
var (
min, err1 = strconv.ParseFloat(ruleVal, 10)
valueN, err2 = strconv.ParseFloat(value, 10)
)
if valueN < min || err1 != nil || err2 != nil {
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":min", strconv.FormatFloat(min, 'f', -1, 64), -1)
2020-05-10 10:56:11 +08:00
}
2020-05-10 17:49:23 +08:00
// Max value.
2020-05-10 10:56:11 +08:00
case "max":
2020-05-16 13:31:24 +08:00
var (
max, err1 = strconv.ParseFloat(ruleVal, 10)
valueN, err2 = strconv.ParseFloat(value, 10)
)
if valueN > max || err1 != nil || err2 != nil {
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":max", strconv.FormatFloat(max, 'f', -1, 64), -1)
2020-05-10 10:56:11 +08:00
}
2020-05-16 13:31:24 +08:00
2020-05-10 10:56:11 +08:00
}
return msg
}