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 (
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-05-10 17:49:23 +08:00
|
|
|
const (
|
|
|
|
gERROR_INVALID_LENGTH_TYPE = `should be type of integer`
|
|
|
|
)
|
|
|
|
|
2020-05-10 16:48:00 +08:00
|
|
|
// checkLength checks the length rules for value.
|
|
|
|
// The length is calculated using unicode string, which means one chinese character or letter
|
|
|
|
// both has the length of 1.
|
2020-05-10 10:56:11 +08:00
|
|
|
func checkLength(value, ruleKey, ruleVal string, customMsgMap map[string]string) string {
|
|
|
|
var (
|
|
|
|
msg = ""
|
|
|
|
runeArray = gconv.Runes(value)
|
|
|
|
valueLen = len(runeArray)
|
|
|
|
)
|
|
|
|
switch ruleKey {
|
|
|
|
case "length":
|
2020-05-10 16:48:00 +08:00
|
|
|
var (
|
|
|
|
min = 0
|
|
|
|
max = 0
|
|
|
|
array = strings.Split(ruleVal, ",")
|
|
|
|
)
|
2020-05-10 10:56:11 +08:00
|
|
|
if len(array) > 0 {
|
|
|
|
if v, err := strconv.Atoi(strings.TrimSpace(array[0])); err == nil {
|
|
|
|
min = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(array) > 1 {
|
|
|
|
if v, err := strconv.Atoi(strings.TrimSpace(array[1])); err == nil {
|
|
|
|
max = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if valueLen < min || valueLen > max {
|
2020-05-10 22:32:10 +08:00
|
|
|
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
2020-05-10 10:56:11 +08:00
|
|
|
msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1)
|
|
|
|
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
case "min-length":
|
|
|
|
if min, err := strconv.Atoi(ruleVal); err == nil {
|
|
|
|
if valueLen < min {
|
2020-05-10 22:32:10 +08:00
|
|
|
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
2020-05-10 10:56:11 +08:00
|
|
|
msg = strings.Replace(msg, ":min", strconv.Itoa(min), -1)
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-10 17:49:23 +08:00
|
|
|
msg = gERROR_INVALID_LENGTH_TYPE
|
2020-05-10 10:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case "max-length":
|
|
|
|
if max, err := strconv.Atoi(ruleVal); err == nil {
|
|
|
|
if valueLen > max {
|
2020-05-10 22:32:10 +08:00
|
|
|
msg = getErrorMessageByRule(ruleKey, customMsgMap)
|
2020-05-10 10:56:11 +08:00
|
|
|
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-10 17:49:23 +08:00
|
|
|
msg = gERROR_INVALID_LENGTH_TYPE
|
2020-05-10 10:56:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|