valid field length

This commit is contained in:
wanna 2021-05-29 12:15:37 +08:00
parent d7b8a2684a
commit 7bfd48e2ab
12 changed files with 36 additions and 4 deletions

View File

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

View File

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

View File

@ -5,6 +5,7 @@
"ReuiredUserName" = "Please input user name"
"ReuiredUserType" = "Please select user type"
"MustSize" = "Size of :attribute must be :size"

View File

@ -4,6 +4,7 @@
"ReuiredUserName" = "请输入用户名称"
"ReuiredUserType" = "请选择用户类型"
"MustSize" = ":attribute长度必须为:size"

View File

@ -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.
// siz 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",

View File

@ -163,7 +163,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 {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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