PageRenderTime 82ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/kingwill101/palaceapi
Go | 1274 lines | 753 code | 359 blank | 162 comment | 203 complexity | 475d09e54139ac990ab3aeb29588360e MD5 | raw 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. if (int32(s[12]-'0'))-((10-(checksum%10))%10) == 0 {
  226. return true
  227. }
  228. return false
  229. }
  230. // IsISBN10 is the validation function for validating if the field's value is a valid v10 ISBN.
  231. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  232. func IsISBN10(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  233. s := strings.Replace(strings.Replace(field.String(), "-", "", 3), " ", "", 3)
  234. if !iSBN10Regex.MatchString(s) {
  235. return false
  236. }
  237. var checksum int32
  238. var i int32
  239. for i = 0; i < 9; i++ {
  240. checksum += (i + 1) * int32(s[i]-'0')
  241. }
  242. if s[9] == 'X' {
  243. checksum += 10 * 10
  244. } else {
  245. checksum += 10 * int32(s[9]-'0')
  246. }
  247. if checksum%11 == 0 {
  248. return true
  249. }
  250. return false
  251. }
  252. // ExcludesRune is the validation function for validating that the field's value does not contain the rune specified withing the param.
  253. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  254. func ExcludesRune(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  255. return !ContainsRune(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  256. }
  257. // ExcludesAll is the validation function for validating that the field's value does not contain any of the characters specified withing the param.
  258. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  259. func ExcludesAll(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  260. return !ContainsAny(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  261. }
  262. // Excludes is the validation function for validating that the field's value does not contain the text specified withing the param.
  263. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  264. func Excludes(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  265. return !Contains(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  266. }
  267. // ContainsRune is the validation function for validating that the field's value contains the rune specified withing the param.
  268. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  269. func ContainsRune(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  270. r, _ := utf8.DecodeRuneInString(param)
  271. return strings.ContainsRune(field.String(), r)
  272. }
  273. // ContainsAny is the validation function for validating that the field's value contains any of the characters specified withing the param.
  274. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  275. func ContainsAny(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  276. return strings.ContainsAny(field.String(), param)
  277. }
  278. // Contains is the validation function for validating that the field's value contains the text specified withing the param.
  279. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  280. func Contains(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  281. return strings.Contains(field.String(), param)
  282. }
  283. // 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.
  284. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  285. func IsNeField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  286. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  287. if !ok || currentKind != fieldKind {
  288. return true
  289. }
  290. switch fieldKind {
  291. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  292. return field.Int() != currentField.Int()
  293. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  294. return field.Uint() != currentField.Uint()
  295. case reflect.Float32, reflect.Float64:
  296. return field.Float() != currentField.Float()
  297. case reflect.Slice, reflect.Map, reflect.Array:
  298. return int64(field.Len()) != int64(currentField.Len())
  299. case reflect.Struct:
  300. // Not Same underlying type i.e. struct and time
  301. if fieldType != currentField.Type() {
  302. return true
  303. }
  304. if fieldType == timeType {
  305. t := currentField.Interface().(time.Time)
  306. fieldTime := field.Interface().(time.Time)
  307. return !fieldTime.Equal(t)
  308. }
  309. }
  310. // default reflect.String:
  311. return field.String() != currentField.String()
  312. }
  313. // IsNe is the validation function for validating that the field's value does not equal the provided param value.
  314. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  315. func IsNe(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  316. return !IsEq(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  317. }
  318. // 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.
  319. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  320. func IsLteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  321. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  322. if !ok || topKind != fieldKind {
  323. return false
  324. }
  325. switch fieldKind {
  326. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  327. return field.Int() <= topField.Int()
  328. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  329. return field.Uint() <= topField.Uint()
  330. case reflect.Float32, reflect.Float64:
  331. return field.Float() <= topField.Float()
  332. case reflect.Slice, reflect.Map, reflect.Array:
  333. return int64(field.Len()) <= int64(topField.Len())
  334. case reflect.Struct:
  335. // Not Same underlying type i.e. struct and time
  336. if fieldType != topField.Type() {
  337. return false
  338. }
  339. if fieldType == timeType {
  340. fieldTime := field.Interface().(time.Time)
  341. topTime := topField.Interface().(time.Time)
  342. return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
  343. }
  344. }
  345. // default reflect.String:
  346. return field.String() <= topField.String()
  347. }
  348. // 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.
  349. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  350. func IsLtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  351. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  352. if !ok || topKind != fieldKind {
  353. return false
  354. }
  355. switch fieldKind {
  356. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  357. return field.Int() < topField.Int()
  358. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  359. return field.Uint() < topField.Uint()
  360. case reflect.Float32, reflect.Float64:
  361. return field.Float() < topField.Float()
  362. case reflect.Slice, reflect.Map, reflect.Array:
  363. return int64(field.Len()) < int64(topField.Len())
  364. case reflect.Struct:
  365. // Not Same underlying type i.e. struct and time
  366. if fieldType != topField.Type() {
  367. return false
  368. }
  369. if fieldType == timeType {
  370. fieldTime := field.Interface().(time.Time)
  371. topTime := topField.Interface().(time.Time)
  372. return fieldTime.Before(topTime)
  373. }
  374. }
  375. // default reflect.String:
  376. return field.String() < topField.String()
  377. }
  378. // 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.
  379. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  380. func IsGteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  381. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  382. if !ok || topKind != fieldKind {
  383. return false
  384. }
  385. switch fieldKind {
  386. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  387. return field.Int() >= topField.Int()
  388. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  389. return field.Uint() >= topField.Uint()
  390. case reflect.Float32, reflect.Float64:
  391. return field.Float() >= topField.Float()
  392. case reflect.Slice, reflect.Map, reflect.Array:
  393. return int64(field.Len()) >= int64(topField.Len())
  394. case reflect.Struct:
  395. // Not Same underlying type i.e. struct and time
  396. if fieldType != topField.Type() {
  397. return false
  398. }
  399. if fieldType == timeType {
  400. fieldTime := field.Interface().(time.Time)
  401. topTime := topField.Interface().(time.Time)
  402. return fieldTime.After(topTime) || fieldTime.Equal(topTime)
  403. }
  404. }
  405. // default reflect.String:
  406. return field.String() >= topField.String()
  407. }
  408. // 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.
  409. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  410. func IsGtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  411. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  412. if !ok || topKind != fieldKind {
  413. return false
  414. }
  415. switch fieldKind {
  416. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  417. return field.Int() > topField.Int()
  418. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  419. return field.Uint() > topField.Uint()
  420. case reflect.Float32, reflect.Float64:
  421. return field.Float() > topField.Float()
  422. case reflect.Slice, reflect.Map, reflect.Array:
  423. return int64(field.Len()) > int64(topField.Len())
  424. case reflect.Struct:
  425. // Not Same underlying type i.e. struct and time
  426. if fieldType != topField.Type() {
  427. return false
  428. }
  429. if fieldType == timeType {
  430. fieldTime := field.Interface().(time.Time)
  431. topTime := topField.Interface().(time.Time)
  432. return fieldTime.After(topTime)
  433. }
  434. }
  435. // default reflect.String:
  436. return field.String() > topField.String()
  437. }
  438. // 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.
  439. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  440. func IsNeCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  441. topField, currentKind, ok := v.GetStructFieldOK(topStruct, param)
  442. if !ok || currentKind != fieldKind {
  443. return true
  444. }
  445. switch fieldKind {
  446. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  447. return topField.Int() != field.Int()
  448. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  449. return topField.Uint() != field.Uint()
  450. case reflect.Float32, reflect.Float64:
  451. return topField.Float() != field.Float()
  452. case reflect.Slice, reflect.Map, reflect.Array:
  453. return int64(topField.Len()) != int64(field.Len())
  454. case reflect.Struct:
  455. // Not Same underlying type i.e. struct and time
  456. if fieldType != topField.Type() {
  457. return true
  458. }
  459. if fieldType == timeType {
  460. t := field.Interface().(time.Time)
  461. fieldTime := topField.Interface().(time.Time)
  462. return !fieldTime.Equal(t)
  463. }
  464. }
  465. // default reflect.String:
  466. return topField.String() != field.String()
  467. }
  468. // 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.
  469. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  470. func IsEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  471. topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
  472. if !ok || topKind != fieldKind {
  473. return false
  474. }
  475. switch fieldKind {
  476. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  477. return topField.Int() == field.Int()
  478. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  479. return topField.Uint() == field.Uint()
  480. case reflect.Float32, reflect.Float64:
  481. return topField.Float() == field.Float()
  482. case reflect.Slice, reflect.Map, reflect.Array:
  483. return int64(topField.Len()) == int64(field.Len())
  484. case reflect.Struct:
  485. // Not Same underlying type i.e. struct and time
  486. if fieldType != topField.Type() {
  487. return false
  488. }
  489. if fieldType == timeType {
  490. t := field.Interface().(time.Time)
  491. fieldTime := topField.Interface().(time.Time)
  492. return fieldTime.Equal(t)
  493. }
  494. }
  495. // default reflect.String:
  496. return topField.String() == field.String()
  497. }
  498. // IsEqField is the validation function for validating if the current field's value is equal to the field specified by the param's value.
  499. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  500. func IsEqField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  501. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  502. if !ok || currentKind != fieldKind {
  503. return false
  504. }
  505. switch fieldKind {
  506. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  507. return field.Int() == currentField.Int()
  508. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  509. return field.Uint() == currentField.Uint()
  510. case reflect.Float32, reflect.Float64:
  511. return field.Float() == currentField.Float()
  512. case reflect.Slice, reflect.Map, reflect.Array:
  513. return int64(field.Len()) == int64(currentField.Len())
  514. case reflect.Struct:
  515. // Not Same underlying type i.e. struct and time
  516. if fieldType != currentField.Type() {
  517. return false
  518. }
  519. if fieldType == timeType {
  520. t := currentField.Interface().(time.Time)
  521. fieldTime := field.Interface().(time.Time)
  522. return fieldTime.Equal(t)
  523. }
  524. }
  525. // default reflect.String:
  526. return field.String() == currentField.String()
  527. }
  528. // IsEq is the validation function for validating if the current field's value is equal to the param's value.
  529. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  530. func IsEq(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  531. switch fieldKind {
  532. case reflect.String:
  533. return field.String() == param
  534. case reflect.Slice, reflect.Map, reflect.Array:
  535. p := asInt(param)
  536. return int64(field.Len()) == p
  537. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  538. p := asInt(param)
  539. return field.Int() == p
  540. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  541. p := asUint(param)
  542. return field.Uint() == p
  543. case reflect.Float32, reflect.Float64:
  544. p := asFloat(param)
  545. return field.Float() == p
  546. }
  547. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  548. }
  549. // IsBase64 is the validation function for validating if the current field's value is a valid base 64.
  550. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  551. func IsBase64(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  552. return base64Regex.MatchString(field.String())
  553. }
  554. // IsURI is the validation function for validating if the current field's value is a valid URI.
  555. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  556. func IsURI(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  557. switch fieldKind {
  558. case reflect.String:
  559. _, err := url.ParseRequestURI(field.String())
  560. return err == nil
  561. }
  562. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  563. }
  564. // IsURL is the validation function for validating if the current field's value is a valid URL.
  565. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  566. func IsURL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  567. switch fieldKind {
  568. case reflect.String:
  569. url, err := url.ParseRequestURI(field.String())
  570. if err != nil {
  571. return false
  572. }
  573. if url.Scheme == blank {
  574. return false
  575. }
  576. return err == nil
  577. }
  578. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  579. }
  580. // IsEmail is the validation function for validating if the current field's value is a valid email address.
  581. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  582. func IsEmail(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  583. return emailRegex.MatchString(field.String())
  584. }
  585. // IsHSLA is the validation function for validating if the current field's value is a valid HSLA color.
  586. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  587. func IsHSLA(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  588. return hslaRegex.MatchString(field.String())
  589. }
  590. // IsHSL is the validation function for validating if the current field's value is a valid HSL color.
  591. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  592. func IsHSL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  593. return hslRegex.MatchString(field.String())
  594. }
  595. // IsRGBA is the validation function for validating if the current field's value is a valid RGBA color.
  596. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  597. func IsRGBA(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  598. return rgbaRegex.MatchString(field.String())
  599. }
  600. // IsRGB is the validation function for validating if the current field's value is a valid RGB color.
  601. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  602. func IsRGB(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  603. return rgbRegex.MatchString(field.String())
  604. }
  605. // IsHEXColor is the validation function for validating if the current field's value is a valid HEX color.
  606. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  607. func IsHEXColor(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  608. return hexcolorRegex.MatchString(field.String())
  609. }
  610. // IsHexadecimal is the validation function for validating if the current field's value is a valid hexadecimal.
  611. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  612. func IsHexadecimal(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  613. return hexadecimalRegex.MatchString(field.String())
  614. }
  615. // IsNumber is the validation function for validating if the current field's value is a valid number.
  616. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  617. func IsNumber(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  618. return numberRegex.MatchString(field.String())
  619. }
  620. // IsNumeric is the validation function for validating if the current field's value is a valid numeric value.
  621. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  622. func IsNumeric(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  623. return numericRegex.MatchString(field.String())
  624. }
  625. // IsAlphanum is the validation function for validating if the current field's value is a valid alphanumeric value.
  626. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  627. func IsAlphanum(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  628. return alphaNumericRegex.MatchString(field.String())
  629. }
  630. // IsAlpha is the validation function for validating if the current field's value is a valid alpha value.
  631. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  632. func IsAlpha(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  633. return alphaRegex.MatchString(field.String())
  634. }
  635. // HasValue is the validation function for validating if the current field's value is not the default static value.
  636. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  637. func HasValue(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  638. switch fieldKind {
  639. case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
  640. return !field.IsNil()
  641. default:
  642. return field.IsValid() && field.Interface() != reflect.Zero(fieldType).Interface()
  643. }
  644. }
  645. // 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.
  646. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  647. func IsGteField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  648. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  649. if !ok || currentKind != fieldKind {
  650. return false
  651. }
  652. switch fieldKind {
  653. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  654. return field.Int() >= currentField.Int()
  655. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  656. return field.Uint() >= currentField.Uint()
  657. case reflect.Float32, reflect.Float64:
  658. return field.Float() >= currentField.Float()
  659. case reflect.Struct:
  660. // Not Same underlying type i.e. struct and time
  661. if fieldType != currentField.Type() {
  662. return false
  663. }
  664. if fieldType == timeType {
  665. t := currentField.Interface().(time.Time)
  666. fieldTime := field.Interface().(time.Time)
  667. return fieldTime.After(t) || fieldTime.Equal(t)
  668. }
  669. }
  670. // default reflect.String
  671. return len(field.String()) >= len(currentField.String())
  672. }
  673. // IsGtField is the validation function for validating if the current field's value is greater than the field specified by the param's value.
  674. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  675. func IsGtField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  676. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  677. if !ok || currentKind != fieldKind {
  678. return false
  679. }
  680. switch fieldKind {
  681. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  682. return field.Int() > currentField.Int()
  683. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  684. return field.Uint() > currentField.Uint()
  685. case reflect.Float32, reflect.Float64:
  686. return field.Float() > currentField.Float()
  687. case reflect.Struct:
  688. // Not Same underlying type i.e. struct and time
  689. if fieldType != currentField.Type() {
  690. return false
  691. }
  692. if fieldType == timeType {
  693. t := currentField.Interface().(time.Time)
  694. fieldTime := field.Interface().(time.Time)
  695. return fieldTime.After(t)
  696. }
  697. }
  698. // default reflect.String
  699. return len(field.String()) > len(currentField.String())
  700. }
  701. // IsGte is the validation function for validating if the current field's value is greater than or equal to the param's value.
  702. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  703. func IsGte(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  704. switch fieldKind {
  705. case reflect.String:
  706. p := asInt(param)
  707. return int64(utf8.RuneCountInString(field.String())) >= p
  708. case reflect.Slice, reflect.Map, reflect.Array:
  709. p := asInt(param)
  710. return int64(field.Len()) >= p
  711. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  712. p := asInt(param)
  713. return field.Int() >= p
  714. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  715. p := asUint(param)
  716. return field.Uint() >= p
  717. case reflect.Float32, reflect.Float64:
  718. p := asFloat(param)
  719. return field.Float() >= p
  720. case reflect.Struct:
  721. if fieldType == timeType || fieldType == timePtrType {
  722. now := time.Now().UTC()
  723. t := field.Interface().(time.Time)
  724. return t.After(now) || t.Equal(now)
  725. }
  726. }
  727. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  728. }
  729. // IsGt is the validation function for validating if the current field's value is greater than the param's value.
  730. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  731. func IsGt(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  732. switch fieldKind {
  733. case reflect.String:
  734. p := asInt(param)
  735. return int64(utf8.RuneCountInString(field.String())) > p
  736. case reflect.Slice, reflect.Map, reflect.Array:
  737. p := asInt(param)
  738. return int64(field.Len()) > p
  739. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  740. p := asInt(param)
  741. return field.Int() > p
  742. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  743. p := asUint(param)
  744. return field.Uint() > p
  745. case reflect.Float32, reflect.Float64:
  746. p := asFloat(param)
  747. return field.Float() > p
  748. case reflect.Struct:
  749. if field.Type() == timeType || field.Type() == timePtrType {
  750. return field.Interface().(time.Time).After(time.Now().UTC())
  751. }
  752. }
  753. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  754. }
  755. // HasLengthOf is the validation function for validating if the current field's value is equal to the param's value.
  756. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  757. func HasLengthOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  758. switch fieldKind {
  759. case reflect.String:
  760. p := asInt(param)
  761. return int64(utf8.RuneCountInString(field.String())) == p
  762. case reflect.Slice, reflect.Map, reflect.Array:
  763. p := asInt(param)
  764. return int64(field.Len()) == p
  765. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  766. p := asInt(param)
  767. return field.Int() == p
  768. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  769. p := asUint(param)
  770. return field.Uint() == p
  771. case reflect.Float32, reflect.Float64:
  772. p := asFloat(param)
  773. return field.Float() == p
  774. }
  775. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  776. }
  777. // HasMinOf is the validation function for validating if the current field's value is greater than or equal to the param's value.
  778. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  779. func HasMinOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  780. return IsGte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  781. }
  782. // 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.
  783. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  784. func IsLteField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  785. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  786. if !ok || currentKind != fieldKind {
  787. return false
  788. }
  789. switch fieldKind {
  790. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  791. return field.Int() <= currentField.Int()
  792. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  793. return field.Uint() <= currentField.Uint()
  794. case reflect.Float32, reflect.Float64:
  795. return field.Float() <= currentField.Float()
  796. case reflect.Struct:
  797. // Not Same underlying type i.e. struct and time
  798. if fieldType != currentField.Type() {
  799. return false
  800. }
  801. if fieldType == timeType {
  802. t := currentField.Interface().(time.Time)
  803. fieldTime := field.Interface().(time.Time)
  804. return fieldTime.Before(t) || fieldTime.Equal(t)
  805. }
  806. }
  807. // default reflect.String
  808. return len(field.String()) <= len(currentField.String())
  809. }
  810. // IsLtField is the validation function for validating if the current field's value is less than the field specified by the param's value.
  811. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  812. func IsLtField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  813. currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
  814. if !ok || currentKind != fieldKind {
  815. return false
  816. }
  817. switch fieldKind {
  818. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  819. return field.Int() < currentField.Int()
  820. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  821. return field.Uint() < currentField.Uint()
  822. case reflect.Float32, reflect.Float64:
  823. return field.Float() < currentField.Float()
  824. case reflect.Struct:
  825. // Not Same underlying type i.e. struct and time
  826. if fieldType != currentField.Type() {
  827. return false
  828. }
  829. if fieldType == timeType {
  830. t := currentField.Interface().(time.Time)
  831. fieldTime := field.Interface().(time.Time)
  832. return fieldTime.Before(t)
  833. }
  834. }
  835. // default reflect.String
  836. return len(field.String()) < len(currentField.String())
  837. }
  838. // IsLte is the validation function for validating if the current field's value is less than or equal to the param's value.
  839. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  840. func IsLte(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  841. switch fieldKind {
  842. case reflect.String:
  843. p := asInt(param)
  844. return int64(utf8.RuneCountInString(field.String())) <= p
  845. case reflect.Slice, reflect.Map, reflect.Array:
  846. p := asInt(param)
  847. return int64(field.Len()) <= p
  848. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  849. p := asInt(param)
  850. return field.Int() <= p
  851. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  852. p := asUint(param)
  853. return field.Uint() <= p
  854. case reflect.Float32, reflect.Float64:
  855. p := asFloat(param)
  856. return field.Float() <= p
  857. case reflect.Struct:
  858. if fieldType == timeType || fieldType == timePtrType {
  859. now := time.Now().UTC()
  860. t := field.Interface().(time.Time)
  861. return t.Before(now) || t.Equal(now)
  862. }
  863. }
  864. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  865. }
  866. // IsLt is the validation function for validating if the current field's value is less than the param's value.
  867. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  868. func IsLt(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  869. switch fieldKind {
  870. case reflect.String:
  871. p := asInt(param)
  872. return int64(utf8.RuneCountInString(field.String())) < p
  873. case reflect.Slice, reflect.Map, reflect.Array:
  874. p := asInt(param)
  875. return int64(field.Len()) < p
  876. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  877. p := asInt(param)
  878. return field.Int() < p
  879. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  880. p := asUint(param)
  881. return field.Uint() < p
  882. case reflect.Float32, reflect.Float64:
  883. p := asFloat(param)
  884. return field.Float() < p
  885. case reflect.Struct:
  886. if field.Type() == timeType || field.Type() == timePtrType {
  887. return field.Interface().(time.Time).Before(time.Now().UTC())
  888. }
  889. }
  890. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  891. }
  892. // HasMaxOf is the validation function for validating if the current field's value is less than or equal to the param's value.
  893. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  894. func HasMaxOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  895. return IsLte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
  896. }
  897. // IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
  898. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  899. func IsTCP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  900. if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
  901. return false
  902. }
  903. _, err := net.ResolveTCPAddr("tcp4", field.String())
  904. return err == nil
  905. }
  906. // IsTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
  907. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  908. func IsTCP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
  909. if !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
  910. return false
  911. }
  912. _, err := net.ResolveTCPAddr("tcp6", field.String())
  913. return err == nil
  914. }
  915. // IsTCPAddrResolvable is the validation function for validating if the field's value is