github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/gvalid_z_example_feature_rule_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/wangyougui/gf. 6 7 package gvalid_test 8 9 import ( 10 "context" 11 "fmt" 12 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/gf/v2/text/gstr" 15 ) 16 17 func ExampleRule_Required() { 18 type BizReq struct { 19 ID uint `v:"required"` 20 Name string `v:"required"` 21 } 22 var ( 23 ctx = context.Background() 24 req = BizReq{ 25 ID: 1, 26 } 27 ) 28 if err := g.Validator().Data(req).Run(ctx); err != nil { 29 fmt.Print(err) 30 } 31 32 // Output: 33 // The Name field is required 34 } 35 36 func ExampleRule_RequiredIf() { 37 type BizReq struct { 38 ID uint `v:"required" dc:"Your ID"` 39 Name string `v:"required" dc:"Your name"` 40 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 41 WifeName string `v:"required-if:gender,1"` 42 HusbandName string `v:"required-if:gender,2"` 43 } 44 var ( 45 ctx = context.Background() 46 req = BizReq{ 47 ID: 1, 48 Name: "test", 49 Gender: 1, 50 } 51 ) 52 if err := g.Validator().Data(req).Run(ctx); err != nil { 53 fmt.Println(err) 54 } 55 56 // Output: 57 // The WifeName field is required 58 } 59 60 func ExampleRule_RequiredUnless() { 61 type BizReq struct { 62 ID uint `v:"required" dc:"Your ID"` 63 Name string `v:"required" dc:"Your name"` 64 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 65 WifeName string `v:"required-unless:gender,0,gender,2"` 66 HusbandName string `v:"required-unless:id,0,gender,2"` 67 } 68 var ( 69 ctx = context.Background() 70 req = BizReq{ 71 ID: 1, 72 Name: "test", 73 Gender: 1, 74 } 75 ) 76 if err := g.Validator().Data(req).Run(ctx); err != nil { 77 fmt.Println(err) 78 } 79 80 // Output: 81 // The WifeName field is required; The HusbandName field is required 82 } 83 84 func ExampleRule_RequiredWith() { 85 type BizReq struct { 86 ID uint `v:"required" dc:"Your ID"` 87 Name string `v:"required" dc:"Your name"` 88 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 89 WifeName string 90 HusbandName string `v:"required-with:WifeName"` 91 } 92 var ( 93 ctx = context.Background() 94 req = BizReq{ 95 ID: 1, 96 Name: "test", 97 Gender: 1, 98 WifeName: "Ann", 99 } 100 ) 101 if err := g.Validator().Data(req).Run(ctx); err != nil { 102 fmt.Println(err) 103 } 104 105 // Output: 106 // The HusbandName field is required 107 } 108 109 func ExampleRule_RequiredWithAll() { 110 type BizReq struct { 111 ID uint `v:"required" dc:"Your ID"` 112 Name string `v:"required" dc:"Your name"` 113 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 114 WifeName string 115 HusbandName string `v:"required-with-all:Id,Name,Gender,WifeName"` 116 } 117 var ( 118 ctx = context.Background() 119 req = BizReq{ 120 ID: 1, 121 Name: "test", 122 Gender: 1, 123 WifeName: "Ann", 124 } 125 ) 126 if err := g.Validator().Data(req).Run(ctx); err != nil { 127 fmt.Println(err) 128 } 129 130 // Output: 131 // The HusbandName field is required 132 } 133 134 func ExampleRule_RequiredWithout() { 135 type BizReq struct { 136 ID uint `v:"required" dc:"Your ID"` 137 Name string `v:"required" dc:"Your name"` 138 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 139 WifeName string 140 HusbandName string `v:"required-without:Id,WifeName"` 141 } 142 var ( 143 ctx = context.Background() 144 req = BizReq{ 145 ID: 1, 146 Name: "test", 147 Gender: 1, 148 } 149 ) 150 if err := g.Validator().Data(req).Run(ctx); err != nil { 151 fmt.Println(err) 152 } 153 154 // Output: 155 // The HusbandName field is required 156 } 157 158 func ExampleRule_RequiredWithoutAll() { 159 type BizReq struct { 160 ID uint `v:"required" dc:"Your ID"` 161 Name string `v:"required" dc:"Your name"` 162 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 163 WifeName string 164 HusbandName string `v:"required-without-all:Id,WifeName"` 165 } 166 var ( 167 ctx = context.Background() 168 req = BizReq{ 169 Name: "test", 170 Gender: 1, 171 } 172 ) 173 if err := g.Validator().Data(req).Run(ctx); err != nil { 174 fmt.Println(err) 175 } 176 177 // Output: 178 // The HusbandName field is required 179 } 180 181 func ExampleRule_Bail() { 182 type BizReq struct { 183 Account string `v:"bail|required|length:6,16|same:QQ"` 184 QQ string 185 Password string `v:"required|same:Password2"` 186 Password2 string `v:"required"` 187 } 188 var ( 189 ctx = context.Background() 190 req = BizReq{ 191 Account: "gf", 192 QQ: "123456", 193 Password: "goframe.org", 194 Password2: "goframe.org", 195 } 196 ) 197 if err := g.Validator().Data(req).Run(ctx); err != nil { 198 fmt.Println(err) 199 } 200 201 // output: 202 // The Account value `gf` length must be between 6 and 16 203 } 204 205 func ExampleRule_CaseInsensitive() { 206 type BizReq struct { 207 Account string `v:"required"` 208 Password string `v:"required|ci|same:Password2"` 209 Password2 string `v:"required"` 210 } 211 var ( 212 ctx = context.Background() 213 req = BizReq{ 214 Account: "gf", 215 Password: "Goframe.org", // Diff from Password2, but because of "ci", rule check passed 216 Password2: "goframe.org", 217 } 218 ) 219 if err := g.Validator().Data(req).Run(ctx); err != nil { 220 fmt.Println(err) 221 } 222 223 // output: 224 } 225 226 func ExampleRule_Date() { 227 type BizReq struct { 228 Date1 string `v:"date"` 229 Date2 string `v:"date"` 230 Date3 string `v:"date"` 231 Date4 string `v:"date"` 232 Date5 string `v:"date"` 233 } 234 235 var ( 236 ctx = context.Background() 237 req = BizReq{ 238 Date1: "2021-10-31", 239 Date2: "2021.10.31", 240 Date3: "2021-Oct-31", 241 Date4: "2021 Octa 31", 242 Date5: "2021/Oct/31", 243 } 244 ) 245 if err := g.Validator().Data(req).Run(ctx); err != nil { 246 fmt.Print(gstr.Join(err.Strings(), "\n")) 247 } 248 249 // Output: 250 // The Date3 value `2021-Oct-31` is not a valid date 251 // The Date4 value `2021 Octa 31` is not a valid date 252 // The Date5 value `2021/Oct/31` is not a valid date 253 } 254 255 func ExampleRule_Datetime() { 256 type BizReq struct { 257 Date1 string `v:"datetime"` 258 Date2 string `v:"datetime"` 259 Date3 string `v:"datetime"` 260 Date4 string `v:"datetime"` 261 } 262 263 var ( 264 ctx = context.Background() 265 req = BizReq{ 266 Date1: "2021-11-01 23:00:00", 267 Date2: "2021-11-01 23:00", // error 268 Date3: "2021/11/01 23:00:00", // error 269 Date4: "2021/Dec/01 23:00:00", // error 270 } 271 ) 272 if err := g.Validator().Data(req).Run(ctx); err != nil { 273 fmt.Print(gstr.Join(err.Strings(), "\n")) 274 } 275 276 // Output: 277 // The Date2 value `2021-11-01 23:00` is not a valid datetime 278 // The Date3 value `2021/11/01 23:00:00` is not a valid datetime 279 // The Date4 value `2021/Dec/01 23:00:00` is not a valid datetime 280 } 281 282 func ExampleRule_DateFormat() { 283 type BizReq struct { 284 Date1 string `v:"date-format:Y-m-d"` 285 Date2 string `v:"date-format:Y-m-d"` 286 Date3 string `v:"date-format:Y-m-d H:i:s"` 287 Date4 string `v:"date-format:Y-m-d H:i:s"` 288 } 289 290 var ( 291 ctx = context.Background() 292 req = BizReq{ 293 Date1: "2021-11-01", 294 Date2: "2021-11-01 23:00", // error 295 Date3: "2021-11-01 23:00:00", 296 Date4: "2021-11-01 23:00", // error 297 } 298 ) 299 if err := g.Validator().Data(req).Run(ctx); err != nil { 300 fmt.Print(gstr.Join(err.Strings(), "\n")) 301 } 302 303 // Output: 304 // The Date2 value `2021-11-01 23:00` does not match the format: Y-m-d 305 // The Date4 value `2021-11-01 23:00` does not match the format: Y-m-d H:i:s 306 } 307 308 func ExampleRule_Email() { 309 type BizReq struct { 310 MailAddr1 string `v:"email"` 311 MailAddr2 string `v:"email"` 312 MailAddr3 string `v:"email"` 313 MailAddr4 string `v:"email"` 314 } 315 316 var ( 317 ctx = context.Background() 318 req = BizReq{ 319 MailAddr1: "gf@goframe.org", 320 MailAddr2: "gf@goframe", // error 321 MailAddr3: "gf@goframe.org.cn", 322 MailAddr4: "gf#goframe.org", // error 323 } 324 ) 325 if err := g.Validator().Data(req).Run(ctx); err != nil { 326 fmt.Print(gstr.Join(err.Strings(), "\n")) 327 } 328 329 // Output: 330 // The MailAddr2 value `gf@goframe` is not a valid email address 331 // The MailAddr4 value `gf#goframe.org` is not a valid email address 332 } 333 334 func ExampleRule_Enums() { 335 type Status string 336 const ( 337 StatusRunning Status = "Running" 338 StatusOffline Status = "Offline" 339 ) 340 type BizReq struct { 341 Id int `v:"required"` 342 Name string `v:"required"` 343 Status Status `v:"enums"` 344 } 345 346 var ( 347 ctx = context.Background() 348 req = BizReq{ 349 Id: 1, 350 Name: "john", 351 Status: Status("Pending"), 352 } 353 ) 354 if err := g.Validator().Data(req).Run(ctx); err != nil { 355 fmt.Print(gstr.Join(err.Strings(), "\n")) 356 } 357 358 // May Output: 359 // The Status value `Pending` should be in enums of: ["Running","Offline"] 360 } 361 362 func ExampleRule_Phone() { 363 type BizReq struct { 364 PhoneNumber1 string `v:"phone"` 365 PhoneNumber2 string `v:"phone"` 366 PhoneNumber3 string `v:"phone"` 367 PhoneNumber4 string `v:"phone"` 368 } 369 370 var ( 371 ctx = context.Background() 372 req = BizReq{ 373 PhoneNumber1: "13578912345", 374 PhoneNumber2: "11578912345", // error 11x not exist 375 PhoneNumber3: "17178912345", // error 171 not exit 376 PhoneNumber4: "1357891234", // error len must be 11 377 } 378 ) 379 if err := g.Validator().Data(req).Run(ctx); err != nil { 380 fmt.Print(gstr.Join(err.Strings(), "\n")) 381 } 382 383 // Output: 384 // The PhoneNumber2 value `11578912345` is not a valid phone number 385 // The PhoneNumber3 value `17178912345` is not a valid phone number 386 // The PhoneNumber4 value `1357891234` is not a valid phone number 387 } 388 389 func ExampleRule_PhoneLoose() { 390 type BizReq struct { 391 PhoneNumber1 string `v:"phone-loose"` 392 PhoneNumber2 string `v:"phone-loose"` 393 PhoneNumber3 string `v:"phone-loose"` 394 PhoneNumber4 string `v:"phone-loose"` 395 } 396 397 var ( 398 ctx = context.Background() 399 req = BizReq{ 400 PhoneNumber1: "13578912345", 401 PhoneNumber2: "11578912345", // error 11x not exist 402 PhoneNumber3: "17178912345", 403 PhoneNumber4: "1357891234", // error len must be 11 404 } 405 ) 406 if err := g.Validator().Data(req).Run(ctx); err != nil { 407 fmt.Print(gstr.Join(err.Strings(), "\n")) 408 } 409 410 // Output: 411 // The PhoneNumber2 value `11578912345` is not a valid phone number 412 // The PhoneNumber4 value `1357891234` is not a valid phone number 413 } 414 415 func ExampleRule_Telephone() { 416 type BizReq struct { 417 Telephone1 string `v:"telephone"` 418 Telephone2 string `v:"telephone"` 419 Telephone3 string `v:"telephone"` 420 Telephone4 string `v:"telephone"` 421 } 422 423 var ( 424 ctx = context.Background() 425 req = BizReq{ 426 Telephone1: "010-77542145", 427 Telephone2: "0571-77542145", 428 Telephone3: "20-77542145", // error 429 Telephone4: "775421451", // error len must be 7 or 8 430 } 431 ) 432 if err := g.Validator().Data(req).Run(ctx); err != nil { 433 fmt.Print(gstr.Join(err.Strings(), "\n")) 434 } 435 436 // Output: 437 // The Telephone3 value `20-77542145` is not a valid telephone number 438 // The Telephone4 value `775421451` is not a valid telephone number 439 } 440 441 func ExampleRule_Passport() { 442 type BizReq struct { 443 Passport1 string `v:"passport"` 444 Passport2 string `v:"passport"` 445 Passport3 string `v:"passport"` 446 Passport4 string `v:"passport"` 447 } 448 449 var ( 450 ctx = context.Background() 451 req = BizReq{ 452 Passport1: "goframe", 453 Passport2: "1356666", // error starting with letter 454 Passport3: "goframe#", // error containing only numbers or underscores 455 Passport4: "gf", // error length between 6 and 18 456 } 457 ) 458 if err := g.Validator().Data(req).Run(ctx); err != nil { 459 fmt.Print(gstr.Join(err.Strings(), "\n")) 460 } 461 462 // Output: 463 // The Passport2 value `1356666` is not a valid passport format 464 // The Passport3 value `goframe#` is not a valid passport format 465 // The Passport4 value `gf` is not a valid passport format 466 } 467 468 func ExampleRule_Password() { 469 type BizReq struct { 470 Password1 string `v:"password"` 471 Password2 string `v:"password"` 472 } 473 474 var ( 475 ctx = context.Background() 476 req = BizReq{ 477 Password1: "goframe", 478 Password2: "gofra", // error length between 6 and 18 479 } 480 ) 481 if err := g.Validator().Data(req).Run(ctx); err != nil { 482 fmt.Print(err) 483 } 484 485 // Output: 486 // The Password2 value `gofra` is not a valid password format 487 } 488 489 func ExampleRule_Password2() { 490 type BizReq struct { 491 Password1 string `v:"password2"` 492 Password2 string `v:"password2"` 493 Password3 string `v:"password2"` 494 Password4 string `v:"password2"` 495 } 496 497 var ( 498 ctx = context.Background() 499 req = BizReq{ 500 Password1: "Goframe123", 501 Password2: "gofra", // error length between 6 and 18 502 Password3: "Goframe", // error must contain lower and upper letters and numbers. 503 Password4: "goframe123", // error must contain lower and upper letters and numbers. 504 } 505 ) 506 if err := g.Validator().Data(req).Run(ctx); err != nil { 507 fmt.Print(gstr.Join(err.Strings(), "\n")) 508 } 509 510 // Output: 511 // The Password2 value `gofra` is not a valid password2 format 512 // The Password3 value `Goframe` is not a valid password2 format 513 // The Password4 value `goframe123` is not a valid password2 format 514 } 515 516 func ExampleRule_Password3() { 517 type BizReq struct { 518 Password1 string `v:"password3"` 519 Password2 string `v:"password3"` 520 Password3 string `v:"password3"` 521 } 522 523 var ( 524 ctx = context.Background() 525 req = BizReq{ 526 Password1: "Goframe123#", 527 Password2: "gofra", // error length between 6 and 18 528 Password3: "Goframe123", // error must contain lower and upper letters, numbers and special chars. 529 } 530 ) 531 if err := g.Validator().Data(req).Run(ctx); err != nil { 532 fmt.Print(gstr.Join(err.Strings(), "\n")) 533 } 534 535 // Output: 536 // The Password2 value `gofra` is not a valid password3 format 537 // The Password3 value `Goframe123` is not a valid password3 format 538 } 539 540 func ExampleRule_Postcode() { 541 type BizReq struct { 542 Postcode1 string `v:"postcode"` 543 Postcode2 string `v:"postcode"` 544 Postcode3 string `v:"postcode"` 545 } 546 547 var ( 548 ctx = context.Background() 549 req = BizReq{ 550 Postcode1: "100000", 551 Postcode2: "10000", // error length must be 6 552 Postcode3: "1000000", // error length must be 6 553 } 554 ) 555 if err := g.Validator().Data(req).Run(ctx); err != nil { 556 fmt.Print(gstr.Join(err.Strings(), "\n")) 557 } 558 559 // Output: 560 // The Postcode2 value `10000` is not a valid postcode format 561 // The Postcode3 value `1000000` is not a valid postcode format 562 } 563 564 func ExampleRule_ResidentId() { 565 type BizReq struct { 566 ResidentID1 string `v:"resident-id"` 567 } 568 569 var ( 570 ctx = context.Background() 571 req = BizReq{ 572 ResidentID1: "320107199506285482", 573 } 574 ) 575 if err := g.Validator().Data(req).Run(ctx); err != nil { 576 fmt.Print(err) 577 } 578 579 // Output: 580 // The ResidentID1 value `320107199506285482` is not a valid resident id number 581 } 582 583 func ExampleRule_BankCard() { 584 type BizReq struct { 585 BankCard1 string `v:"bank-card"` 586 } 587 588 var ( 589 ctx = context.Background() 590 req = BizReq{ 591 BankCard1: "6225760079930218", 592 } 593 ) 594 if err := g.Validator().Data(req).Run(ctx); err != nil { 595 fmt.Print(err) 596 } 597 598 // Output: 599 // The BankCard1 value `6225760079930218` is not a valid bank card number 600 } 601 602 func ExampleRule_QQ() { 603 type BizReq struct { 604 QQ1 string `v:"qq"` 605 QQ2 string `v:"qq"` 606 QQ3 string `v:"qq"` 607 } 608 609 var ( 610 ctx = context.Background() 611 req = BizReq{ 612 QQ1: "389961817", 613 QQ2: "9999", // error >= 10000 614 QQ3: "514258412a", // error all number 615 } 616 ) 617 if err := g.Validator().Data(req).Run(ctx); err != nil { 618 fmt.Print(gstr.Join(err.Strings(), "\n")) 619 } 620 621 // Output: 622 // The QQ2 value `9999` is not a valid QQ number 623 // The QQ3 value `514258412a` is not a valid QQ number 624 } 625 626 func ExampleRule_IP() { 627 type BizReq struct { 628 IP1 string `v:"ip"` 629 IP2 string `v:"ip"` 630 IP3 string `v:"ip"` 631 IP4 string `v:"ip"` 632 } 633 634 var ( 635 ctx = context.Background() 636 req = BizReq{ 637 IP1: "127.0.0.1", 638 IP2: "fe80::812b:1158:1f43:f0d1", 639 IP3: "520.255.255.255", // error >= 10000 640 IP4: "ze80::812b:1158:1f43:f0d1", 641 } 642 ) 643 if err := g.Validator().Data(req).Run(ctx); err != nil { 644 fmt.Print(gstr.Join(err.Strings(), "\n")) 645 } 646 647 // Output: 648 // The IP3 value `520.255.255.255` is not a valid IP address 649 // The IP4 value `ze80::812b:1158:1f43:f0d1` is not a valid IP address 650 } 651 652 func ExampleRule_IPV4() { 653 type BizReq struct { 654 IP1 string `v:"ipv4"` 655 IP2 string `v:"ipv4"` 656 } 657 658 var ( 659 ctx = context.Background() 660 req = BizReq{ 661 IP1: "127.0.0.1", 662 IP2: "520.255.255.255", 663 } 664 ) 665 if err := g.Validator().Data(req).Run(ctx); err != nil { 666 fmt.Print(err) 667 } 668 669 // Output: 670 // The IP2 value `520.255.255.255` is not a valid IPv4 address 671 } 672 673 func ExampleRule_IPV6() { 674 type BizReq struct { 675 IP1 string `v:"ipv6"` 676 IP2 string `v:"ipv6"` 677 } 678 679 var ( 680 ctx = context.Background() 681 req = BizReq{ 682 IP1: "fe80::812b:1158:1f43:f0d1", 683 IP2: "ze80::812b:1158:1f43:f0d1", 684 } 685 ) 686 if err := g.Validator().Data(req).Run(ctx); err != nil { 687 fmt.Print(err) 688 } 689 690 // Output: 691 // The IP2 value `ze80::812b:1158:1f43:f0d1` is not a valid IPv6 address 692 } 693 694 func ExampleRule_Mac() { 695 type BizReq struct { 696 Mac1 string `v:"mac"` 697 Mac2 string `v:"mac"` 698 } 699 700 var ( 701 ctx = context.Background() 702 req = BizReq{ 703 Mac1: "4C-CC-6A-D6-B1-1A", 704 Mac2: "Z0-CC-6A-D6-B1-1A", 705 } 706 ) 707 if err := g.Validator().Data(req).Run(ctx); err != nil { 708 fmt.Print(err) 709 } 710 711 // Output: 712 // The Mac2 value `Z0-CC-6A-D6-B1-1A` is not a valid MAC address 713 } 714 715 func ExampleRule_Url() { 716 type BizReq struct { 717 URL1 string `v:"url"` 718 URL2 string `v:"url"` 719 URL3 string `v:"url"` 720 } 721 722 var ( 723 ctx = context.Background() 724 req = BizReq{ 725 URL1: "http://goframe.org", 726 URL2: "ftp://goframe.org", 727 URL3: "ws://goframe.org", 728 } 729 ) 730 if err := g.Validator().Data(req).Run(ctx); err != nil { 731 fmt.Print(err) 732 } 733 734 // Output: 735 // The URL3 value `ws://goframe.org` is not a valid URL address 736 } 737 738 func ExampleRule_Domain() { 739 type BizReq struct { 740 Domain1 string `v:"domain"` 741 Domain2 string `v:"domain"` 742 Domain3 string `v:"domain"` 743 Domain4 string `v:"domain"` 744 } 745 746 var ( 747 ctx = context.Background() 748 req = BizReq{ 749 Domain1: "goframe.org", 750 Domain2: "a.b", 751 Domain3: "goframe#org", 752 Domain4: "1a.2b", 753 } 754 ) 755 if err := g.Validator().Data(req).Run(ctx); err != nil { 756 fmt.Print(gstr.Join(err.Strings(), "\n")) 757 } 758 759 // Output: 760 // The Domain3 value `goframe#org` is not a valid domain format 761 // The Domain4 value `1a.2b` is not a valid domain format 762 } 763 764 func ExampleRule_Size() { 765 type BizReq struct { 766 Size1 string `v:"size:10"` 767 Size2 string `v:"size:5"` 768 } 769 770 var ( 771 ctx = context.Background() 772 req = BizReq{ 773 Size1: "goframe欢迎你", 774 Size2: "goframe", 775 } 776 ) 777 if err := g.Validator().Data(req).Run(ctx); err != nil { 778 fmt.Print(err) 779 } 780 781 // Output: 782 // The Size2 value `goframe` length must be 5 783 } 784 785 func ExampleRule_Length() { 786 type BizReq struct { 787 Length1 string `v:"length:5,10"` 788 Length2 string `v:"length:10,15"` 789 } 790 791 var ( 792 ctx = context.Background() 793 req = BizReq{ 794 Length1: "goframe欢迎你", 795 Length2: "goframe", 796 } 797 ) 798 if err := g.Validator().Data(req).Run(ctx); err != nil { 799 fmt.Print(err) 800 } 801 802 // Output: 803 // The Length2 value `goframe` length must be between 10 and 15 804 } 805 806 func ExampleRule_MinLength() { 807 type BizReq struct { 808 MinLength1 string `v:"min-length:10"` 809 MinLength2 string `v:"min-length:8"` 810 } 811 812 var ( 813 ctx = context.Background() 814 req = BizReq{ 815 MinLength1: "goframe欢迎你", 816 MinLength2: "goframe", 817 } 818 ) 819 if err := g.Validator().Data(req).Run(ctx); err != nil { 820 fmt.Print(err) 821 } 822 823 // Output: 824 // The MinLength2 value `goframe` length must be equal or greater than 8 825 } 826 827 func ExampleRule_MaxLength() { 828 type BizReq struct { 829 MaxLength1 string `v:"max-length:10"` 830 MaxLength2 string `v:"max-length:5"` 831 } 832 833 var ( 834 ctx = context.Background() 835 req = BizReq{ 836 MaxLength1: "goframe欢迎你", 837 MaxLength2: "goframe", 838 } 839 ) 840 if err := g.Validator().Data(req).Run(ctx); err != nil { 841 fmt.Print(err) 842 } 843 844 // Output: 845 // The MaxLength2 value `goframe` length must be equal or lesser than 5 846 } 847 848 func ExampleRule_Between() { 849 type BizReq struct { 850 Age1 int `v:"between:1,100"` 851 Age2 int `v:"between:1,100"` 852 Score1 float32 `v:"between:0.0,10.0"` 853 Score2 float32 `v:"between:0.0,10.0"` 854 } 855 856 var ( 857 ctx = context.Background() 858 req = BizReq{ 859 Age1: 50, 860 Age2: 101, 861 Score1: 9.8, 862 Score2: -0.5, 863 } 864 ) 865 if err := g.Validator().Data(req).Run(ctx); err != nil { 866 fmt.Print(gstr.Join(err.Strings(), "\n")) 867 } 868 869 // Output: 870 // The Age2 value `101` must be between 1 and 100 871 // The Score2 value `-0.5` must be between 0 and 10 872 } 873 874 func ExampleRule_Min() { 875 type BizReq struct { 876 Age1 int `v:"min:100"` 877 Age2 int `v:"min:100"` 878 Score1 float32 `v:"min:10.0"` 879 Score2 float32 `v:"min:10.0"` 880 } 881 882 var ( 883 ctx = context.Background() 884 req = BizReq{ 885 Age1: 50, 886 Age2: 101, 887 Score1: 9.8, 888 Score2: 10.1, 889 } 890 ) 891 if err := g.Validator().Data(req).Run(ctx); err != nil { 892 fmt.Print(gstr.Join(err.Strings(), "\n")) 893 } 894 895 // Output: 896 // The Age1 value `50` must be equal or greater than 100 897 // The Score1 value `9.8` must be equal or greater than 10 898 } 899 900 func ExampleRule_Max() { 901 type BizReq struct { 902 Age1 int `v:"max:100"` 903 Age2 int `v:"max:100"` 904 Score1 float32 `v:"max:10.0"` 905 Score2 float32 `v:"max:10.0"` 906 } 907 908 var ( 909 ctx = context.Background() 910 req = BizReq{ 911 Age1: 99, 912 Age2: 101, 913 Score1: 9.9, 914 Score2: 10.1, 915 } 916 ) 917 if err := g.Validator().Data(req).Run(ctx); err != nil { 918 fmt.Print(gstr.Join(err.Strings(), "\n")) 919 } 920 921 // Output: 922 // The Age2 value `101` must be equal or lesser than 100 923 // The Score2 value `10.1` must be equal or lesser than 10 924 } 925 926 func ExampleRule_Json() { 927 type BizReq struct { 928 JSON1 string `v:"json"` 929 JSON2 string `v:"json"` 930 } 931 932 var ( 933 ctx = context.Background() 934 req = BizReq{ 935 JSON1: "{\"name\":\"goframe\",\"author\":\"郭强\"}", 936 JSON2: "{\"name\":\"goframe\",\"author\":\"郭强\",\"test\"}", 937 } 938 ) 939 if err := g.Validator().Data(req).Run(ctx); err != nil { 940 fmt.Print(err) 941 } 942 943 // Output: 944 // The JSON2 value `{"name":"goframe","author":"郭强","test"}` is not a valid JSON string 945 } 946 947 func ExampleRule_Integer() { 948 type BizReq struct { 949 Integer string `v:"integer"` 950 Float string `v:"integer"` 951 Str string `v:"integer"` 952 } 953 954 var ( 955 ctx = context.Background() 956 req = BizReq{ 957 Integer: "100", 958 Float: "10.0", 959 Str: "goframe", 960 } 961 ) 962 if err := g.Validator().Data(req).Run(ctx); err != nil { 963 fmt.Print(gstr.Join(err.Strings(), "\n")) 964 } 965 966 // Output: 967 // The Float value `10.0` is not an integer 968 // The Str value `goframe` is not an integer 969 } 970 971 func ExampleRule_Float() { 972 type BizReq struct { 973 Integer string `v:"float"` 974 Float string `v:"float"` 975 Str string `v:"float"` 976 } 977 978 var ( 979 ctx = context.Background() 980 req = BizReq{ 981 Integer: "100", 982 Float: "10.0", 983 Str: "goframe", 984 } 985 ) 986 if err := g.Validator().Data(req).Run(ctx); err != nil { 987 fmt.Print(err) 988 } 989 990 // Output: 991 // The Str value `goframe` is not of valid float type 992 } 993 994 func ExampleRule_Boolean() { 995 type BizReq struct { 996 Boolean bool `v:"boolean"` 997 Integer int `v:"boolean"` 998 Float float32 `v:"boolean"` 999 Str1 string `v:"boolean"` 1000 Str2 string `v:"boolean"` 1001 Str3 string `v:"boolean"` 1002 } 1003 1004 var ( 1005 ctx = context.Background() 1006 req = BizReq{ 1007 Boolean: true, 1008 Integer: 1, 1009 Float: 10.0, 1010 Str1: "on", 1011 Str2: "", 1012 Str3: "goframe", 1013 } 1014 ) 1015 if err := g.Validator().Data(req).Run(ctx); err != nil { 1016 fmt.Print(gstr.Join(err.Strings(), "\n")) 1017 } 1018 1019 // Output: 1020 // The Float value `10` field must be true or false 1021 // The Str3 value `goframe` field must be true or false 1022 } 1023 1024 func ExampleRule_Same() { 1025 type BizReq struct { 1026 Name string `v:"required"` 1027 Password string `v:"required|same:Password2"` 1028 Password2 string `v:"required"` 1029 } 1030 var ( 1031 ctx = context.Background() 1032 req = BizReq{ 1033 Name: "gf", 1034 Password: "goframe.org", 1035 Password2: "goframe.net", 1036 } 1037 ) 1038 if err := g.Validator().Data(req).Run(ctx); err != nil { 1039 fmt.Println(err) 1040 } 1041 1042 // Output: 1043 // The Password value `goframe.org` must be the same as field Password2 value `goframe.net` 1044 } 1045 1046 func ExampleRule_Different() { 1047 type BizReq struct { 1048 Name string `v:"required"` 1049 MailAddr string `v:"required"` 1050 OtherMailAddr string `v:"required|different:MailAddr"` 1051 } 1052 var ( 1053 ctx = context.Background() 1054 req = BizReq{ 1055 Name: "gf", 1056 MailAddr: "gf@goframe.org", 1057 OtherMailAddr: "gf@goframe.org", 1058 } 1059 ) 1060 if err := g.Validator().Data(req).Run(ctx); err != nil { 1061 fmt.Println(err) 1062 } 1063 1064 // Output: 1065 // The OtherMailAddr value `gf@goframe.org` must be different from field MailAddr value `gf@goframe.org` 1066 } 1067 1068 func ExampleRule_In() { 1069 type BizReq struct { 1070 ID uint `v:"required" dc:"Your Id"` 1071 Name string `v:"required" dc:"Your name"` 1072 Gender uint `v:"in:0,1,2" dc:"0:Secret;1:Male;2:Female"` 1073 } 1074 var ( 1075 ctx = context.Background() 1076 req = BizReq{ 1077 ID: 1, 1078 Name: "test", 1079 Gender: 3, 1080 } 1081 ) 1082 if err := g.Validator().Data(req).Run(ctx); err != nil { 1083 fmt.Println(err) 1084 } 1085 1086 // Output: 1087 // The Gender value `3` is not in acceptable range: 0,1,2 1088 } 1089 1090 func ExampleRule_NotIn() { 1091 type BizReq struct { 1092 ID uint `v:"required" dc:"Your Id"` 1093 Name string `v:"required" dc:"Your name"` 1094 InvalidIndex uint `v:"not-in:-1,0,1"` 1095 } 1096 var ( 1097 ctx = context.Background() 1098 req = BizReq{ 1099 ID: 1, 1100 Name: "test", 1101 InvalidIndex: 1, 1102 } 1103 ) 1104 if err := g.Validator().Data(req).Run(ctx); err != nil { 1105 fmt.Println(err) 1106 } 1107 1108 // Output: 1109 // The InvalidIndex value `1` must not be in range: -1,0,1 1110 } 1111 1112 func ExampleRule_Regex() { 1113 type BizReq struct { 1114 Regex1 string `v:"regex:[1-9][0-9]{4,14}"` 1115 Regex2 string `v:"regex:[1-9][0-9]{4,14}"` 1116 Regex3 string `v:"regex:[1-9][0-9]{4,14}"` 1117 } 1118 var ( 1119 ctx = context.Background() 1120 req = BizReq{ 1121 Regex1: "1234", 1122 Regex2: "01234", 1123 Regex3: "10000", 1124 } 1125 ) 1126 if err := g.Validator().Data(req).Run(ctx); err != nil { 1127 fmt.Print(gstr.Join(err.Strings(), "\n")) 1128 } 1129 1130 // Output: 1131 // The Regex1 value `1234` must be in regex of: [1-9][0-9]{4,14} 1132 // The Regex2 value `01234` must be in regex of: [1-9][0-9]{4,14} 1133 } 1134 1135 func ExampleRule_NotRegex() { 1136 type BizReq struct { 1137 Regex1 string `v:"regex:\\d{4}"` 1138 Regex2 string `v:"not-regex:\\d{4}"` 1139 } 1140 var ( 1141 ctx = context.Background() 1142 req = BizReq{ 1143 Regex1: "1234", 1144 Regex2: "1234", 1145 } 1146 ) 1147 if err := g.Validator().Data(req).Run(ctx); err != nil { 1148 fmt.Print(gstr.Join(err.Strings(), "\n")) 1149 } 1150 1151 // Output: 1152 // The Regex2 value `1234` should not be in regex of: \d{4} 1153 } 1154 1155 func ExampleRule_After() { 1156 type BizReq struct { 1157 Time1 string 1158 Time2 string `v:"after:Time1"` 1159 Time3 string `v:"after:Time1"` 1160 } 1161 var ( 1162 ctx = context.Background() 1163 req = BizReq{ 1164 Time1: "2022-09-01", 1165 Time2: "2022-09-01", 1166 Time3: "2022-09-02", 1167 } 1168 ) 1169 if err := g.Validator().Data(req).Run(ctx); err != nil { 1170 fmt.Println(err.String()) 1171 } 1172 1173 // Output: 1174 // The Time2 value `2022-09-01` must be after field Time1 value `2022-09-01` 1175 } 1176 1177 func ExampleRule_AfterEqual() { 1178 type BizReq struct { 1179 Time1 string 1180 Time2 string `v:"after-equal:Time1"` 1181 Time3 string `v:"after-equal:Time1"` 1182 } 1183 var ( 1184 ctx = context.Background() 1185 req = BizReq{ 1186 Time1: "2022-09-02", 1187 Time2: "2022-09-01", 1188 Time3: "2022-09-02", 1189 } 1190 ) 1191 if err := g.Validator().Data(req).Run(ctx); err != nil { 1192 fmt.Print(gstr.Join(err.Strings(), "\n")) 1193 } 1194 1195 // Output: 1196 // The Time2 value `2022-09-01` must be after or equal to field Time1 value `2022-09-02` 1197 } 1198 1199 func ExampleRule_Before() { 1200 type BizReq struct { 1201 Time1 string `v:"before:Time3"` 1202 Time2 string `v:"before:Time3"` 1203 Time3 string 1204 } 1205 var ( 1206 ctx = context.Background() 1207 req = BizReq{ 1208 Time1: "2022-09-02", 1209 Time2: "2022-09-03", 1210 Time3: "2022-09-03", 1211 } 1212 ) 1213 if err := g.Validator().Data(req).Run(ctx); err != nil { 1214 fmt.Println(err.String()) 1215 } 1216 1217 // Output: 1218 // The Time2 value `2022-09-03` must be before field Time3 value `2022-09-03` 1219 } 1220 1221 func ExampleRule_BeforeEqual() { 1222 type BizReq struct { 1223 Time1 string `v:"before-equal:Time3"` 1224 Time2 string `v:"before-equal:Time3"` 1225 Time3 string 1226 } 1227 var ( 1228 ctx = context.Background() 1229 req = BizReq{ 1230 Time1: "2022-09-02", 1231 Time2: "2022-09-01", 1232 Time3: "2022-09-01", 1233 } 1234 ) 1235 if err := g.Validator().Data(req).Run(ctx); err != nil { 1236 fmt.Print(gstr.Join(err.Strings(), "\n")) 1237 } 1238 1239 // Output: 1240 // The Time1 value `2022-09-02` must be before or equal to field Time3 1241 } 1242 1243 func ExampleRule_Array() { 1244 type BizReq struct { 1245 Value1 string `v:"array"` 1246 Value2 string `v:"array"` 1247 Value3 string `v:"array"` 1248 Value4 []string `v:"array"` 1249 } 1250 var ( 1251 ctx = context.Background() 1252 req = BizReq{ 1253 Value1: "1,2,3", 1254 Value2: "[]", 1255 Value3: "[1,2,3]", 1256 Value4: []string{}, 1257 } 1258 ) 1259 if err := g.Validator().Data(req).Run(ctx); err != nil { 1260 fmt.Print(gstr.Join(err.Strings(), "\n")) 1261 } 1262 1263 // Output: 1264 // The Value1 value `1,2,3` is not of valid array type 1265 } 1266 1267 func ExampleRule_EQ() { 1268 type BizReq struct { 1269 Name string `v:"required"` 1270 Password string `v:"required|eq:Password2"` 1271 Password2 string `v:"required"` 1272 } 1273 var ( 1274 ctx = context.Background() 1275 req = BizReq{ 1276 Name: "gf", 1277 Password: "goframe.org", 1278 Password2: "goframe.net", 1279 } 1280 ) 1281 if err := g.Validator().Data(req).Run(ctx); err != nil { 1282 fmt.Println(err) 1283 } 1284 1285 // Output: 1286 // The Password value `goframe.org` must be equal to field Password2 value `goframe.net` 1287 } 1288 1289 func ExampleRule_NotEQ() { 1290 type BizReq struct { 1291 Name string `v:"required"` 1292 MailAddr string `v:"required"` 1293 OtherMailAddr string `v:"required|not-eq:MailAddr"` 1294 } 1295 var ( 1296 ctx = context.Background() 1297 req = BizReq{ 1298 Name: "gf", 1299 MailAddr: "gf@goframe.org", 1300 OtherMailAddr: "gf@goframe.org", 1301 } 1302 ) 1303 if err := g.Validator().Data(req).Run(ctx); err != nil { 1304 fmt.Println(err) 1305 } 1306 1307 // Output: 1308 // The OtherMailAddr value `gf@goframe.org` must not be equal to field MailAddr value `gf@goframe.org` 1309 } 1310 1311 func ExampleRule_GT() { 1312 type BizReq struct { 1313 Value1 int 1314 Value2 int `v:"gt:Value1"` 1315 Value3 int `v:"gt:Value1"` 1316 } 1317 var ( 1318 ctx = context.Background() 1319 req = BizReq{ 1320 Value1: 1, 1321 Value2: 1, 1322 Value3: 2, 1323 } 1324 ) 1325 if err := g.Validator().Data(req).Run(ctx); err != nil { 1326 fmt.Println(err.String()) 1327 } 1328 1329 // Output: 1330 // The Value2 value `1` must be greater than field Value1 value `1` 1331 } 1332 1333 func ExampleRule_GTE() { 1334 type BizReq struct { 1335 Value1 int 1336 Value2 int `v:"gte:Value1"` 1337 Value3 int `v:"gte:Value1"` 1338 } 1339 var ( 1340 ctx = context.Background() 1341 req = BizReq{ 1342 Value1: 2, 1343 Value2: 1, 1344 Value3: 2, 1345 } 1346 ) 1347 if err := g.Validator().Data(req).Run(ctx); err != nil { 1348 fmt.Println(err.String()) 1349 } 1350 1351 // Output: 1352 // The Value2 value `1` must be greater than or equal to field Value1 value `2` 1353 } 1354 1355 func ExampleRule_LT() { 1356 type BizReq struct { 1357 Value1 int 1358 Value2 int `v:"lt:Value1"` 1359 Value3 int `v:"lt:Value1"` 1360 } 1361 var ( 1362 ctx = context.Background() 1363 req = BizReq{ 1364 Value1: 2, 1365 Value2: 1, 1366 Value3: 2, 1367 } 1368 ) 1369 if err := g.Validator().Data(req).Run(ctx); err != nil { 1370 fmt.Println(err.String()) 1371 } 1372 1373 // Output: 1374 // The Value3 value `2` must be lesser than field Value1 value `2` 1375 } 1376 1377 func ExampleRule_LTE() { 1378 type BizReq struct { 1379 Value1 int 1380 Value2 int `v:"lte:Value1"` 1381 Value3 int `v:"lte:Value1"` 1382 } 1383 var ( 1384 ctx = context.Background() 1385 req = BizReq{ 1386 Value1: 1, 1387 Value2: 1, 1388 Value3: 2, 1389 } 1390 ) 1391 if err := g.Validator().Data(req).Run(ctx); err != nil { 1392 fmt.Println(err.String()) 1393 } 1394 1395 // Output: 1396 // The Value3 value `2` must be lesser than or equal to field Value1 value `1` 1397 } 1398 1399 func ExampleRule_Foreach() { 1400 type BizReq struct { 1401 Value1 []int `v:"foreach|in:1,2,3"` 1402 Value2 []int `v:"foreach|in:1,2,3"` 1403 } 1404 var ( 1405 ctx = context.Background() 1406 req = BizReq{ 1407 Value1: []int{1, 2, 3}, 1408 Value2: []int{3, 4, 5}, 1409 } 1410 ) 1411 if err := g.Validator().Bail().Data(req).Run(ctx); err != nil { 1412 fmt.Println(err.String()) 1413 } 1414 1415 // Output: 1416 // The Value2 value `4` is not in acceptable range: 1,2,3 1417 }