baked_in.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. package validator
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "net/url"
  7. "reflect"
  8. "strings"
  9. "time"
  10. "unicode/utf8"
  11. )
  12. // Func accepts a FieldLevel interface for all validation needs. The return
  13. // value should be true when validation succeeds.
  14. type Func func(fl FieldLevel) bool
  15. // FuncCtx accepts a context.Context and FieldLevel interface for all
  16. // validation needs. The return value should be true when validation succeeds.
  17. type FuncCtx func(ctx context.Context, fl FieldLevel) bool
  18. // wrapFunc wraps noramal Func makes it compatible with FuncCtx
  19. func wrapFunc(fn Func) FuncCtx {
  20. if fn == nil {
  21. return nil // be sure not to wrap a bad function.
  22. }
  23. return func(ctx context.Context, fl FieldLevel) bool {
  24. return fn(fl)
  25. }
  26. }
  27. var (
  28. restrictedTags = map[string]struct{}{
  29. diveTag: {},
  30. keysTag: {},
  31. endKeysTag: {},
  32. structOnlyTag: {},
  33. omitempty: {},
  34. skipValidationTag: {},
  35. utf8HexComma: {},
  36. utf8Pipe: {},
  37. noStructLevelTag: {},
  38. requiredTag: {},
  39. isdefault: {},
  40. }
  41. // BakedInAliasValidators is a default mapping of a single validation tag that
  42. // defines a common or complex set of validation(s) to simplify
  43. // adding validation to structs.
  44. bakedInAliases = map[string]string{
  45. "iscolor": "hexcolor|rgb|rgba|hsl|hsla",
  46. }
  47. // BakedInValidators is the default map of ValidationFunc
  48. // you can add, remove or even replace items to suite your needs,
  49. // or even disregard and use your own map if so desired.
  50. bakedInValidators = map[string]Func{
  51. "required": hasValue,
  52. "isdefault": isDefault,
  53. "len": hasLengthOf,
  54. "min": hasMinOf,
  55. "max": hasMaxOf,
  56. "eq": isEq,
  57. "ne": isNe,
  58. "lt": isLt,
  59. "lte": isLte,
  60. "gt": isGt,
  61. "gte": isGte,
  62. "eqfield": isEqField,
  63. "eqcsfield": isEqCrossStructField,
  64. "necsfield": isNeCrossStructField,
  65. "gtcsfield": isGtCrossStructField,
  66. "gtecsfield": isGteCrossStructField,
  67. "ltcsfield": isLtCrossStructField,
  68. "ltecsfield": isLteCrossStructField,
  69. "nefield": isNeField,
  70. "gtefield": isGteField,
  71. "gtfield": isGtField,
  72. "ltefield": isLteField,
  73. "ltfield": isLtField,
  74. "alpha": isAlpha,
  75. "alphanum": isAlphanum,
  76. "alphaunicode": isAlphaUnicode,
  77. "alphanumunicode": isAlphanumUnicode,
  78. "numeric": isNumeric,
  79. "number": isNumber,
  80. "hexadecimal": isHexadecimal,
  81. "hexcolor": isHEXColor,
  82. "rgb": isRGB,
  83. "rgba": isRGBA,
  84. "hsl": isHSL,
  85. "hsla": isHSLA,
  86. "email": isEmail,
  87. "url": isURL,
  88. "uri": isURI,
  89. "base64": isBase64,
  90. "contains": contains,
  91. "containsany": containsAny,
  92. "containsrune": containsRune,
  93. "excludes": excludes,
  94. "excludesall": excludesAll,
  95. "excludesrune": excludesRune,
  96. "isbn": isISBN,
  97. "isbn10": isISBN10,
  98. "isbn13": isISBN13,
  99. "uuid": isUUID,
  100. "uuid3": isUUID3,
  101. "uuid4": isUUID4,
  102. "uuid5": isUUID5,
  103. "ascii": isASCII,
  104. "printascii": isPrintableASCII,
  105. "multibyte": hasMultiByteCharacter,
  106. "datauri": isDataURI,
  107. "latitude": isLatitude,
  108. "longitude": isLongitude,
  109. "ssn": isSSN,
  110. "ipv4": isIPv4,
  111. "ipv6": isIPv6,
  112. "ip": isIP,
  113. "cidrv4": isCIDRv4,
  114. "cidrv6": isCIDRv6,
  115. "cidr": isCIDR,
  116. "tcp4_addr": isTCP4AddrResolvable,
  117. "tcp6_addr": isTCP6AddrResolvable,
  118. "tcp_addr": isTCPAddrResolvable,
  119. "udp4_addr": isUDP4AddrResolvable,
  120. "udp6_addr": isUDP6AddrResolvable,
  121. "udp_addr": isUDPAddrResolvable,
  122. "ip4_addr": isIP4AddrResolvable,
  123. "ip6_addr": isIP6AddrResolvable,
  124. "ip_addr": isIPAddrResolvable,
  125. "unix_addr": isUnixAddrResolvable,
  126. "mac": isMAC,
  127. "hostname": isHostname,
  128. "fqdn": isFQDN,
  129. "unique": isUnique,
  130. }
  131. )
  132. // isUnique is the validation function for validating if each array|slice element is unique
  133. func isUnique(fl FieldLevel) bool {
  134. field := fl.Field()
  135. v := reflect.ValueOf(struct{}{})
  136. switch field.Kind() {
  137. case reflect.Slice, reflect.Array:
  138. m := reflect.MakeMap(reflect.MapOf(fl.Field().Type().Elem(), v.Type()))
  139. for i := 0; i < field.Len(); i++ {
  140. m.SetMapIndex(field.Index(i), v)
  141. }
  142. return field.Len() == m.Len()
  143. default:
  144. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  145. }
  146. }
  147. // IsMAC is the validation function for validating if the field's value is a valid MAC address.
  148. func isMAC(fl FieldLevel) bool {
  149. _, err := net.ParseMAC(fl.Field().String())
  150. return err == nil
  151. }
  152. // IsCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
  153. func isCIDRv4(fl FieldLevel) bool {
  154. ip, _, err := net.ParseCIDR(fl.Field().String())
  155. return err == nil && ip.To4() != nil
  156. }
  157. // IsCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.
  158. func isCIDRv6(fl FieldLevel) bool {
  159. ip, _, err := net.ParseCIDR(fl.Field().String())
  160. return err == nil && ip.To4() == nil
  161. }
  162. // IsCIDR is the validation function for validating if the field's value is a valid v4 or v6 CIDR address.
  163. func isCIDR(fl FieldLevel) bool {
  164. _, _, err := net.ParseCIDR(fl.Field().String())
  165. return err == nil
  166. }
  167. // IsIPv4 is the validation function for validating if a value is a valid v4 IP address.
  168. func isIPv4(fl FieldLevel) bool {
  169. ip := net.ParseIP(fl.Field().String())
  170. return ip != nil && ip.To4() != nil
  171. }
  172. // IsIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
  173. func isIPv6(fl FieldLevel) bool {
  174. ip := net.ParseIP(fl.Field().String())
  175. return ip != nil && ip.To4() == nil
  176. }
  177. // IsIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.
  178. func isIP(fl FieldLevel) bool {
  179. ip := net.ParseIP(fl.Field().String())
  180. return ip != nil
  181. }
  182. // IsSSN is the validation function for validating if the field's value is a valid SSN.
  183. func isSSN(fl FieldLevel) bool {
  184. field := fl.Field()
  185. if field.Len() != 11 {
  186. return false
  187. }
  188. return sSNRegex.MatchString(field.String())
  189. }
  190. // IsLongitude is the validation function for validating if the field's value is a valid longitude coordinate.
  191. func isLongitude(fl FieldLevel) bool {
  192. return longitudeRegex.MatchString(fl.Field().String())
  193. }
  194. // IsLatitude is the validation function for validating if the field's value is a valid latitude coordinate.
  195. func isLatitude(fl FieldLevel) bool {
  196. return latitudeRegex.MatchString(fl.Field().String())
  197. }
  198. // IsDataURI is the validation function for validating if the field's value is a valid data URI.
  199. func isDataURI(fl FieldLevel) bool {
  200. uri := strings.SplitN(fl.Field().String(), ",", 2)
  201. if len(uri) != 2 {
  202. return false
  203. }
  204. if !dataURIRegex.MatchString(uri[0]) {
  205. return false
  206. }
  207. return base64Regex.MatchString(uri[1])
  208. }
  209. // HasMultiByteCharacter is the validation function for validating if the field's value has a multi byte character.
  210. func hasMultiByteCharacter(fl FieldLevel) bool {
  211. field := fl.Field()
  212. if field.Len() == 0 {
  213. return true
  214. }
  215. return multibyteRegex.MatchString(field.String())
  216. }
  217. // IsPrintableASCII is the validation function for validating if the field's value is a valid printable ASCII character.
  218. func isPrintableASCII(fl FieldLevel) bool {
  219. return printableASCIIRegex.MatchString(fl.Field().String())
  220. }
  221. // IsASCII is the validation function for validating if the field's value is a valid ASCII character.
  222. func isASCII(fl FieldLevel) bool {
  223. return aSCIIRegex.MatchString(fl.Field().String())
  224. }
  225. // IsUUID5 is the validation function for validating if the field's value is a valid v5 UUID.
  226. func isUUID5(fl FieldLevel) bool {
  227. return uUID5Regex.MatchString(fl.Field().String())
  228. }
  229. // IsUUID4 is the validation function for validating if the field's value is a valid v4 UUID.
  230. func isUUID4(fl FieldLevel) bool {
  231. return uUID4Regex.MatchString(fl.Field().String())
  232. }
  233. // IsUUID3 is the validation function for validating if the field's value is a valid v3 UUID.
  234. func isUUID3(fl FieldLevel) bool {
  235. return uUID3Regex.MatchString(fl.Field().String())
  236. }
  237. // IsUUID is the validation function for validating if the field's value is a valid UUID of any version.
  238. func isUUID(fl FieldLevel) bool {
  239. return uUIDRegex.MatchString(fl.Field().String())
  240. }
  241. // IsISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
  242. func isISBN(fl FieldLevel) bool {
  243. return isISBN10(fl) || isISBN13(fl)
  244. }
  245. // IsISBN13 is the validation function for validating if the field's value is a valid v13 ISBN.
  246. func isISBN13(fl FieldLevel) bool {
  247. s := strings.Replace(strings.Replace(fl.Field().String(), "-", "", 4), " ", "", 4)
  248. if !iSBN13Regex.MatchString(s) {
  249. return false
  250. }
  251. var checksum int32
  252. var i int32
  253. factor := []int32{1, 3}
  254. for i = 0; i < 12; i++ {
  255. checksum += factor[i%2] * int32(s[i]-'0')
  256. }
  257. return (int32(s[12]-'0'))-((10-(checksum%10))%10) == 0
  258. }
  259. // IsISBN10 is the validation function for validating if the field's value is a valid v10 ISBN.
  260. func isISBN10(fl FieldLevel) bool {
  261. s := strings.Replace(strings.Replace(fl.Field().String(), "-", "", 3), " ", "", 3)
  262. if !iSBN10Regex.MatchString(s) {
  263. return false
  264. }
  265. var checksum int32
  266. var i int32
  267. for i = 0; i < 9; i++ {
  268. checksum += (i + 1) * int32(s[i]-'0')
  269. }
  270. if s[9] == 'X' {
  271. checksum += 10 * 10
  272. } else {
  273. checksum += 10 * int32(s[9]-'0')
  274. }
  275. return checksum%11 == 0
  276. }
  277. // ExcludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
  278. func excludesRune(fl FieldLevel) bool {
  279. return !containsRune(fl)
  280. }
  281. // ExcludesAll is the validation function for validating that the field's value does not contain any of the characters specified within the param.
  282. func excludesAll(fl FieldLevel) bool {
  283. return !containsAny(fl)
  284. }
  285. // Excludes is the validation function for validating that the field's value does not contain the text specified within the param.
  286. func excludes(fl FieldLevel) bool {
  287. return !contains(fl)
  288. }
  289. // ContainsRune is the validation function for validating that the field's value contains the rune specified within the param.
  290. func containsRune(fl FieldLevel) bool {
  291. r, _ := utf8.DecodeRuneInString(fl.Param())
  292. return strings.ContainsRune(fl.Field().String(), r)
  293. }
  294. // ContainsAny is the validation function for validating that the field's value contains any of the characters specified within the param.
  295. func containsAny(fl FieldLevel) bool {
  296. return strings.ContainsAny(fl.Field().String(), fl.Param())
  297. }
  298. // Contains is the validation function for validating that the field's value contains the text specified within the param.
  299. func contains(fl FieldLevel) bool {
  300. return strings.Contains(fl.Field().String(), fl.Param())
  301. }
  302. // 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.
  303. func isNeField(fl FieldLevel) bool {
  304. field := fl.Field()
  305. kind := field.Kind()
  306. currentField, currentKind, ok := fl.GetStructFieldOK()
  307. if !ok || currentKind != kind {
  308. return true
  309. }
  310. switch kind {
  311. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  312. return field.Int() != currentField.Int()
  313. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  314. return field.Uint() != currentField.Uint()
  315. case reflect.Float32, reflect.Float64:
  316. return field.Float() != currentField.Float()
  317. case reflect.Slice, reflect.Map, reflect.Array:
  318. return int64(field.Len()) != int64(currentField.Len())
  319. case reflect.Struct:
  320. fieldType := field.Type()
  321. // Not Same underlying type i.e. struct and time
  322. if fieldType != currentField.Type() {
  323. return true
  324. }
  325. if fieldType == timeType {
  326. t := currentField.Interface().(time.Time)
  327. fieldTime := field.Interface().(time.Time)
  328. return !fieldTime.Equal(t)
  329. }
  330. }
  331. // default reflect.String:
  332. return field.String() != currentField.String()
  333. }
  334. // IsNe is the validation function for validating that the field's value does not equal the provided param value.
  335. func isNe(fl FieldLevel) bool {
  336. return !isEq(fl)
  337. }
  338. // 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.
  339. func isLteCrossStructField(fl FieldLevel) bool {
  340. field := fl.Field()
  341. kind := field.Kind()
  342. topField, topKind, ok := fl.GetStructFieldOK()
  343. if !ok || topKind != kind {
  344. return false
  345. }
  346. switch kind {
  347. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  348. return field.Int() <= topField.Int()
  349. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  350. return field.Uint() <= topField.Uint()
  351. case reflect.Float32, reflect.Float64:
  352. return field.Float() <= topField.Float()
  353. case reflect.Slice, reflect.Map, reflect.Array:
  354. return int64(field.Len()) <= int64(topField.Len())
  355. case reflect.Struct:
  356. fieldType := field.Type()
  357. // Not Same underlying type i.e. struct and time
  358. if fieldType != topField.Type() {
  359. return false
  360. }
  361. if fieldType == timeType {
  362. fieldTime := field.Interface().(time.Time)
  363. topTime := topField.Interface().(time.Time)
  364. return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
  365. }
  366. }
  367. // default reflect.String:
  368. return field.String() <= topField.String()
  369. }
  370. // 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.
  371. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  372. func isLtCrossStructField(fl FieldLevel) bool {
  373. field := fl.Field()
  374. kind := field.Kind()
  375. topField, topKind, ok := fl.GetStructFieldOK()
  376. if !ok || topKind != kind {
  377. return false
  378. }
  379. switch kind {
  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. fieldType := field.Type()
  390. // Not Same underlying type i.e. struct and time
  391. if fieldType != topField.Type() {
  392. return false
  393. }
  394. if fieldType == timeType {
  395. fieldTime := field.Interface().(time.Time)
  396. topTime := topField.Interface().(time.Time)
  397. return fieldTime.Before(topTime)
  398. }
  399. }
  400. // default reflect.String:
  401. return field.String() < topField.String()
  402. }
  403. // 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.
  404. func isGteCrossStructField(fl FieldLevel) bool {
  405. field := fl.Field()
  406. kind := field.Kind()
  407. topField, topKind, ok := fl.GetStructFieldOK()
  408. if !ok || topKind != kind {
  409. return false
  410. }
  411. switch kind {
  412. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  413. return field.Int() >= topField.Int()
  414. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  415. return field.Uint() >= topField.Uint()
  416. case reflect.Float32, reflect.Float64:
  417. return field.Float() >= topField.Float()
  418. case reflect.Slice, reflect.Map, reflect.Array:
  419. return int64(field.Len()) >= int64(topField.Len())
  420. case reflect.Struct:
  421. fieldType := field.Type()
  422. // Not Same underlying type i.e. struct and time
  423. if fieldType != topField.Type() {
  424. return false
  425. }
  426. if fieldType == timeType {
  427. fieldTime := field.Interface().(time.Time)
  428. topTime := topField.Interface().(time.Time)
  429. return fieldTime.After(topTime) || fieldTime.Equal(topTime)
  430. }
  431. }
  432. // default reflect.String:
  433. return field.String() >= topField.String()
  434. }
  435. // 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.
  436. func isGtCrossStructField(fl FieldLevel) bool {
  437. field := fl.Field()
  438. kind := field.Kind()
  439. topField, topKind, ok := fl.GetStructFieldOK()
  440. if !ok || topKind != kind {
  441. return false
  442. }
  443. switch kind {
  444. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  445. return field.Int() > topField.Int()
  446. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  447. return field.Uint() > topField.Uint()
  448. case reflect.Float32, reflect.Float64:
  449. return field.Float() > topField.Float()
  450. case reflect.Slice, reflect.Map, reflect.Array:
  451. return int64(field.Len()) > int64(topField.Len())
  452. case reflect.Struct:
  453. fieldType := field.Type()
  454. // Not Same underlying type i.e. struct and time
  455. if fieldType != topField.Type() {
  456. return false
  457. }
  458. if fieldType == timeType {
  459. fieldTime := field.Interface().(time.Time)
  460. topTime := topField.Interface().(time.Time)
  461. return fieldTime.After(topTime)
  462. }
  463. }
  464. // default reflect.String:
  465. return field.String() > topField.String()
  466. }
  467. // 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.
  468. func isNeCrossStructField(fl FieldLevel) bool {
  469. field := fl.Field()
  470. kind := field.Kind()
  471. topField, currentKind, ok := fl.GetStructFieldOK()
  472. if !ok || currentKind != kind {
  473. return true
  474. }
  475. switch kind {
  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. fieldType := field.Type()
  486. // Not Same underlying type i.e. struct and time
  487. if fieldType != topField.Type() {
  488. return true
  489. }
  490. if fieldType == timeType {
  491. t := field.Interface().(time.Time)
  492. fieldTime := topField.Interface().(time.Time)
  493. return !fieldTime.Equal(t)
  494. }
  495. }
  496. // default reflect.String:
  497. return topField.String() != field.String()
  498. }
  499. // 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.
  500. func isEqCrossStructField(fl FieldLevel) bool {
  501. field := fl.Field()
  502. kind := field.Kind()
  503. topField, topKind, ok := fl.GetStructFieldOK()
  504. if !ok || topKind != kind {
  505. return false
  506. }
  507. switch kind {
  508. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  509. return topField.Int() == field.Int()
  510. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  511. return topField.Uint() == field.Uint()
  512. case reflect.Float32, reflect.Float64:
  513. return topField.Float() == field.Float()
  514. case reflect.Slice, reflect.Map, reflect.Array:
  515. return int64(topField.Len()) == int64(field.Len())
  516. case reflect.Struct:
  517. fieldType := field.Type()
  518. // Not Same underlying type i.e. struct and time
  519. if fieldType != topField.Type() {
  520. return false
  521. }
  522. if fieldType == timeType {
  523. t := field.Interface().(time.Time)
  524. fieldTime := topField.Interface().(time.Time)
  525. return fieldTime.Equal(t)
  526. }
  527. }
  528. // default reflect.String:
  529. return topField.String() == field.String()
  530. }
  531. // IsEqField is the validation function for validating if the current field's value is equal to the field specified by the param's value.
  532. func isEqField(fl FieldLevel) bool {
  533. field := fl.Field()
  534. kind := field.Kind()
  535. currentField, currentKind, ok := fl.GetStructFieldOK()
  536. if !ok || currentKind != kind {
  537. return false
  538. }
  539. switch kind {
  540. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  541. return field.Int() == currentField.Int()
  542. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  543. return field.Uint() == currentField.Uint()
  544. case reflect.Float32, reflect.Float64:
  545. return field.Float() == currentField.Float()
  546. case reflect.Slice, reflect.Map, reflect.Array:
  547. return int64(field.Len()) == int64(currentField.Len())
  548. case reflect.Struct:
  549. fieldType := field.Type()
  550. // Not Same underlying type i.e. struct and time
  551. if fieldType != currentField.Type() {
  552. return false
  553. }
  554. if fieldType == timeType {
  555. t := currentField.Interface().(time.Time)
  556. fieldTime := field.Interface().(time.Time)
  557. return fieldTime.Equal(t)
  558. }
  559. }
  560. // default reflect.String:
  561. return field.String() == currentField.String()
  562. }
  563. // IsEq is the validation function for validating if the current field's value is equal to the param's value.
  564. func isEq(fl FieldLevel) bool {
  565. field := fl.Field()
  566. param := fl.Param()
  567. switch field.Kind() {
  568. case reflect.String:
  569. return field.String() == param
  570. case reflect.Slice, reflect.Map, reflect.Array:
  571. p := asInt(param)
  572. return int64(field.Len()) == p
  573. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  574. p := asInt(param)
  575. return field.Int() == p
  576. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  577. p := asUint(param)
  578. return field.Uint() == p
  579. case reflect.Float32, reflect.Float64:
  580. p := asFloat(param)
  581. return field.Float() == p
  582. }
  583. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  584. }
  585. // IsBase64 is the validation function for validating if the current field's value is a valid base 64.
  586. func isBase64(fl FieldLevel) bool {
  587. return base64Regex.MatchString(fl.Field().String())
  588. }
  589. // IsURI is the validation function for validating if the current field's value is a valid URI.
  590. func isURI(fl FieldLevel) bool {
  591. field := fl.Field()
  592. switch field.Kind() {
  593. case reflect.String:
  594. s := field.String()
  595. // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
  596. // emulate browser and strip the '#' suffix prior to validation. see issue-#237
  597. if i := strings.Index(s, "#"); i > -1 {
  598. s = s[:i]
  599. }
  600. if len(s) == 0 {
  601. return false
  602. }
  603. _, err := url.ParseRequestURI(s)
  604. return err == nil
  605. }
  606. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  607. }
  608. // IsURL is the validation function for validating if the current field's value is a valid URL.
  609. func isURL(fl FieldLevel) bool {
  610. field := fl.Field()
  611. switch field.Kind() {
  612. case reflect.String:
  613. var i int
  614. s := field.String()
  615. // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
  616. // emulate browser and strip the '#' suffix prior to validation. see issue-#237
  617. if i = strings.Index(s, "#"); i > -1 {
  618. s = s[:i]
  619. }
  620. if len(s) == 0 {
  621. return false
  622. }
  623. url, err := url.ParseRequestURI(s)
  624. if err != nil || url.Scheme == "" {
  625. return false
  626. }
  627. return err == nil
  628. }
  629. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  630. }
  631. // IsEmail is the validation function for validating if the current field's value is a valid email address.
  632. func isEmail(fl FieldLevel) bool {
  633. return emailRegex.MatchString(fl.Field().String())
  634. }
  635. // IsHSLA is the validation function for validating if the current field's value is a valid HSLA color.
  636. func isHSLA(fl FieldLevel) bool {
  637. return hslaRegex.MatchString(fl.Field().String())
  638. }
  639. // IsHSL is the validation function for validating if the current field's value is a valid HSL color.
  640. func isHSL(fl FieldLevel) bool {
  641. return hslRegex.MatchString(fl.Field().String())
  642. }
  643. // IsRGBA is the validation function for validating if the current field's value is a valid RGBA color.
  644. func isRGBA(fl FieldLevel) bool {
  645. return rgbaRegex.MatchString(fl.Field().String())
  646. }
  647. // IsRGB is the validation function for validating if the current field's value is a valid RGB color.
  648. func isRGB(fl FieldLevel) bool {
  649. return rgbRegex.MatchString(fl.Field().String())
  650. }
  651. // IsHEXColor is the validation function for validating if the current field's value is a valid HEX color.
  652. func isHEXColor(fl FieldLevel) bool {
  653. return hexcolorRegex.MatchString(fl.Field().String())
  654. }
  655. // IsHexadecimal is the validation function for validating if the current field's value is a valid hexadecimal.
  656. func isHexadecimal(fl FieldLevel) bool {
  657. return hexadecimalRegex.MatchString(fl.Field().String())
  658. }
  659. // IsNumber is the validation function for validating if the current field's value is a valid number.
  660. func isNumber(fl FieldLevel) bool {
  661. return numberRegex.MatchString(fl.Field().String())
  662. }
  663. // IsNumeric is the validation function for validating if the current field's value is a valid numeric value.
  664. func isNumeric(fl FieldLevel) bool {
  665. return numericRegex.MatchString(fl.Field().String())
  666. }
  667. // IsAlphanum is the validation function for validating if the current field's value is a valid alphanumeric value.
  668. func isAlphanum(fl FieldLevel) bool {
  669. return alphaNumericRegex.MatchString(fl.Field().String())
  670. }
  671. // IsAlpha is the validation function for validating if the current field's value is a valid alpha value.
  672. func isAlpha(fl FieldLevel) bool {
  673. return alphaRegex.MatchString(fl.Field().String())
  674. }
  675. // IsAlphanumUnicode is the validation function for validating if the current field's value is a valid alphanumeric unicode value.
  676. func isAlphanumUnicode(fl FieldLevel) bool {
  677. return alphaUnicodeNumericRegex.MatchString(fl.Field().String())
  678. }
  679. // IsAlphaUnicode is the validation function for validating if the current field's value is a valid alpha unicode value.
  680. func isAlphaUnicode(fl FieldLevel) bool {
  681. return alphaUnicodeRegex.MatchString(fl.Field().String())
  682. }
  683. // isDefault is the opposite of required aka hasValue
  684. func isDefault(fl FieldLevel) bool {
  685. return !hasValue(fl)
  686. }
  687. // HasValue is the validation function for validating if the current field's value is not the default static value.
  688. func hasValue(fl FieldLevel) bool {
  689. field := fl.Field()
  690. switch field.Kind() {
  691. case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
  692. return !field.IsNil()
  693. default:
  694. if fl.(*validate).fldIsPointer && field.Interface() != nil {
  695. return true
  696. }
  697. return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
  698. }
  699. }
  700. // 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.
  701. func isGteField(fl FieldLevel) bool {
  702. field := fl.Field()
  703. kind := field.Kind()
  704. currentField, currentKind, ok := fl.GetStructFieldOK()
  705. if !ok || currentKind != kind {
  706. return false
  707. }
  708. switch kind {
  709. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  710. return field.Int() >= currentField.Int()
  711. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  712. return field.Uint() >= currentField.Uint()
  713. case reflect.Float32, reflect.Float64:
  714. return field.Float() >= currentField.Float()
  715. case reflect.Struct:
  716. fieldType := field.Type()
  717. // Not Same underlying type i.e. struct and time
  718. if fieldType != currentField.Type() {
  719. return false
  720. }
  721. if fieldType == timeType {
  722. t := currentField.Interface().(time.Time)
  723. fieldTime := field.Interface().(time.Time)
  724. return fieldTime.After(t) || fieldTime.Equal(t)
  725. }
  726. }
  727. // default reflect.String
  728. return len(field.String()) >= len(currentField.String())
  729. }
  730. // IsGtField is the validation function for validating if the current field's value is greater than the field specified by the param's value.
  731. func isGtField(fl FieldLevel) bool {
  732. field := fl.Field()
  733. kind := field.Kind()
  734. currentField, currentKind, ok := fl.GetStructFieldOK()
  735. if !ok || currentKind != kind {
  736. return false
  737. }
  738. switch kind {
  739. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  740. return field.Int() > currentField.Int()
  741. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  742. return field.Uint() > currentField.Uint()
  743. case reflect.Float32, reflect.Float64:
  744. return field.Float() > currentField.Float()
  745. case reflect.Struct:
  746. fieldType := field.Type()
  747. // Not Same underlying type i.e. struct and time
  748. if fieldType != currentField.Type() {
  749. return false
  750. }
  751. if fieldType == timeType {
  752. t := currentField.Interface().(time.Time)
  753. fieldTime := field.Interface().(time.Time)
  754. return fieldTime.After(t)
  755. }
  756. }
  757. // default reflect.String
  758. return len(field.String()) > len(currentField.String())
  759. }
  760. // IsGte is the validation function for validating if the current field's value is greater than or equal to the param's value.
  761. func isGte(fl FieldLevel) bool {
  762. field := fl.Field()
  763. param := fl.Param()
  764. switch field.Kind() {
  765. case reflect.String:
  766. p := asInt(param)
  767. return int64(utf8.RuneCountInString(field.String())) >= p
  768. case reflect.Slice, reflect.Map, reflect.Array:
  769. p := asInt(param)
  770. return int64(field.Len()) >= p
  771. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  772. p := asInt(param)
  773. return field.Int() >= p
  774. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  775. p := asUint(param)
  776. return field.Uint() >= p
  777. case reflect.Float32, reflect.Float64:
  778. p := asFloat(param)
  779. return field.Float() >= p
  780. case reflect.Struct:
  781. if field.Type() == timeType {
  782. now := time.Now().UTC()
  783. t := field.Interface().(time.Time)
  784. return t.After(now) || t.Equal(now)
  785. }
  786. }
  787. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  788. }
  789. // IsGt is the validation function for validating if the current field's value is greater than the param's value.
  790. func isGt(fl FieldLevel) bool {
  791. field := fl.Field()
  792. param := fl.Param()
  793. switch field.Kind() {
  794. case reflect.String:
  795. p := asInt(param)
  796. return int64(utf8.RuneCountInString(field.String())) > p
  797. case reflect.Slice, reflect.Map, reflect.Array:
  798. p := asInt(param)
  799. return int64(field.Len()) > p
  800. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  801. p := asInt(param)
  802. return field.Int() > p
  803. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  804. p := asUint(param)
  805. return field.Uint() > p
  806. case reflect.Float32, reflect.Float64:
  807. p := asFloat(param)
  808. return field.Float() > p
  809. case reflect.Struct:
  810. if field.Type() == timeType {
  811. return field.Interface().(time.Time).After(time.Now().UTC())
  812. }
  813. }
  814. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  815. }
  816. // HasLengthOf is the validation function for validating if the current field's value is equal to the param's value.
  817. func hasLengthOf(fl FieldLevel) bool {
  818. field := fl.Field()
  819. param := fl.Param()
  820. switch field.Kind() {
  821. case reflect.String:
  822. p := asInt(param)
  823. return int64(utf8.RuneCountInString(field.String())) == p
  824. case reflect.Slice, reflect.Map, reflect.Array:
  825. p := asInt(param)
  826. return int64(field.Len()) == p
  827. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  828. p := asInt(param)
  829. return field.Int() == p
  830. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  831. p := asUint(param)
  832. return field.Uint() == p
  833. case reflect.Float32, reflect.Float64:
  834. p := asFloat(param)
  835. return field.Float() == p
  836. }
  837. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  838. }
  839. // HasMinOf is the validation function for validating if the current field's value is greater than or equal to the param's value.
  840. func hasMinOf(fl FieldLevel) bool {
  841. return isGte(fl)
  842. }
  843. // 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.
  844. func isLteField(fl FieldLevel) bool {
  845. field := fl.Field()
  846. kind := field.Kind()
  847. currentField, currentKind, ok := fl.GetStructFieldOK()
  848. if !ok || currentKind != kind {
  849. return false
  850. }
  851. switch kind {
  852. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  853. return field.Int() <= currentField.Int()
  854. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  855. return field.Uint() <= currentField.Uint()
  856. case reflect.Float32, reflect.Float64:
  857. return field.Float() <= currentField.Float()
  858. case reflect.Struct:
  859. fieldType := field.Type()
  860. // Not Same underlying type i.e. struct and time
  861. if fieldType != currentField.Type() {
  862. return false
  863. }
  864. if fieldType == timeType {
  865. t := currentField.Interface().(time.Time)
  866. fieldTime := field.Interface().(time.Time)
  867. return fieldTime.Before(t) || fieldTime.Equal(t)
  868. }
  869. }
  870. // default reflect.String
  871. return len(field.String()) <= len(currentField.String())
  872. }
  873. // IsLtField is the validation function for validating if the current field's value is less than the field specified by the param's value.
  874. func isLtField(fl FieldLevel) bool {
  875. field := fl.Field()
  876. kind := field.Kind()
  877. currentField, currentKind, ok := fl.GetStructFieldOK()
  878. if !ok || currentKind != kind {
  879. return false
  880. }
  881. switch kind {
  882. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  883. return field.Int() < currentField.Int()
  884. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  885. return field.Uint() < currentField.Uint()
  886. case reflect.Float32, reflect.Float64:
  887. return field.Float() < currentField.Float()
  888. case reflect.Struct:
  889. fieldType := field.Type()
  890. // Not Same underlying type i.e. struct and time
  891. if fieldType != currentField.Type() {
  892. return false
  893. }
  894. if fieldType == timeType {
  895. t := currentField.Interface().(time.Time)
  896. fieldTime := field.Interface().(time.Time)
  897. return fieldTime.Before(t)
  898. }
  899. }
  900. // default reflect.String
  901. return len(field.String()) < len(currentField.String())
  902. }
  903. // IsLte is the validation function for validating if the current field's value is less than or equal to the param's value.
  904. func isLte(fl FieldLevel) bool {
  905. field := fl.Field()
  906. param := fl.Param()
  907. switch field.Kind() {
  908. case reflect.String:
  909. p := asInt(param)
  910. return int64(utf8.RuneCountInString(field.String())) <= p
  911. case reflect.Slice, reflect.Map, reflect.Array:
  912. p := asInt(param)
  913. return int64(field.Len()) <= p
  914. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  915. p := asInt(param)
  916. return field.Int() <= p
  917. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  918. p := asUint(param)
  919. return field.Uint() <= p
  920. case reflect.Float32, reflect.Float64:
  921. p := asFloat(param)
  922. return field.Float() <= p
  923. case reflect.Struct:
  924. if field.Type() == timeType {
  925. now := time.Now().UTC()
  926. t := field.Interface().(time.Time)
  927. return t.Before(now) || t.Equal(now)
  928. }
  929. }
  930. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  931. }
  932. // IsLt is the validation function for validating if the current field's value is less than the param's value.
  933. func isLt(fl FieldLevel) bool {
  934. field := fl.Field()
  935. param := fl.Param()
  936. switch field.Kind() {
  937. case reflect.String:
  938. p := asInt(param)
  939. return int64(utf8.RuneCountInString(field.String())) < p
  940. case reflect.Slice, reflect.Map, reflect.Array:
  941. p := asInt(param)
  942. return int64(field.Len()) < p
  943. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  944. p := asInt(param)
  945. return field.Int() < p
  946. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  947. p := asUint(param)
  948. return field.Uint() < p
  949. case reflect.Float32, reflect.Float64:
  950. p := asFloat(param)
  951. return field.Float() < p
  952. case reflect.Struct:
  953. if field.Type() == timeType {
  954. return field.Interface().(time.Time).Before(time.Now().UTC())
  955. }
  956. }
  957. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  958. }
  959. // HasMaxOf is the validation function for validating if the current field's value is less than or equal to the param's value.
  960. func hasMaxOf(fl FieldLevel) bool {
  961. return isLte(fl)
  962. }
  963. // IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
  964. func isTCP4AddrResolvable(fl FieldLevel) bool {
  965. if !isIP4Addr(fl) {
  966. return false
  967. }
  968. _, err := net.ResolveTCPAddr("tcp4", fl.Field().String())
  969. return err == nil
  970. }
  971. // IsTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
  972. func isTCP6AddrResolvable(fl FieldLevel) bool {
  973. if !isIP6Addr(fl) {
  974. return false
  975. }
  976. _, err := net.ResolveTCPAddr("tcp6", fl.Field().String())
  977. return err == nil
  978. }
  979. // IsTCPAddrResolvable is the validation function for validating if the field's value is a resolvable tcp address.
  980. func isTCPAddrResolvable(fl FieldLevel) bool {
  981. if !isIP4Addr(fl) && !isIP6Addr(fl) {
  982. return false
  983. }
  984. _, err := net.ResolveTCPAddr("tcp", fl.Field().String())
  985. return err == nil
  986. }
  987. // IsUDP4AddrResolvable is the validation function for validating if the field's value is a resolvable udp4 address.
  988. func isUDP4AddrResolvable(fl FieldLevel) bool {
  989. if !isIP4Addr(fl) {
  990. return false
  991. }
  992. _, err := net.ResolveUDPAddr("udp4", fl.Field().String())
  993. return err == nil
  994. }
  995. // IsUDP6AddrResolvable is the validation function for validating if the field's value is a resolvable udp6 address.
  996. func isUDP6AddrResolvable(fl FieldLevel) bool {
  997. if !isIP6Addr(fl) {
  998. return false
  999. }
  1000. _, err := net.ResolveUDPAddr("udp6", fl.Field().String())
  1001. return err == nil
  1002. }
  1003. // IsUDPAddrResolvable is the validation function for validating if the field's value is a resolvable udp address.
  1004. func isUDPAddrResolvable(fl FieldLevel) bool {
  1005. if !isIP4Addr(fl) && !isIP6Addr(fl) {
  1006. return false
  1007. }
  1008. _, err := net.ResolveUDPAddr("udp", fl.Field().String())
  1009. return err == nil
  1010. }
  1011. // IsIP4AddrResolvable is the validation function for validating if the field's value is a resolvable ip4 address.
  1012. func isIP4AddrResolvable(fl FieldLevel) bool {
  1013. if !isIPv4(fl) {
  1014. return false
  1015. }
  1016. _, err := net.ResolveIPAddr("ip4", fl.Field().String())
  1017. return err == nil
  1018. }
  1019. // IsIP6AddrResolvable is the validation function for validating if the field's value is a resolvable ip6 address.
  1020. func isIP6AddrResolvable(fl FieldLevel) bool {
  1021. if !isIPv6(fl) {
  1022. return false
  1023. }
  1024. _, err := net.ResolveIPAddr("ip6", fl.Field().String())
  1025. return err == nil
  1026. }
  1027. // IsIPAddrResolvable is the validation function for validating if the field's value is a resolvable ip address.
  1028. func isIPAddrResolvable(fl FieldLevel) bool {
  1029. if !isIP(fl) {
  1030. return false
  1031. }
  1032. _, err := net.ResolveIPAddr("ip", fl.Field().String())
  1033. return err == nil
  1034. }
  1035. // IsUnixAddrResolvable is the validation function for validating if the field's value is a resolvable unix address.
  1036. func isUnixAddrResolvable(fl FieldLevel) bool {
  1037. _, err := net.ResolveUnixAddr("unix", fl.Field().String())
  1038. return err == nil
  1039. }
  1040. func isIP4Addr(fl FieldLevel) bool {
  1041. val := fl.Field().String()
  1042. if idx := strings.LastIndex(val, ":"); idx != -1 {
  1043. val = val[0:idx]
  1044. }
  1045. ip := net.ParseIP(val)
  1046. return ip != nil && ip.To4() != nil
  1047. }
  1048. func isIP6Addr(fl FieldLevel) bool {
  1049. val := fl.Field().String()
  1050. if idx := strings.LastIndex(val, ":"); idx != -1 {
  1051. if idx != 0 && val[idx-1:idx] == "]" {
  1052. val = val[1 : idx-1]
  1053. }
  1054. }
  1055. ip := net.ParseIP(val)
  1056. return ip != nil && ip.To4() == nil
  1057. }
  1058. func isHostname(fl FieldLevel) bool {
  1059. return hostnameRegex.MatchString(fl.Field().String())
  1060. }
  1061. func isFQDN(fl FieldLevel) bool {
  1062. val := fl.Field().String()
  1063. if val == "" {
  1064. return false
  1065. }
  1066. if val[len(val)-1] == '.' {
  1067. val = val[0 : len(val)-1]
  1068. }
  1069. return (strings.IndexAny(val, ".") > -1) &&
  1070. hostnameRegex.MatchString(val)
  1071. }