Merge pull request #1268 from wangle201210/master

"gvalid  length" compatible with only one parameter
This commit is contained in:
John Guo
2021-05-28 13:44:36 +08:00
committed by GitHub
2 changed files with 13 additions and 0 deletions

View File

@ -31,6 +31,8 @@ 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 {

View File

@ -658,6 +658,17 @@ 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) {