另客网go项目公用的代码库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. Why not a better error message?
  7. Because this library intends for you to handle your own error messages.
  8. Why should I handle my own errors?
  9. Many reasons. We built an internationalized application and needed to know the
  10. field, and what validation failed so we could provide a localized error.
  11. if fieldErr.Field == "Name" {
  12. switch fieldErr.ErrorTag
  13. case "required":
  14. return "Translated string based on field + error"
  15. default:
  16. return "Translated string based on field"
  17. }
  18. Validation Functions Return Type error
  19. Doing things this way is actually the way the standard library does, see the
  20. file.Open method here:
  21. https://golang.org/pkg/os/#Open.
  22. The authors return type "error" to avoid the issue discussed in the following,
  23. where err is always != nil:
  24. http://stackoverflow.com/a/29138676/3158232
  25. https://github.com/go-playground/validator/issues/134
  26. Validator only returns nil or ValidationErrors as type error; so, in your code
  27. all you need to do is check if the error returned is not nil, and if it's not
  28. type cast it to type ValidationErrors like so err.(validator.ValidationErrors).
  29. Custom Functions
  30. Custom functions can be added. Example:
  31. // Structure
  32. func customFunc(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  33. if whatever {
  34. return false
  35. }
  36. return true
  37. }
  38. validate.RegisterValidation("custom tag name", customFunc)
  39. // NOTES: using the same tag name as an existing function
  40. // will overwrite the existing one
  41. Cross-Field Validation
  42. Cross-Field Validation can be done via the following tags:
  43. - eqfield
  44. - nefield
  45. - gtfield
  46. - gtefield
  47. - ltfield
  48. - ltefield
  49. - eqcsfield
  50. - necsfield
  51. - gtcsfield
  52. - ftecsfield
  53. - ltcsfield
  54. - ltecsfield
  55. If, however, some custom cross-field validation is required, it can be done
  56. using a custom validation.
  57. Why not just have cross-fields validation tags (i.e. only eqcsfield and not
  58. eqfield)?
  59. The reason is efficiency. If you want to check a field within the same struct
  60. "eqfield" only has to find the field on the same struct (1 level). But, if we
  61. used "eqcsfield" it could be multiple levels down. Example:
  62. type Inner struct {
  63. StartDate time.Time
  64. }
  65. type Outer struct {
  66. InnerStructField *Inner
  67. CreatedAt time.Time `validate:"ltecsfield=InnerStructField.StartDate"`
  68. }
  69. now := time.Now()
  70. inner := &Inner{
  71. StartDate: now,
  72. }
  73. outer := &Outer{
  74. InnerStructField: inner,
  75. CreatedAt: now,
  76. }
  77. errs := validate.Struct(outer)
  78. // NOTE: when calling validate.Struct(val) topStruct will be the top level struct passed
  79. // into the function
  80. // when calling validate.FieldWithValue(val, field, tag) val will be
  81. // whatever you pass, struct, field...
  82. // when calling validate.Field(field, tag) val will be nil
  83. Multiple Validators
  84. Multiple validators on a field will process in the order defined. Example:
  85. type Test struct {
  86. Field `validate:"max=10,min=1"`
  87. }
  88. // max will be checked then min
  89. Bad Validator definitions are not handled by the library. Example:
  90. type Test struct {
  91. Field `validate:"min=10,max=0"`
  92. }
  93. // this definition of min max will never succeed
  94. Using Validator Tags
  95. Baked In Cross-Field validation only compares fields on the same struct.
  96. If Cross-Field + Cross-Struct validation is needed you should implement your
  97. own custom validator.
  98. Comma (",") is the default separator of validation tags. If you wish to
  99. have a comma included within the parameter (i.e. excludesall=,) you will need to
  100. use the UTF-8 hex representation 0x2C, which is replaced in the code as a comma,
  101. so the above will become excludesall=0x2C.
  102. type Test struct {
  103. Field `validate:"excludesall=,"` // BAD! Do not include a comma.
  104. Field `validate:"excludesall=0x2C"` // GOOD! Use the UTF-8 hex representation.
  105. }
  106. Pipe ("|") is the default separator of validation tags. If you wish to
  107. have a pipe included within the parameter i.e. excludesall=| you will need to
  108. use the UTF-8 hex representation 0x7C, which is replaced in the code as a pipe,
  109. so the above will become excludesall=0x7C
  110. type Test struct {
  111. Field `validate:"excludesall=|"` // BAD! Do not include a a pipe!
  112. Field `validate:"excludesall=0x7C"` // GOOD! Use the UTF-8 hex representation.
  113. }
  114. Baked In Validators and Tags
  115. Here is a list of the current built in validators:
  116. Skip Field
  117. Tells the validation to skip this struct field; this is particularly
  118. handy in ignoring embedded structs from being validated. (Usage: -)
  119. Usage: -
  120. Or Operator
  121. This is the 'or' operator allowing multiple validators to be used and
  122. accepted. (Usage: rbg|rgba) <-- this would allow either rgb or rgba
  123. colors to be accepted. This can also be combined with 'and' for example
  124. ( Usage: omitempty,rgb|rgba)
  125. Usage: |
  126. StructOnly
  127. When a field that is a nested struct is encountered, and contains this flag
  128. any validation on the nested struct will be run, but none of the nested
  129. struct fields will be validated. This is usefull if inside of you program
  130. you know the struct will be valid, but need to verify it has been assigned.
  131. NOTE: only "required" and "omitempty" can be used on a struct itself.
  132. Usage: structonly
  133. NoStructLevel
  134. Same as structonly tag except that any struct level validations will not run.
  135. Usage: nostructlevel
  136. Exists
  137. Is a special tag without a validation function attached. It is used when a field
  138. is a Pointer, Interface or Invalid and you wish to validate that it exists.
  139. Example: want to ensure a bool exists if you define the bool as a pointer and
  140. use exists it will ensure there is a value; couldn't use required as it would
  141. fail when the bool was false. exists will fail is the value is a Pointer, Interface
  142. or Invalid and is nil.
  143. Usage: exists
  144. Omit Empty
  145. Allows conditional validation, for example if a field is not set with
  146. a value (Determined by the "required" validator) then other validation
  147. such as min or max won't run, but if a value is set validation will run.
  148. Usage: omitempty
  149. Dive
  150. This tells the validator to dive into a slice, array or map and validate that
  151. level of the slice, array or map with the validation tags that follow.
  152. Multidimensional nesting is also supported, each level you wish to dive will
  153. require another dive tag.
  154. Usage: dive
  155. Example #1
  156. [][]string with validation tag "gt=0,dive,len=1,dive,required"
  157. // gt=0 will be applied to []
  158. // len=1 will be applied to []string
  159. // required will be applied to string
  160. Example #2
  161. [][]string with validation tag "gt=0,dive,dive,required"
  162. // gt=0 will be applied to []
  163. // []string will be spared validation
  164. // required will be applied to string
  165. Required
  166. This validates that the value is not the data types default zero value.
  167. For numbers ensures value is not zero. For strings ensures value is
  168. not "". For slices, maps, pointers, interfaces, channels and functions
  169. ensures the value is not nil.
  170. Usage: required
  171. Length
  172. For numbers, max will ensure that the value is
  173. equal to the parameter given. For strings, it checks that
  174. the string length is exactly that number of characters. For slices,
  175. arrays, and maps, validates the number of items.
  176. Usage: len=10
  177. Maximum
  178. For numbers, max will ensure that the value is
  179. less than or equal to the parameter given. For strings, it checks
  180. that the string length is at most that number of characters. For
  181. slices, arrays, and maps, validates the number of items.
  182. Usage: max=10
  183. Mininum
  184. For numbers, min will ensure that the value is
  185. greater or equal to the parameter given. For strings, it checks that
  186. the string length is at least that number of characters. For slices,
  187. arrays, and maps, validates the number of items.
  188. Usage: min=10
  189. Equals
  190. For strings & numbers, eq will ensure that the value is
  191. equal to the parameter given. For slices, arrays, and maps,
  192. validates the number of items.
  193. Usage: eq=10
  194. Not Equal
  195. For strings & numbers, ne will ensure that the value is not
  196. equal to the parameter given. For slices, arrays, and maps,
  197. validates the number of items.
  198. Usage: ne=10
  199. Greater Than
  200. For numbers, this will ensure that the value is greater than the
  201. parameter given. For strings, it checks that the string length
  202. is greater than that number of characters. For slices, arrays
  203. and maps it validates the number of items.
  204. Example #1
  205. Usage: gt=10
  206. Example #2 (time.Time)
  207. For time.Time ensures the time value is greater than time.Now.UTC().
  208. Usage: gt
  209. Greater Than or Equal
  210. Same as 'min' above. Kept both to make terminology with 'len' easier.
  211. Example #1
  212. Usage: gte=10
  213. Example #2 (time.Time)
  214. For time.Time ensures the time value is greater than or equal to time.Now.UTC().
  215. Usage: gte
  216. Less Than
  217. For numbers, this will ensure that the value is less than the parameter given.
  218. For strings, it checks that the string length is less than that number of
  219. characters. For slices, arrays, and maps it validates the number of items.
  220. Example #1
  221. Usage: lt=10
  222. Example #2 (time.Time)
  223. For time.Time ensures the time value is less than time.Now.UTC().
  224. Usage: lt
  225. Less Than or Equal
  226. Same as 'max' above. Kept both to make terminology with 'len' easier.
  227. Example #1
  228. Usage: lte=10
  229. Example #2 (time.Time)
  230. For time.Time ensures the time value is less than or equal to time.Now.UTC().
  231. Usage: lte
  232. Field Equals Another Field
  233. This will validate the field value against another fields value either within
  234. a struct or passed in field.
  235. Example #1:
  236. // Validation on Password field using:
  237. Usage: eqfield=ConfirmPassword
  238. Example #2:
  239. // Validating by field:
  240. validate.FieldWithValue(password, confirmpassword, "eqfield")
  241. Field Equals Another Field (relative)
  242. This does the same as eqfield except that it validates the field provided relative
  243. to the top level struct.
  244. Usage: eqcsfield=InnerStructField.Field)
  245. Field Does Not Equal Another Field
  246. This will validate the field value against another fields value either within
  247. a struct or passed in field.
  248. Examples:
  249. // Confirm two colors are not the same:
  250. //
  251. // Validation on Color field:
  252. Usage: nefield=Color2
  253. // Validating by field:
  254. validate.FieldWithValue(color1, color2, "nefield")
  255. Field Does Not Equal Another Field (relative)
  256. This does the same as nefield except that it validates the field provided
  257. relative to the top level struct.
  258. Usage: necsfield=InnerStructField.Field
  259. Field Greater Than Another Field
  260. Only valid for Numbers and time.Time types, this will validate the field value
  261. against another fields value either within a struct or passed in field.
  262. usage examples are for validation of a Start and End date:
  263. Example #1:
  264. // Validation on End field using:
  265. validate.Struct Usage(gtfield=Start)
  266. Example #2:
  267. // Validating by field:
  268. validate.FieldWithValue(start, end, "gtfield")
  269. Field Greater Than Another Relative Field
  270. This does the same as gtfield except that it validates the field provided
  271. relative to the top level struct.
  272. Usage: gtcsfield=InnerStructField.Field
  273. Field Greater Than or Equal To Another Field
  274. Only valid for Numbers and time.Time types, this will validate the field value
  275. against another fields value either within a struct or passed in field.
  276. usage examples are for validation of a Start and End date:
  277. Example #1:
  278. // Validation on End field using:
  279. validate.Struct Usage(gtefield=Start)
  280. Example #2:
  281. // Validating by field:
  282. validate.FieldWithValue(start, end, "gtefield")
  283. Field Greater Than or Equal To Another Relative Field
  284. This does the same as gtefield except that it validates the field provided relative
  285. to the top level struct.
  286. Usage: gtecsfield=InnerStructField.Field
  287. Less Than Another Field
  288. Only valid for Numbers and time.Time types, this will validate the field value
  289. against another fields value either within a struct or passed in field.
  290. usage examples are for validation of a Start and End date:
  291. Example #1:
  292. // Validation on End field using:
  293. validate.Struct Usage(ltfield=Start)
  294. Example #2:
  295. // Validating by field:
  296. validate.FieldWithValue(start, end, "ltfield")
  297. Less Than Another Relative Field
  298. This does the same as ltfield except that it validates the field provided relative
  299. to the top level struct.
  300. Usage: ltcsfield=InnerStructField.Field
  301. Less Than or Equal To Another Field
  302. Only valid for Numbers and time.Time types, this will validate the field value
  303. against another fields value either within a struct or passed in field.
  304. usage examples are for validation of a Start and End date:
  305. Example #1:
  306. // Validation on End field using:
  307. validate.Struct Usage(ltefield=Start)
  308. Example #2:
  309. // Validating by field:
  310. validate.FieldWithValue(start, end, "ltefield")
  311. Less Than or Equal To Another Relative Field
  312. This does the same as ltefield except that it validates the field provided relative
  313. to the top level struct.
  314. Usage: ltecsfield=InnerStructField.Field
  315. Alpha Only
  316. This validates that a string value contains alpha characters only
  317. Usage: alpha
  318. Alphanumeric
  319. This validates that a string value contains alphanumeric characters only
  320. Usage: alphanum
  321. Numeric
  322. This validates that a string value contains a basic numeric value.
  323. basic excludes exponents etc...
  324. Usage: numeric
  325. Hexadecimal String
  326. This validates that a string value contains a valid hexadecimal.
  327. Usage: hexadecimal
  328. Hexcolor String
  329. This validates that a string value contains a valid hex color including
  330. hashtag (#)
  331. Usage: hexcolor
  332. RGB String
  333. This validates that a string value contains a valid rgb color
  334. Usage: rgb
  335. RGBA String
  336. This validates that a string value contains a valid rgba color
  337. Usage: rgba
  338. HSL String
  339. This validates that a string value contains a valid hsl color
  340. Usage: hsl
  341. HSLA String
  342. This validates that a string value contains a valid hsla color
  343. Usage: hsla
  344. E-mail String
  345. This validates that a string value contains a valid email
  346. This may not conform to all possibilities of any rfc standard, but neither
  347. does any email provider accept all posibilities.
  348. Usage: email
  349. URL String
  350. This validates that a string value contains a valid url
  351. This will accept any url the golang request uri accepts but must contain
  352. a schema for example http:// or rtmp://
  353. Usage: url
  354. URI String
  355. This validates that a string value contains a valid uri
  356. This will accept any uri the golang request uri accepts
  357. Usage: uri
  358. Base64 String
  359. This validates that a string value contains a valid base64 value.
  360. Although an empty string is valid base64 this will report an empty string
  361. as an error, if you wish to accept an empty string as valid you can use
  362. this with the omitempty tag.
  363. Usage: base64
  364. Contains
  365. This validates that a string value contains the substring value.
  366. Usage: contains=@
  367. Contains Any
  368. This validates that a string value contains any Unicode code points
  369. in the substring value.
  370. Usage: containsany=!@#?
  371. Contains Rune
  372. This validates that a string value contains the supplied rune value.
  373. Usage: containsrune=@
  374. Excludes
  375. This validates that a string value does not contain the substring value.
  376. Usage: excludes=@
  377. Excludes All
  378. This validates that a string value does not contain any Unicode code
  379. points in the substring value.
  380. Usage: excludesall=!@#?
  381. Excludes Rune
  382. This validates that a string value does not contain the supplied rune value.
  383. Usage: excludesrune=@
  384. International Standard Book Number
  385. This validates that a string value contains a valid isbn10 or isbn13 value.
  386. Usage: isbn
  387. International Standard Book Number 10
  388. This validates that a string value contains a valid isbn10 value.
  389. Usage: isbn10
  390. International Standard Book Number 13
  391. This validates that a string value contains a valid isbn13 value.
  392. Usage: isbn13
  393. Universally Unique Identifier UUID
  394. This validates that a string value contains a valid UUID.
  395. Usage: uuid
  396. Universally Unique Identifier UUID v3
  397. This validates that a string value contains a valid version 3 UUID.
  398. Usage: uuid3
  399. Universally Unique Identifier UUID v4
  400. This validates that a string value contains a valid version 4 UUID.
  401. Usage: uuid4
  402. Universally Unique Identifier UUID v5
  403. This validates that a string value contains a valid version 5 UUID.
  404. Usage: uuid5
  405. ASCII
  406. This validates that a string value contains only ASCII characters.
  407. NOTE: if the string is blank, this validates as true.
  408. Usage: ascii
  409. Printable ASCII
  410. This validates that a string value contains only printable ASCII characters.
  411. NOTE: if the string is blank, this validates as true.
  412. Usage: asciiprint
  413. Multi-Byte Characters
  414. This validates that a string value contains one or more multibyte characters.
  415. NOTE: if the string is blank, this validates as true.
  416. Usage: multibyte
  417. Data URL
  418. This validates that a string value contains a valid DataURI.
  419. NOTE: this will also validate that the data portion is valid base64
  420. Usage: datauri
  421. Latitude
  422. This validates that a string value contains a valid latitude.
  423. Usage: latitude
  424. Longitude
  425. This validates that a string value contains a valid longitude.
  426. Usage: longitude
  427. Social Security Number SSN
  428. This validates that a string value contains a valid U.S. Social Security Number.
  429. Usage: ssn
  430. Internet Protocol Address IP
  431. This validates that a string value contains a valid IP Adress.
  432. Usage: ip
  433. Internet Protocol Address IPv4
  434. This validates that a string value contains a valid v4 IP Adress.
  435. Usage: ipv4
  436. Internet Protocol Address IPv6
  437. This validates that a string value contains a valid v6 IP Adress.
  438. Usage: ipv6
  439. Classless Inter-Domain Routing CIDR
  440. This validates that a string value contains a valid CIDR Adress.
  441. Usage: cidr
  442. Classless Inter-Domain Routing CIDRv4
  443. This validates that a string value contains a valid v4 CIDR Adress.
  444. Usage: cidrv4
  445. Classless Inter-Domain Routing CIDRv6
  446. This validates that a string value contains a valid v6 CIDR Adress.
  447. Usage: cidrv6
  448. Transmission Control Protocol Address TCP
  449. This validates that a string value contains a valid resolvable TCP Adress.
  450. Usage: tcp_addr
  451. Transmission Control Protocol Address TCPv4
  452. This validates that a string value contains a valid resolvable v4 TCP Adress.
  453. Usage: tcp4_addr
  454. Transmission Control Protocol Address TCPv6
  455. This validates that a string value contains a valid resolvable v6 TCP Adress.
  456. Usage: tcp6_addr
  457. User Datagram Protocol Address UDP
  458. This validates that a string value contains a valid resolvable UDP Adress.
  459. Usage: udp_addr
  460. User Datagram Protocol Address UDPv4
  461. This validates that a string value contains a valid resolvable v4 UDP Adress.
  462. Usage: udp4_addr
  463. User Datagram Protocol Address UDPv6
  464. This validates that a string value contains a valid resolvable v6 UDP Adress.
  465. Usage: udp6_addr
  466. Internet Protocol Address IP
  467. This validates that a string value contains a valid resolvable IP Adress.
  468. Usage: ip_addr
  469. Internet Protocol Address IPv4
  470. This validates that a string value contains a valid resolvable v4 IP Adress.
  471. Usage: ip4_addr
  472. Internet Protocol Address IPv6
  473. This validates that a string value contains a valid resolvable v6 IP Adress.
  474. Usage: ip6_addr
  475. Unix domain socket end point Address
  476. This validates that a string value contains a valid Unix Adress.
  477. Usage: unix_addr
  478. Media Access Control Address MAC
  479. This validates that a string value contains a valid MAC Adress.
  480. Usage: mac
  481. Note: See Go's ParseMAC for accepted formats and types:
  482. http://golang.org/src/net/mac.go?s=866:918#L29
  483. Alias Validators and Tags
  484. NOTE: When returning an error, the tag returned in "FieldError" will be
  485. the alias tag unless the dive tag is part of the alias. Everything after the
  486. dive tag is not reported as the alias tag. Also, the "ActualTag" in the before
  487. case will be the actual tag within the alias that failed.
  488. Here is a list of the current built in alias tags:
  489. "iscolor"
  490. alias is "hexcolor|rgb|rgba|hsl|hsla" (Usage: iscolor)
  491. Validator notes:
  492. regex
  493. a regex validator won't be added because commas and = signs can be part
  494. of a regex which conflict with the validation definitions. Although
  495. workarounds can be made, they take away from using pure regex's.
  496. Furthermore it's quick and dirty but the regex's become harder to
  497. maintain and are not reusable, so it's as much a programming philosiphy
  498. as anything.
  499. In place of this new validator functions should be created; a regex can
  500. be used within the validator function and even be precompiled for better
  501. efficiency within regexes.go.
  502. And the best reason, you can submit a pull request and we can keep on
  503. adding to the validation library of this package!
  504. Panics
  505. This package panics when bad input is provided, this is by design, bad code like
  506. that should not make it to production.
  507. type Test struct {
  508. TestField string `validate:"nonexistantfunction=1"`
  509. }
  510. t := &Test{
  511. TestField: "Test"
  512. }
  513. validate.Struct(t) // this will panic
  514. */
  515. package validator