github.com/zhongdalu/gf@v1.0.0/g/util/gvalid/gvalid_unit_checkstruct_test.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 package gvalid_test 8 9 import ( 10 "testing" 11 12 "github.com/zhongdalu/gf/g" 13 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "github.com/zhongdalu/gf/g/util/gvalid" 16 ) 17 18 func Test_CheckStruct(t *testing.T) { 19 gtest.Case(t, func() { 20 type Object struct { 21 Name string 22 Age int 23 } 24 rules := []string{ 25 "@required|length:6,16", 26 "@between:18,30", 27 } 28 msgs := map[string]interface{}{ 29 "Name": map[string]string{ 30 "required": "名称不能为空", 31 "length": "名称长度为:min到:max个字符", 32 }, 33 "Age": "年龄为18到30周岁", 34 } 35 obj := &Object{"john", 16} 36 err := gvalid.CheckStruct(obj, rules, msgs) 37 gtest.Assert(err, nil) 38 }) 39 40 gtest.Case(t, func() { 41 type Object struct { 42 Name string 43 Age int 44 } 45 rules := []string{ 46 "Name@required|length:6,16#名称不能为空", 47 "Age@between:18,30", 48 } 49 msgs := map[string]interface{}{ 50 "Name": map[string]string{ 51 "required": "名称不能为空", 52 "length": "名称长度为:min到:max个字符", 53 }, 54 "Age": "年龄为18到30周岁", 55 } 56 obj := &Object{"john", 16} 57 err := gvalid.CheckStruct(obj, rules, msgs) 58 gtest.AssertNE(err, nil) 59 gtest.Assert(len(err.Maps()), 2) 60 gtest.Assert(err.Maps()["Name"]["required"], "") 61 gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符") 62 gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁") 63 }) 64 65 gtest.Case(t, func() { 66 type Object struct { 67 Name string 68 Age int 69 } 70 rules := []string{ 71 "Name@required|length:6,16#名称不能为空|", 72 "Age@between:18,30", 73 } 74 msgs := map[string]interface{}{ 75 "Name": map[string]string{ 76 "required": "名称不能为空", 77 "length": "名称长度为:min到:max个字符", 78 }, 79 "Age": "年龄为18到30周岁", 80 } 81 obj := &Object{"john", 16} 82 err := gvalid.CheckStruct(obj, rules, msgs) 83 gtest.AssertNE(err, nil) 84 gtest.Assert(len(err.Maps()), 2) 85 gtest.Assert(err.Maps()["Name"]["required"], "") 86 gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符") 87 gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁") 88 }) 89 90 gtest.Case(t, func() { 91 type Object struct { 92 Name string 93 Age int 94 } 95 rules := map[string]string{ 96 "Name": "required|length:6,16", 97 "Age": "between:18,30", 98 } 99 msgs := map[string]interface{}{ 100 "Name": map[string]string{ 101 "required": "名称不能为空", 102 "length": "名称长度为:min到:max个字符", 103 }, 104 "Age": "年龄为18到30周岁", 105 } 106 obj := &Object{"john", 16} 107 err := gvalid.CheckStruct(obj, rules, msgs) 108 gtest.AssertNE(err, nil) 109 gtest.Assert(len(err.Maps()), 2) 110 gtest.Assert(err.Maps()["Name"]["required"], "") 111 gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符") 112 gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁") 113 }) 114 115 gtest.Case(t, func() { 116 type LoginRequest struct { 117 Username string `json:"username" gvalid:"username@required#用户名不能为空"` 118 Password string `json:"password" gvalid:"password@required#登录密码不能为空"` 119 } 120 var login LoginRequest 121 err := gvalid.CheckStruct(login, nil) 122 gtest.AssertNE(err, nil) 123 gtest.Assert(len(err.Maps()), 2) 124 gtest.Assert(err.Maps()["username"]["required"], "用户名不能为空") 125 gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空") 126 }) 127 128 gtest.Case(t, func() { 129 type LoginRequest struct { 130 Username string `json:"username" gvalid:"@required#用户名不能为空"` 131 Password string `json:"password" gvalid:"@required#登录密码不能为空"` 132 } 133 var login LoginRequest 134 err := gvalid.CheckStruct(login, nil) 135 gtest.Assert(err, nil) 136 }) 137 138 gtest.Case(t, func() { 139 type LoginRequest struct { 140 username string `json:"username" gvalid:"username@required#用户名不能为空"` 141 Password string `json:"password" gvalid:"password@required#登录密码不能为空"` 142 } 143 var login LoginRequest 144 err := gvalid.CheckStruct(login, nil) 145 gtest.AssertNE(err, nil) 146 gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空") 147 }) 148 149 // gvalid tag 150 gtest.Case(t, func() { 151 type User struct { 152 Id int `gvalid:"uid@required|min:10#|ID不能为空"` 153 Age int `gvalid:"age@required#年龄不能为空"` 154 Username string `json:"username" gvalid:"username@required#用户名不能为空"` 155 Password string `json:"password" gvalid:"password@required#登录密码不能为空"` 156 } 157 user := &User{ 158 Id: 1, 159 Username: "john", 160 Password: "123456", 161 } 162 err := gvalid.CheckStruct(user, nil) 163 gtest.AssertNE(err, nil) 164 gtest.Assert(len(err.Maps()), 1) 165 gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空") 166 }) 167 168 gtest.Case(t, func() { 169 type User struct { 170 Id int `gvalid:"uid@required|min:10#|ID不能为空"` 171 Age int `gvalid:"age@required#年龄不能为空"` 172 Username string `json:"username" gvalid:"username@required#用户名不能为空"` 173 Password string `json:"password" gvalid:"password@required#登录密码不能为空"` 174 } 175 user := &User{ 176 Id: 1, 177 Username: "john", 178 Password: "123456", 179 } 180 181 rules := []string{ 182 "username@required#用户名不能为空", 183 } 184 185 err := gvalid.CheckStruct(user, rules) 186 gtest.AssertNE(err, nil) 187 gtest.Assert(len(err.Maps()), 1) 188 gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空") 189 }) 190 191 gtest.Case(t, func() { 192 type User struct { 193 Id int `gvalid:"uid@required|min:10#ID不能为空"` 194 Age int `gvalid:"age@required#年龄不能为空"` 195 Username string `json:"username" gvalid:"username@required#用户名不能为空"` 196 Password string `json:"password" gvalid:"password@required#登录密码不能为空"` 197 } 198 user := &User{ 199 Id: 1, 200 Username: "john", 201 Password: "123456", 202 } 203 err := gvalid.CheckStruct(user, nil) 204 gtest.AssertNE(err, nil) 205 gtest.Assert(len(err.Maps()), 1) 206 }) 207 208 // valid tag 209 gtest.Case(t, func() { 210 type User struct { 211 Id int `valid:"uid@required|min:10#|ID不能为空"` 212 Age int `valid:"age@required#年龄不能为空"` 213 Username string `json:"username" gvalid:"username@required#用户名不能为空"` 214 Password string `json:"password" gvalid:"password@required#登录密码不能为空"` 215 } 216 user := &User{ 217 Id: 1, 218 Username: "john", 219 Password: "123456", 220 } 221 err := gvalid.CheckStruct(user, nil) 222 gtest.AssertNE(err, nil) 223 gtest.Assert(len(err.Maps()), 1) 224 gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空") 225 }) 226 } 227 228 func Test_CheckStruct_With_Inherit(t *testing.T) { 229 gtest.Case(t, func() { 230 type Pass struct { 231 Pass1 string `valid:"password1@required|same:password2#请输入您的密码|您两次输入的密码不一致"` 232 Pass2 string `valid:"password2@required|same:password1#请再次输入您的密码|您两次输入的密码不一致"` 233 } 234 type User struct { 235 Id int 236 Name string `valid:"name@required#请输入您的姓名"` 237 Pass Pass 238 } 239 user := &User{ 240 Name: "", 241 Pass: Pass{ 242 Pass1: "1", 243 Pass2: "2", 244 }, 245 } 246 err := gvalid.CheckStruct(user, nil) 247 gtest.AssertNE(err, nil) 248 gtest.Assert(err.Maps()["name"], g.Map{"required": "请输入您的姓名"}) 249 gtest.Assert(err.Maps()["password1"], g.Map{"same": "您两次输入的密码不一致"}) 250 gtest.Assert(err.Maps()["password2"], g.Map{"same": "您两次输入的密码不一致"}) 251 }) 252 }