mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package gvalid
This commit is contained in:
@ -12,11 +12,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
gERROR_INVALID_LENGTH_TYPE = `should be type of integer`
|
||||
)
|
||||
|
||||
// checkLength checks the length rules for value.
|
||||
// checkLength checks <value> using length rules.
|
||||
// The length is calculated using unicode string, which means one chinese character or letter
|
||||
// both has the length of 1.
|
||||
func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
||||
@ -50,23 +46,17 @@ func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
}
|
||||
|
||||
case "min-length":
|
||||
if min, err := strconv.Atoi(ruleVal); err == nil {
|
||||
if valueLen < min {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1)
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_LENGTH_TYPE
|
||||
min, err := strconv.Atoi(ruleVal)
|
||||
if valueLen < min || err != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1)
|
||||
}
|
||||
|
||||
case "max-length":
|
||||
if max, err := strconv.Atoi(ruleVal); err == nil {
|
||||
if valueLen > max {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_LENGTH_TYPE
|
||||
max, err := strconv.Atoi(ruleVal)
|
||||
if valueLen > max || err != nil {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
package gvalid
|
||||
|
||||
// checkLuHn checks value with LUHN algorithm.
|
||||
// checkLuHn checks <value> with LUHN algorithm.
|
||||
// It's usually used for bank card number validation.
|
||||
func checkLuHn(value string) bool {
|
||||
var (
|
||||
|
||||
@ -11,11 +11,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
gERROR_INVALID_RANGE_TYPE = `should be type of integer/float`
|
||||
)
|
||||
|
||||
// checkRange checks the range rules.
|
||||
// checkRange checks <value> using range rules.
|
||||
func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
||||
msg := ""
|
||||
switch ruleKey {
|
||||
@ -34,45 +30,35 @@ func checkRange(value, ruleKey, ruleVal string, customMsgMap map[string]string)
|
||||
max = v
|
||||
}
|
||||
}
|
||||
if v, err := strconv.ParseFloat(value, 10); err == nil {
|
||||
if v < min || v > max {
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_RANGE_TYPE
|
||||
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)
|
||||
}
|
||||
|
||||
// Min value.
|
||||
case "min":
|
||||
if min, err := strconv.ParseFloat(ruleVal, 10); err == nil {
|
||||
if v, err := strconv.ParseFloat(value, 10); err == nil {
|
||||
if v < min {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":min", strconv.FormatFloat(min, 'f', -1, 64), -1)
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_RANGE_TYPE
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_RANGE_TYPE
|
||||
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)
|
||||
}
|
||||
|
||||
// Max value.
|
||||
case "max":
|
||||
if max, err := strconv.ParseFloat(ruleVal, 10); err == nil {
|
||||
if v, err := strconv.ParseFloat(value, 10); err == nil {
|
||||
if v > max {
|
||||
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":max", strconv.FormatFloat(max, 'f', -1, 64), -1)
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_RANGE_TYPE
|
||||
}
|
||||
} else {
|
||||
msg = gERROR_INVALID_RANGE_TYPE
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// checkRequired checks the required rules.
|
||||
// checkRequired checks <value> using required rules.
|
||||
func checkRequired(value, ruleKey, ruleVal string, params map[string]string) bool {
|
||||
required := false
|
||||
switch ruleKey {
|
||||
|
||||
Reference in New Issue
Block a user