github.com/gogf/gf/v2@v2.7.4/util/gvalid/gvalid_z_unit_feature_checkmap_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gvalid_test 8 9 import ( 10 "context" 11 "testing" 12 13 "github.com/gogf/gf/v2/errors/gerror" 14 "github.com/gogf/gf/v2/frame/g" 15 "github.com/gogf/gf/v2/test/gtest" 16 "github.com/gogf/gf/v2/util/gvalid" 17 ) 18 19 func Test_CheckMap1(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 data := map[string]interface{}{ 22 "id": "0", 23 "name": "john", 24 } 25 rules := map[string]string{ 26 "id": "required|between:1,100", 27 "name": "required|length:6,16", 28 } 29 if m := g.Validator().Data(data).Rules(rules).Run(context.TODO()); m == nil { 30 t.Error("CheckMap校验失败") 31 } else { 32 t.Assert(len(m.Maps()), 2) 33 t.Assert(m.Maps()["id"]["between"], "The id value `0` must be between 1 and 100") 34 t.Assert(m.Maps()["name"]["length"], "The name value `john` length must be between 6 and 16") 35 } 36 }) 37 } 38 39 func Test_CheckMap2(t *testing.T) { 40 var params interface{} 41 gtest.C(t, func(t *gtest.T) { 42 if err := g.Validator().Data(params).Run(context.TODO()); err == nil { 43 t.AssertNil(err) 44 } 45 }) 46 47 kvmap := map[string]interface{}{ 48 "id": "0", 49 "name": "john", 50 } 51 rules := map[string]string{ 52 "id": "required|between:1,100", 53 "name": "required|length:6,16", 54 } 55 msgs := gvalid.CustomMsg{ 56 "id": "ID不能为空|ID范围应当为{min}到{max}", 57 "name": map[string]string{ 58 "required": "名称不能为空", 59 "length": "名称长度为{min}到{max}个字符", 60 }, 61 } 62 if m := g.Validator().Data(kvmap).Rules(rules).Messages(msgs).Run(context.TODO()); m == nil { 63 t.Error("CheckMap校验失败") 64 } 65 66 kvmap = map[string]interface{}{ 67 "id": "1", 68 "name": "john", 69 } 70 rules = map[string]string{ 71 "id": "required|between:1,100", 72 "name": "required|length:4,16", 73 } 74 msgs = map[string]interface{}{ 75 "id": "ID不能为空|ID范围应当为{min}到{max}", 76 "name": map[string]string{ 77 "required": "名称不能为空", 78 "length": "名称长度为{min}到{max}个字符", 79 }, 80 } 81 if m := g.Validator().Data(kvmap).Rules(rules).Messages(msgs).Run(context.TODO()); m != nil { 82 t.Error(m) 83 } 84 85 kvmap = map[string]interface{}{ 86 "id": "1", 87 "name": "john", 88 } 89 rules = map[string]string{ 90 "id": "", 91 "name": "", 92 } 93 msgs = map[string]interface{}{ 94 "id": "ID不能为空|ID范围应当为{min}到{max}", 95 "name": map[string]string{ 96 "required": "名称不能为空", 97 "length": "名称长度为{min}到{max}个字符", 98 }, 99 } 100 if m := g.Validator().Data(kvmap).Rules(rules).Messages(msgs).Run(context.TODO()); m != nil { 101 t.Error(m) 102 } 103 104 kvmap = map[string]interface{}{ 105 "id": "1", 106 "name": "john", 107 } 108 rules2 := []string{ 109 "@required|between:1,100", 110 "@required|length:4,16", 111 } 112 msgs = map[string]interface{}{ 113 "id": "ID不能为空|ID范围应当为{min}到{max}", 114 "name": map[string]string{ 115 "required": "名称不能为空", 116 "length": "名称长度为{min}到{max}个字符", 117 }, 118 } 119 if m := g.Validator().Data(kvmap).Rules(rules2).Messages(msgs).Run(context.TODO()); m != nil { 120 t.Error(m) 121 } 122 123 kvmap = map[string]interface{}{ 124 "id": "1", 125 "name": "john", 126 } 127 rules2 = []string{ 128 "id@required|between:1,100", 129 "name@required|length:4,16#名称不能为空|", 130 } 131 msgs = map[string]interface{}{ 132 "id": "ID不能为空|ID范围应当为{min}到{max}", 133 "name": map[string]string{ 134 "required": "名称不能为空", 135 "length": "名称长度为{min}到{max}个字符", 136 }, 137 } 138 if m := g.Validator().Data(kvmap).Rules(rules2).Messages(msgs).Run(context.TODO()); m != nil { 139 t.Error(m) 140 } 141 142 kvmap = map[string]interface{}{ 143 "id": "1", 144 "name": "john", 145 } 146 rules2 = []string{ 147 "id@required|between:1,100", 148 "name@required|length:4,16#名称不能为空", 149 } 150 msgs = map[string]interface{}{ 151 "id": "ID不能为空|ID范围应当为{min}到{max}", 152 "name": map[string]string{ 153 "required": "名称不能为空", 154 "length": "名称长度为{min}到{max}个字符", 155 }, 156 } 157 if m := g.Validator().Data(kvmap).Rules(rules2).Messages(msgs).Run(context.TODO()); m != nil { 158 t.Error(m) 159 } 160 } 161 162 func Test_CheckMapWithNilAndNotRequiredField(t *testing.T) { 163 data := map[string]interface{}{ 164 "id": "1", 165 } 166 rules := map[string]string{ 167 "id": "required", 168 "name": "length:4,16", 169 } 170 if m := g.Validator().Data(data).Rules(rules).Run(context.TODO()); m != nil { 171 t.Error(m) 172 } 173 } 174 175 func Test_Sequence(t *testing.T) { 176 gtest.C(t, func(t *gtest.T) { 177 params := map[string]interface{}{ 178 "passport": "", 179 "password": "123456", 180 "password2": "1234567", 181 } 182 rules := []string{ 183 "passport@required|length:6,16#账号不能为空|账号长度应当在{min}到{max}之间", 184 "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等", 185 "password2@required|length:6,16#", 186 } 187 err := g.Validator().Data(params).Rules(rules).Run(context.TODO()) 188 t.AssertNE(err, nil) 189 t.Assert(len(err.Map()), 2) 190 t.Assert(err.Map()["required"], "账号不能为空") 191 t.Assert(err.Map()["length"], "账号长度应当在6到16之间") 192 t.Assert(len(err.Maps()), 2) 193 194 t.Assert(len(err.Items()), 2) 195 t.Assert(err.Items()[0]["passport"]["length"], "账号长度应当在6到16之间") 196 t.Assert(err.Items()[0]["passport"]["required"], "账号不能为空") 197 t.Assert(err.Items()[1]["password"]["same"], "两次密码输入不相等") 198 199 t.Assert(err.String(), "账号不能为空; 账号长度应当在6到16之间; 两次密码输入不相等") 200 t.Assert(err.Strings(), []string{"账号不能为空", "账号长度应当在6到16之间", "两次密码输入不相等"}) 201 202 k, m := err.FirstItem() 203 t.Assert(k, "passport") 204 t.Assert(m, err.Map()) 205 206 r, s := err.FirstRule() 207 t.Assert(r, "required") 208 t.Assert(s, "账号不能为空") 209 210 t.Assert(gerror.Current(err), "账号不能为空") 211 }) 212 } 213 214 func Test_Map_Bail(t *testing.T) { 215 // global bail 216 gtest.C(t, func(t *gtest.T) { 217 params := map[string]interface{}{ 218 "passport": "", 219 "password": "123456", 220 "password2": "1234567", 221 } 222 rules := []string{ 223 "passport@required|length:6,16#账号不能为空|账号长度应当在{min}到{max}之间", 224 "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等", 225 "password2@required|length:6,16#", 226 } 227 err := g.Validator().Bail().Rules(rules).Data(params).Run(ctx) 228 t.AssertNE(err, nil) 229 t.Assert(err.String(), "账号不能为空") 230 }) 231 // global bail with rule bail 232 gtest.C(t, func(t *gtest.T) { 233 params := map[string]interface{}{ 234 "passport": "", 235 "password": "123456", 236 "password2": "1234567", 237 } 238 rules := []string{ 239 "passport@bail|required|length:6,16#|账号不能为空|账号长度应当在{min}到{max}之间", 240 "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在{min}到{max}之间|两次密码输入不相等", 241 "password2@required|length:6,16#", 242 } 243 err := g.Validator().Bail().Rules(rules).Data(params).Run(ctx) 244 t.AssertNE(err, nil) 245 t.Assert(err.String(), "账号不能为空") 246 }) 247 }