github.com/gogf/gf@v1.16.9/util/gvalid/gvalid_z_unit_basic_all_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 "github.com/gogf/gf/errors/gcode" 12 "github.com/gogf/gf/errors/gerror" 13 "github.com/gogf/gf/os/gtime" 14 "testing" 15 "time" 16 17 "github.com/gogf/gf/frame/g" 18 "github.com/gogf/gf/test/gtest" 19 "github.com/gogf/gf/util/gvalid" 20 ) 21 22 func Test_Check(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 rule := "abc:6,16" 25 val1 := 0 26 val2 := 7 27 val3 := 20 28 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 29 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 30 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 31 t.Assert(err1, "InvalidRules: abc:6,16") 32 t.Assert(err2, "InvalidRules: abc:6,16") 33 t.Assert(err3, "InvalidRules: abc:6,16") 34 }) 35 } 36 37 func Test_Required(t *testing.T) { 38 if m := gvalid.CheckValue(context.TODO(), "1", "required", nil); m != nil { 39 t.Error(m) 40 } 41 if m := gvalid.CheckValue(context.TODO(), "", "required", nil); m == nil { 42 t.Error(m) 43 } 44 if m := gvalid.CheckValue(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil { 45 t.Error("Required校验失败") 46 } 47 if m := gvalid.CheckValue(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil { 48 t.Error("Required校验失败") 49 } 50 } 51 52 func Test_RequiredIf(t *testing.T) { 53 gtest.C(t, func(t *gtest.T) { 54 rule := "required-if:id,1,age,18" 55 t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil) 56 t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil) 57 t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil) 58 t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil) 59 }) 60 } 61 62 func Test_RequiredUnless(t *testing.T) { 63 gtest.C(t, func(t *gtest.T) { 64 rule := "required-unless:id,1,age,18" 65 t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil) 66 t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil) 67 t.Assert(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil) 68 t.AssertNE(gvalid.CheckValue(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil) 69 }) 70 } 71 72 func Test_RequiredWith(t *testing.T) { 73 gtest.C(t, func(t *gtest.T) { 74 rule := "required-with:id,name" 75 val1 := "" 76 params1 := g.Map{ 77 "age": 18, 78 } 79 params2 := g.Map{ 80 "id": 100, 81 } 82 params3 := g.Map{ 83 "id": 100, 84 "name": "john", 85 } 86 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 87 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 88 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 89 t.Assert(err1, nil) 90 t.AssertNE(err2, nil) 91 t.AssertNE(err3, nil) 92 }) 93 // time.Time 94 gtest.C(t, func(t *gtest.T) { 95 rule := "required-with:id,time" 96 val1 := "" 97 params1 := g.Map{ 98 "age": 18, 99 } 100 params2 := g.Map{ 101 "id": 100, 102 } 103 params3 := g.Map{ 104 "time": time.Time{}, 105 } 106 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 107 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 108 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 109 t.Assert(err1, nil) 110 t.AssertNE(err2, nil) 111 t.Assert(err3, nil) 112 }) 113 gtest.C(t, func(t *gtest.T) { 114 rule := "required-with:id,time" 115 val1 := "" 116 params1 := g.Map{ 117 "age": 18, 118 } 119 params2 := g.Map{ 120 "id": 100, 121 } 122 params3 := g.Map{ 123 "time": time.Now(), 124 } 125 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 126 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 127 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 128 t.Assert(err1, nil) 129 t.AssertNE(err2, nil) 130 t.AssertNE(err3, nil) 131 }) 132 // gtime.Time 133 gtest.C(t, func(t *gtest.T) { 134 type UserApiSearch struct { 135 Uid int64 `json:"uid"` 136 Nickname string `json:"nickname" v:"required-with:Uid"` 137 StartTime *gtime.Time `json:"start_time" v:"required-with:EndTime"` 138 EndTime *gtime.Time `json:"end_time" v:"required-with:StartTime"` 139 } 140 data := UserApiSearch{ 141 StartTime: nil, 142 EndTime: nil, 143 } 144 t.Assert(gvalid.CheckStruct(context.TODO(), data, nil), nil) 145 }) 146 gtest.C(t, func(t *gtest.T) { 147 type UserApiSearch struct { 148 Uid int64 `json:"uid"` 149 Nickname string `json:"nickname" v:"required-with:Uid"` 150 StartTime *gtime.Time `json:"start_time" v:"required-with:EndTime"` 151 EndTime *gtime.Time `json:"end_time" v:"required-with:StartTime"` 152 } 153 data := UserApiSearch{ 154 StartTime: nil, 155 EndTime: gtime.Now(), 156 } 157 t.AssertNE(gvalid.CheckStruct(context.TODO(), data, nil), nil) 158 }) 159 } 160 161 func Test_RequiredWithAll(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 rule := "required-with-all:id,name" 164 val1 := "" 165 params1 := g.Map{ 166 "age": 18, 167 } 168 params2 := g.Map{ 169 "id": 100, 170 } 171 params3 := g.Map{ 172 "id": 100, 173 "name": "john", 174 } 175 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 176 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 177 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 178 t.Assert(err1, nil) 179 t.Assert(err2, nil) 180 t.AssertNE(err3, nil) 181 }) 182 } 183 184 func Test_RequiredWithOut(t *testing.T) { 185 gtest.C(t, func(t *gtest.T) { 186 rule := "required-without:id,name" 187 val1 := "" 188 params1 := g.Map{ 189 "age": 18, 190 } 191 params2 := g.Map{ 192 "id": 100, 193 } 194 params3 := g.Map{ 195 "id": 100, 196 "name": "john", 197 } 198 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 199 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 200 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 201 t.AssertNE(err1, nil) 202 t.AssertNE(err2, nil) 203 t.Assert(err3, nil) 204 }) 205 } 206 207 func Test_RequiredWithOutAll(t *testing.T) { 208 gtest.C(t, func(t *gtest.T) { 209 rule := "required-without-all:id,name" 210 val1 := "" 211 params1 := g.Map{ 212 "age": 18, 213 } 214 params2 := g.Map{ 215 "id": 100, 216 } 217 params3 := g.Map{ 218 "id": 100, 219 "name": "john", 220 } 221 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 222 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 223 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 224 t.AssertNE(err1, nil) 225 t.Assert(err2, nil) 226 t.Assert(err3, nil) 227 }) 228 } 229 230 func Test_Date(t *testing.T) { 231 gtest.C(t, func(t *gtest.T) { 232 rule := "date" 233 val1 := "2010" 234 val2 := "201011" 235 val3 := "20101101" 236 val4 := "2010-11-01" 237 val5 := "2010.11.01" 238 val6 := "2010/11/01" 239 val7 := "2010=11=01" 240 val8 := "123" 241 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 242 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 243 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 244 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 245 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 246 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 247 err7 := gvalid.CheckValue(context.TODO(), val7, rule, nil) 248 err8 := gvalid.CheckValue(context.TODO(), val8, rule, nil) 249 t.AssertNE(err1, nil) 250 t.AssertNE(err2, nil) 251 t.Assert(err3, nil) 252 t.Assert(err4, nil) 253 t.Assert(err5, nil) 254 t.Assert(err6, nil) 255 t.AssertNE(err7, nil) 256 t.AssertNE(err8, nil) 257 }) 258 } 259 260 func Test_DateFormat(t *testing.T) { 261 gtest.C(t, func(t *gtest.T) { 262 val1 := "2010" 263 val2 := "201011" 264 val3 := "2010.11" 265 val4 := "201011-01" 266 val5 := "2010~11~01" 267 val6 := "2010-11~01" 268 err1 := gvalid.CheckValue(context.TODO(), val1, "date-format:Y", nil) 269 err2 := gvalid.CheckValue(context.TODO(), val2, "date-format:Ym", nil) 270 err3 := gvalid.CheckValue(context.TODO(), val3, "date-format:Y.m", nil) 271 err4 := gvalid.CheckValue(context.TODO(), val4, "date-format:Ym-d", nil) 272 err5 := gvalid.CheckValue(context.TODO(), val5, "date-format:Y~m~d", nil) 273 err6 := gvalid.CheckValue(context.TODO(), val6, "date-format:Y~m~d", nil) 274 t.Assert(err1, nil) 275 t.Assert(err2, nil) 276 t.Assert(err3, nil) 277 t.Assert(err4, nil) 278 t.Assert(err5, nil) 279 t.AssertNE(err6, nil) 280 }) 281 gtest.C(t, func(t *gtest.T) { 282 t1 := gtime.Now() 283 t2 := time.Time{} 284 err1 := gvalid.CheckValue(context.TODO(), t1, "date-format:Y", nil) 285 err2 := gvalid.CheckValue(context.TODO(), t2, "date-format:Y", nil) 286 t.Assert(err1, nil) 287 t.AssertNE(err2, nil) 288 }) 289 } 290 291 func Test_Email(t *testing.T) { 292 gtest.C(t, func(t *gtest.T) { 293 rule := "email" 294 value1 := "m@johngcn" 295 value2 := "m@www@johngcn" 296 value3 := "m-m_m@mail.johng.cn" 297 value4 := "m.m-m@johng.cn" 298 err1 := gvalid.CheckValue(context.TODO(), value1, rule, nil) 299 err2 := gvalid.CheckValue(context.TODO(), value2, rule, nil) 300 err3 := gvalid.CheckValue(context.TODO(), value3, rule, nil) 301 err4 := gvalid.CheckValue(context.TODO(), value4, rule, nil) 302 t.AssertNE(err1, nil) 303 t.AssertNE(err2, nil) 304 t.Assert(err3, nil) 305 t.Assert(err4, nil) 306 }) 307 } 308 309 func Test_Phone(t *testing.T) { 310 gtest.C(t, func(t *gtest.T) { 311 err1 := gvalid.CheckValue(context.TODO(), "1361990897", "phone", nil) 312 err2 := gvalid.CheckValue(context.TODO(), "13619908979", "phone", nil) 313 err3 := gvalid.CheckValue(context.TODO(), "16719908979", "phone", nil) 314 err4 := gvalid.CheckValue(context.TODO(), "19719908989", "phone", nil) 315 t.AssertNE(err1.String(), nil) 316 t.Assert(err2, nil) 317 t.Assert(err3, nil) 318 t.Assert(err4, nil) 319 }) 320 } 321 322 func Test_PhoneLoose(t *testing.T) { 323 gtest.C(t, func(t *gtest.T) { 324 err1 := gvalid.CheckValue(context.TODO(), "13333333333", "phone-loose", nil) 325 err2 := gvalid.CheckValue(context.TODO(), "15555555555", "phone-loose", nil) 326 err3 := gvalid.CheckValue(context.TODO(), "16666666666", "phone-loose", nil) 327 err4 := gvalid.CheckValue(context.TODO(), "23333333333", "phone-loose", nil) 328 err5 := gvalid.CheckValue(context.TODO(), "1333333333", "phone-loose", nil) 329 err6 := gvalid.CheckValue(context.TODO(), "10333333333", "phone-loose", nil) 330 t.Assert(err1, nil) 331 t.Assert(err2, nil) 332 t.Assert(err3, nil) 333 t.AssertNE(err4, nil) 334 t.AssertNE(err5, nil) 335 t.AssertNE(err6, nil) 336 }) 337 } 338 func Test_Telephone(t *testing.T) { 339 gtest.C(t, func(t *gtest.T) { 340 rule := "telephone" 341 val1 := "869265" 342 val2 := "028-869265" 343 val3 := "86292651" 344 val4 := "028-8692651" 345 val5 := "0830-8692651" 346 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 347 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 348 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 349 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 350 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 351 t.AssertNE(err1, nil) 352 t.AssertNE(err2, nil) 353 t.Assert(err3, nil) 354 t.Assert(err4, nil) 355 t.Assert(err5, nil) 356 }) 357 } 358 359 func Test_Passport(t *testing.T) { 360 gtest.C(t, func(t *gtest.T) { 361 rule := "passport" 362 val1 := "123456" 363 val2 := "a12345-6" 364 val3 := "aaaaa" 365 val4 := "aaaaaa" 366 val5 := "a123_456" 367 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 368 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 369 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 370 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 371 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 372 t.AssertNE(err1, nil) 373 t.AssertNE(err2, nil) 374 t.AssertNE(err3, nil) 375 t.Assert(err4, nil) 376 t.Assert(err5, nil) 377 }) 378 } 379 380 func Test_Password(t *testing.T) { 381 gtest.C(t, func(t *gtest.T) { 382 rule := "password" 383 val1 := "12345" 384 val2 := "aaaaa" 385 val3 := "a12345-6" 386 val4 := ">,/;'[09-" 387 val5 := "a123_456" 388 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 389 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 390 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 391 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 392 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 393 t.AssertNE(err1, nil) 394 t.AssertNE(err2, nil) 395 t.Assert(err3, nil) 396 t.Assert(err4, nil) 397 t.Assert(err5, nil) 398 }) 399 } 400 401 func Test_Password2(t *testing.T) { 402 gtest.C(t, func(t *gtest.T) { 403 rule := "password2" 404 val1 := "12345" 405 val2 := "Naaaa" 406 val3 := "a12345-6" 407 val4 := ">,/;'[09-" 408 val5 := "a123_456" 409 val6 := "Nant1986" 410 val7 := "Nant1986!" 411 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 412 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 413 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 414 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 415 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 416 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 417 err7 := gvalid.CheckValue(context.TODO(), val7, rule, nil) 418 t.AssertNE(err1, nil) 419 t.AssertNE(err2, nil) 420 t.AssertNE(err3, nil) 421 t.AssertNE(err4, nil) 422 t.AssertNE(err5, nil) 423 t.Assert(err6, nil) 424 t.Assert(err7, nil) 425 }) 426 } 427 428 func Test_Password3(t *testing.T) { 429 gtest.C(t, func(t *gtest.T) { 430 rule := "password3" 431 val1 := "12345" 432 val2 := "Naaaa" 433 val3 := "a12345-6" 434 val4 := ">,/;'[09-" 435 val5 := "a123_456" 436 val6 := "Nant1986" 437 val7 := "Nant1986!" 438 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 439 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 440 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 441 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 442 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 443 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 444 err7 := gvalid.CheckValue(context.TODO(), val7, rule, nil) 445 t.AssertNE(err1, nil) 446 t.AssertNE(err2, nil) 447 t.AssertNE(err3, nil) 448 t.AssertNE(err4, nil) 449 t.AssertNE(err5, nil) 450 t.AssertNE(err6, nil) 451 t.Assert(err7, nil) 452 }) 453 } 454 455 func Test_Postcode(t *testing.T) { 456 gtest.C(t, func(t *gtest.T) { 457 rule := "postcode" 458 val1 := "12345" 459 val2 := "610036" 460 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 461 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 462 t.AssertNE(err1, nil) 463 t.Assert(err2, nil) 464 }) 465 } 466 467 func Test_ResidentId(t *testing.T) { 468 gtest.C(t, func(t *gtest.T) { 469 rule := "resident-id" 470 val1 := "11111111111111" 471 val2 := "1111111111111111" 472 val3 := "311128500121201" 473 val4 := "510521198607185367" 474 val5 := "51052119860718536x" 475 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 476 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 477 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 478 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 479 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 480 t.AssertNE(err1, nil) 481 t.AssertNE(err2, nil) 482 t.AssertNE(err3, nil) 483 t.AssertNE(err4, nil) 484 t.Assert(err5, nil) 485 }) 486 } 487 488 func Test_BankCard(t *testing.T) { 489 gtest.C(t, func(t *gtest.T) { 490 rule := "bank-card" 491 val1 := "6230514630000424470" 492 val2 := "6230514630000424473" 493 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 494 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 495 t.AssertNE(err1, nil) 496 t.Assert(err2, nil) 497 }) 498 } 499 500 func Test_QQ(t *testing.T) { 501 gtest.C(t, func(t *gtest.T) { 502 rule := "qq" 503 val1 := "100" 504 val2 := "1" 505 val3 := "10000" 506 val4 := "38996181" 507 val5 := "389961817" 508 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 509 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 510 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 511 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 512 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 513 t.AssertNE(err1, nil) 514 t.AssertNE(err2, nil) 515 t.Assert(err3, nil) 516 t.Assert(err4, nil) 517 t.Assert(err5, nil) 518 }) 519 } 520 521 func Test_Ip(t *testing.T) { 522 if m := gvalid.CheckValue(context.TODO(), "10.0.0.1", "ip", nil); m != nil { 523 t.Error(m) 524 } 525 if m := gvalid.CheckValue(context.TODO(), "10.0.0.1", "ipv4", nil); m != nil { 526 t.Error(m) 527 } 528 if m := gvalid.CheckValue(context.TODO(), "0.0.0.0", "ipv4", nil); m != nil { 529 t.Error(m) 530 } 531 if m := gvalid.CheckValue(context.TODO(), "1920.0.0.0", "ipv4", nil); m == nil { 532 t.Error("ipv4校验失败") 533 } 534 if m := gvalid.CheckValue(context.TODO(), "1920.0.0.0", "ip", nil); m == nil { 535 t.Error("ipv4校验失败") 536 } 537 if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil { 538 t.Error(m) 539 } 540 if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil { 541 t.Error(m) 542 } 543 if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799", "ip", nil); m != nil { 544 t.Error(m) 545 } 546 if m := gvalid.CheckValue(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil { 547 t.Error(m) 548 } 549 } 550 551 func Test_IPv4(t *testing.T) { 552 gtest.C(t, func(t *gtest.T) { 553 rule := "ipv4" 554 val1 := "0.0.0" 555 val2 := "0.0.0.0" 556 val3 := "1.1.1.1" 557 val4 := "255.255.255.0" 558 val5 := "127.0.0.1" 559 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 560 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 561 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 562 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 563 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 564 t.AssertNE(err1, nil) 565 t.Assert(err2, nil) 566 t.Assert(err3, nil) 567 t.Assert(err4, nil) 568 t.Assert(err5, nil) 569 }) 570 } 571 572 func Test_IPv6(t *testing.T) { 573 gtest.C(t, func(t *gtest.T) { 574 rule := "ipv6" 575 val1 := "192.168.1.1" 576 val2 := "CDCD:910A:2222:5498:8475:1111:3900:2020" 577 val3 := "1030::C9B4:FF12:48AA:1A2B" 578 val4 := "2000:0:0:0:0:0:0:1" 579 val5 := "0000:0000:0000:0000:0000:ffff:c0a8:5909" 580 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 581 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 582 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 583 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 584 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 585 t.AssertNE(err1, nil) 586 t.Assert(err2, nil) 587 t.Assert(err3, nil) 588 t.Assert(err4, nil) 589 t.Assert(err5, nil) 590 }) 591 } 592 593 func Test_MAC(t *testing.T) { 594 gtest.C(t, func(t *gtest.T) { 595 rule := "mac" 596 val1 := "192.168.1.1" 597 val2 := "44-45-53-54-00-00" 598 val3 := "01:00:5e:00:00:00" 599 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 600 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 601 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 602 t.AssertNE(err1, nil) 603 t.Assert(err2, nil) 604 t.Assert(err3, nil) 605 }) 606 } 607 608 func Test_URL(t *testing.T) { 609 gtest.C(t, func(t *gtest.T) { 610 rule := "url" 611 val1 := "127.0.0.1" 612 val2 := "https://www.baidu.com" 613 val3 := "http://127.0.0.1" 614 val4 := "file:///tmp/test.txt" 615 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 616 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 617 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 618 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 619 t.AssertNE(err1, nil) 620 t.Assert(err2, nil) 621 t.Assert(err3, nil) 622 t.Assert(err4, nil) 623 }) 624 } 625 626 func Test_Domain(t *testing.T) { 627 gtest.C(t, func(t *gtest.T) { 628 m := g.MapStrBool{ 629 "localhost": false, 630 "baidu.com": true, 631 "www.baidu.com": true, 632 "jn.np": true, 633 "www.jn.np": true, 634 "w.www.jn.np": true, 635 "127.0.0.1": false, 636 "www.360.com": true, 637 "www.360": false, 638 "360": false, 639 "my-gf": false, 640 "my-gf.com": true, 641 "my-gf.360.com": true, 642 } 643 var err error 644 for k, v := range m { 645 err = gvalid.CheckValue(context.TODO(), k, "domain", nil) 646 if v { 647 //fmt.Println(k) 648 t.Assert(err, nil) 649 } else { 650 //fmt.Println(k) 651 t.AssertNE(err, nil) 652 } 653 } 654 }) 655 } 656 657 func Test_Length(t *testing.T) { 658 rule := "length:6,16" 659 if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m != nil { 660 t.Error(m) 661 } 662 if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m == nil { 663 t.Error("长度校验失败") 664 } 665 } 666 667 func Test_MinLength(t *testing.T) { 668 rule := "min-length:6" 669 msgs := map[string]string{ 670 "min-length": "地址长度至少为:min位", 671 } 672 if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m != nil { 673 t.Error(m) 674 } 675 if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m == nil { 676 t.Error("长度校验失败") 677 } 678 if m := gvalid.CheckValue(context.TODO(), "12345", rule, msgs); m == nil { 679 t.Error("长度校验失败") 680 } 681 682 rule2 := "min-length:abc" 683 if m := gvalid.CheckValue(context.TODO(), "123456", rule2, nil); m == nil { 684 t.Error("长度校验失败") 685 } 686 } 687 688 func Test_MaxLength(t *testing.T) { 689 rule := "max-length:6" 690 msgs := map[string]string{ 691 "max-length": "地址长度至大为:max位", 692 } 693 if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m != nil { 694 t.Error(m) 695 } 696 if m := gvalid.CheckValue(context.TODO(), "1234567", rule, nil); m == nil { 697 t.Error("长度校验失败") 698 } 699 if m := gvalid.CheckValue(context.TODO(), "1234567", rule, msgs); m == nil { 700 t.Error("长度校验失败") 701 } 702 703 rule2 := "max-length:abc" 704 if m := gvalid.CheckValue(context.TODO(), "123456", rule2, nil); m == nil { 705 t.Error("长度校验失败") 706 } 707 } 708 709 func Test_Size(t *testing.T) { 710 rule := "size:5" 711 if m := gvalid.CheckValue(context.TODO(), "12345", rule, nil); m != nil { 712 t.Error(m) 713 } 714 if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m == nil { 715 t.Error("长度校验失败") 716 } 717 } 718 719 func Test_Between(t *testing.T) { 720 rule := "between:6.01, 10.01" 721 if m := gvalid.CheckValue(context.TODO(), 10, rule, nil); m != nil { 722 t.Error(m) 723 } 724 if m := gvalid.CheckValue(context.TODO(), 10.02, rule, nil); m == nil { 725 t.Error("大小范围校验失败") 726 } 727 if m := gvalid.CheckValue(context.TODO(), "a", rule, nil); m == nil { 728 t.Error("大小范围校验失败") 729 } 730 } 731 732 func Test_Min(t *testing.T) { 733 gtest.C(t, func(t *gtest.T) { 734 rule := "min:100" 735 val1 := "1" 736 val2 := "99" 737 val3 := "100" 738 val4 := "1000" 739 val5 := "a" 740 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 741 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 742 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 743 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 744 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 745 t.AssertNE(err1, nil) 746 t.AssertNE(err2, nil) 747 t.Assert(err3, nil) 748 t.Assert(err4, nil) 749 t.AssertNE(err5, nil) 750 751 rule2 := "min:a" 752 err6 := gvalid.CheckValue(context.TODO(), val1, rule2, nil) 753 t.AssertNE(err6, nil) 754 }) 755 } 756 757 func Test_Max(t *testing.T) { 758 gtest.C(t, func(t *gtest.T) { 759 rule := "max:100" 760 val1 := "1" 761 val2 := "99" 762 val3 := "100" 763 val4 := "1000" 764 val5 := "a" 765 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 766 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 767 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 768 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 769 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 770 t.Assert(err1, nil) 771 t.Assert(err2, nil) 772 t.Assert(err3, nil) 773 t.AssertNE(err4, nil) 774 t.AssertNE(err5, nil) 775 776 rule2 := "max:a" 777 err6 := gvalid.CheckValue(context.TODO(), val1, rule2, nil) 778 t.AssertNE(err6, nil) 779 }) 780 } 781 782 func Test_Json(t *testing.T) { 783 gtest.C(t, func(t *gtest.T) { 784 rule := "json" 785 val1 := "" 786 val2 := "." 787 val3 := "{}" 788 val4 := "[]" 789 val5 := "[1,2,3,4]" 790 val6 := `{"list":[1,2,3,4]}` 791 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 792 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 793 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 794 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 795 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 796 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 797 t.AssertNE(err1, nil) 798 t.AssertNE(err2, nil) 799 t.Assert(err3, nil) 800 t.Assert(err4, nil) 801 t.Assert(err5, nil) 802 t.Assert(err6, nil) 803 }) 804 } 805 806 func Test_Integer(t *testing.T) { 807 gtest.C(t, func(t *gtest.T) { 808 rule := "integer" 809 val1 := "" 810 val2 := "1.0" 811 val3 := "001" 812 val4 := "1" 813 val5 := "100" 814 val6 := `999999999` 815 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 816 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 817 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 818 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 819 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 820 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 821 t.AssertNE(err1, nil) 822 t.AssertNE(err2, nil) 823 t.Assert(err3, nil) 824 t.Assert(err4, nil) 825 t.Assert(err5, nil) 826 t.Assert(err6, nil) 827 }) 828 } 829 830 func Test_Float(t *testing.T) { 831 gtest.C(t, func(t *gtest.T) { 832 rule := "float" 833 val1 := "" 834 val2 := "a" 835 val3 := "1" 836 val4 := "1.0" 837 val5 := "1.1" 838 val6 := `0.1` 839 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 840 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 841 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 842 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 843 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 844 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 845 t.AssertNE(err1, nil) 846 t.AssertNE(err2, nil) 847 t.Assert(err3, nil) 848 t.Assert(err4, nil) 849 t.Assert(err5, nil) 850 t.Assert(err6, nil) 851 }) 852 } 853 854 func Test_Boolean(t *testing.T) { 855 gtest.C(t, func(t *gtest.T) { 856 rule := "boolean" 857 val1 := "a" 858 val2 := "-" 859 val3 := "" 860 val4 := "1" 861 val5 := "true" 862 val6 := `off` 863 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 864 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 865 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 866 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 867 err5 := gvalid.CheckValue(context.TODO(), val5, rule, nil) 868 err6 := gvalid.CheckValue(context.TODO(), val6, rule, nil) 869 t.AssertNE(err1, nil) 870 t.AssertNE(err2, nil) 871 t.Assert(err3, nil) 872 t.Assert(err4, nil) 873 t.Assert(err5, nil) 874 t.Assert(err6, nil) 875 }) 876 } 877 878 func Test_Same(t *testing.T) { 879 gtest.C(t, func(t *gtest.T) { 880 rule := "same:id" 881 val1 := "100" 882 params1 := g.Map{ 883 "age": 18, 884 } 885 params2 := g.Map{ 886 "id": 100, 887 } 888 params3 := g.Map{ 889 "id": 100, 890 "name": "john", 891 } 892 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 893 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 894 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 895 t.AssertNE(err1, nil) 896 t.Assert(err2, nil) 897 t.Assert(err3, nil) 898 }) 899 } 900 901 func Test_Different(t *testing.T) { 902 gtest.C(t, func(t *gtest.T) { 903 rule := "different:id" 904 val1 := "100" 905 params1 := g.Map{ 906 "age": 18, 907 } 908 params2 := g.Map{ 909 "id": 100, 910 } 911 params3 := g.Map{ 912 "id": 100, 913 "name": "john", 914 } 915 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params1) 916 err2 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params2) 917 err3 := gvalid.CheckValue(context.TODO(), val1, rule, nil, params3) 918 t.Assert(err1, nil) 919 t.AssertNE(err2, nil) 920 t.AssertNE(err3, nil) 921 }) 922 } 923 924 func Test_In(t *testing.T) { 925 gtest.C(t, func(t *gtest.T) { 926 rule := "in:100,200" 927 val1 := "" 928 val2 := "1" 929 val3 := "100" 930 val4 := "200" 931 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 932 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 933 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 934 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 935 t.AssertNE(err1, nil) 936 t.AssertNE(err2, nil) 937 t.Assert(err3, nil) 938 t.Assert(err4, nil) 939 }) 940 } 941 942 func Test_NotIn(t *testing.T) { 943 gtest.C(t, func(t *gtest.T) { 944 rule := "not-in:100" 945 val1 := "" 946 val2 := "1" 947 val3 := "100" 948 val4 := "200" 949 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 950 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 951 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 952 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 953 t.Assert(err1, nil) 954 t.Assert(err2, nil) 955 t.AssertNE(err3, nil) 956 t.Assert(err4, nil) 957 }) 958 gtest.C(t, func(t *gtest.T) { 959 rule := "not-in:100,200" 960 val1 := "" 961 val2 := "1" 962 val3 := "100" 963 val4 := "200" 964 err1 := gvalid.CheckValue(context.TODO(), val1, rule, nil) 965 err2 := gvalid.CheckValue(context.TODO(), val2, rule, nil) 966 err3 := gvalid.CheckValue(context.TODO(), val3, rule, nil) 967 err4 := gvalid.CheckValue(context.TODO(), val4, rule, nil) 968 t.Assert(err1, nil) 969 t.Assert(err2, nil) 970 t.AssertNE(err3, nil) 971 t.AssertNE(err4, nil) 972 }) 973 } 974 975 func Test_Regex1(t *testing.T) { 976 rule := `regex:\d{6}|\D{6}|length:6,16` 977 if m := gvalid.CheckValue(context.TODO(), "123456", rule, nil); m != nil { 978 t.Error(m) 979 } 980 if m := gvalid.CheckValue(context.TODO(), "abcde6", rule, nil); m == nil { 981 t.Error("校验失败") 982 } 983 } 984 985 func Test_Regex2(t *testing.T) { 986 gtest.C(t, func(t *gtest.T) { 987 rule := `required|min-length:6|regex:^data:image\/(jpeg|png);base64,` 988 str1 := "" 989 str2 := "data" 990 str3 := "data:image/jpeg;base64,/9jrbattq22r" 991 err1 := gvalid.CheckValue(context.TODO(), str1, rule, nil) 992 err2 := gvalid.CheckValue(context.TODO(), str2, rule, nil) 993 err3 := gvalid.CheckValue(context.TODO(), str3, rule, nil) 994 t.AssertNE(err1, nil) 995 t.AssertNE(err2, nil) 996 t.Assert(err3, nil) 997 998 t.AssertNE(err1.Map()["required"], nil) 999 t.AssertNE(err2.Map()["min-length"], nil) 1000 }) 1001 } 1002 1003 // issue: https://github.com/gogf/gf/issues/1077 1004 func Test_InternalError_String(t *testing.T) { 1005 gtest.C(t, func(t *gtest.T) { 1006 type a struct { 1007 Name string `v:"hh"` 1008 } 1009 aa := a{Name: "2"} 1010 err := gvalid.CheckStruct(context.TODO(), &aa, nil) 1011 1012 t.Assert(err.String(), "InvalidRules: hh") 1013 t.Assert(err.Strings(), g.Slice{"InvalidRules: hh"}) 1014 t.Assert(err.FirstString(), "InvalidRules: hh") 1015 t.Assert(gerror.Current(err), "InvalidRules: hh") 1016 }) 1017 } 1018 1019 func Test_Code(t *testing.T) { 1020 gtest.C(t, func(t *gtest.T) { 1021 err := g.Validator().Rules("required").CheckValue("") 1022 t.AssertNE(err, nil) 1023 t.Assert(gerror.Code(err), gcode.CodeValidationFailed) 1024 }) 1025 1026 gtest.C(t, func(t *gtest.T) { 1027 err := g.Validator().Rules("none-exist-rule").CheckValue("") 1028 t.AssertNE(err, nil) 1029 t.Assert(gerror.Code(err), gcode.CodeInternalError) 1030 }) 1031 } 1032 1033 func Test_Bail(t *testing.T) { 1034 // check value with no bail 1035 gtest.C(t, func(t *gtest.T) { 1036 err := g.Validator(). 1037 Rules("required|min:1|between:1,100"). 1038 Messages("|min number is 1|size is between 1 and 100"). 1039 CheckValue(-1) 1040 t.AssertNE(err, nil) 1041 t.Assert(err.Error(), "min number is 1; size is between 1 and 100") 1042 }) 1043 1044 // check value with bail 1045 gtest.C(t, func(t *gtest.T) { 1046 err := g.Validator(). 1047 Rules("bail|required|min:1|between:1,100"). 1048 Messages("||min number is 1|size is between 1 and 100"). 1049 CheckValue(-1) 1050 t.AssertNE(err, nil) 1051 t.Assert(err.Error(), "min number is 1") 1052 }) 1053 1054 // struct with no bail 1055 gtest.C(t, func(t *gtest.T) { 1056 type Params struct { 1057 Page int `v:"required|min:1"` 1058 Size int `v:"required|min:1|between:1,100 # |min number is 1|size is between 1 and 100"` 1059 } 1060 obj := &Params{ 1061 Page: 1, 1062 Size: -1, 1063 } 1064 err := g.Validator().CheckStruct(obj) 1065 t.AssertNE(err, nil) 1066 t.Assert(err.Error(), "min number is 1; size is between 1 and 100") 1067 }) 1068 // struct with bail 1069 gtest.C(t, func(t *gtest.T) { 1070 type Params struct { 1071 Page int `v:"required|min:1"` 1072 Size int `v:"bail|required|min:1|between:1,100 # ||min number is 1|size is between 1 and 100"` 1073 } 1074 obj := &Params{ 1075 Page: 1, 1076 Size: -1, 1077 } 1078 err := g.Validator().CheckStruct(obj) 1079 t.AssertNE(err, nil) 1080 t.Assert(err.Error(), "min number is 1") 1081 }) 1082 }