mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
Merge pull request #1271 from wangle201210/master
add valid size method
This commit is contained in:
@ -48,11 +48,13 @@ func main() {
|
||||
"passport": "john",
|
||||
"password": "123456",
|
||||
"password2": "1234567",
|
||||
"name": "gf",
|
||||
}
|
||||
rules := map[string]string{
|
||||
"passport": "required|length:6,16",
|
||||
"password": "required|length:6,16|same:password2",
|
||||
"password2": "required|length:6,16",
|
||||
"name": "size:5",
|
||||
}
|
||||
msgs := map[string]interface{}{
|
||||
"passport": "账号不能为空|账号长度应当在:min到:max之间",
|
||||
@ -60,6 +62,7 @@ func main() {
|
||||
"required": "密码不能为空",
|
||||
"same": "两次密码输入不相等",
|
||||
},
|
||||
"name": "名字长度必须为:size",
|
||||
}
|
||||
if e := gvalid.CheckMap(context.TODO(), params, rules, msgs); e != nil {
|
||||
g.Dump(e.Maps())
|
||||
|
||||
@ -10,12 +10,14 @@ import (
|
||||
|
||||
func main() {
|
||||
type User struct {
|
||||
Name string `v:"required#ReuiredUserName"`
|
||||
Type int `v:"required#ReuiredUserType"`
|
||||
Name string `v:"required#ReuiredUserName"`
|
||||
Type int `v:"required#ReuiredUserType"`
|
||||
Project string `v:"size:10#MustSize"`
|
||||
}
|
||||
var (
|
||||
data = g.Map{
|
||||
"name": "john",
|
||||
"name": "john",
|
||||
"project": "gf",
|
||||
}
|
||||
user = User{}
|
||||
ctxEn = gi18n.WithLanguage(context.TODO(), "en")
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
|
||||
"ReuiredUserName" = "Please input user name"
|
||||
"ReuiredUserType" = "Please select user type"
|
||||
"MustSize" = "Size of :attribute must be :size"
|
||||
|
||||
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
"ReuiredUserName" = "请输入用户名称"
|
||||
"ReuiredUserType" = "请选择用户类型"
|
||||
"MustSize" = ":attribute长度必须为:size"
|
||||
|
||||
|
||||
|
||||
|
||||
@ -47,6 +47,7 @@ import (
|
||||
// length format: length:min,max brief: Length between :min and :max. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.
|
||||
// min-length format: min-length:min brief: Length is equal or greater than :min. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.
|
||||
// max-length format: max-length:max brief: Length is equal or lesser than :max. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.
|
||||
// size format: size brief: Length must be :size. The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.
|
||||
// between format: between:min,max brief: Range between :min and :max. It supports both integer and float.
|
||||
// min format: min:min brief: Equal or greater than :min. It supports both integer and float.
|
||||
// max format: max:max brief: Equal or lesser than :max. It supports both integer and float.
|
||||
@ -143,6 +144,7 @@ var (
|
||||
"length": {},
|
||||
"min-length": {},
|
||||
"max-length": {},
|
||||
"size": {},
|
||||
"between": {},
|
||||
"min": {},
|
||||
"max": {},
|
||||
@ -200,6 +202,7 @@ var (
|
||||
"length": "The :attribute value length must be between :min and :max",
|
||||
"min-length": "The :attribute value length must be equal or greater than :min",
|
||||
"max-length": "The :attribute value length must be equal or lesser than :max",
|
||||
"size": "The :attribute value length must be :size",
|
||||
"between": "The :attribute value must be between :min and :max",
|
||||
"min": "The :attribute value must be equal or greater than :min",
|
||||
"max": "The :attribute value must be equal or lesser than :max",
|
||||
|
||||
@ -172,7 +172,8 @@ func (v *Validator) doCheckBuildInRules(
|
||||
case
|
||||
"length",
|
||||
"min-length",
|
||||
"max-length":
|
||||
"max-length",
|
||||
"size":
|
||||
if msg := v.checkLength(valueStr, ruleKey, rulePattern, customMsgMap); msg != "" {
|
||||
return match, errors.New(msg)
|
||||
} else {
|
||||
|
||||
@ -31,8 +31,6 @@ func (v *Validator) checkLength(value, ruleKey, ruleVal string, customMsgMap map
|
||||
if len(array) > 0 {
|
||||
if v, err := strconv.Atoi(strings.TrimSpace(array[0])); err == nil {
|
||||
min = v
|
||||
// compatible with only one parameter
|
||||
max = v
|
||||
}
|
||||
}
|
||||
if len(array) > 1 {
|
||||
@ -60,6 +58,13 @@ func (v *Validator) checkLength(value, ruleKey, ruleVal string, customMsgMap map
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":max", strconv.Itoa(max), -1)
|
||||
}
|
||||
|
||||
case "size":
|
||||
size, err := strconv.Atoi(ruleVal)
|
||||
if valueLen != size || err != nil {
|
||||
msg = v.getErrorMessageByRule(ruleKey, customMsgMap)
|
||||
msg = strings.Replace(msg, ":size", strconv.Itoa(size), -1)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
@ -658,17 +658,6 @@ func Test_Length(t *testing.T) {
|
||||
if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m == nil {
|
||||
t.Error("长度校验失败")
|
||||
}
|
||||
// only one parameter
|
||||
rule2 := "length:5"
|
||||
if m := gvalid.CheckValue(context.TODO(), "1234", rule2, nil); m == nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.CheckValue(context.TODO(), "12345", rule2, nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.CheckValue(context.TODO(), "123456", rule2, nil); m == nil {
|
||||
t.Error("长度校验失败")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_MinLength(t *testing.T) {
|
||||
@ -713,6 +702,16 @@ func Test_MaxLength(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Size(t *testing.T) {
|
||||
rule := "size:5"
|
||||
if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m == nil {
|
||||
t.Error("长度校验失败")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Between(t *testing.T) {
|
||||
rule := "between:6.01, 10.01"
|
||||
if m := gvalid.CheckValue(context.TODO(), 10, rule, nil); m != nil {
|
||||
|
||||
@ -28,6 +28,7 @@
|
||||
"gf.gvalid.rule.length" = ":attribute 字段长度为:min到:max个字符"
|
||||
"gf.gvalid.rule.min-length" = ":attribute 字段最小长度为:min"
|
||||
"gf.gvalid.rule.max-length" = ":attribute 字段最大长度为:max"
|
||||
"gf.gvalid.rule.size" = ":attribute 字段长度必须为:size"
|
||||
"gf.gvalid.rule.between" = ":attribute 字段大小为:min到:max"
|
||||
"gf.gvalid.rule.min" = ":attribute 字段最小值为:min"
|
||||
"gf.gvalid.rule.max" = ":attribute 字段最大值为:max"
|
||||
|
||||
@ -28,6 +28,7 @@
|
||||
"gf.gvalid.rule.length" = "The :attribute value length must be between :min and :max"
|
||||
"gf.gvalid.rule.min-length" = "The :attribute value length must be equal or greater than :min"
|
||||
"gf.gvalid.rule.max-length" = "The :attribute value length must be equal or lesser than :max"
|
||||
"gf.gvalid.rule.size" = "The :attribute value length must be :size"
|
||||
"gf.gvalid.rule.between" = "The :attribute value must be between :min and :max"
|
||||
"gf.gvalid.rule.min" = "The :attribute value must be equal or greater than :min"
|
||||
"gf.gvalid.rule.max" = "The :attribute value must be equal or lesser than :max"
|
||||
|
||||
1
util/gvalid/testdata/i18n/cn/validation.toml
vendored
1
util/gvalid/testdata/i18n/cn/validation.toml
vendored
@ -28,6 +28,7 @@
|
||||
"gf.gvalid.rule.length" = ":attribute 字段长度为:min到:max个字符"
|
||||
"gf.gvalid.rule.min-length" = ":attribute 字段最小长度为:min"
|
||||
"gf.gvalid.rule.max-length" = ":attribute 字段最大长度为:max"
|
||||
"gf.gvalid.rule.size" = ":attribute 字段长度为必须为:size"
|
||||
"gf.gvalid.rule.between" = ":attribute 字段大小为:min到:max"
|
||||
"gf.gvalid.rule.min" = ":attribute 字段最小值为:min"
|
||||
"gf.gvalid.rule.max" = ":attribute 字段最大值为:max"
|
||||
|
||||
1
util/gvalid/testdata/i18n/en/validation.toml
vendored
1
util/gvalid/testdata/i18n/en/validation.toml
vendored
@ -28,6 +28,7 @@
|
||||
"gf.gvalid.rule.length" = "The :attribute value length must be between :min and :max"
|
||||
"gf.gvalid.rule.min-length" = "The :attribute value length must be equal or greater than :min"
|
||||
"gf.gvalid.rule.max-length" = "The :attribute value length must be equal or lesser than :max"
|
||||
"gf.gvalid.rule.size" = "The :attribute value length must be :size"
|
||||
"gf.gvalid.rule.between" = "The :attribute value must be between :min and :max"
|
||||
"gf.gvalid.rule.min" = "The :attribute value must be equal or greater than :min"
|
||||
"gf.gvalid.rule.max" = "The :attribute value must be equal or lesser than :max"
|
||||
|
||||
Reference in New Issue
Block a user