1/* 2Package validator implements value validations for structs and individual fields 3based on tags. 4 5It can also handle Cross-Field and Cross-Struct validation for nested structs 6and has the ability to dive into arrays and maps of any type. 7 8see more examples https://github.com/go-playground/validator/tree/master/_examples 9 10Validation Functions Return Type error 11 12Doing things this way is actually the way the standard library does, see the 13file.Open method here: 14 15 https://golang.org/pkg/os/#Open. 16 17The authors return type "error" to avoid the issue discussed in the following, 18where err is always != nil: 19 20 http://stackoverflow.com/a/29138676/3158232 21 https://github.com/go-playground/validator/issues/134 22 23Validator only InvalidValidationError for bad validation input, nil or 24ValidationErrors as type error; so, in your code all you need to do is check 25if the error returned is not nil, and if it's not check if error is 26InvalidValidationError ( if necessary, most of the time it isn't ) type cast 27it to type ValidationErrors like so err.(validator.ValidationErrors). 28 29Custom Validation Functions 30 31Custom Validation functions can be added. Example: 32 33 // Structure 34 func customFunc(fl validator.FieldLevel) bool { 35 36 if fl.Field().String() == "invalid" { 37 return false 38 } 39 40 return true 41 } 42 43 validate.RegisterValidation("custom tag name", customFunc) 44 // NOTES: using the same tag name as an existing function 45 // will overwrite the existing one 46 47Cross-Field Validation 48 49Cross-Field Validation can be done via the following tags: 50 - eqfield 51 - nefield 52 - gtfield 53 - gtefield 54 - ltfield 55 - ltefield 56 - eqcsfield 57 - necsfield 58 - gtcsfield 59 - gtecsfield 60 - ltcsfield 61 - ltecsfield 62 63If, however, some custom cross-field validation is required, it can be done 64using a custom validation. 65 66Why not just have cross-fields validation tags (i.e. only eqcsfield and not 67eqfield)? 68 69The reason is efficiency. If you want to check a field within the same struct 70"eqfield" only has to find the field on the same struct (1 level). But, if we 71used "eqcsfield" it could be multiple levels down. Example: 72 73 type Inner struct { 74 StartDate time.Time 75 } 76 77 type Outer struct { 78 InnerStructField *Inner 79 CreatedAt time.Time `validate:"ltecsfield=InnerStructField.StartDate"` 80 } 81 82 now := time.Now() 83 84 inner := &Inner{ 85 StartDate: now, 86 } 87 88 outer := &Outer{ 89 InnerStructField: inner, 90 CreatedAt: now, 91 } 92 93 errs := validate.Struct(outer) 94 95 // NOTE: when calling validate.Struct(val) topStruct will be the top level struct passed 96 // into the function 97 // when calling validate.VarWithValue(val, field, tag) val will be 98 // whatever you pass, struct, field... 99 // when calling validate.Field(field, tag) val will be nil 100 101Multiple Validators 102 103Multiple validators on a field will process in the order defined. Example: 104 105 type Test struct { 106 Field `validate:"max=10,min=1"` 107 } 108 109 // max will be checked then min 110 111Bad Validator definitions are not handled by the library. Example: 112 113 type Test struct { 114 Field `validate:"min=10,max=0"` 115 } 116 117 // this definition of min max will never succeed 118 119Using Validator Tags 120 121Baked In Cross-Field validation only compares fields on the same struct. 122If Cross-Field + Cross-Struct validation is needed you should implement your 123own custom validator. 124 125Comma (",") is the default separator of validation tags. If you wish to 126have a comma included within the parameter (i.e. excludesall=,) you will need to 127use the UTF-8 hex representation 0x2C, which is replaced in the code as a comma, 128so the above will become excludesall=0x2C. 129 130 type Test struct { 131 Field `validate:"excludesall=,"` // BAD! Do not include a comma. 132 Field `validate:"excludesall=0x2C"` // GOOD! Use the UTF-8 hex representation. 133 } 134 135Pipe ("|") is the 'or' validation tags deparator. If you wish to 136have a pipe included within the parameter i.e. excludesall=| you will need to 137use the UTF-8 hex representation 0x7C, which is replaced in the code as a pipe, 138so the above will become excludesall=0x7C 139 140 type Test struct { 141 Field `validate:"excludesall=|"` // BAD! Do not include a a pipe! 142 Field `validate:"excludesall=0x7C"` // GOOD! Use the UTF-8 hex representation. 143 } 144 145 146Baked In Validators and Tags 147 148Here is a list of the current built in validators: 149 150 151Skip Field 152 153Tells the validation to skip this struct field; this is particularly 154handy in ignoring embedded structs from being validated. (Usage: -) 155 Usage: - 156 157 158Or Operator 159 160This is the 'or' operator allowing multiple validators to be used and 161accepted. (Usage: rgb|rgba) <-- this would allow either rgb or rgba 162colors to be accepted. This can also be combined with 'and' for example 163( Usage: omitempty,rgb|rgba) 164 165 Usage: | 166 167StructOnly 168 169When a field that is a nested struct is encountered, and contains this flag 170any validation on the nested struct will be run, but none of the nested 171struct fields will be validated. This is useful if inside of your program 172you know the struct will be valid, but need to verify it has been assigned. 173NOTE: only "required" and "omitempty" can be used on a struct itself. 174 175 Usage: structonly 176 177NoStructLevel 178 179Same as structonly tag except that any struct level validations will not run. 180 181 Usage: nostructlevel 182 183Omit Empty 184 185Allows conditional validation, for example if a field is not set with 186a value (Determined by the "required" validator) then other validation 187such as min or max won't run, but if a value is set validation will run. 188 189 Usage: omitempty 190 191Dive 192 193This tells the validator to dive into a slice, array or map and validate that 194level of the slice, array or map with the validation tags that follow. 195Multidimensional nesting is also supported, each level you wish to dive will 196require another dive tag. dive has some sub-tags, 'keys' & 'endkeys', please see 197the Keys & EndKeys section just below. 198 199 Usage: dive 200 201Example #1 202 203 [][]string with validation tag "gt=0,dive,len=1,dive,required" 204 // gt=0 will be applied to [] 205 // len=1 will be applied to []string 206 // required will be applied to string 207 208Example #2 209 210 [][]string with validation tag "gt=0,dive,dive,required" 211 // gt=0 will be applied to [] 212 // []string will be spared validation 213 // required will be applied to string 214 215Keys & EndKeys 216 217These are to be used together directly after the dive tag and tells the validator 218that anything between 'keys' and 'endkeys' applies to the keys of a map and not the 219values; think of it like the 'dive' tag, but for map keys instead of values. 220Multidimensional nesting is also supported, each level you wish to validate will 221require another 'keys' and 'endkeys' tag. These tags are only valid for maps. 222 223 Usage: dive,keys,othertagvalidation(s),endkeys,valuevalidationtags 224 225Example #1 226 227 map[string]string with validation tag "gt=0,dive,keys,eg=1|eq=2,endkeys,required" 228 // gt=0 will be applied to the map itself 229 // eg=1|eq=2 will be applied to the map keys 230 // required will be applied to map values 231 232Example #2 233 234 map[[2]string]string with validation tag "gt=0,dive,keys,dive,eq=1|eq=2,endkeys,required" 235 // gt=0 will be applied to the map itself 236 // eg=1|eq=2 will be applied to each array element in the the map keys 237 // required will be applied to map values 238 239Required 240 241This validates that the value is not the data types default zero value. 242For numbers ensures value is not zero. For strings ensures value is 243not "". For slices, maps, pointers, interfaces, channels and functions 244ensures the value is not nil. 245 246 Usage: required 247 248Required With 249 250The field under validation must be present and not empty only if any 251of the other specified fields are present. For strings ensures value is 252not "". For slices, maps, pointers, interfaces, channels and functions 253ensures the value is not nil. 254 255 Usage: required_with 256 257Examples: 258 259 // require the field if the Field1 is present: 260 Usage: required_with=Field1 261 262 // require the field if the Field1 or Field2 is present: 263 Usage: required_with=Field1 Field2 264 265Required With All 266 267The field under validation must be present and not empty only if all 268of the other specified fields are present. For strings ensures value is 269not "". For slices, maps, pointers, interfaces, channels and functions 270ensures the value is not nil. 271 272 Usage: required_with_all 273 274Example: 275 276 // require the field if the Field1 and Field2 is present: 277 Usage: required_with_all=Field1 Field2 278 279Required Without 280 281The field under validation must be present and not empty only when any 282of the other specified fields are not present. For strings ensures value is 283not "". For slices, maps, pointers, interfaces, channels and functions 284ensures the value is not nil. 285 286 Usage: required_without 287 288Examples: 289 290 // require the field if the Field1 is not present: 291 Usage: required_without=Field1 292 293 // require the field if the Field1 or Field2 is not present: 294 Usage: required_without=Field1 Field2 295 296Required Without All 297 298The field under validation must be present and not empty only when all 299of the other specified fields are not present. For strings ensures value is 300not "". For slices, maps, pointers, interfaces, channels and functions 301ensures the value is not nil. 302 303 Usage: required_without_all 304 305Example: 306 307 // require the field if the Field1 and Field2 is not present: 308 Usage: required_without_all=Field1 Field2 309 310Is Default 311 312This validates that the value is the default value and is almost the 313opposite of required. 314 315 Usage: isdefault 316 317Length 318 319For numbers, length will ensure that the value is 320equal to the parameter given. For strings, it checks that 321the string length is exactly that number of characters. For slices, 322arrays, and maps, validates the number of items. 323 324 Usage: len=10 325 326Maximum 327 328For numbers, max will ensure that the value is 329less than or equal to the parameter given. For strings, it checks 330that the string length is at most that number of characters. For 331slices, arrays, and maps, validates the number of items. 332 333 Usage: max=10 334 335Minimum 336 337For numbers, min will ensure that the value is 338greater or equal to the parameter given. For strings, it checks that 339the string length is at least that number of characters. For slices, 340arrays, and maps, validates the number of items. 341 342 Usage: min=10 343 344Equals 345 346For strings & numbers, eq will ensure that the value is 347equal to the parameter given. For slices, arrays, and maps, 348validates the number of items. 349 350 Usage: eq=10 351 352Not Equal 353 354For strings & numbers, ne will ensure that the value is not 355equal to the parameter given. For slices, arrays, and maps, 356validates the number of items. 357 358 Usage: ne=10 359 360One Of 361 362For strings, ints, and uints, oneof will ensure that the value 363is one of the values in the parameter. The parameter should be 364a list of values separated by whitespace. Values may be 365strings or numbers. To match strings with spaces in them, include 366the target string between single quotes. 367 368 Usage: oneof=red green 369 oneof='red green' 'blue yellow' 370 oneof=5 7 9 371 372Greater Than 373 374For numbers, this will ensure that the value is greater than the 375parameter given. For strings, it checks that the string length 376is greater than that number of characters. For slices, arrays 377and maps it validates the number of items. 378 379Example #1 380 381 Usage: gt=10 382 383Example #2 (time.Time) 384 385For time.Time ensures the time value is greater than time.Now.UTC(). 386 387 Usage: gt 388 389Greater Than or Equal 390 391Same as 'min' above. Kept both to make terminology with 'len' easier. 392 393 394Example #1 395 396 Usage: gte=10 397 398Example #2 (time.Time) 399 400For time.Time ensures the time value is greater than or equal to time.Now.UTC(). 401 402 Usage: gte 403 404Less Than 405 406For numbers, this will ensure that the value is less than the parameter given. 407For strings, it checks that the string length is less than that number of 408characters. For slices, arrays, and maps it validates the number of items. 409 410Example #1 411 412 Usage: lt=10 413 414Example #2 (time.Time) 415For time.Time ensures the time value is less than time.Now.UTC(). 416 417 Usage: lt 418 419Less Than or Equal 420 421Same as 'max' above. Kept both to make terminology with 'len' easier. 422 423Example #1 424 425 Usage: lte=10 426 427Example #2 (time.Time) 428 429For time.Time ensures the time value is less than or equal to time.Now.UTC(). 430 431 Usage: lte 432 433Field Equals Another Field 434 435This will validate the field value against another fields value either within 436a struct or passed in field. 437 438Example #1: 439 440 // Validation on Password field using: 441 Usage: eqfield=ConfirmPassword 442 443Example #2: 444 445 // Validating by field: 446 validate.VarWithValue(password, confirmpassword, "eqfield") 447 448Field Equals Another Field (relative) 449 450This does the same as eqfield except that it validates the field provided relative 451to the top level struct. 452 453 Usage: eqcsfield=InnerStructField.Field) 454 455Field Does Not Equal Another Field 456 457This will validate the field value against another fields value either within 458a struct or passed in field. 459 460Examples: 461 462 // Confirm two colors are not the same: 463 // 464 // Validation on Color field: 465 Usage: nefield=Color2 466 467 // Validating by field: 468 validate.VarWithValue(color1, color2, "nefield") 469 470Field Does Not Equal Another Field (relative) 471 472This does the same as nefield except that it validates the field provided 473relative to the top level struct. 474 475 Usage: necsfield=InnerStructField.Field 476 477Field Greater Than Another Field 478 479Only valid for Numbers and time.Time types, this will validate the field value 480against another fields value either within a struct or passed in field. 481usage examples are for validation of a Start and End date: 482 483Example #1: 484 485 // Validation on End field using: 486 validate.Struct Usage(gtfield=Start) 487 488Example #2: 489 490 // Validating by field: 491 validate.VarWithValue(start, end, "gtfield") 492 493 494Field Greater Than Another Relative Field 495 496This does the same as gtfield except that it validates the field provided 497relative to the top level struct. 498 499 Usage: gtcsfield=InnerStructField.Field 500 501Field Greater Than or Equal To Another Field 502 503Only valid for Numbers and time.Time types, this will validate the field value 504against another fields value either within a struct or passed in field. 505usage examples are for validation of a Start and End date: 506 507Example #1: 508 509 // Validation on End field using: 510 validate.Struct Usage(gtefield=Start) 511 512Example #2: 513 514 // Validating by field: 515 validate.VarWithValue(start, end, "gtefield") 516 517Field Greater Than or Equal To Another Relative Field 518 519This does the same as gtefield except that it validates the field provided relative 520to the top level struct. 521 522 Usage: gtecsfield=InnerStructField.Field 523 524Less Than Another Field 525 526Only valid for Numbers and time.Time types, this will validate the field value 527against another fields value either within a struct or passed in field. 528usage examples are for validation of a Start and End date: 529 530Example #1: 531 532 // Validation on End field using: 533 validate.Struct Usage(ltfield=Start) 534 535Example #2: 536 537 // Validating by field: 538 validate.VarWithValue(start, end, "ltfield") 539 540Less Than Another Relative Field 541 542This does the same as ltfield except that it validates the field provided relative 543to the top level struct. 544 545 Usage: ltcsfield=InnerStructField.Field 546 547Less Than or Equal To Another Field 548 549Only valid for Numbers and time.Time types, this will validate the field value 550against another fields value either within a struct or passed in field. 551usage examples are for validation of a Start and End date: 552 553Example #1: 554 555 // Validation on End field using: 556 validate.Struct Usage(ltefield=Start) 557 558Example #2: 559 560 // Validating by field: 561 validate.VarWithValue(start, end, "ltefield") 562 563Less Than or Equal To Another Relative Field 564 565This does the same as ltefield except that it validates the field provided relative 566to the top level struct. 567 568 Usage: ltecsfield=InnerStructField.Field 569 570Field Contains Another Field 571 572This does the same as contains except for struct fields. It should only be used 573with string types. See the behavior of reflect.Value.String() for behavior on 574other types. 575 576 Usage: containsfield=InnerStructField.Field 577 578Field Excludes Another Field 579 580This does the same as excludes except for struct fields. It should only be used 581with string types. See the behavior of reflect.Value.String() for behavior on 582other types. 583 584 Usage: excludesfield=InnerStructField.Field 585 586Unique 587 588For arrays & slices, unique will ensure that there are no duplicates. 589For maps, unique will ensure that there are no duplicate values. 590For slices of struct, unique will ensure that there are no duplicate values 591in a field of the struct specified via a parameter. 592 593 // For arrays, slices, and maps: 594 Usage: unique 595 596 // For slices of struct: 597 Usage: unique=field 598 599Alpha Only 600 601This validates that a string value contains ASCII alpha characters only 602 603 Usage: alpha 604 605Alphanumeric 606 607This validates that a string value contains ASCII alphanumeric characters only 608 609 Usage: alphanum 610 611Alpha Unicode 612 613This validates that a string value contains unicode alpha characters only 614 615 Usage: alphaunicode 616 617Alphanumeric Unicode 618 619This validates that a string value contains unicode alphanumeric characters only 620 621 Usage: alphanumunicode 622 623Numeric 624 625This validates that a string value contains a basic numeric value. 626basic excludes exponents etc... 627for integers or float it returns true. 628 629 Usage: numeric 630 631Hexadecimal String 632 633This validates that a string value contains a valid hexadecimal. 634 635 Usage: hexadecimal 636 637Hexcolor String 638 639This validates that a string value contains a valid hex color including 640hashtag (#) 641 642 Usage: hexcolor 643 644Lowercase String 645 646This validates that a string value contains only lowercase characters. An empty string is not a valid lowercase string. 647 648 Usage: lowercase 649 650Uppercase String 651 652This validates that a string value contains only uppercase characters. An empty string is not a valid uppercase string. 653 654 Usage: uppercase 655 656RGB String 657 658This validates that a string value contains a valid rgb color 659 660 Usage: rgb 661 662RGBA String 663 664This validates that a string value contains a valid rgba color 665 666 Usage: rgba 667 668HSL String 669 670This validates that a string value contains a valid hsl color 671 672 Usage: hsl 673 674HSLA String 675 676This validates that a string value contains a valid hsla color 677 678 Usage: hsla 679 680E-mail String 681 682This validates that a string value contains a valid email 683This may not conform to all possibilities of any rfc standard, but neither 684does any email provider accept all possibilities. 685 686 Usage: email 687 688JSON String 689 690This validates that a string value is valid JSON 691 692 Usage: json 693 694File path 695 696This validates that a string value contains a valid file path and that 697the file exists on the machine. 698This is done using os.Stat, which is a platform independent function. 699 700 Usage: file 701 702URL String 703 704This validates that a string value contains a valid url 705This will accept any url the golang request uri accepts but must contain 706a schema for example http:// or rtmp:// 707 708 Usage: url 709 710URI String 711 712This validates that a string value contains a valid uri 713This will accept any uri the golang request uri accepts 714 715 Usage: uri 716 717Urn RFC 2141 String 718 719This validataes that a string value contains a valid URN 720according to the RFC 2141 spec. 721 722 Usage: urn_rfc2141 723 724Base64 String 725 726This validates that a string value contains a valid base64 value. 727Although an empty string is valid base64 this will report an empty string 728as an error, if you wish to accept an empty string as valid you can use 729this with the omitempty tag. 730 731 Usage: base64 732 733Base64URL String 734 735This validates that a string value contains a valid base64 URL safe value 736according the the RFC4648 spec. 737Although an empty string is a valid base64 URL safe value, this will report 738an empty string as an error, if you wish to accept an empty string as valid 739you can use this with the omitempty tag. 740 741 Usage: base64url 742 743Bitcoin Address 744 745This validates that a string value contains a valid bitcoin address. 746The format of the string is checked to ensure it matches one of the three formats 747P2PKH, P2SH and performs checksum validation. 748 749 Usage: btc_addr 750 751Bitcoin Bech32 Address (segwit) 752 753This validates that a string value contains a valid bitcoin Bech32 address as defined 754by bip-0173 (https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) 755Special thanks to Pieter Wuille for providng reference implementations. 756 757 Usage: btc_addr_bech32 758 759Ethereum Address 760 761This validates that a string value contains a valid ethereum address. 762The format of the string is checked to ensure it matches the standard Ethereum address format 763Full validation is blocked by https://github.com/golang/crypto/pull/28 764 765 Usage: eth_addr 766 767Contains 768 769This validates that a string value contains the substring value. 770 771 Usage: contains=@ 772 773Contains Any 774 775This validates that a string value contains any Unicode code points 776in the substring value. 777 778 Usage: containsany=!@#? 779 780Contains Rune 781 782This validates that a string value contains the supplied rune value. 783 784 Usage: containsrune=@ 785 786Excludes 787 788This validates that a string value does not contain the substring value. 789 790 Usage: excludes=@ 791 792Excludes All 793 794This validates that a string value does not contain any Unicode code 795points in the substring value. 796 797 Usage: excludesall=!@#? 798 799Excludes Rune 800 801This validates that a string value does not contain the supplied rune value. 802 803 Usage: excludesrune=@ 804 805Starts With 806 807This validates that a string value starts with the supplied string value 808 809 Usage: startswith=hello 810 811Ends With 812 813This validates that a string value ends with the supplied string value 814 815 Usage: endswith=goodbye 816 817International Standard Book Number 818 819This validates that a string value contains a valid isbn10 or isbn13 value. 820 821 Usage: isbn 822 823International Standard Book Number 10 824 825This validates that a string value contains a valid isbn10 value. 826 827 Usage: isbn10 828 829International Standard Book Number 13 830 831This validates that a string value contains a valid isbn13 value. 832 833 Usage: isbn13 834 835Universally Unique Identifier UUID 836 837This validates that a string value contains a valid UUID. Uppercase UUID values will not pass - use `uuid_rfc4122` instead. 838 839 Usage: uuid 840 841Universally Unique Identifier UUID v3 842 843This validates that a string value contains a valid version 3 UUID. Uppercase UUID values will not pass - use `uuid3_rfc4122` instead. 844 845 Usage: uuid3 846 847Universally Unique Identifier UUID v4 848 849This validates that a string value contains a valid version 4 UUID. Uppercase UUID values will not pass - use `uuid4_rfc4122` instead. 850 851 Usage: uuid4 852 853Universally Unique Identifier UUID v5 854 855This validates that a string value contains a valid version 5 UUID. Uppercase UUID values will not pass - use `uuid5_rfc4122` instead. 856 857 Usage: uuid5 858 859ASCII 860 861This validates that a string value contains only ASCII characters. 862NOTE: if the string is blank, this validates as true. 863 864 Usage: ascii 865 866Printable ASCII 867 868This validates that a string value contains only printable ASCII characters. 869NOTE: if the string is blank, this validates as true. 870 871 Usage: printascii 872 873Multi-Byte Characters 874 875This validates that a string value contains one or more multibyte characters. 876NOTE: if the string is blank, this validates as true. 877 878 Usage: multibyte 879 880Data URL 881 882This validates that a string value contains a valid DataURI. 883NOTE: this will also validate that the data portion is valid base64 884 885 Usage: datauri 886 887Latitude 888 889This validates that a string value contains a valid latitude. 890 891 Usage: latitude 892 893Longitude 894 895This validates that a string value contains a valid longitude. 896 897 Usage: longitude 898 899Social Security Number SSN 900 901This validates that a string value contains a valid U.S. Social Security Number. 902 903 Usage: ssn 904 905Internet Protocol Address IP 906 907This validates that a string value contains a valid IP Address. 908 909 Usage: ip 910 911Internet Protocol Address IPv4 912 913This validates that a string value contains a valid v4 IP Address. 914 915 Usage: ipv4 916 917Internet Protocol Address IPv6 918 919This validates that a string value contains a valid v6 IP Address. 920 921 Usage: ipv6 922 923Classless Inter-Domain Routing CIDR 924 925This validates that a string value contains a valid CIDR Address. 926 927 Usage: cidr 928 929Classless Inter-Domain Routing CIDRv4 930 931This validates that a string value contains a valid v4 CIDR Address. 932 933 Usage: cidrv4 934 935Classless Inter-Domain Routing CIDRv6 936 937This validates that a string value contains a valid v6 CIDR Address. 938 939 Usage: cidrv6 940 941Transmission Control Protocol Address TCP 942 943This validates that a string value contains a valid resolvable TCP Address. 944 945 Usage: tcp_addr 946 947Transmission Control Protocol Address TCPv4 948 949This validates that a string value contains a valid resolvable v4 TCP Address. 950 951 Usage: tcp4_addr 952 953Transmission Control Protocol Address TCPv6 954 955This validates that a string value contains a valid resolvable v6 TCP Address. 956 957 Usage: tcp6_addr 958 959User Datagram Protocol Address UDP 960 961This validates that a string value contains a valid resolvable UDP Address. 962 963 Usage: udp_addr 964 965User Datagram Protocol Address UDPv4 966 967This validates that a string value contains a valid resolvable v4 UDP Address. 968 969 Usage: udp4_addr 970 971User Datagram Protocol Address UDPv6 972 973This validates that a string value contains a valid resolvable v6 UDP Address. 974 975 Usage: udp6_addr 976 977Internet Protocol Address IP 978 979This validates that a string value contains a valid resolvable IP Address. 980 981 Usage: ip_addr 982 983Internet Protocol Address IPv4 984 985This validates that a string value contains a valid resolvable v4 IP Address. 986 987 Usage: ip4_addr 988 989Internet Protocol Address IPv6 990 991This validates that a string value contains a valid resolvable v6 IP Address. 992 993 Usage: ip6_addr 994 995Unix domain socket end point Address 996 997This validates that a string value contains a valid Unix Address. 998 999 Usage: unix_addr 1000 1001Media Access Control Address MAC 1002 1003This validates that a string value contains a valid MAC Address. 1004 1005 Usage: mac 1006 1007Note: See Go's ParseMAC for accepted formats and types: 1008 1009 http://golang.org/src/net/mac.go?s=866:918#L29 1010 1011Hostname RFC 952 1012 1013This validates that a string value is a valid Hostname according to RFC 952 https://tools.ietf.org/html/rfc952 1014 1015 Usage: hostname 1016 1017Hostname RFC 1123 1018 1019This validates that a string value is a valid Hostname according to RFC 1123 https://tools.ietf.org/html/rfc1123 1020 1021 Usage: hostname_rfc1123 or if you want to continue to use 'hostname' in your tags, create an alias. 1022 1023Full Qualified Domain Name (FQDN) 1024 1025This validates that a string value contains a valid FQDN. 1026 1027 Usage: fqdn 1028 1029HTML Tags 1030 1031This validates that a string value appears to be an HTML element tag 1032including those described at https://developer.mozilla.org/en-US/docs/Web/HTML/Element 1033 1034 Usage: html 1035 1036HTML Encoded 1037 1038This validates that a string value is a proper character reference in decimal 1039or hexadecimal format 1040 1041 Usage: html_encoded 1042 1043URL Encoded 1044 1045This validates that a string value is percent-encoded (URL encoded) according 1046to https://tools.ietf.org/html/rfc3986#section-2.1 1047 1048 Usage: url_encoded 1049 1050Directory 1051 1052This validates that a string value contains a valid directory and that 1053it exists on the machine. 1054This is done using os.Stat, which is a platform independent function. 1055 1056 Usage: dir 1057 1058HostPort 1059 1060This validates that a string value contains a valid DNS hostname and port that 1061can be used to valiate fields typically passed to sockets and connections. 1062 1063 Usage: hostname_port 1064 1065Datetime 1066 1067This validates that a string value is a valid datetime based on the supplied datetime format. 1068Supplied format must match the official Go time format layout as documented in https://golang.org/pkg/time/ 1069 1070 Usage: datetime=2006-01-02 1071 1072Alias Validators and Tags 1073 1074NOTE: When returning an error, the tag returned in "FieldError" will be 1075the alias tag unless the dive tag is part of the alias. Everything after the 1076dive tag is not reported as the alias tag. Also, the "ActualTag" in the before 1077case will be the actual tag within the alias that failed. 1078 1079Here is a list of the current built in alias tags: 1080 1081 "iscolor" 1082 alias is "hexcolor|rgb|rgba|hsl|hsla" (Usage: iscolor) 1083 1084Validator notes: 1085 1086 regex 1087 a regex validator won't be added because commas and = signs can be part 1088 of a regex which conflict with the validation definitions. Although 1089 workarounds can be made, they take away from using pure regex's. 1090 Furthermore it's quick and dirty but the regex's become harder to 1091 maintain and are not reusable, so it's as much a programming philosophy 1092 as anything. 1093 1094 In place of this new validator functions should be created; a regex can 1095 be used within the validator function and even be precompiled for better 1096 efficiency within regexes.go. 1097 1098 And the best reason, you can submit a pull request and we can keep on 1099 adding to the validation library of this package! 1100 1101Non standard validators 1102 1103A collection of validation rules that are frequently needed but are more 1104complex than the ones found in the baked in validators. 1105A non standard validator must be registered manually like you would 1106with your own custom validation functions. 1107 1108Example of registration and use: 1109 1110 type Test struct { 1111 TestField string `validate:"yourtag"` 1112 } 1113 1114 t := &Test{ 1115 TestField: "Test" 1116 } 1117 1118 validate := validator.New() 1119 validate.RegisterValidation("yourtag", validators.NotBlank) 1120 1121Here is a list of the current non standard validators: 1122 1123 NotBlank 1124 This validates that the value is not blank or with length zero. 1125 For strings ensures they do not contain only spaces. For channels, maps, slices and arrays 1126 ensures they don't have zero length. For others, a non empty value is required. 1127 1128 Usage: notblank 1129 1130Panics 1131 1132This package panics when bad input is provided, this is by design, bad code like 1133that should not make it to production. 1134 1135 type Test struct { 1136 TestField string `validate:"nonexistantfunction=1"` 1137 } 1138 1139 t := &Test{ 1140 TestField: "Test" 1141 } 1142 1143 validate.Struct(t) // this will panic 1144*/ 1145package validator 1146