improve package gvalid

This commit is contained in:
john
2020-05-16 13:31:24 +08:00
parent acc2a6a353
commit 9355bc73a2
4 changed files with 32 additions and 56 deletions

View File

@ -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

View File

@ -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 (

View File

@ -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
}

View File

@ -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 {