PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/store/vendor/gopkg.in/go-playground/validator.v8/baked_in.go

https://bitbucket.org/dzaycevteam/server
Go | 1410 lines | 836 code | 393 blank | 181 comment | 232 complexity | 732eba4421fe431f8c497a0a16f83ca1 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, 0BSD, MIT

Large files files are truncated, but you can click here to view the full file

  1. package validator
  2. import (
  3. "fmt"
  4. "net"
  5. "net/url"
  6. "reflect"
  7. "strings"
  8. "time"
  9. "unicode/utf8"
  10. )
  11. // BakedInAliasValidators is a default mapping of a single validationstag that
  12. // defines a common or complex set of validation(s) to simplify
  13. // adding validation to structs. i.e. set key "_ageok" and the tags
  14. // are "gt=0,lte=130" or key "_preferredname" and tags "omitempty,gt=0,lte=60"
  15. var bakedInAliasValidators = map[string]string{
  16. "iscolor": "hexcolor|rgb|rgba|hsl|hsla",
  17. }
  18. // BakedInValidators is the default map of ValidationFunc
  19. // you can add, remove or even replace items to suite your needs,
  20. // or even disregard and use your own map if so desired.
  21. var bakedInValidators = map[string]Func{
  22. "required": HasValue,
  23. "len": HasLengthOf,
  24. "min": HasMinOf,
  25. "max": HasMaxOf,
  26. "eq": IsEq,
  27. "ne": IsNe,
  28. "lt": IsLt,
  29. "lte": IsLte,
  30. "gt": IsGt,
  31. "gte": IsGte,
  32. "eqfield": IsEqField,
  33. "eqcsfield": IsEqCrossStructField,
  34. "necsfield": IsNeCrossStructField,
  35. "gtcsfield": IsGtCrossStructField,
  36. "gtecsfield": IsGteCrossStructField,
  37. "ltcsfield": IsLtCrossStructField,
  38. "ltecsfield": IsLteCrossStructField,
  39. "nefield": IsNeField,
  40. "gtefield": IsGteField,
  41. "gtfield": IsGtField,
  42. "ltefield": IsLteField,
  43. "ltfield": IsLtField,
  44. "alpha": IsAlpha,
  45. "alphanum": IsAlphanum,
  46. "numeric": IsNumeric,
  47. "number": IsNumber,
  48. "hexadecimal": IsHexadecimal,
  49. "hexcolor": IsHEXColor,
  50. "rgb": IsRGB,
  51. "rgba": IsRGBA,
  52. "hsl": IsHSL,
  53. "hsla": IsHSLA,
  54. "email": IsEmail,
  55. "url": IsURL,
  56. "uri": IsURI,
  57. "base64": IsBase64,
  58. "contains": Contains,
  59. "containsany": ContainsAny,
  60. "containsrune": ContainsRune,
  61. "excludes": Excludes,
  62. "excludesall": ExcludesAll,
  63. "excludesrune": ExcludesRune,
  64. "isbn": IsISBN,
  65. "isbn10": IsISBN10,
  66. "isbn13": IsISBN13,
  67. "uuid": IsUUID,
  68. "uuid3": IsUUID3,
  69. "uuid4": IsUUID4,
  70. "uuid5": IsUUID5,
  71. "ascii": IsASCII,
  72. "printascii": IsPrintableASCII,
  73. "multibyte": HasMultiByteCharacter,
  74. "datauri": IsDataURI,
  75. "latitude": IsLatitude,
  76. "longitude": IsLongitude,
  77. "ssn": IsSSN,
  78. "ipv4": IsIPv4,
  79. "ipv6": IsIPv6,
  80. "ip": IsIP,
  81. "cidrv4": IsCIDRv4,
  82. "cidrv6": IsCIDRv6,
  83. "cidr": IsCIDR,
  84. "tcp4_addr": IsTCP4AddrResolvable,
  85. "tcp6_addr": IsTCP6AddrResolvable,
  86. "tcp_addr": IsTCPAddrResolvable,
  87. "udp4_addr": IsUDP4AddrResolvable,
  88. "udp6_addr": IsUDP6AddrResolvable,
  89. "udp_addr": IsUDPAddrResolvable,
  90. "ip4_addr": IsIP4AddrResolvable,
  91. "ip6_addr": IsIP6AddrResolvable,
  92. "ip_addr": IsIPAddrResolvable,
  93. "unix_addr": IsUnixAddrResolvable,
  94. "mac": IsMAC,
  95. }
  96. // IsMAC is the validation function for validating if the field's value is a valid MAC address.
  97. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  98. func IsMAC(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  99. _, err := net.ParseMAC(field.String())
  100. return err == nil
  101. }
  102. // IsCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
  103. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  104. func IsCIDRv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  105. ip, _, err := net.ParseCIDR(field.String())
  106. return err == nil && ip.To4() != nil
  107. }
  108. // IsCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.
  109. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  110. func IsCIDRv6(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  111. ip, _, err := net.ParseCIDR(field.String())
  112. return err == nil && ip.To4() == nil
  113. }
  114. // IsCIDR is the validation function for validating if the field's value is a valid v4 or v6 CIDR address.
  115. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  116. func IsCIDR(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  117. _, _, err := net.ParseCIDR(field.String())
  118. return err == nil
  119. }
  120. // IsIPv4 is the validation function for validating if a value is a valid v4 IP address.
  121. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  122. func IsIPv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  123. ip := net.ParseIP(field.String())
  124. return ip != nil && ip.To4() != nil
  125. }
  126. // IsIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
  127. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  128. func IsIPv6(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  129. ip := net.ParseIP(field.String())
  130. return ip != nil && ip.To4() == nil
  131. }
  132. // IsIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.
  133. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  134. func IsIP(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  135. ip := net.ParseIP(field.String())
  136. return ip != nil
  137. }
  138. // IsSSN is the validation function for validating if the field's value is a valid SSN.
  139. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  140. func IsSSN(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  141. if field.Len() != 11 {
  142. return false
  143. }
  144. return sSNRegex.MatchString(field.String())
  145. }
  146. // IsLongitude is the validation function for validating if the field's value is a valid longitude coordinate.
  147. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  148. func IsLongitude(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  149. return longitudeRegex.MatchString(field.String())
  150. }
  151. // IsLatitude is the validation function for validating if the field's value is a valid latitude coordinate.
  152. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  153. func IsLatitude(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  154. return latitudeRegex.MatchString(field.String())
  155. }
  156. // IsDataURI is the validation function for validating if the field's value is a valid data URI.
  157. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  158. func IsDataURI(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  159. uri := strings.SplitN(field.String(), ",", 2)
  160. if len(uri) != 2 {
  161. return false
  162. }
  163. if !dataURIRegex.MatchString(uri[0]) {
  164. return false
  165. }
  166. fld := reflect.ValueOf(uri[1])
  167. return IsBase64(v, topStruct, currentStructOrField, fld, fld.Type(), fld.Kind(), param)
  168. }
  169. // HasMultiByteCharacter is the validation function for validating if the field's value has a multi byte character.
  170. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  171. func HasMultiByteCharacter(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  172. if field.Len() == 0 {
  173. return true
  174. }
  175. return multibyteRegex.MatchString(field.String())
  176. }
  177. // IsPrintableASCII is the validation function for validating if the field's value is a valid printable ASCII character.
  178. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  179. func IsPrintableASCII(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  180. return printableASCIIRegex.MatchString(field.String())
  181. }
  182. // IsASCII is the validation function for validating if the field's value is a valid ASCII character.
  183. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  184. func IsASCII(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  185. return aSCIIRegex.MatchString(field.String())
  186. }
  187. // IsUUID5 is the validation function for validating if the field's value is a valid v5 UUID.
  188. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  189. func IsUUID5(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  190. return uUID5Regex.MatchString(field.String())
  191. }
  192. // IsUUID4 is the validation function for validating if the field's value is a valid v4 UUID.
  193. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  194. func IsUUID4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  195. return uUID4Regex.MatchString(field.String())
  196. }
  197. // IsUUID3 is the validation function for validating if the field's value is a valid v3 UUID.
  198. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  199. func IsUUID3(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  200. return uUID3Regex.MatchString(field.String())
  201. }
  202. // IsUUID is the validation function for validating if the field's value is a valid UUID of any version.
  203. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  204. func IsUUID(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  205. return uUIDRegex.MatchString(field.String())
  206. }
  207. // IsISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
  208. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  209. func IsISBN(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  210. return IsISBN10(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) || IsISBN13(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  211. }
  212. // IsISBN13 is the validation function for validating if the field's value is a valid v13 ISBN.
  213. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  214. func IsISBN13(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  215. s := strings.Replace(strings.Replace(field.String(), "-", "", 4), " ", "", 4)
  216. if !iSBN13Regex.MatchString(s) {
  217. return false
  218. }
  219. var checksum int32
  220. var i int32
  221. factor := []int32{1, 3}
  222. for i = 0; i < 12; i++ {
  223. checksum += factor[i%2] * int32(s[i]-'0')
  224. }
  225. return (int32(s[12]-'0'))-((10-(checksum%10))%10) == 0
  226. }
  227. // IsISBN10 is the validation function for validating if the field's value is a valid v10 ISBN.
  228. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  229. func IsISBN10(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  230. s := strings.Replace(strings.Replace(field.String(), "-", "", 3), " ", "", 3)
  231. if !iSBN10Regex.MatchString(s) {
  232. return false
  233. }
  234. var checksum int32
  235. var i int32
  236. for i = 0; i < 9; i++ {
  237. checksum += (i + 1) * int32(s[i]-'0')
  238. }
  239. if s[9] == 'X' {
  240. checksum += 10 * 10
  241. } else {
  242. checksum += 10 * int32(s[9]-'0')
  243. }
  244. return checksum%11 == 0
  245. }
  246. // ExcludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
  247. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  248. func ExcludesRune(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  249. return !ContainsRune(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  250. }
  251. // ExcludesAll is the validation function for validating that the field's value does not contain any of the characters specified within the param.
  252. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  253. func ExcludesAll(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  254. return !ContainsAny(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  255. }
  256. // Excludes is the validation function for validating that the field's value does not contain the text specified within the param.
  257. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  258. func Excludes(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  259. return !Contains(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  260. }
  261. // ContainsRune is the validation function for validating that the field's value contains the rune specified within the param.
  262. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  263. func ContainsRune(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  264. r, _ := utf8.DecodeRuneInString(param)
  265. return strings.ContainsRune(field.String(), r)
  266. }
  267. // ContainsAny is the validation function for validating that the field's value contains any of the characters specified within the param.
  268. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  269. func ContainsAny(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  270. return strings.ContainsAny(field.String(), param)
  271. }
  272. // Contains is the validation function for validating that the field's value contains the text specified within the param.
  273. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  274. func Contains(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  275. return strings.Contains(field.String(), param)
  276. }
  277. // IsNeField is the validation function for validating if the current field's value is not equal to the field specified by the param's value.
  278. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  279. func IsNeField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  280. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  281. if !ok || currentKind != fieldKind {
  282. return true
  283. }
  284. switch fieldKind {
  285. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  286. return field.Int() != currentField.Int()
  287. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  288. return field.Uint() != currentField.Uint()
  289. case reflect.Float32, reflect.Float64:
  290. return field.Float() != currentField.Float()
  291. case reflect.Slice, reflect.Map, reflect.Array:
  292. return int64(field.Len()) != int64(currentField.Len())
  293. case reflect.Struct:
  294. // Not Same underlying type i.e. struct and time
  295. if fieldType != currentField.Type() {
  296. return true
  297. }
  298. if fieldType == timeType {
  299. t := currentField.Interface().(time.Time)
  300. fieldTime := field.Interface().(time.Time)
  301. return !fieldTime.Equal(t)
  302. }
  303. }
  304. // default reflect.String:
  305. return field.String() != currentField.String()
  306. }
  307. // IsNe is the validation function for validating that the field's value does not equal the provided param value.
  308. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  309. func IsNe(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  310. return !IsEq(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  311. }
  312. // IsLteCrossStructField is the validation function for validating if the current field's value is less than or equal to the field, within a separate struct, specified by the param's value.
  313. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  314. func IsLteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  315. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  316. if !ok || topKind != fieldKind {
  317. return false
  318. }
  319. switch fieldKind {
  320. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  321. return field.Int() <= topField.Int()
  322. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  323. return field.Uint() <= topField.Uint()
  324. case reflect.Float32, reflect.Float64:
  325. return field.Float() <= topField.Float()
  326. case reflect.Slice, reflect.Map, reflect.Array:
  327. return int64(field.Len()) <= int64(topField.Len())
  328. case reflect.Struct:
  329. // Not Same underlying type i.e. struct and time
  330. if fieldType != topField.Type() {
  331. return false
  332. }
  333. if fieldType == timeType {
  334. fieldTime := field.Interface().(time.Time)
  335. topTime := topField.Interface().(time.Time)
  336. return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
  337. }
  338. }
  339. // default reflect.String:
  340. return field.String() <= topField.String()
  341. }
  342. // IsLtCrossStructField is the validation function for validating if the current field's value is less than the field, within a separate struct, specified by the param's value.
  343. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  344. func IsLtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  345. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  346. if !ok || topKind != fieldKind {
  347. return false
  348. }
  349. switch fieldKind {
  350. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  351. return field.Int() < topField.Int()
  352. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  353. return field.Uint() < topField.Uint()
  354. case reflect.Float32, reflect.Float64:
  355. return field.Float() < topField.Float()
  356. case reflect.Slice, reflect.Map, reflect.Array:
  357. return int64(field.Len()) < int64(topField.Len())
  358. case reflect.Struct:
  359. // Not Same underlying type i.e. struct and time
  360. if fieldType != topField.Type() {
  361. return false
  362. }
  363. if fieldType == timeType {
  364. fieldTime := field.Interface().(time.Time)
  365. topTime := topField.Interface().(time.Time)
  366. return fieldTime.Before(topTime)
  367. }
  368. }
  369. // default reflect.String:
  370. return field.String() < topField.String()
  371. }
  372. // IsGteCrossStructField is the validation function for validating if the current field's value is greater than or equal to the field, within a separate struct, specified by the param's value.
  373. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  374. func IsGteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  375. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  376. if !ok || topKind != fieldKind {
  377. return false
  378. }
  379. switch fieldKind {
  380. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  381. return field.Int() >= topField.Int()
  382. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  383. return field.Uint() >= topField.Uint()
  384. case reflect.Float32, reflect.Float64:
  385. return field.Float() >= topField.Float()
  386. case reflect.Slice, reflect.Map, reflect.Array:
  387. return int64(field.Len()) >= int64(topField.Len())
  388. case reflect.Struct:
  389. // Not Same underlying type i.e. struct and time
  390. if fieldType != topField.Type() {
  391. return false
  392. }
  393. if fieldType == timeType {
  394. fieldTime := field.Interface().(time.Time)
  395. topTime := topField.Interface().(time.Time)
  396. return fieldTime.After(topTime) || fieldTime.Equal(topTime)
  397. }
  398. }
  399. // default reflect.String:
  400. return field.String() >= topField.String()
  401. }
  402. // IsGtCrossStructField is the validation function for validating if the current field's value is greater than the field, within a separate struct, specified by the param's value.
  403. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  404. func IsGtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  405. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  406. if !ok || topKind != fieldKind {
  407. return false
  408. }
  409. switch fieldKind {
  410. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  411. return field.Int() > topField.Int()
  412. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  413. return field.Uint() > topField.Uint()
  414. case reflect.Float32, reflect.Float64:
  415. return field.Float() > topField.Float()
  416. case reflect.Slice, reflect.Map, reflect.Array:
  417. return int64(field.Len()) > int64(topField.Len())
  418. case reflect.Struct:
  419. // Not Same underlying type i.e. struct and time
  420. if fieldType != topField.Type() {
  421. return false
  422. }
  423. if fieldType == timeType {
  424. fieldTime := field.Interface().(time.Time)
  425. topTime := topField.Interface().(time.Time)
  426. return fieldTime.After(topTime)
  427. }
  428. }
  429. // default reflect.String:
  430. return field.String() > topField.String()
  431. }
  432. // IsNeCrossStructField is the validation function for validating that the current field's value is not equal to the field, within a separate struct, specified by the param's value.
  433. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  434. func IsNeCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  435. topField, currentKind, ok := v.GetStructFieldOK(topStruct, param)
  436. if !ok || currentKind != fieldKind {
  437. return true
  438. }
  439. switch fieldKind {
  440. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  441. return topField.Int() != field.Int()
  442. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  443. return topField.Uint() != field.Uint()
  444. case reflect.Float32, reflect.Float64:
  445. return topField.Float() != field.Float()
  446. case reflect.Slice, reflect.Map, reflect.Array:
  447. return int64(topField.Len()) != int64(field.Len())
  448. case reflect.Struct:
  449. // Not Same underlying type i.e. struct and time
  450. if fieldType != topField.Type() {
  451. return true
  452. }
  453. if fieldType == timeType {
  454. t := field.Interface().(time.Time)
  455. fieldTime := topField.Interface().(time.Time)
  456. return !fieldTime.Equal(t)
  457. }
  458. }
  459. // default reflect.String:
  460. return topField.String() != field.String()
  461. }
  462. // IsEqCrossStructField is the validation function for validating that the current field's value is equal to the field, within a separate struct, specified by the param's value.
  463. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  464. func IsEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  465. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  466. if !ok || topKind != fieldKind {
  467. return false
  468. }
  469. switch fieldKind {
  470. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  471. return topField.Int() == field.Int()
  472. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  473. return topField.Uint() == field.Uint()
  474. case reflect.Float32, reflect.Float64:
  475. return topField.Float() == field.Float()
  476. case reflect.Slice, reflect.Map, reflect.Array:
  477. return int64(topField.Len()) == int64(field.Len())
  478. case reflect.Struct:
  479. // Not Same underlying type i.e. struct and time
  480. if fieldType != topField.Type() {
  481. return false
  482. }
  483. if fieldType == timeType {
  484. t := field.Interface().(time.Time)
  485. fieldTime := topField.Interface().(time.Time)
  486. return fieldTime.Equal(t)
  487. }
  488. }
  489. // default reflect.String:
  490. return topField.String() == field.String()
  491. }
  492. // IsEqField is the validation function for validating if the current field's value is equal to the field specified by the param's value.
  493. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  494. func IsEqField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  495. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  496. if !ok || currentKind != fieldKind {
  497. return false
  498. }
  499. switch fieldKind {
  500. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  501. return field.Int() == currentField.Int()
  502. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  503. return field.Uint() == currentField.Uint()
  504. case reflect.Float32, reflect.Float64:
  505. return field.Float() == currentField.Float()
  506. case reflect.Slice, reflect.Map, reflect.Array:
  507. return int64(field.Len()) == int64(currentField.Len())
  508. case reflect.Struct:
  509. // Not Same underlying type i.e. struct and time
  510. if fieldType != currentField.Type() {
  511. return false
  512. }
  513. if fieldType == timeType {
  514. t := currentField.Interface().(time.Time)
  515. fieldTime := field.Interface().(time.Time)
  516. return fieldTime.Equal(t)
  517. }
  518. }
  519. // default reflect.String:
  520. return field.String() == currentField.String()
  521. }
  522. // IsEq is the validation function for validating if the current field's value is equal to the param's value.
  523. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  524. func IsEq(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  525. switch fieldKind {
  526. case reflect.String:
  527. return field.String() == param
  528. case reflect.Slice, reflect.Map, reflect.Array:
  529. p := asInt(param)
  530. return int64(field.Len()) == p
  531. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  532. p := asInt(param)
  533. return field.Int() == p
  534. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  535. p := asUint(param)
  536. return field.Uint() == p
  537. case reflect.Float32, reflect.Float64:
  538. p := asFloat(param)
  539. return field.Float() == p
  540. }
  541. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  542. }
  543. // IsBase64 is the validation function for validating if the current field's value is a valid base 64.
  544. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  545. func IsBase64(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  546. return base64Regex.MatchString(field.String())
  547. }
  548. // IsURI is the validation function for validating if the current field's value is a valid URI.
  549. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  550. func IsURI(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  551. switch fieldKind {
  552. case reflect.String:
  553. s := field.String()
  554. // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
  555. // emulate browser and strip the '#' suffix prior to validation. see issue-#237
  556. if i := strings.Index(s, "#"); i > -1 {
  557. s = s[:i]
  558. }
  559. if s == blank {
  560. return false
  561. }
  562. _, err := url.ParseRequestURI(s)
  563. return err == nil
  564. }
  565. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  566. }
  567. // IsURL is the validation function for validating if the current field's value is a valid URL.
  568. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  569. func IsURL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  570. switch fieldKind {
  571. case reflect.String:
  572. var i int
  573. s := field.String()
  574. // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
  575. // emulate browser and strip the '#' suffix prior to validation. see issue-#237
  576. if i = strings.Index(s, "#"); i > -1 {
  577. s = s[:i]
  578. }
  579. if s == blank {
  580. return false
  581. }
  582. url, err := url.ParseRequestURI(s)
  583. if err != nil || url.Scheme == blank {
  584. return false
  585. }
  586. return err == nil
  587. }
  588. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  589. }
  590. // IsEmail is the validation function for validating if the current field's value is a valid email address.
  591. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  592. func IsEmail(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  593. return emailRegex.MatchString(field.String())
  594. }
  595. // IsHSLA is the validation function for validating if the current field's value is a valid HSLA color.
  596. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  597. func IsHSLA(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  598. return hslaRegex.MatchString(field.String())
  599. }
  600. // IsHSL is the validation function for validating if the current field's value is a valid HSL color.
  601. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  602. func IsHSL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  603. return hslRegex.MatchString(field.String())
  604. }
  605. // IsRGBA is the validation function for validating if the current field's value is a valid RGBA color.
  606. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  607. func IsRGBA(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  608. return rgbaRegex.MatchString(field.String())
  609. }
  610. // IsRGB is the validation function for validating if the current field's value is a valid RGB color.
  611. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  612. func IsRGB(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  613. return rgbRegex.MatchString(field.String())
  614. }
  615. // IsHEXColor is the validation function for validating if the current field's value is a valid HEX color.
  616. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  617. func IsHEXColor(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  618. return hexcolorRegex.MatchString(field.String())
  619. }
  620. // IsHexadecimal is the validation function for validating if the current field's value is a valid hexadecimal.
  621. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  622. func IsHexadecimal(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  623. return hexadecimalRegex.MatchString(field.String())
  624. }
  625. // IsNumber is the validation function for validating if the current field's value is a valid number.
  626. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  627. func IsNumber(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  628. return numberRegex.MatchString(field.String())
  629. }
  630. // IsNumeric is the validation function for validating if the current field's value is a valid numeric value.
  631. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  632. func IsNumeric(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  633. return numericRegex.MatchString(field.String())
  634. }
  635. // IsAlphanum is the validation function for validating if the current field's value is a valid alphanumeric value.
  636. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  637. func IsAlphanum(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  638. return alphaNumericRegex.MatchString(field.String())
  639. }
  640. // IsAlpha is the validation function for validating if the current field's value is a valid alpha value.
  641. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  642. func IsAlpha(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  643. return alphaRegex.MatchString(field.String())
  644. }
  645. // HasValue is the validation function for validating if the current field's value is not the default static value.
  646. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  647. func HasValue(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  648. switch fieldKind {
  649. case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
  650. return !field.IsNil()
  651. default:
  652. return field.IsValid() && field.Interface() != reflect.Zero(fieldType).Interface()
  653. }
  654. }
  655. // IsGteField is the validation function for validating if the current field's value is greater than or equal to the field specified by the param's value.
  656. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  657. func IsGteField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  658. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  659. if !ok || currentKind != fieldKind {
  660. return false
  661. }
  662. switch fieldKind {
  663. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  664. return field.Int() >= currentField.Int()
  665. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  666. return field.Uint() >= currentField.Uint()
  667. case reflect.Float32, reflect.Float64:
  668. return field.Float() >= currentField.Float()
  669. case reflect.Struct:
  670. // Not Same underlying type i.e. struct and time
  671. if fieldType != currentField.Type() {
  672. return false
  673. }
  674. if fieldType == timeType {
  675. t := currentField.Interface().(time.Time)
  676. fieldTime := field.Interface().(time.Time)
  677. return fieldTime.After(t) || fieldTime.Equal(t)
  678. }
  679. }
  680. // default reflect.String
  681. return len(field.String()) >= len(currentField.String())
  682. }
  683. // IsGtField is the validation function for validating if the current field's value is greater than the field specified by the param's value.
  684. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  685. func IsGtField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  686. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  687. if !ok || currentKind != fieldKind {
  688. return false
  689. }
  690. switch fieldKind {
  691. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  692. return field.Int() > currentField.Int()
  693. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  694. return field.Uint() > currentField.Uint()
  695. case reflect.Float32, reflect.Float64:
  696. return field.Float() > currentField.Float()
  697. case reflect.Struct:
  698. // Not Same underlying type i.e. struct and time
  699. if fieldType != currentField.Type() {
  700. return false
  701. }
  702. if fieldType == timeType {
  703. t := currentField.Interface().(time.Time)
  704. fieldTime := field.Interface().(time.Time)
  705. return fieldTime.After(t)
  706. }
  707. }
  708. // default reflect.String
  709. return len(field.String()) > len(currentField.String())
  710. }
  711. // IsGte is the validation function for validating if the current field's value is greater than or equal to the param's value.
  712. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  713. func IsGte(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  714. switch fieldKind {
  715. case reflect.String:
  716. p := asInt(param)
  717. return int64(utf8.RuneCountInString(field.String())) >= p
  718. case reflect.Slice, reflect.Map, reflect.Array:
  719. p := asInt(param)
  720. return int64(field.Len()) >= p
  721. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  722. p := asInt(param)
  723. return field.Int() >= p
  724. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  725. p := asUint(param)
  726. return field.Uint() >= p
  727. case reflect.Float32, reflect.Float64:
  728. p := asFloat(param)
  729. return field.Float() >= p
  730. case reflect.Struct:
  731. if fieldType == timeType || fieldType == timePtrType {
  732. now := time.Now().UTC()
  733. t := field.Interface().(time.Time)
  734. return t.After(now) || t.Equal(now)
  735. }
  736. }
  737. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  738. }
  739. // IsGt is the validation function for validating if the current field's value is greater than the param's value.
  740. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  741. func IsGt(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  742. switch fieldKind {
  743. case reflect.String:
  744. p := asInt(param)
  745. return int64(utf8.RuneCountInString(field.String())) > p
  746. case reflect.Slice, reflect.Map, reflect.Array:
  747. p := asInt(param)
  748. return int64(field.Len()) > p
  749. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  750. p := asInt(param)
  751. return field.Int() > p
  752. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  753. p := asUint(param)
  754. return field.Uint() > p
  755. case reflect.Float32, reflect.Float64:
  756. p := asFloat(param)
  757. return field.Float() > p
  758. case reflect.Struct:
  759. if fieldType == timeType || fieldType == timePtrType {
  760. return field.Interface().(time.Time).After(time.Now().UTC())
  761. }
  762. }
  763. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  764. }
  765. // HasLengthOf is the validation function for validating if the current field's value is equal to the param's value.
  766. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  767. func HasLengthOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  768. switch fieldKind {
  769. case reflect.String:
  770. p := asInt(param)
  771. return int64(utf8.RuneCountInString(field.String())) == p
  772. case reflect.Slice, reflect.Map, reflect.Array:
  773. p := asInt(param)
  774. return int64(field.Len()) == p
  775. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  776. p := asInt(param)
  777. return field.Int() == p
  778. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  779. p := asUint(param)
  780. return field.Uint() == p
  781. case reflect.Float32, reflect.Float64:
  782. p := asFloat(param)
  783. return field.Float() == p
  784. }
  785. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  786. }
  787. // HasMinOf is the validation function for validating if the current field's value is greater than or equal to the param's value.
  788. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  789. func HasMinOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  790. return IsGte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  791. }
  792. // IsLteField is the validation function for validating if the current field's value is less than or equal to the field specified by the param's value.
  793. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  794. func IsLteField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  795. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  796. if !ok || currentKind != fieldKind {
  797. return false
  798. }
  799. switch fieldKind {
  800. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  801. return field.Int() <= currentField.Int()
  802. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  803. return field.Uint() <= currentField.Uint()
  804. case reflect.Float32, reflect.Float64:
  805. return field.Float() <= currentField.Float()
  806. case reflect.Struct:
  807. // Not Same underlying type i.e. struct and time
  808. if fieldType != currentField.Type() {
  809. return false
  810. }
  811. if fieldType == timeType {
  812. t := currentField.Interface().(time.Time)
  813. fieldTime := field.Interface().(time.Time)
  814. return fieldTime.Before(t) || fieldTime.Equal(t)
  815. }
  816. }
  817. // default reflect.String
  818. return len(field.String()) <= len(currentField.String())
  819. }
  820. // IsLtField is the validation function for validating if the current field's value is less than the field specified by the param's value.
  821. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  822. func IsLtField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  823. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  824. if !ok || currentKind != fieldKind {
  825. return false
  826. }
  827. switch fieldKind {
  828. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  829. return field.Int() < currentField.Int()
  830. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  831. return field.Uint() < currentField.Uint()
  832. case reflect.Float32, reflect.Float64:
  833. return field.Float() < currentField.Float()
  834. case reflect.Struct:
  835. // Not Same underlying type i.e. struct and time
  836. if fieldType != currentField.Type() {
  837. return false
  838. }
  839. if fieldType == timeType {
  840. t := currentField.Interface().(time.Time)
  841. fieldTime := field.Interface().(time.Time)
  842. return fieldTime.Before(t)
  843. }
  844. }
  845. // default reflect.String
  846. return len(field.String()) < len(currentField.String())
  847. }
  848. // IsLte is the validation function for validating if the current field's value is less than or equal to the param's value.
  849. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  850. func IsLte(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  851. switch fieldKind {
  852. case reflect.String:
  853. p := asInt(param)
  854. return int64(utf8.RuneCountInString(field.String())) <= p
  855. case reflect.Slice, reflect.Map, reflect.Array:
  856. p := asInt(param)
  857. return int64(field.Len()) <= p
  858. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  859. p := asInt(param)
  860. return field.Int() <= p
  861. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  862. p := asUint(param)
  863. return field.Uint() <= p
  864. case reflect.Float32, reflect.Float64:
  865. p := asFloat(param)
  866. return field.Float() <= p
  867. case reflect.Struct:
  868. if fieldType == timeType || fieldType == timePtrType {
  869. now := time.Now().UTC()
  870. t := field.Interface().(time.Time)
  871. return t.Before(now) || t.Equal(now)
  872. }
  873. }
  874. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  875. }
  876. // IsLt is the validation function for validating if the current field's value is less than the param's value.
  877. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  878. func IsLt(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  879. switch fieldKind {
  880. case reflect.String:
  881. p := asInt(param)
  882. return int64(utf8.RuneCountInString(field.String())) < p
  883. case reflect.Slice, reflect.Map, reflect.Array:
  884. p := asInt(param)
  885. return int64(field.Len()) < p
  886. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  887. p := asInt(param)
  888. return field.Int() < p
  889. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  890. p := asUint(param)
  891. return field.Uint() < p
  892. case reflect.Float32, reflect.Float64:
  893. p := asFloat(param)
  894. return field.Float() < p
  895. case reflect.Struct:
  896. if fieldType == timeType || fieldType == timePtrType {
  897. return field.Interface().(time.Time).Before(time.Now().UTC())
  898. }
  899. }
  900. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  901. }
  902. // HasMaxOf is the validation function for validating if the current field's value is less than or equal to the param's value.
  903. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  904. func HasMaxOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  905. return IsLte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  906. }
  907. // IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
  908. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  909. func IsTCP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  910. if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
  911. return false
  912. }
  913. _, err := net.ResolveTCPAddr("tcp4", field.String())
  914. return err == nil
  915. }
  916. // IsTCP6AddrResolvable is the validation function for v…

Large files files are truncated, but you can click here to view the full file