doc.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. Package validator implements value validations for structs and individual fields
  3. based on tags.
  4. It can also handle Cross-Field and Cross-Struct validation for nested structs
  5. and has the ability to dive into arrays and maps of any type.
  6. see more examples https://github.com/go-playground/validator/tree/v9/_examples
  7. Validation Functions Return Type error
  8. Doing things this way is actually the way the standard library does, see the
  9. file.Open method here:
  10. https://golang.org/pkg/os/#Open.
  11. The authors return type "error" to avoid the issue discussed in the following,
  12. where err is always != nil:
  13. http://stackoverflow.com/a/29138676/3158232
  14. https://github.com/go-playground/validator/issues/134
  15. Validator only InvalidValidationError for bad validation input, nil or
  16. ValidationErrors as type error; so, in your code all you need to do is check
  17. if the error returned is not nil, and if it's not check if error is
  18. InvalidValidationError ( if necessary, most of the time it isn't ) type cast
  19. it to type ValidationErrors like so err.(validator.ValidationErrors).
  20. Custom Validation Functions
  21. Custom Validation functions can be added. Example:
  22. // Structure
  23. func customFunc(fl validator.FieldLevel) bool {
  24. if fl.Field().String() == "invalid" {
  25. return false
  26. }
  27. return true
  28. }
  29. validate.RegisterValidation("custom tag name", customFunc)
  30. // NOTES: using the same tag name as an existing function
  31. // will overwrite the existing one
  32. Cross-Field Validation
  33. Cross-Field Validation can be done via the following tags:
  34. - eqfield
  35. - nefield
  36. - gtfield
  37. - gtefield
  38. - ltfield
  39. - ltefield
  40. - eqcsfield
  41. - necsfield
  42. - gtcsfield
  43. - gtecsfield
  44. - ltcsfield
  45. - ltecsfield
  46. If, however, some custom cross-field validation is required, it can be done
  47. using a custom validation.
  48. Why not just have cross-fields validation tags (i.e. only eqcsfield and not
  49. eqfield)?
  50. The reason is efficiency. If you want to check a field within the same struct
  51. "eqfield" only has to find the field on the same struct (1 level). But, if we
  52. used "eqcsfield" it could be multiple levels down. Example:
  53. type Inner struct {
  54. StartDate time.Time
  55. }
  56. type Outer struct {
  57. InnerStructField *Inner
  58. CreatedAt time.Time `validate:"ltecsfield=InnerStructField.StartDate"`
  59. }
  60. now := time.Now()
  61. inner := &Inner{
  62. StartDate: now,
  63. }
  64. outer := &Outer{
  65. InnerStructField: inner,
  66. CreatedAt: now,
  67. }
  68. errs := validate.Struct(outer)
  69. // NOTE: when calling validate.Struct(val) topStruct will be the top level struct passed
  70. // into the function
  71. // when calling validate.VarWithValue(val, field, tag) val will be
  72. // whatever you pass, struct, field...
  73. // when calling validate.Field(field, tag) val will be nil
  74. Multiple Validators
  75. Multiple validators on a field will process in the order defined. Example:
  76. type Test struct {
  77. Field `validate:"max=10,min=1"`
  78. }
  79. // max will be checked then min
  80. Bad Validator definitions are not handled by the library. Example:
  81. type Test struct {
  82. Field `validate:"min=10,max=0"`
  83. }
  84. // this definition of min max will never succeed
  85. Using Validator Tags
  86. Baked In Cross-Field validation only compares fields on the same struct.
  87. If Cross-Field + Cross-Struct validation is needed you should implement your
  88. own custom validator.
  89. Comma (",") is the default separator of validation tags. If you wish to
  90. have a comma included within the parameter (i.e. excludesall=,) you will need to
  91. use the UTF-8 hex representation 0x2C, which is replaced in the code as a comma,
  92. so the above will become excludesall=0x2C.
  93. type Test struct {
  94. Field `validate:"excludesall=,"` // BAD! Do not include a comma.
  95. Field `validate:"excludesall=0x2C"` // GOOD! Use the UTF-8 hex representation.
  96. }
  97. Pipe ("|") is the 'or' validation tags deparator. If you wish to
  98. have a pipe included within the parameter i.e. excludesall=| you will need to
  99. use the UTF-8 hex representation 0x7C, which is replaced in the code as a pipe,
  100. so the above will become excludesall=0x7C
  101. type Test struct {
  102. Field `validate:"excludesall=|"` // BAD! Do not include a a pipe!
  103. Field `validate:"excludesall=0x7C"` // GOOD! Use the UTF-8 hex representation.
  104. }
  105. Baked In Validators and Tags
  106. Here is a list of the current built in validators:
  107. Skip Field
  108. Tells the validation to skip this struct field; this is particularly
  109. handy in ignoring embedded structs from being validated. (Usage: -)
  110. Usage: -
  111. Or Operator
  112. This is the 'or' operator allowing multiple validators to be used and
  113. accepted. (Usage: rbg|rgba) <-- this would allow either rgb or rgba
  114. colors to be accepted. This can also be combined with 'and' for example
  115. ( Usage: omitempty,rgb|rgba)
  116. Usage: |
  117. StructOnly
  118. When a field that is a nested struct is encountered, and contains this flag
  119. any validation on the nested struct will be run, but none of the nested
  120. struct fields will be validated. This is useful if inside of your program
  121. you know the struct will be valid, but need to verify it has been assigned.
  122. NOTE: only "required" and "omitempty" can be used on a struct itself.
  123. Usage: structonly
  124. NoStructLevel
  125. Same as structonly tag except that any struct level validations will not run.
  126. Usage: nostructlevel
  127. Omit Empty
  128. Allows conditional validation, for example if a field is not set with
  129. a value (Determined by the "required" validator) then other validation
  130. such as min or max won't run, but if a value is set validation will run.
  131. Usage: omitempty
  132. Dive
  133. This tells the validator to dive into a slice, array or map and validate that
  134. level of the slice, array or map with the validation tags that follow.
  135. Multidimensional nesting is also supported, each level you wish to dive will
  136. require another dive tag. dive has some sub-tags, 'keys' & 'endkeys', please see
  137. the Keys & EndKeys section just below.
  138. Usage: dive
  139. Example #1
  140. [][]string with validation tag "gt=0,dive,len=1,dive,required"
  141. // gt=0 will be applied to []
  142. // len=1 will be applied to []string
  143. // required will be applied to string
  144. Example #2
  145. [][]string with validation tag "gt=0,dive,dive,required"
  146. // gt=0 will be applied to []
  147. // []string will be spared validation
  148. // required will be applied to string
  149. Keys & EndKeys
  150. These are to be used together directly after the dive tag and tells the validator
  151. that anything between 'keys' and 'endkeys' applies to the keys of a map and not the
  152. values; think of it like the 'dive' tag, but for map keys instead of values.
  153. Multidimensional nesting is also supported, each level you wish to validate will
  154. require another 'keys' and 'endkeys' tag. These tags are only valid for maps.
  155. Usage: dive,keys,othertagvalidation(s),endkeys,valuevalidationtags
  156. Example #1
  157. map[string]string with validation tag "gt=0,dive,keys,eg=1|eq=2,endkeys,required"
  158. // gt=0 will be applied to the map itself
  159. // eg=1|eq=2 will be applied to the map keys
  160. // required will be applied to map values
  161. Example #2
  162. map[[2]string]string with validation tag "gt=0,dive,keys,dive,eq=1|eq=2,endkeys,required"
  163. // gt=0 will be applied to the map itself
  164. // eg=1|eq=2 will be applied to each array element in the the map keys
  165. // required will be applied to map values
  166. Required
  167. This validates that the value is not the data types default zero value.
  168. For numbers ensures value is not zero. For strings ensures value is
  169. not "". For slices, maps, pointers, interfaces, channels and functions
  170. ensures the value is not nil.
  171. Usage: required
  172. Required With
  173. The field under validation must be present and not empty only if any
  174. of the other specified fields are present. For strings ensures value is
  175. not "". For slices, maps, pointers, interfaces, channels and functions
  176. ensures the value is not nil.
  177. Usage: required_with
  178. Examples:
  179. // require the field if the Field1 is present:
  180. Usage: required_with=Field1
  181. // require the field if the Field1 or Field2 is present:
  182. Usage: required_with=Field1 Field2
  183. Required With All
  184. The field under validation must be present and not empty only if all
  185. of the other specified fields are present. For strings ensures value is
  186. not "". For slices, maps, pointers, interfaces, channels and functions
  187. ensures the value is not nil.
  188. Usage: required_with_all
  189. Example:
  190. // require the field if the Field1 and Field2 is present:
  191. Usage: required_with_all=Field1 Field2
  192. Required Without
  193. The field under validation must be present and not empty only when any
  194. of the other specified fields are not present. For strings ensures value is
  195. not "". For slices, maps, pointers, interfaces, channels and functions
  196. ensures the value is not nil.
  197. Usage: required_without
  198. Examples:
  199. // require the field if the Field1 is not present:
  200. Usage: required_without=Field1
  201. // require the field if the Field1 or Field2 is not present:
  202. Usage: required_without=Field1 Field2
  203. Required Without All
  204. The field under validation must be present and not empty only when all
  205. of the other specified fields are not present. For strings ensures value is
  206. not "". For slices, maps, pointers, interfaces, channels and functions
  207. ensures the value is not nil.
  208. Usage: required_without_all
  209. Example:
  210. // require the field if the Field1 and Field2 is not present:
  211. Usage: required_without_all=Field1 Field2
  212. Is Default
  213. This validates that the value is the default value and is almost the
  214. opposite of required.
  215. Usage: isdefault
  216. Length
  217. For numbers, length will ensure that the value is
  218. equal to the parameter given. For strings, it checks that
  219. the string length is exactly that number of characters. For slices,
  220. arrays, and maps, validates the number of items.
  221. Usage: len=10
  222. Maximum
  223. For numbers, max will ensure that the value is
  224. less than or equal to the parameter given. For strings, it checks
  225. that the string length is at most that number of characters. For
  226. slices, arrays, and maps, validates the number of items.
  227. Usage: max=10
  228. Minimum
  229. For numbers, min will ensure that the value is
  230. greater or equal to the parameter given. For strings, it checks that
  231. the string length is at least that number of characters. For slices,
  232. arrays, and maps, validates the number of items.
  233. Usage: min=10
  234. Equals
  235. For strings & numbers, eq will ensure that the value is
  236. equal to the parameter given. For slices, arrays, and maps,
  237. validates the number of items.
  238. Usage: eq=10
  239. Not Equal
  240. For strings & numbers, ne will ensure that the value is not
  241. equal to the parameter given. For slices, arrays, and maps,
  242. validates the number of items.
  243. Usage: ne=10
  244. One Of
  245. For strings, ints, and uints, oneof will ensure that the value
  246. is one of the values in the parameter. The parameter should be
  247. a list of values separated by whitespace. Values may be
  248. strings or numbers.
  249. Usage: oneof=red green
  250. oneof=5 7 9
  251. Greater Than
  252. For numbers, this will ensure that the value is greater than the
  253. parameter given. For strings, it checks that the string length
  254. is greater than that number of characters. For slices, arrays
  255. and maps it validates the number of items.
  256. Example #1
  257. Usage: gt=10
  258. Example #2 (time.Time)
  259. For time.Time ensures the time value is greater than time.Now.UTC().
  260. Usage: gt
  261. Greater Than or Equal
  262. Same as 'min' above. Kept both to make terminology with 'len' easier.
  263. Example #1
  264. Usage: gte=10
  265. Example #2 (time.Time)
  266. For time.Time ensures the time value is greater than or equal to time.Now.UTC().
  267. Usage: gte
  268. Less Than
  269. For numbers, this will ensure that the value is less than the parameter given.
  270. For strings, it checks that the string length is less than that number of
  271. characters. For slices, arrays, and maps it validates the number of items.
  272. Example #1
  273. Usage: lt=10
  274. Example #2 (time.Time)
  275. For time.Time ensures the time value is less than time.Now.UTC().
  276. Usage: lt
  277. Less Than or Equal
  278. Same as 'max' above. Kept both to make terminology with 'len' easier.
  279. Example #1
  280. Usage: lte=10
  281. Example #2 (time.Time)
  282. For time.Time ensures the time value is less than or equal to time.Now.UTC().
  283. Usage: lte
  284. Field Equals Another Field
  285. This will validate the field value against another fields value either within
  286. a struct or passed in field.
  287. Example #1:
  288. // Validation on Password field using:
  289. Usage: eqfield=ConfirmPassword
  290. Example #2:
  291. // Validating by field:
  292. validate.VarWithValue(password, confirmpassword, "eqfield")
  293. Field Equals Another Field (relative)
  294. This does the same as eqfield except that it validates the field provided relative
  295. to the top level struct.
  296. Usage: eqcsfield=InnerStructField.Field)
  297. Field Does Not Equal Another Field
  298. This will validate the field value against another fields value either within
  299. a struct or passed in field.
  300. Examples:
  301. // Confirm two colors are not the same:
  302. //
  303. // Validation on Color field:
  304. Usage: nefield=Color2
  305. // Validating by field:
  306. validate.VarWithValue(color1, color2, "nefield")
  307. Field Does Not Equal Another Field (relative)
  308. This does the same as nefield except that it validates the field provided
  309. relative to the top level struct.
  310. Usage: necsfield=InnerStructField.Field
  311. Field Greater Than Another Field
  312. Only valid for Numbers and time.Time types, this will validate the field value
  313. against another fields value either within a struct or passed in field.
  314. usage examples are for validation of a Start and End date:
  315. Example #1:
  316. // Validation on End field using:
  317. validate.Struct Usage(gtfield=Start)
  318. Example #2:
  319. // Validating by field:
  320. validate.VarWithValue(start, end, "gtfield")
  321. Field Greater Than Another Relative Field
  322. This does the same as gtfield except that it validates the field provided
  323. relative to the top level struct.
  324. Usage: gtcsfield=InnerStructField.Field
  325. Field Greater Than or Equal To Another Field
  326. Only valid for Numbers and time.Time types, this will validate the field value
  327. against another fields value either within a struct or passed in field.
  328. usage examples are for validation of a Start and End date:
  329. Example #1:
  330. // Validation on End field using:
  331. validate.Struct Usage(gtefield=Start)
  332. Example #2:
  333. // Validating by field:
  334. validate.VarWithValue(start, end, "gtefield")
  335. Field Greater Than or Equal To Another Relative Field
  336. This does the same as gtefield except that it validates the field provided relative
  337. to the top level struct.
  338. Usage: gtecsfield=InnerStructField.Field
  339. Less Than Another Field
  340. Only valid for Numbers and time.Time types, this will validate the field value
  341. against another fields value either within a struct or passed in field.
  342. usage examples are for validation of a Start and End date:
  343. Example #1:
  344. // Validation on End field using:
  345. validate.Struct Usage(ltfield=Start)
  346. Example #2:
  347. // Validating by field:
  348. validate.VarWithValue(start, end, "ltfield")
  349. Less Than Another Relative Field
  350. This does the same as ltfield except that it validates the field provided relative
  351. to the top level struct.
  352. Usage: ltcsfield=InnerStructField.Field
  353. Less Than or Equal To Another Field
  354. Only valid for Numbers and time.Time types, this will validate the field value
  355. against another fields value either within a struct or passed in field.
  356. usage examples are for validation of a Start and End date:
  357. Example #1:
  358. // Validation on End field using:
  359. validate.Struct Usage(ltefield=Start)
  360. Example #2:
  361. // Validating by field:
  362. validate.VarWithValue(start, end, "ltefield")
  363. Less Than or Equal To Another Relative Field
  364. This does the same as ltefield except that it validates the field provided relative
  365. to the top level struct.
  366. Usage: ltecsfield=InnerStructField.Field
  367. Field Contains Another Field
  368. This does the same as contains except for struct fields. It should only be used
  369. with string types. See the behavior of reflect.Value.String() for behavior on
  370. other types.
  371. Usage: containsfield=InnerStructField.Field
  372. Field Excludes Another Field
  373. This does the same as excludes except for struct fields. It should only be used
  374. with string types. See the behavior of reflect.Value.String() for behavior on
  375. other types.
  376. Usage: excludesfield=InnerStructField.Field
  377. Unique
  378. For arrays & slices, unique will ensure that there are no duplicates.
  379. For maps, unique will ensure that there are no duplicate values.
  380. Usage: unique
  381. Alpha Only
  382. This validates that a string value contains ASCII alpha characters only
  383. Usage: alpha
  384. Alphanumeric
  385. This validates that a string value contains ASCII alphanumeric characters only
  386. Usage: alphanum
  387. Alpha Unicode
  388. This validates that a string value contains unicode alpha characters only
  389. Usage: alphaunicode
  390. Alphanumeric Unicode
  391. This validates that a string value contains unicode alphanumeric characters only
  392. Usage: alphanumunicode
  393. Numeric
  394. This validates that a string value contains a basic numeric value.
  395. basic excludes exponents etc...
  396. for integers or float it returns true.
  397. Usage: numeric
  398. Hexadecimal String
  399. This validates that a string value contains a valid hexadecimal.
  400. Usage: hexadecimal
  401. Hexcolor String
  402. This validates that a string value contains a valid hex color including
  403. hashtag (#)
  404. Usage: hexcolor
  405. RGB String
  406. This validates that a string value contains a valid rgb color
  407. Usage: rgb
  408. RGBA String
  409. This validates that a string value contains a valid rgba color
  410. Usage: rgba
  411. HSL String
  412. This validates that a string value contains a valid hsl color
  413. Usage: hsl
  414. HSLA String
  415. This validates that a string value contains a valid hsla color
  416. Usage: hsla
  417. E-mail String
  418. This validates that a string value contains a valid email
  419. This may not conform to all possibilities of any rfc standard, but neither
  420. does any email provider accept all possibilities.
  421. Usage: email
  422. File path
  423. This validates that a string value contains a valid file path and that
  424. the file exists on the machine.
  425. This is done using os.Stat, which is a platform independent function.
  426. Usage: file
  427. URL String
  428. This validates that a string value contains a valid url
  429. This will accept any url the golang request uri accepts but must contain
  430. a schema for example http:// or rtmp://
  431. Usage: url
  432. URI String
  433. This validates that a string value contains a valid uri
  434. This will accept any uri the golang request uri accepts
  435. Usage: uri
  436. Urn RFC 2141 String
  437. This validataes that a string value contains a valid URN
  438. according to the RFC 2141 spec.
  439. Usage: urn_rfc2141
  440. Base64 String
  441. This validates that a string value contains a valid base64 value.
  442. Although an empty string is valid base64 this will report an empty string
  443. as an error, if you wish to accept an empty string as valid you can use
  444. this with the omitempty tag.
  445. Usage: base64
  446. Base64URL String
  447. This validates that a string value contains a valid base64 URL safe value
  448. according the the RFC4648 spec.
  449. Although an empty string is a valid base64 URL safe value, this will report
  450. an empty string as an error, if you wish to accept an empty string as valid
  451. you can use this with the omitempty tag.
  452. Usage: base64url
  453. Bitcoin Address
  454. This validates that a string value contains a valid bitcoin address.
  455. The format of the string is checked to ensure it matches one of the three formats
  456. P2PKH, P2SH and performs checksum validation.
  457. Usage: btc_addr
  458. Bitcoin Bech32 Address (segwit)
  459. This validates that a string value contains a valid bitcoin Bech32 address as defined
  460. by bip-0173 (https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki)
  461. Special thanks to Pieter Wuille for providng reference implementations.
  462. Usage: btc_addr_bech32
  463. Ethereum Address
  464. This validates that a string value contains a valid ethereum address.
  465. The format of the string is checked to ensure it matches the standard Ethereum address format
  466. Full validation is blocked by https://github.com/golang/crypto/pull/28
  467. Usage: eth_addr
  468. Contains
  469. This validates that a string value contains the substring value.
  470. Usage: contains=@
  471. Contains Any
  472. This validates that a string value contains any Unicode code points
  473. in the substring value.
  474. Usage: containsany=!@#?
  475. Contains Rune
  476. This validates that a string value contains the supplied rune value.
  477. Usage: containsrune=@
  478. Excludes
  479. This validates that a string value does not contain the substring value.
  480. Usage: excludes=@
  481. Excludes All
  482. This validates that a string value does not contain any Unicode code
  483. points in the substring value.
  484. Usage: excludesall=!@#?
  485. Excludes Rune
  486. This validates that a string value does not contain the supplied rune value.
  487. Usage: excludesrune=@
  488. Starts With
  489. This validates that a string value starts with the supplied string value
  490. Usage: startswith=hello
  491. Ends With
  492. This validates that a string value ends with the supplied string value
  493. Usage: endswith=goodbye
  494. International Standard Book Number
  495. This validates that a string value contains a valid isbn10 or isbn13 value.
  496. Usage: isbn
  497. International Standard Book Number 10
  498. This validates that a string value contains a valid isbn10 value.
  499. Usage: isbn10
  500. International Standard Book Number 13
  501. This validates that a string value contains a valid isbn13 value.
  502. Usage: isbn13
  503. Universally Unique Identifier UUID
  504. This validates that a string value contains a valid UUID. Uppercase UUID values will not pass - use `uuid_rfc4122` instead.
  505. Usage: uuid
  506. Universally Unique Identifier UUID v3
  507. This validates that a string value contains a valid version 3 UUID. Uppercase UUID values will not pass - use `uuid3_rfc4122` instead.
  508. Usage: uuid3
  509. Universally Unique Identifier UUID v4
  510. This validates that a string value contains a valid version 4 UUID. Uppercase UUID values will not pass - use `uuid4_rfc4122` instead.
  511. Usage: uuid4
  512. Universally Unique Identifier UUID v5
  513. This validates that a string value contains a valid version 5 UUID. Uppercase UUID values will not pass - use `uuid5_rfc4122` instead.
  514. Usage: uuid5
  515. ASCII
  516. This validates that a string value contains only ASCII characters.
  517. NOTE: if the string is blank, this validates as true.
  518. Usage: ascii
  519. Printable ASCII
  520. This validates that a string value contains only printable ASCII characters.
  521. NOTE: if the string is blank, this validates as true.
  522. Usage: printascii
  523. Multi-Byte Characters
  524. This validates that a string value contains one or more multibyte characters.
  525. NOTE: if the string is blank, this validates as true.
  526. Usage: multibyte
  527. Data URL
  528. This validates that a string value contains a valid DataURI.
  529. NOTE: this will also validate that the data portion is valid base64
  530. Usage: datauri
  531. Latitude
  532. This validates that a string value contains a valid latitude.
  533. Usage: latitude
  534. Longitude
  535. This validates that a string value contains a valid longitude.
  536. Usage: longitude
  537. Social Security Number SSN
  538. This validates that a string value contains a valid U.S. Social Security Number.
  539. Usage: ssn
  540. Internet Protocol Address IP
  541. This validates that a string value contains a valid IP Address.
  542. Usage: ip
  543. Internet Protocol Address IPv4
  544. This validates that a string value contains a valid v4 IP Address.
  545. Usage: ipv4
  546. Internet Protocol Address IPv6
  547. This validates that a string value contains a valid v6 IP Address.
  548. Usage: ipv6
  549. Classless Inter-Domain Routing CIDR
  550. This validates that a string value contains a valid CIDR Address.
  551. Usage: cidr
  552. Classless Inter-Domain Routing CIDRv4
  553. This validates that a string value contains a valid v4 CIDR Address.
  554. Usage: cidrv4
  555. Classless Inter-Domain Routing CIDRv6
  556. This validates that a string value contains a valid v6 CIDR Address.
  557. Usage: cidrv6
  558. Transmission Control Protocol Address TCP
  559. This validates that a string value contains a valid resolvable TCP Address.
  560. Usage: tcp_addr
  561. Transmission Control Protocol Address TCPv4
  562. This validates that a string value contains a valid resolvable v4 TCP Address.
  563. Usage: tcp4_addr
  564. Transmission Control Protocol Address TCPv6
  565. This validates that a string value contains a valid resolvable v6 TCP Address.
  566. Usage: tcp6_addr
  567. User Datagram Protocol Address UDP
  568. This validates that a string value contains a valid resolvable UDP Address.
  569. Usage: udp_addr
  570. User Datagram Protocol Address UDPv4
  571. This validates that a string value contains a valid resolvable v4 UDP Address.
  572. Usage: udp4_addr
  573. User Datagram Protocol Address UDPv6
  574. This validates that a string value contains a valid resolvable v6 UDP Address.
  575. Usage: udp6_addr
  576. Internet Protocol Address IP
  577. This validates that a string value contains a valid resolvable IP Address.
  578. Usage: ip_addr
  579. Internet Protocol Address IPv4
  580. This validates that a string value contains a valid resolvable v4 IP Address.
  581. Usage: ip4_addr
  582. Internet Protocol Address IPv6
  583. This validates that a string value contains a valid resolvable v6 IP Address.
  584. Usage: ip6_addr
  585. Unix domain socket end point Address
  586. This validates that a string value contains a valid Unix Address.
  587. Usage: unix_addr
  588. Media Access Control Address MAC
  589. This validates that a string value contains a valid MAC Address.
  590. Usage: mac
  591. Note: See Go's ParseMAC for accepted formats and types:
  592. http://golang.org/src/net/mac.go?s=866:918#L29
  593. Hostname RFC 952
  594. This validates that a string value is a valid Hostname according to RFC 952 https://tools.ietf.org/html/rfc952
  595. Usage: hostname
  596. Hostname RFC 1123
  597. This validates that a string value is a valid Hostname according to RFC 1123 https://tools.ietf.org/html/rfc1123
  598. Usage: hostname_rfc1123 or if you want to continue to use 'hostname' in your tags, create an alias.
  599. Full Qualified Domain Name (FQDN)
  600. This validates that a string value contains a valid FQDN.
  601. Usage: fqdn
  602. HTML Tags
  603. This validates that a string value appears to be an HTML element tag
  604. including those described at https://developer.mozilla.org/en-US/docs/Web/HTML/Element
  605. Usage: html
  606. HTML Encoded
  607. This validates that a string value is a proper character reference in decimal
  608. or hexadecimal format
  609. Usage: html_encoded
  610. URL Encoded
  611. This validates that a string value is percent-encoded (URL encoded) according
  612. to https://tools.ietf.org/html/rfc3986#section-2.1
  613. Usage: url_encoded
  614. Directory
  615. This validates that a string value contains a valid directory and that
  616. it exists on the machine.
  617. This is done using os.Stat, which is a platform independent function.
  618. Usage: dir
  619. Alias Validators and Tags
  620. NOTE: When returning an error, the tag returned in "FieldError" will be
  621. the alias tag unless the dive tag is part of the alias. Everything after the
  622. dive tag is not reported as the alias tag. Also, the "ActualTag" in the before
  623. case will be the actual tag within the alias that failed.
  624. Here is a list of the current built in alias tags:
  625. "iscolor"
  626. alias is "hexcolor|rgb|rgba|hsl|hsla" (Usage: iscolor)
  627. Validator notes:
  628. regex
  629. a regex validator won't be added because commas and = signs can be part
  630. of a regex which conflict with the validation definitions. Although
  631. workarounds can be made, they take away from using pure regex's.
  632. Furthermore it's quick and dirty but the regex's become harder to
  633. maintain and are not reusable, so it's as much a programming philosophy
  634. as anything.
  635. In place of this new validator functions should be created; a regex can
  636. be used within the validator function and even be precompiled for better
  637. efficiency within regexes.go.
  638. And the best reason, you can submit a pull request and we can keep on
  639. adding to the validation library of this package!
  640. Panics
  641. This package panics when bad input is provided, this is by design, bad code like
  642. that should not make it to production.
  643. type Test struct {
  644. TestField string `validate:"nonexistantfunction=1"`
  645. }
  646. t := &Test{
  647. TestField: "Test"
  648. }
  649. validate.Struct(t) // this will panic
  650. Non standard validators
  651. A collection of validation rules that are frequently needed but are more
  652. complex than the ones found in the baked in validators.
  653. A non standard validator must be registered manually using any tag you like.
  654. See below examples of registration and use.
  655. type Test struct {
  656. TestField string `validate:"yourtag"`
  657. }
  658. t := &Test{
  659. TestField: "Test"
  660. }
  661. validate := validator.New()
  662. validate.RegisterValidation("yourtag", validations.ValidatorName)
  663. NotBlank
  664. This validates that the value is not blank or with length zero.
  665. For strings ensures they do not contain only spaces. For channels, maps, slices and arrays
  666. ensures they don't have zero length. For others, a non empty value is required.
  667. Usage: notblank
  668. */
  669. package validator