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