baked_in.go 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081
  1. package validator
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "io/fs"
  10. "net"
  11. "net/mail"
  12. "net/url"
  13. "os"
  14. "reflect"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "syscall"
  19. "time"
  20. "unicode/utf8"
  21. "golang.org/x/crypto/sha3"
  22. "golang.org/x/text/language"
  23. "github.com/gabriel-vasile/mimetype"
  24. urn "github.com/leodido/go-urn"
  25. )
  26. // Func accepts a FieldLevel interface for all validation needs. The return
  27. // value should be true when validation succeeds.
  28. type Func func(fl FieldLevel) bool
  29. // FuncCtx accepts a context.Context and FieldLevel interface for all
  30. // validation needs. The return value should be true when validation succeeds.
  31. type FuncCtx func(ctx context.Context, fl FieldLevel) bool
  32. // wrapFunc wraps normal Func makes it compatible with FuncCtx
  33. func wrapFunc(fn Func) FuncCtx {
  34. if fn == nil {
  35. return nil // be sure not to wrap a bad function.
  36. }
  37. return func(ctx context.Context, fl FieldLevel) bool {
  38. return fn(fl)
  39. }
  40. }
  41. var (
  42. restrictedTags = map[string]struct{}{
  43. diveTag: {},
  44. keysTag: {},
  45. endKeysTag: {},
  46. structOnlyTag: {},
  47. omitzero: {},
  48. omitempty: {},
  49. omitnil: {},
  50. skipValidationTag: {},
  51. utf8HexComma: {},
  52. utf8Pipe: {},
  53. noStructLevelTag: {},
  54. requiredTag: {},
  55. isdefault: {},
  56. }
  57. // bakedInAliases is a default mapping of a single validation tag that
  58. // defines a common or complex set of validation(s) to simplify
  59. // adding validation to structs.
  60. bakedInAliases = map[string]string{
  61. "iscolor": "hexcolor|rgb|rgba|hsl|hsla",
  62. "country_code": "iso3166_1_alpha2|iso3166_1_alpha3|iso3166_1_alpha_numeric",
  63. "eu_country_code": "iso3166_1_alpha2_eu|iso3166_1_alpha3_eu|iso3166_1_alpha_numeric_eu",
  64. }
  65. // bakedInValidators is the default map of ValidationFunc
  66. // you can add, remove or even replace items to suite your needs,
  67. // or even disregard and use your own map if so desired.
  68. bakedInValidators = map[string]Func{
  69. "required": hasValue,
  70. "required_if": requiredIf,
  71. "required_unless": requiredUnless,
  72. "skip_unless": skipUnless,
  73. "required_with": requiredWith,
  74. "required_with_all": requiredWithAll,
  75. "required_without": requiredWithout,
  76. "required_without_all": requiredWithoutAll,
  77. "excluded_if": excludedIf,
  78. "excluded_unless": excludedUnless,
  79. "excluded_with": excludedWith,
  80. "excluded_with_all": excludedWithAll,
  81. "excluded_without": excludedWithout,
  82. "excluded_without_all": excludedWithoutAll,
  83. "isdefault": isDefault,
  84. "len": hasLengthOf,
  85. "min": hasMinOf,
  86. "max": hasMaxOf,
  87. "eq": isEq,
  88. "eq_ignore_case": isEqIgnoreCase,
  89. "ne": isNe,
  90. "ne_ignore_case": isNeIgnoreCase,
  91. "lt": isLt,
  92. "lte": isLte,
  93. "gt": isGt,
  94. "gte": isGte,
  95. "eqfield": isEqField,
  96. "eqcsfield": isEqCrossStructField,
  97. "necsfield": isNeCrossStructField,
  98. "gtcsfield": isGtCrossStructField,
  99. "gtecsfield": isGteCrossStructField,
  100. "ltcsfield": isLtCrossStructField,
  101. "ltecsfield": isLteCrossStructField,
  102. "nefield": isNeField,
  103. "gtefield": isGteField,
  104. "gtfield": isGtField,
  105. "ltefield": isLteField,
  106. "ltfield": isLtField,
  107. "fieldcontains": fieldContains,
  108. "fieldexcludes": fieldExcludes,
  109. "alpha": isAlpha,
  110. "alphanum": isAlphanum,
  111. "alphaunicode": isAlphaUnicode,
  112. "alphanumunicode": isAlphanumUnicode,
  113. "boolean": isBoolean,
  114. "numeric": isNumeric,
  115. "number": isNumber,
  116. "hexadecimal": isHexadecimal,
  117. "hexcolor": isHEXColor,
  118. "rgb": isRGB,
  119. "rgba": isRGBA,
  120. "hsl": isHSL,
  121. "hsla": isHSLA,
  122. "e164": isE164,
  123. "email": isEmail,
  124. "url": isURL,
  125. "http_url": isHttpURL,
  126. "uri": isURI,
  127. "urn_rfc2141": isUrnRFC2141, // RFC 2141
  128. "file": isFile,
  129. "filepath": isFilePath,
  130. "base32": isBase32,
  131. "base64": isBase64,
  132. "base64url": isBase64URL,
  133. "base64rawurl": isBase64RawURL,
  134. "contains": contains,
  135. "containsany": containsAny,
  136. "containsrune": containsRune,
  137. "excludes": excludes,
  138. "excludesall": excludesAll,
  139. "excludesrune": excludesRune,
  140. "startswith": startsWith,
  141. "endswith": endsWith,
  142. "startsnotwith": startsNotWith,
  143. "endsnotwith": endsNotWith,
  144. "image": isImage,
  145. "isbn": isISBN,
  146. "isbn10": isISBN10,
  147. "isbn13": isISBN13,
  148. "issn": isISSN,
  149. "eth_addr": isEthereumAddress,
  150. "eth_addr_checksum": isEthereumAddressChecksum,
  151. "btc_addr": isBitcoinAddress,
  152. "btc_addr_bech32": isBitcoinBech32Address,
  153. "uuid": isUUID,
  154. "uuid3": isUUID3,
  155. "uuid4": isUUID4,
  156. "uuid5": isUUID5,
  157. "uuid_rfc4122": isUUIDRFC4122,
  158. "uuid3_rfc4122": isUUID3RFC4122,
  159. "uuid4_rfc4122": isUUID4RFC4122,
  160. "uuid5_rfc4122": isUUID5RFC4122,
  161. "ulid": isULID,
  162. "md4": isMD4,
  163. "md5": isMD5,
  164. "sha256": isSHA256,
  165. "sha384": isSHA384,
  166. "sha512": isSHA512,
  167. "ripemd128": isRIPEMD128,
  168. "ripemd160": isRIPEMD160,
  169. "tiger128": isTIGER128,
  170. "tiger160": isTIGER160,
  171. "tiger192": isTIGER192,
  172. "ascii": isASCII,
  173. "printascii": isPrintableASCII,
  174. "multibyte": hasMultiByteCharacter,
  175. "datauri": isDataURI,
  176. "latitude": isLatitude,
  177. "longitude": isLongitude,
  178. "ssn": isSSN,
  179. "ipv4": isIPv4,
  180. "ipv6": isIPv6,
  181. "ip": isIP,
  182. "cidrv4": isCIDRv4,
  183. "cidrv6": isCIDRv6,
  184. "cidr": isCIDR,
  185. "tcp4_addr": isTCP4AddrResolvable,
  186. "tcp6_addr": isTCP6AddrResolvable,
  187. "tcp_addr": isTCPAddrResolvable,
  188. "udp4_addr": isUDP4AddrResolvable,
  189. "udp6_addr": isUDP6AddrResolvable,
  190. "udp_addr": isUDPAddrResolvable,
  191. "ip4_addr": isIP4AddrResolvable,
  192. "ip6_addr": isIP6AddrResolvable,
  193. "ip_addr": isIPAddrResolvable,
  194. "unix_addr": isUnixAddrResolvable,
  195. "mac": isMAC,
  196. "hostname": isHostnameRFC952, // RFC 952
  197. "hostname_rfc1123": isHostnameRFC1123, // RFC 1123
  198. "fqdn": isFQDN,
  199. "unique": isUnique,
  200. "oneof": isOneOf,
  201. "oneofci": isOneOfCI,
  202. "html": isHTML,
  203. "html_encoded": isHTMLEncoded,
  204. "url_encoded": isURLEncoded,
  205. "dir": isDir,
  206. "dirpath": isDirPath,
  207. "json": isJSON,
  208. "jwt": isJWT,
  209. "hostname_port": isHostnamePort,
  210. "port": isPort,
  211. "lowercase": isLowercase,
  212. "uppercase": isUppercase,
  213. "datetime": isDatetime,
  214. "timezone": isTimeZone,
  215. "iso3166_1_alpha2": isIso3166Alpha2,
  216. "iso3166_1_alpha2_eu": isIso3166Alpha2EU,
  217. "iso3166_1_alpha3": isIso3166Alpha3,
  218. "iso3166_1_alpha3_eu": isIso3166Alpha3EU,
  219. "iso3166_1_alpha_numeric": isIso3166AlphaNumeric,
  220. "iso3166_1_alpha_numeric_eu": isIso3166AlphaNumericEU,
  221. "iso3166_2": isIso31662,
  222. "iso4217": isIso4217,
  223. "iso4217_numeric": isIso4217Numeric,
  224. "bcp47_language_tag": isBCP47LanguageTag,
  225. "postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
  226. "postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
  227. "bic": isIsoBicFormat,
  228. "semver": isSemverFormat,
  229. "dns_rfc1035_label": isDnsRFC1035LabelFormat,
  230. "credit_card": isCreditCard,
  231. "cve": isCveFormat,
  232. "luhn_checksum": hasLuhnChecksum,
  233. "mongodb": isMongoDBObjectId,
  234. "mongodb_connection_string": isMongoDBConnectionString,
  235. "cron": isCron,
  236. "spicedb": isSpiceDB,
  237. "ein": isEIN,
  238. }
  239. )
  240. var (
  241. oneofValsCache = map[string][]string{}
  242. oneofValsCacheRWLock = sync.RWMutex{}
  243. )
  244. func parseOneOfParam2(s string) []string {
  245. oneofValsCacheRWLock.RLock()
  246. vals, ok := oneofValsCache[s]
  247. oneofValsCacheRWLock.RUnlock()
  248. if !ok {
  249. oneofValsCacheRWLock.Lock()
  250. vals = splitParamsRegex().FindAllString(s, -1)
  251. for i := 0; i < len(vals); i++ {
  252. vals[i] = strings.ReplaceAll(vals[i], "'", "")
  253. }
  254. oneofValsCache[s] = vals
  255. oneofValsCacheRWLock.Unlock()
  256. }
  257. return vals
  258. }
  259. func isURLEncoded(fl FieldLevel) bool {
  260. return uRLEncodedRegex().MatchString(fl.Field().String())
  261. }
  262. func isHTMLEncoded(fl FieldLevel) bool {
  263. return hTMLEncodedRegex().MatchString(fl.Field().String())
  264. }
  265. func isHTML(fl FieldLevel) bool {
  266. return hTMLRegex().MatchString(fl.Field().String())
  267. }
  268. func isOneOf(fl FieldLevel) bool {
  269. vals := parseOneOfParam2(fl.Param())
  270. field := fl.Field()
  271. var v string
  272. switch field.Kind() {
  273. case reflect.String:
  274. v = field.String()
  275. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  276. v = strconv.FormatInt(field.Int(), 10)
  277. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  278. v = strconv.FormatUint(field.Uint(), 10)
  279. default:
  280. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  281. }
  282. for i := 0; i < len(vals); i++ {
  283. if vals[i] == v {
  284. return true
  285. }
  286. }
  287. return false
  288. }
  289. // isOneOfCI is the validation function for validating if the current field's value is one of the provided string values (case insensitive).
  290. func isOneOfCI(fl FieldLevel) bool {
  291. vals := parseOneOfParam2(fl.Param())
  292. field := fl.Field()
  293. if field.Kind() != reflect.String {
  294. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  295. }
  296. v := field.String()
  297. for _, val := range vals {
  298. if strings.EqualFold(val, v) {
  299. return true
  300. }
  301. }
  302. return false
  303. }
  304. // isUnique is the validation function for validating if each array|slice|map value is unique
  305. func isUnique(fl FieldLevel) bool {
  306. field := fl.Field()
  307. param := fl.Param()
  308. v := reflect.ValueOf(struct{}{})
  309. switch field.Kind() {
  310. case reflect.Slice, reflect.Array:
  311. elem := field.Type().Elem()
  312. if elem.Kind() == reflect.Ptr {
  313. elem = elem.Elem()
  314. }
  315. if param == "" {
  316. m := reflect.MakeMap(reflect.MapOf(elem, v.Type()))
  317. for i := 0; i < field.Len(); i++ {
  318. m.SetMapIndex(reflect.Indirect(field.Index(i)), v)
  319. }
  320. return field.Len() == m.Len()
  321. }
  322. sf, ok := elem.FieldByName(param)
  323. if !ok {
  324. panic(fmt.Sprintf("Bad field name %s", param))
  325. }
  326. sfTyp := sf.Type
  327. if sfTyp.Kind() == reflect.Ptr {
  328. sfTyp = sfTyp.Elem()
  329. }
  330. m := reflect.MakeMap(reflect.MapOf(sfTyp, v.Type()))
  331. var fieldlen int
  332. for i := 0; i < field.Len(); i++ {
  333. key := reflect.Indirect(reflect.Indirect(field.Index(i)).FieldByName(param))
  334. if key.IsValid() {
  335. fieldlen++
  336. m.SetMapIndex(key, v)
  337. }
  338. }
  339. return fieldlen == m.Len()
  340. case reflect.Map:
  341. var m reflect.Value
  342. if field.Type().Elem().Kind() == reflect.Ptr {
  343. m = reflect.MakeMap(reflect.MapOf(field.Type().Elem().Elem(), v.Type()))
  344. } else {
  345. m = reflect.MakeMap(reflect.MapOf(field.Type().Elem(), v.Type()))
  346. }
  347. for _, k := range field.MapKeys() {
  348. m.SetMapIndex(reflect.Indirect(field.MapIndex(k)), v)
  349. }
  350. return field.Len() == m.Len()
  351. default:
  352. if parent := fl.Parent(); parent.Kind() == reflect.Struct {
  353. uniqueField := parent.FieldByName(param)
  354. if uniqueField == reflect.ValueOf(nil) {
  355. panic(fmt.Sprintf("Bad field name provided %s", param))
  356. }
  357. if uniqueField.Kind() != field.Kind() {
  358. panic(fmt.Sprintf("Bad field type %T:%T", field.Interface(), uniqueField.Interface()))
  359. }
  360. return field.Interface() != uniqueField.Interface()
  361. }
  362. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  363. }
  364. }
  365. // isMAC is the validation function for validating if the field's value is a valid MAC address.
  366. func isMAC(fl FieldLevel) bool {
  367. _, err := net.ParseMAC(fl.Field().String())
  368. return err == nil
  369. }
  370. // isCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
  371. func isCIDRv4(fl FieldLevel) bool {
  372. ip, net, err := net.ParseCIDR(fl.Field().String())
  373. return err == nil && ip.To4() != nil && net.IP.Equal(ip)
  374. }
  375. // isCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.
  376. func isCIDRv6(fl FieldLevel) bool {
  377. ip, _, err := net.ParseCIDR(fl.Field().String())
  378. return err == nil && ip.To4() == nil
  379. }
  380. // isCIDR is the validation function for validating if the field's value is a valid v4 or v6 CIDR address.
  381. func isCIDR(fl FieldLevel) bool {
  382. _, _, err := net.ParseCIDR(fl.Field().String())
  383. return err == nil
  384. }
  385. // isIPv4 is the validation function for validating if a value is a valid v4 IP address.
  386. func isIPv4(fl FieldLevel) bool {
  387. ip := net.ParseIP(fl.Field().String())
  388. return ip != nil && ip.To4() != nil
  389. }
  390. // isIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
  391. func isIPv6(fl FieldLevel) bool {
  392. ip := net.ParseIP(fl.Field().String())
  393. return ip != nil && ip.To4() == nil
  394. }
  395. // isIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.
  396. func isIP(fl FieldLevel) bool {
  397. ip := net.ParseIP(fl.Field().String())
  398. return ip != nil
  399. }
  400. // isSSN is the validation function for validating if the field's value is a valid SSN.
  401. func isSSN(fl FieldLevel) bool {
  402. field := fl.Field()
  403. if field.Len() != 11 {
  404. return false
  405. }
  406. return sSNRegex().MatchString(field.String())
  407. }
  408. // isLongitude is the validation function for validating if the field's value is a valid longitude coordinate.
  409. func isLongitude(fl FieldLevel) bool {
  410. field := fl.Field()
  411. var v string
  412. switch field.Kind() {
  413. case reflect.String:
  414. v = field.String()
  415. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  416. v = strconv.FormatInt(field.Int(), 10)
  417. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  418. v = strconv.FormatUint(field.Uint(), 10)
  419. case reflect.Float32:
  420. v = strconv.FormatFloat(field.Float(), 'f', -1, 32)
  421. case reflect.Float64:
  422. v = strconv.FormatFloat(field.Float(), 'f', -1, 64)
  423. default:
  424. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  425. }
  426. return longitudeRegex().MatchString(v)
  427. }
  428. // isLatitude is the validation function for validating if the field's value is a valid latitude coordinate.
  429. func isLatitude(fl FieldLevel) bool {
  430. field := fl.Field()
  431. var v string
  432. switch field.Kind() {
  433. case reflect.String:
  434. v = field.String()
  435. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  436. v = strconv.FormatInt(field.Int(), 10)
  437. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  438. v = strconv.FormatUint(field.Uint(), 10)
  439. case reflect.Float32:
  440. v = strconv.FormatFloat(field.Float(), 'f', -1, 32)
  441. case reflect.Float64:
  442. v = strconv.FormatFloat(field.Float(), 'f', -1, 64)
  443. default:
  444. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  445. }
  446. return latitudeRegex().MatchString(v)
  447. }
  448. // isDataURI is the validation function for validating if the field's value is a valid data URI.
  449. func isDataURI(fl FieldLevel) bool {
  450. uri := strings.SplitN(fl.Field().String(), ",", 2)
  451. if len(uri) != 2 {
  452. return false
  453. }
  454. if !dataURIRegex().MatchString(uri[0]) {
  455. return false
  456. }
  457. return base64Regex().MatchString(uri[1])
  458. }
  459. // hasMultiByteCharacter is the validation function for validating if the field's value has a multi byte character.
  460. func hasMultiByteCharacter(fl FieldLevel) bool {
  461. field := fl.Field()
  462. if field.Len() == 0 {
  463. return true
  464. }
  465. return multibyteRegex().MatchString(field.String())
  466. }
  467. // isPrintableASCII is the validation function for validating if the field's value is a valid printable ASCII character.
  468. func isPrintableASCII(fl FieldLevel) bool {
  469. return printableASCIIRegex().MatchString(fl.Field().String())
  470. }
  471. // isASCII is the validation function for validating if the field's value is a valid ASCII character.
  472. func isASCII(fl FieldLevel) bool {
  473. return aSCIIRegex().MatchString(fl.Field().String())
  474. }
  475. // isUUID5 is the validation function for validating if the field's value is a valid v5 UUID.
  476. func isUUID5(fl FieldLevel) bool {
  477. return fieldMatchesRegexByStringerValOrString(uUID5Regex, fl)
  478. }
  479. // isUUID4 is the validation function for validating if the field's value is a valid v4 UUID.
  480. func isUUID4(fl FieldLevel) bool {
  481. return fieldMatchesRegexByStringerValOrString(uUID4Regex, fl)
  482. }
  483. // isUUID3 is the validation function for validating if the field's value is a valid v3 UUID.
  484. func isUUID3(fl FieldLevel) bool {
  485. return fieldMatchesRegexByStringerValOrString(uUID3Regex, fl)
  486. }
  487. // isUUID is the validation function for validating if the field's value is a valid UUID of any version.
  488. func isUUID(fl FieldLevel) bool {
  489. return fieldMatchesRegexByStringerValOrString(uUIDRegex, fl)
  490. }
  491. // isUUID5RFC4122 is the validation function for validating if the field's value is a valid RFC4122 v5 UUID.
  492. func isUUID5RFC4122(fl FieldLevel) bool {
  493. return fieldMatchesRegexByStringerValOrString(uUID5RFC4122Regex, fl)
  494. }
  495. // isUUID4RFC4122 is the validation function for validating if the field's value is a valid RFC4122 v4 UUID.
  496. func isUUID4RFC4122(fl FieldLevel) bool {
  497. return fieldMatchesRegexByStringerValOrString(uUID4RFC4122Regex, fl)
  498. }
  499. // isUUID3RFC4122 is the validation function for validating if the field's value is a valid RFC4122 v3 UUID.
  500. func isUUID3RFC4122(fl FieldLevel) bool {
  501. return fieldMatchesRegexByStringerValOrString(uUID3RFC4122Regex, fl)
  502. }
  503. // isUUIDRFC4122 is the validation function for validating if the field's value is a valid RFC4122 UUID of any version.
  504. func isUUIDRFC4122(fl FieldLevel) bool {
  505. return fieldMatchesRegexByStringerValOrString(uUIDRFC4122Regex, fl)
  506. }
  507. // isULID is the validation function for validating if the field's value is a valid ULID.
  508. func isULID(fl FieldLevel) bool {
  509. return fieldMatchesRegexByStringerValOrString(uLIDRegex, fl)
  510. }
  511. // isMD4 is the validation function for validating if the field's value is a valid MD4.
  512. func isMD4(fl FieldLevel) bool {
  513. return md4Regex().MatchString(fl.Field().String())
  514. }
  515. // isMD5 is the validation function for validating if the field's value is a valid MD5.
  516. func isMD5(fl FieldLevel) bool {
  517. return md5Regex().MatchString(fl.Field().String())
  518. }
  519. // isSHA256 is the validation function for validating if the field's value is a valid SHA256.
  520. func isSHA256(fl FieldLevel) bool {
  521. return sha256Regex().MatchString(fl.Field().String())
  522. }
  523. // isSHA384 is the validation function for validating if the field's value is a valid SHA384.
  524. func isSHA384(fl FieldLevel) bool {
  525. return sha384Regex().MatchString(fl.Field().String())
  526. }
  527. // isSHA512 is the validation function for validating if the field's value is a valid SHA512.
  528. func isSHA512(fl FieldLevel) bool {
  529. return sha512Regex().MatchString(fl.Field().String())
  530. }
  531. // isRIPEMD128 is the validation function for validating if the field's value is a valid PIPEMD128.
  532. func isRIPEMD128(fl FieldLevel) bool {
  533. return ripemd128Regex().MatchString(fl.Field().String())
  534. }
  535. // isRIPEMD160 is the validation function for validating if the field's value is a valid PIPEMD160.
  536. func isRIPEMD160(fl FieldLevel) bool {
  537. return ripemd160Regex().MatchString(fl.Field().String())
  538. }
  539. // isTIGER128 is the validation function for validating if the field's value is a valid TIGER128.
  540. func isTIGER128(fl FieldLevel) bool {
  541. return tiger128Regex().MatchString(fl.Field().String())
  542. }
  543. // isTIGER160 is the validation function for validating if the field's value is a valid TIGER160.
  544. func isTIGER160(fl FieldLevel) bool {
  545. return tiger160Regex().MatchString(fl.Field().String())
  546. }
  547. // isTIGER192 is the validation function for validating if the field's value is a valid isTIGER192.
  548. func isTIGER192(fl FieldLevel) bool {
  549. return tiger192Regex().MatchString(fl.Field().String())
  550. }
  551. // isISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
  552. func isISBN(fl FieldLevel) bool {
  553. return isISBN10(fl) || isISBN13(fl)
  554. }
  555. // isISBN13 is the validation function for validating if the field's value is a valid v13 ISBN.
  556. func isISBN13(fl FieldLevel) bool {
  557. s := strings.Replace(strings.Replace(fl.Field().String(), "-", "", 4), " ", "", 4)
  558. if !iSBN13Regex().MatchString(s) {
  559. return false
  560. }
  561. var checksum int32
  562. var i int32
  563. factor := []int32{1, 3}
  564. for i = 0; i < 12; i++ {
  565. checksum += factor[i%2] * int32(s[i]-'0')
  566. }
  567. return (int32(s[12]-'0'))-((10-(checksum%10))%10) == 0
  568. }
  569. // isISBN10 is the validation function for validating if the field's value is a valid v10 ISBN.
  570. func isISBN10(fl FieldLevel) bool {
  571. s := strings.Replace(strings.Replace(fl.Field().String(), "-", "", 3), " ", "", 3)
  572. if !iSBN10Regex().MatchString(s) {
  573. return false
  574. }
  575. var checksum int32
  576. var i int32
  577. for i = 0; i < 9; i++ {
  578. checksum += (i + 1) * int32(s[i]-'0')
  579. }
  580. if s[9] == 'X' {
  581. checksum += 10 * 10
  582. } else {
  583. checksum += 10 * int32(s[9]-'0')
  584. }
  585. return checksum%11 == 0
  586. }
  587. // isISSN is the validation function for validating if the field's value is a valid ISSN.
  588. func isISSN(fl FieldLevel) bool {
  589. s := fl.Field().String()
  590. if !iSSNRegex().MatchString(s) {
  591. return false
  592. }
  593. s = strings.ReplaceAll(s, "-", "")
  594. pos := 8
  595. checksum := 0
  596. for i := 0; i < 7; i++ {
  597. checksum += pos * int(s[i]-'0')
  598. pos--
  599. }
  600. if s[7] == 'X' {
  601. checksum += 10
  602. } else {
  603. checksum += int(s[7] - '0')
  604. }
  605. return checksum%11 == 0
  606. }
  607. // isEthereumAddress is the validation function for validating if the field's value is a valid Ethereum address.
  608. func isEthereumAddress(fl FieldLevel) bool {
  609. address := fl.Field().String()
  610. return ethAddressRegex().MatchString(address)
  611. }
  612. // isEthereumAddressChecksum is the validation function for validating if the field's value is a valid checksummed Ethereum address.
  613. func isEthereumAddressChecksum(fl FieldLevel) bool {
  614. address := fl.Field().String()
  615. if !ethAddressRegex().MatchString(address) {
  616. return false
  617. }
  618. // Checksum validation. Reference: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
  619. address = address[2:] // Skip "0x" prefix.
  620. h := sha3.NewLegacyKeccak256()
  621. // hash.Hash's io.Writer implementation says it never returns an error. https://golang.org/pkg/hash/#Hash
  622. _, _ = h.Write([]byte(strings.ToLower(address)))
  623. hash := hex.EncodeToString(h.Sum(nil))
  624. for i := 0; i < len(address); i++ {
  625. if address[i] <= '9' { // Skip 0-9 digits: they don't have upper/lower-case.
  626. continue
  627. }
  628. if hash[i] > '7' && address[i] >= 'a' || hash[i] <= '7' && address[i] <= 'F' {
  629. return false
  630. }
  631. }
  632. return true
  633. }
  634. // isBitcoinAddress is the validation function for validating if the field's value is a valid btc address
  635. func isBitcoinAddress(fl FieldLevel) bool {
  636. address := fl.Field().String()
  637. if !btcAddressRegex().MatchString(address) {
  638. return false
  639. }
  640. alphabet := []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
  641. decode := [25]byte{}
  642. for _, n := range []byte(address) {
  643. d := bytes.IndexByte(alphabet, n)
  644. for i := 24; i >= 0; i-- {
  645. d += 58 * int(decode[i])
  646. decode[i] = byte(d % 256)
  647. d /= 256
  648. }
  649. }
  650. h := sha256.New()
  651. _, _ = h.Write(decode[:21])
  652. d := h.Sum([]byte{})
  653. h = sha256.New()
  654. _, _ = h.Write(d)
  655. validchecksum := [4]byte{}
  656. computedchecksum := [4]byte{}
  657. copy(computedchecksum[:], h.Sum(d[:0]))
  658. copy(validchecksum[:], decode[21:])
  659. return validchecksum == computedchecksum
  660. }
  661. // isBitcoinBech32Address is the validation function for validating if the field's value is a valid bech32 btc address
  662. func isBitcoinBech32Address(fl FieldLevel) bool {
  663. address := fl.Field().String()
  664. if !btcLowerAddressRegexBech32().MatchString(address) && !btcUpperAddressRegexBech32().MatchString(address) {
  665. return false
  666. }
  667. am := len(address) % 8
  668. if am == 0 || am == 3 || am == 5 {
  669. return false
  670. }
  671. address = strings.ToLower(address)
  672. alphabet := "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
  673. hr := []int{3, 3, 0, 2, 3} // the human readable part will always be bc
  674. addr := address[3:]
  675. dp := make([]int, 0, len(addr))
  676. for _, c := range addr {
  677. dp = append(dp, strings.IndexRune(alphabet, c))
  678. }
  679. ver := dp[0]
  680. if ver < 0 || ver > 16 {
  681. return false
  682. }
  683. if ver == 0 {
  684. if len(address) != 42 && len(address) != 62 {
  685. return false
  686. }
  687. }
  688. values := append(hr, dp...)
  689. GEN := []int{0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3}
  690. p := 1
  691. for _, v := range values {
  692. b := p >> 25
  693. p = (p&0x1ffffff)<<5 ^ v
  694. for i := 0; i < 5; i++ {
  695. if (b>>uint(i))&1 == 1 {
  696. p ^= GEN[i]
  697. }
  698. }
  699. }
  700. if p != 1 {
  701. return false
  702. }
  703. b := uint(0)
  704. acc := 0
  705. mv := (1 << 5) - 1
  706. var sw []int
  707. for _, v := range dp[1 : len(dp)-6] {
  708. acc = (acc << 5) | v
  709. b += 5
  710. for b >= 8 {
  711. b -= 8
  712. sw = append(sw, (acc>>b)&mv)
  713. }
  714. }
  715. if len(sw) < 2 || len(sw) > 40 {
  716. return false
  717. }
  718. return true
  719. }
  720. // excludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
  721. func excludesRune(fl FieldLevel) bool {
  722. return !containsRune(fl)
  723. }
  724. // excludesAll is the validation function for validating that the field's value does not contain any of the characters specified within the param.
  725. func excludesAll(fl FieldLevel) bool {
  726. return !containsAny(fl)
  727. }
  728. // excludes is the validation function for validating that the field's value does not contain the text specified within the param.
  729. func excludes(fl FieldLevel) bool {
  730. return !contains(fl)
  731. }
  732. // containsRune is the validation function for validating that the field's value contains the rune specified within the param.
  733. func containsRune(fl FieldLevel) bool {
  734. r, _ := utf8.DecodeRuneInString(fl.Param())
  735. return strings.ContainsRune(fl.Field().String(), r)
  736. }
  737. // containsAny is the validation function for validating that the field's value contains any of the characters specified within the param.
  738. func containsAny(fl FieldLevel) bool {
  739. return strings.ContainsAny(fl.Field().String(), fl.Param())
  740. }
  741. // contains is the validation function for validating that the field's value contains the text specified within the param.
  742. func contains(fl FieldLevel) bool {
  743. return strings.Contains(fl.Field().String(), fl.Param())
  744. }
  745. // startsWith is the validation function for validating that the field's value starts with the text specified within the param.
  746. func startsWith(fl FieldLevel) bool {
  747. return strings.HasPrefix(fl.Field().String(), fl.Param())
  748. }
  749. // endsWith is the validation function for validating that the field's value ends with the text specified within the param.
  750. func endsWith(fl FieldLevel) bool {
  751. return strings.HasSuffix(fl.Field().String(), fl.Param())
  752. }
  753. // startsNotWith is the validation function for validating that the field's value does not start with the text specified within the param.
  754. func startsNotWith(fl FieldLevel) bool {
  755. return !startsWith(fl)
  756. }
  757. // endsNotWith is the validation function for validating that the field's value does not end with the text specified within the param.
  758. func endsNotWith(fl FieldLevel) bool {
  759. return !endsWith(fl)
  760. }
  761. // fieldContains is the validation function for validating if the current field's value contains the field specified by the param's value.
  762. func fieldContains(fl FieldLevel) bool {
  763. field := fl.Field()
  764. currentField, _, ok := fl.GetStructFieldOK()
  765. if !ok {
  766. return false
  767. }
  768. return strings.Contains(field.String(), currentField.String())
  769. }
  770. // fieldExcludes is the validation function for validating if the current field's value excludes the field specified by the param's value.
  771. func fieldExcludes(fl FieldLevel) bool {
  772. field := fl.Field()
  773. currentField, _, ok := fl.GetStructFieldOK()
  774. if !ok {
  775. return true
  776. }
  777. return !strings.Contains(field.String(), currentField.String())
  778. }
  779. // 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.
  780. func isNeField(fl FieldLevel) bool {
  781. field := fl.Field()
  782. kind := field.Kind()
  783. currentField, currentKind, ok := fl.GetStructFieldOK()
  784. if !ok || currentKind != kind {
  785. return true
  786. }
  787. switch kind {
  788. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  789. return field.Int() != currentField.Int()
  790. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  791. return field.Uint() != currentField.Uint()
  792. case reflect.Float32, reflect.Float64:
  793. return field.Float() != currentField.Float()
  794. case reflect.Slice, reflect.Map, reflect.Array:
  795. return int64(field.Len()) != int64(currentField.Len())
  796. case reflect.Bool:
  797. return field.Bool() != currentField.Bool()
  798. case reflect.Struct:
  799. fieldType := field.Type()
  800. if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {
  801. t := currentField.Interface().(time.Time)
  802. fieldTime := field.Interface().(time.Time)
  803. return !fieldTime.Equal(t)
  804. }
  805. // Not Same underlying type i.e. struct and time
  806. if fieldType != currentField.Type() {
  807. return true
  808. }
  809. }
  810. // default reflect.String:
  811. return field.String() != currentField.String()
  812. }
  813. // isNe is the validation function for validating that the field's value does not equal the provided param value.
  814. func isNe(fl FieldLevel) bool {
  815. return !isEq(fl)
  816. }
  817. // isNeIgnoreCase is the validation function for validating that the field's string value does not equal the
  818. // provided param value. The comparison is case-insensitive
  819. func isNeIgnoreCase(fl FieldLevel) bool {
  820. return !isEqIgnoreCase(fl)
  821. }
  822. // 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.
  823. func isLteCrossStructField(fl FieldLevel) bool {
  824. field := fl.Field()
  825. kind := field.Kind()
  826. topField, topKind, ok := fl.GetStructFieldOK()
  827. if !ok || topKind != kind {
  828. return false
  829. }
  830. switch kind {
  831. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  832. return field.Int() <= topField.Int()
  833. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  834. return field.Uint() <= topField.Uint()
  835. case reflect.Float32, reflect.Float64:
  836. return field.Float() <= topField.Float()
  837. case reflect.Slice, reflect.Map, reflect.Array:
  838. return int64(field.Len()) <= int64(topField.Len())
  839. case reflect.Struct:
  840. fieldType := field.Type()
  841. if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {
  842. fieldTime := field.Convert(timeType).Interface().(time.Time)
  843. topTime := topField.Convert(timeType).Interface().(time.Time)
  844. return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
  845. }
  846. // Not Same underlying type i.e. struct and time
  847. if fieldType != topField.Type() {
  848. return false
  849. }
  850. }
  851. // default reflect.String:
  852. return field.String() <= topField.String()
  853. }
  854. // 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.
  855. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
  856. func isLtCrossStructField(fl FieldLevel) bool {
  857. field := fl.Field()
  858. kind := field.Kind()
  859. topField, topKind, ok := fl.GetStructFieldOK()
  860. if !ok || topKind != kind {
  861. return false
  862. }
  863. switch kind {
  864. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  865. return field.Int() < topField.Int()
  866. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  867. return field.Uint() < topField.Uint()
  868. case reflect.Float32, reflect.Float64:
  869. return field.Float() < topField.Float()
  870. case reflect.Slice, reflect.Map, reflect.Array:
  871. return int64(field.Len()) < int64(topField.Len())
  872. case reflect.Struct:
  873. fieldType := field.Type()
  874. if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {
  875. fieldTime := field.Convert(timeType).Interface().(time.Time)
  876. topTime := topField.Convert(timeType).Interface().(time.Time)
  877. return fieldTime.Before(topTime)
  878. }
  879. // Not Same underlying type i.e. struct and time
  880. if fieldType != topField.Type() {
  881. return false
  882. }
  883. }
  884. // default reflect.String:
  885. return field.String() < topField.String()
  886. }
  887. // 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.
  888. func isGteCrossStructField(fl FieldLevel) bool {
  889. field := fl.Field()
  890. kind := field.Kind()
  891. topField, topKind, ok := fl.GetStructFieldOK()
  892. if !ok || topKind != kind {
  893. return false
  894. }
  895. switch kind {
  896. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  897. return field.Int() >= topField.Int()
  898. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  899. return field.Uint() >= topField.Uint()
  900. case reflect.Float32, reflect.Float64:
  901. return field.Float() >= topField.Float()
  902. case reflect.Slice, reflect.Map, reflect.Array:
  903. return int64(field.Len()) >= int64(topField.Len())
  904. case reflect.Struct:
  905. fieldType := field.Type()
  906. if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {
  907. fieldTime := field.Convert(timeType).Interface().(time.Time)
  908. topTime := topField.Convert(timeType).Interface().(time.Time)
  909. return fieldTime.After(topTime) || fieldTime.Equal(topTime)
  910. }
  911. // Not Same underlying type i.e. struct and time
  912. if fieldType != topField.Type() {
  913. return false
  914. }
  915. }
  916. // default reflect.String:
  917. return field.String() >= topField.String()
  918. }
  919. // 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.
  920. func isGtCrossStructField(fl FieldLevel) bool {
  921. field := fl.Field()
  922. kind := field.Kind()
  923. topField, topKind, ok := fl.GetStructFieldOK()
  924. if !ok || topKind != kind {
  925. return false
  926. }
  927. switch kind {
  928. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  929. return field.Int() > topField.Int()
  930. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  931. return field.Uint() > topField.Uint()
  932. case reflect.Float32, reflect.Float64:
  933. return field.Float() > topField.Float()
  934. case reflect.Slice, reflect.Map, reflect.Array:
  935. return int64(field.Len()) > int64(topField.Len())
  936. case reflect.Struct:
  937. fieldType := field.Type()
  938. if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {
  939. fieldTime := field.Convert(timeType).Interface().(time.Time)
  940. topTime := topField.Convert(timeType).Interface().(time.Time)
  941. return fieldTime.After(topTime)
  942. }
  943. // Not Same underlying type i.e. struct and time
  944. if fieldType != topField.Type() {
  945. return false
  946. }
  947. }
  948. // default reflect.String:
  949. return field.String() > topField.String()
  950. }
  951. // 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.
  952. func isNeCrossStructField(fl FieldLevel) bool {
  953. field := fl.Field()
  954. kind := field.Kind()
  955. topField, currentKind, ok := fl.GetStructFieldOK()
  956. if !ok || currentKind != kind {
  957. return true
  958. }
  959. switch kind {
  960. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  961. return topField.Int() != field.Int()
  962. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  963. return topField.Uint() != field.Uint()
  964. case reflect.Float32, reflect.Float64:
  965. return topField.Float() != field.Float()
  966. case reflect.Slice, reflect.Map, reflect.Array:
  967. return int64(topField.Len()) != int64(field.Len())
  968. case reflect.Bool:
  969. return topField.Bool() != field.Bool()
  970. case reflect.Struct:
  971. fieldType := field.Type()
  972. if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {
  973. t := field.Convert(timeType).Interface().(time.Time)
  974. fieldTime := topField.Convert(timeType).Interface().(time.Time)
  975. return !fieldTime.Equal(t)
  976. }
  977. // Not Same underlying type i.e. struct and time
  978. if fieldType != topField.Type() {
  979. return true
  980. }
  981. }
  982. // default reflect.String:
  983. return topField.String() != field.String()
  984. }
  985. // 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.
  986. func isEqCrossStructField(fl FieldLevel) bool {
  987. field := fl.Field()
  988. kind := field.Kind()
  989. topField, topKind, ok := fl.GetStructFieldOK()
  990. if !ok || topKind != kind {
  991. return false
  992. }
  993. switch kind {
  994. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  995. return topField.Int() == field.Int()
  996. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  997. return topField.Uint() == field.Uint()
  998. case reflect.Float32, reflect.Float64:
  999. return topField.Float() == field.Float()
  1000. case reflect.Slice, reflect.Map, reflect.Array:
  1001. return int64(topField.Len()) == int64(field.Len())
  1002. case reflect.Bool:
  1003. return topField.Bool() == field.Bool()
  1004. case reflect.Struct:
  1005. fieldType := field.Type()
  1006. if fieldType.ConvertibleTo(timeType) && topField.Type().ConvertibleTo(timeType) {
  1007. t := field.Convert(timeType).Interface().(time.Time)
  1008. fieldTime := topField.Convert(timeType).Interface().(time.Time)
  1009. return fieldTime.Equal(t)
  1010. }
  1011. // Not Same underlying type i.e. struct and time
  1012. if fieldType != topField.Type() {
  1013. return false
  1014. }
  1015. }
  1016. // default reflect.String:
  1017. return topField.String() == field.String()
  1018. }
  1019. // isEqField is the validation function for validating if the current field's value is equal to the field specified by the param's value.
  1020. func isEqField(fl FieldLevel) bool {
  1021. field := fl.Field()
  1022. kind := field.Kind()
  1023. currentField, currentKind, ok := fl.GetStructFieldOK()
  1024. if !ok || currentKind != kind {
  1025. return false
  1026. }
  1027. switch kind {
  1028. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1029. return field.Int() == currentField.Int()
  1030. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1031. return field.Uint() == currentField.Uint()
  1032. case reflect.Float32, reflect.Float64:
  1033. return field.Float() == currentField.Float()
  1034. case reflect.Slice, reflect.Map, reflect.Array:
  1035. return int64(field.Len()) == int64(currentField.Len())
  1036. case reflect.Bool:
  1037. return field.Bool() == currentField.Bool()
  1038. case reflect.Struct:
  1039. fieldType := field.Type()
  1040. if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {
  1041. t := currentField.Convert(timeType).Interface().(time.Time)
  1042. fieldTime := field.Convert(timeType).Interface().(time.Time)
  1043. return fieldTime.Equal(t)
  1044. }
  1045. // Not Same underlying type i.e. struct and time
  1046. if fieldType != currentField.Type() {
  1047. return false
  1048. }
  1049. }
  1050. // default reflect.String:
  1051. return field.String() == currentField.String()
  1052. }
  1053. // isEq is the validation function for validating if the current field's value is equal to the param's value.
  1054. func isEq(fl FieldLevel) bool {
  1055. field := fl.Field()
  1056. param := fl.Param()
  1057. switch field.Kind() {
  1058. case reflect.String:
  1059. return field.String() == param
  1060. case reflect.Slice, reflect.Map, reflect.Array:
  1061. p := asInt(param)
  1062. return int64(field.Len()) == p
  1063. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1064. p := asIntFromType(field.Type(), param)
  1065. return field.Int() == p
  1066. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1067. p := asUint(param)
  1068. return field.Uint() == p
  1069. case reflect.Float32:
  1070. p := asFloat32(param)
  1071. return field.Float() == p
  1072. case reflect.Float64:
  1073. p := asFloat64(param)
  1074. return field.Float() == p
  1075. case reflect.Bool:
  1076. p := asBool(param)
  1077. return field.Bool() == p
  1078. }
  1079. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1080. }
  1081. // isEqIgnoreCase is the validation function for validating if the current field's string value is
  1082. // equal to the param's value.
  1083. // The comparison is case-insensitive.
  1084. func isEqIgnoreCase(fl FieldLevel) bool {
  1085. field := fl.Field()
  1086. param := fl.Param()
  1087. switch field.Kind() {
  1088. case reflect.String:
  1089. return strings.EqualFold(field.String(), param)
  1090. }
  1091. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1092. }
  1093. // isPostcodeByIso3166Alpha2 validates by value which is country code in iso 3166 alpha 2
  1094. // example: `postcode_iso3166_alpha2=US`
  1095. func isPostcodeByIso3166Alpha2(fl FieldLevel) bool {
  1096. field := fl.Field()
  1097. param := fl.Param()
  1098. postcodeRegexInit.Do(initPostcodes)
  1099. reg, found := postCodeRegexDict[param]
  1100. if !found {
  1101. return false
  1102. }
  1103. return reg.MatchString(field.String())
  1104. }
  1105. // isPostcodeByIso3166Alpha2Field validates by field which represents for a value of country code in iso 3166 alpha 2
  1106. // example: `postcode_iso3166_alpha2_field=CountryCode`
  1107. func isPostcodeByIso3166Alpha2Field(fl FieldLevel) bool {
  1108. field := fl.Field()
  1109. params := parseOneOfParam2(fl.Param())
  1110. if len(params) != 1 {
  1111. return false
  1112. }
  1113. currentField, kind, _, found := fl.GetStructFieldOKAdvanced2(fl.Parent(), params[0])
  1114. if !found {
  1115. return false
  1116. }
  1117. if kind != reflect.String {
  1118. panic(fmt.Sprintf("Bad field type %T", currentField.Interface()))
  1119. }
  1120. postcodeRegexInit.Do(initPostcodes)
  1121. reg, found := postCodeRegexDict[currentField.String()]
  1122. if !found {
  1123. return false
  1124. }
  1125. return reg.MatchString(field.String())
  1126. }
  1127. // isBase32 is the validation function for validating if the current field's value is a valid base 32.
  1128. func isBase32(fl FieldLevel) bool {
  1129. return base32Regex().MatchString(fl.Field().String())
  1130. }
  1131. // isBase64 is the validation function for validating if the current field's value is a valid base 64.
  1132. func isBase64(fl FieldLevel) bool {
  1133. return base64Regex().MatchString(fl.Field().String())
  1134. }
  1135. // isBase64URL is the validation function for validating if the current field's value is a valid base64 URL safe string.
  1136. func isBase64URL(fl FieldLevel) bool {
  1137. return base64URLRegex().MatchString(fl.Field().String())
  1138. }
  1139. // isBase64RawURL is the validation function for validating if the current field's value is a valid base64 URL safe string without '=' padding.
  1140. func isBase64RawURL(fl FieldLevel) bool {
  1141. return base64RawURLRegex().MatchString(fl.Field().String())
  1142. }
  1143. // isURI is the validation function for validating if the current field's value is a valid URI.
  1144. func isURI(fl FieldLevel) bool {
  1145. field := fl.Field()
  1146. switch field.Kind() {
  1147. case reflect.String:
  1148. s := field.String()
  1149. // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
  1150. // emulate browser and strip the '#' suffix prior to validation. see issue-#237
  1151. if i := strings.Index(s, "#"); i > -1 {
  1152. s = s[:i]
  1153. }
  1154. if len(s) == 0 {
  1155. return false
  1156. }
  1157. _, err := url.ParseRequestURI(s)
  1158. return err == nil
  1159. }
  1160. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1161. }
  1162. // isFileURL is the helper function for validating if the `path` valid file URL as per RFC8089
  1163. func isFileURL(path string) bool {
  1164. if !strings.HasPrefix(path, "file:/") {
  1165. return false
  1166. }
  1167. _, err := url.ParseRequestURI(path)
  1168. return err == nil
  1169. }
  1170. // isURL is the validation function for validating if the current field's value is a valid URL.
  1171. func isURL(fl FieldLevel) bool {
  1172. field := fl.Field()
  1173. switch field.Kind() {
  1174. case reflect.String:
  1175. s := strings.ToLower(field.String())
  1176. if len(s) == 0 {
  1177. return false
  1178. }
  1179. if isFileURL(s) {
  1180. return true
  1181. }
  1182. url, err := url.Parse(s)
  1183. if err != nil || url.Scheme == "" {
  1184. return false
  1185. }
  1186. if url.Host == "" && url.Fragment == "" && url.Opaque == "" {
  1187. return false
  1188. }
  1189. return true
  1190. }
  1191. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1192. }
  1193. // isHttpURL is the validation function for validating if the current field's value is a valid HTTP(s) URL.
  1194. func isHttpURL(fl FieldLevel) bool {
  1195. if !isURL(fl) {
  1196. return false
  1197. }
  1198. field := fl.Field()
  1199. switch field.Kind() {
  1200. case reflect.String:
  1201. s := strings.ToLower(field.String())
  1202. url, err := url.Parse(s)
  1203. if err != nil || url.Host == "" {
  1204. return false
  1205. }
  1206. return url.Scheme == "http" || url.Scheme == "https"
  1207. }
  1208. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1209. }
  1210. // isUrnRFC2141 is the validation function for validating if the current field's value is a valid URN as per RFC 2141.
  1211. func isUrnRFC2141(fl FieldLevel) bool {
  1212. field := fl.Field()
  1213. switch field.Kind() {
  1214. case reflect.String:
  1215. str := field.String()
  1216. _, match := urn.Parse([]byte(str))
  1217. return match
  1218. }
  1219. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1220. }
  1221. // isFile is the validation function for validating if the current field's value is a valid existing file path.
  1222. func isFile(fl FieldLevel) bool {
  1223. field := fl.Field()
  1224. switch field.Kind() {
  1225. case reflect.String:
  1226. fileInfo, err := os.Stat(field.String())
  1227. if err != nil {
  1228. return false
  1229. }
  1230. return !fileInfo.IsDir()
  1231. }
  1232. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1233. }
  1234. // isImage is the validation function for validating if the current field's value contains the path to a valid image file
  1235. func isImage(fl FieldLevel) bool {
  1236. mimetypes := map[string]bool{
  1237. "image/bmp": true,
  1238. "image/cis-cod": true,
  1239. "image/gif": true,
  1240. "image/ief": true,
  1241. "image/jpeg": true,
  1242. "image/jp2": true,
  1243. "image/jpx": true,
  1244. "image/jpm": true,
  1245. "image/pipeg": true,
  1246. "image/png": true,
  1247. "image/svg+xml": true,
  1248. "image/tiff": true,
  1249. "image/webp": true,
  1250. "image/x-cmu-raster": true,
  1251. "image/x-cmx": true,
  1252. "image/x-icon": true,
  1253. "image/x-portable-anymap": true,
  1254. "image/x-portable-bitmap": true,
  1255. "image/x-portable-graymap": true,
  1256. "image/x-portable-pixmap": true,
  1257. "image/x-rgb": true,
  1258. "image/x-xbitmap": true,
  1259. "image/x-xpixmap": true,
  1260. "image/x-xwindowdump": true,
  1261. }
  1262. field := fl.Field()
  1263. switch field.Kind() {
  1264. case reflect.String:
  1265. filePath := field.String()
  1266. fileInfo, err := os.Stat(filePath)
  1267. if err != nil {
  1268. return false
  1269. }
  1270. if fileInfo.IsDir() {
  1271. return false
  1272. }
  1273. file, err := os.Open(filePath)
  1274. if err != nil {
  1275. return false
  1276. }
  1277. defer func() {
  1278. _ = file.Close()
  1279. }()
  1280. mime, err := mimetype.DetectReader(file)
  1281. if err != nil {
  1282. return false
  1283. }
  1284. if _, ok := mimetypes[mime.String()]; ok {
  1285. return true
  1286. }
  1287. }
  1288. return false
  1289. }
  1290. // isFilePath is the validation function for validating if the current field's value is a valid file path.
  1291. func isFilePath(fl FieldLevel) bool {
  1292. var exists bool
  1293. var err error
  1294. field := fl.Field()
  1295. // Not valid if it is a directory.
  1296. if isDir(fl) {
  1297. return false
  1298. }
  1299. // If it exists, it obviously is valid.
  1300. // This is done first to avoid code duplication and unnecessary additional logic.
  1301. if exists = isFile(fl); exists {
  1302. return true
  1303. }
  1304. // It does not exist but may still be a valid filepath.
  1305. switch field.Kind() {
  1306. case reflect.String:
  1307. // Every OS allows for whitespace, but none
  1308. // let you use a file with no filename (to my knowledge).
  1309. // Unless you're dealing with raw inodes, but I digress.
  1310. if strings.TrimSpace(field.String()) == "" {
  1311. return false
  1312. }
  1313. // We make sure it isn't a directory.
  1314. if strings.HasSuffix(field.String(), string(os.PathSeparator)) {
  1315. return false
  1316. }
  1317. if _, err = os.Stat(field.String()); err != nil {
  1318. switch t := err.(type) {
  1319. case *fs.PathError:
  1320. if t.Err == syscall.EINVAL {
  1321. // It's definitely an invalid character in the filepath.
  1322. return false
  1323. }
  1324. // It could be a permission error, a does-not-exist error, etc.
  1325. // Out-of-scope for this validation, though.
  1326. return true
  1327. default:
  1328. // Something went *seriously* wrong.
  1329. /*
  1330. Per https://pkg.go.dev/os#Stat:
  1331. "If there is an error, it will be of type *PathError."
  1332. */
  1333. panic(err)
  1334. }
  1335. }
  1336. }
  1337. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1338. }
  1339. // isE164 is the validation function for validating if the current field's value is a valid e.164 formatted phone number.
  1340. func isE164(fl FieldLevel) bool {
  1341. return e164Regex().MatchString(fl.Field().String())
  1342. }
  1343. // isEmail is the validation function for validating if the current field's value is a valid email address.
  1344. func isEmail(fl FieldLevel) bool {
  1345. _, err := mail.ParseAddress(fl.Field().String())
  1346. if err != nil {
  1347. return false
  1348. }
  1349. return emailRegex().MatchString(fl.Field().String())
  1350. }
  1351. // isHSLA is the validation function for validating if the current field's value is a valid HSLA color.
  1352. func isHSLA(fl FieldLevel) bool {
  1353. return hslaRegex().MatchString(fl.Field().String())
  1354. }
  1355. // isHSL is the validation function for validating if the current field's value is a valid HSL color.
  1356. func isHSL(fl FieldLevel) bool {
  1357. return hslRegex().MatchString(fl.Field().String())
  1358. }
  1359. // isRGBA is the validation function for validating if the current field's value is a valid RGBA color.
  1360. func isRGBA(fl FieldLevel) bool {
  1361. return rgbaRegex().MatchString(fl.Field().String())
  1362. }
  1363. // isRGB is the validation function for validating if the current field's value is a valid RGB color.
  1364. func isRGB(fl FieldLevel) bool {
  1365. return rgbRegex().MatchString(fl.Field().String())
  1366. }
  1367. // isHEXColor is the validation function for validating if the current field's value is a valid HEX color.
  1368. func isHEXColor(fl FieldLevel) bool {
  1369. return hexColorRegex().MatchString(fl.Field().String())
  1370. }
  1371. // isHexadecimal is the validation function for validating if the current field's value is a valid hexadecimal.
  1372. func isHexadecimal(fl FieldLevel) bool {
  1373. return hexadecimalRegex().MatchString(fl.Field().String())
  1374. }
  1375. // isNumber is the validation function for validating if the current field's value is a valid number.
  1376. func isNumber(fl FieldLevel) bool {
  1377. switch fl.Field().Kind() {
  1378. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
  1379. return true
  1380. default:
  1381. return numberRegex().MatchString(fl.Field().String())
  1382. }
  1383. }
  1384. // isNumeric is the validation function for validating if the current field's value is a valid numeric value.
  1385. func isNumeric(fl FieldLevel) bool {
  1386. switch fl.Field().Kind() {
  1387. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
  1388. return true
  1389. default:
  1390. return numericRegex().MatchString(fl.Field().String())
  1391. }
  1392. }
  1393. // isAlphanum is the validation function for validating if the current field's value is a valid alphanumeric value.
  1394. func isAlphanum(fl FieldLevel) bool {
  1395. return alphaNumericRegex().MatchString(fl.Field().String())
  1396. }
  1397. // isAlpha is the validation function for validating if the current field's value is a valid alpha value.
  1398. func isAlpha(fl FieldLevel) bool {
  1399. return alphaRegex().MatchString(fl.Field().String())
  1400. }
  1401. // isAlphanumUnicode is the validation function for validating if the current field's value is a valid alphanumeric unicode value.
  1402. func isAlphanumUnicode(fl FieldLevel) bool {
  1403. return alphaUnicodeNumericRegex().MatchString(fl.Field().String())
  1404. }
  1405. // isAlphaUnicode is the validation function for validating if the current field's value is a valid alpha unicode value.
  1406. func isAlphaUnicode(fl FieldLevel) bool {
  1407. return alphaUnicodeRegex().MatchString(fl.Field().String())
  1408. }
  1409. // isBoolean is the validation function for validating if the current field's value is a valid boolean value or can be safely converted to a boolean value.
  1410. func isBoolean(fl FieldLevel) bool {
  1411. switch fl.Field().Kind() {
  1412. case reflect.Bool:
  1413. return true
  1414. default:
  1415. _, err := strconv.ParseBool(fl.Field().String())
  1416. return err == nil
  1417. }
  1418. }
  1419. // isDefault is the opposite of required aka hasValue
  1420. func isDefault(fl FieldLevel) bool {
  1421. return !hasValue(fl)
  1422. }
  1423. // hasValue is the validation function for validating if the current field's value is not the default static value.
  1424. func hasValue(fl FieldLevel) bool {
  1425. field := fl.Field()
  1426. switch field.Kind() {
  1427. case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
  1428. return !field.IsNil()
  1429. default:
  1430. if fl.(*validate).fldIsPointer && field.Interface() != nil {
  1431. return true
  1432. }
  1433. return field.IsValid() && !field.IsZero()
  1434. }
  1435. }
  1436. // hasNotZeroValue is the validation function for validating if the current field's value is not the zero value for its type.
  1437. func hasNotZeroValue(fl FieldLevel) bool {
  1438. field := fl.Field()
  1439. switch field.Kind() {
  1440. case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
  1441. return !field.IsNil()
  1442. default:
  1443. if fl.(*validate).fldIsPointer && field.Interface() != nil {
  1444. return !field.IsZero()
  1445. }
  1446. return field.IsValid() && !field.IsZero()
  1447. }
  1448. }
  1449. // requireCheckFieldKind is a func for check field kind
  1450. func requireCheckFieldKind(fl FieldLevel, param string, defaultNotFoundValue bool) bool {
  1451. field := fl.Field()
  1452. kind := field.Kind()
  1453. var nullable, found bool
  1454. if len(param) > 0 {
  1455. field, kind, nullable, found = fl.GetStructFieldOKAdvanced2(fl.Parent(), param)
  1456. if !found {
  1457. return defaultNotFoundValue
  1458. }
  1459. }
  1460. switch kind {
  1461. case reflect.Invalid:
  1462. return defaultNotFoundValue
  1463. case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
  1464. return field.IsNil()
  1465. default:
  1466. if nullable && field.Interface() != nil {
  1467. return false
  1468. }
  1469. return field.IsValid() && field.IsZero()
  1470. }
  1471. }
  1472. // requireCheckFieldValue is a func for check field value
  1473. func requireCheckFieldValue(
  1474. fl FieldLevel, param string, value string, defaultNotFoundValue bool,
  1475. ) bool {
  1476. field, kind, _, found := fl.GetStructFieldOKAdvanced2(fl.Parent(), param)
  1477. if !found {
  1478. return defaultNotFoundValue
  1479. }
  1480. switch kind {
  1481. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1482. return field.Int() == asInt(value)
  1483. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1484. return field.Uint() == asUint(value)
  1485. case reflect.Float32:
  1486. return field.Float() == asFloat32(value)
  1487. case reflect.Float64:
  1488. return field.Float() == asFloat64(value)
  1489. case reflect.Slice, reflect.Map, reflect.Array:
  1490. return int64(field.Len()) == asInt(value)
  1491. case reflect.Bool:
  1492. return field.Bool() == (value == "true")
  1493. case reflect.Ptr:
  1494. if field.IsNil() {
  1495. return value == "nil"
  1496. }
  1497. // Handle non-nil pointers
  1498. return requireCheckFieldValue(fl, param, value, defaultNotFoundValue)
  1499. }
  1500. // default reflect.String:
  1501. return field.String() == value
  1502. }
  1503. // requiredIf is the validation function
  1504. // The field under validation must be present and not empty only if all the other specified fields are equal to the value following with the specified field.
  1505. func requiredIf(fl FieldLevel) bool {
  1506. params := parseOneOfParam2(fl.Param())
  1507. if len(params)%2 != 0 {
  1508. panic(fmt.Sprintf("Bad param number for required_if %s", fl.FieldName()))
  1509. }
  1510. for i := 0; i < len(params); i += 2 {
  1511. if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
  1512. return true
  1513. }
  1514. }
  1515. return hasValue(fl)
  1516. }
  1517. // excludedIf is the validation function
  1518. // The field under validation must not be present or is empty only if all the other specified fields are equal to the value following with the specified field.
  1519. func excludedIf(fl FieldLevel) bool {
  1520. params := parseOneOfParam2(fl.Param())
  1521. if len(params)%2 != 0 {
  1522. panic(fmt.Sprintf("Bad param number for excluded_if %s", fl.FieldName()))
  1523. }
  1524. for i := 0; i < len(params); i += 2 {
  1525. if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
  1526. return true
  1527. }
  1528. }
  1529. return !hasValue(fl)
  1530. }
  1531. // requiredUnless is the validation function
  1532. // The field under validation must be present and not empty only unless all the other specified fields are equal to the value following with the specified field.
  1533. func requiredUnless(fl FieldLevel) bool {
  1534. params := parseOneOfParam2(fl.Param())
  1535. if len(params)%2 != 0 {
  1536. panic(fmt.Sprintf("Bad param number for required_unless %s", fl.FieldName()))
  1537. }
  1538. for i := 0; i < len(params); i += 2 {
  1539. if requireCheckFieldValue(fl, params[i], params[i+1], false) {
  1540. return true
  1541. }
  1542. }
  1543. return hasValue(fl)
  1544. }
  1545. // skipUnless is the validation function
  1546. // The field under validation must be present and not empty only unless all the other specified fields are equal to the value following with the specified field.
  1547. func skipUnless(fl FieldLevel) bool {
  1548. params := parseOneOfParam2(fl.Param())
  1549. if len(params)%2 != 0 {
  1550. panic(fmt.Sprintf("Bad param number for skip_unless %s", fl.FieldName()))
  1551. }
  1552. for i := 0; i < len(params); i += 2 {
  1553. if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
  1554. return true
  1555. }
  1556. }
  1557. return hasValue(fl)
  1558. }
  1559. // excludedUnless is the validation function
  1560. // The field under validation must not be present or is empty unless all the other specified fields are equal to the value following with the specified field.
  1561. func excludedUnless(fl FieldLevel) bool {
  1562. params := parseOneOfParam2(fl.Param())
  1563. if len(params)%2 != 0 {
  1564. panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName()))
  1565. }
  1566. for i := 0; i < len(params); i += 2 {
  1567. if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
  1568. return !hasValue(fl)
  1569. }
  1570. }
  1571. return true
  1572. }
  1573. // excludedWith is the validation function
  1574. // The field under validation must not be present or is empty if any of the other specified fields are present.
  1575. func excludedWith(fl FieldLevel) bool {
  1576. params := parseOneOfParam2(fl.Param())
  1577. for _, param := range params {
  1578. if !requireCheckFieldKind(fl, param, true) {
  1579. return !hasValue(fl)
  1580. }
  1581. }
  1582. return true
  1583. }
  1584. // requiredWith is the validation function
  1585. // The field under validation must be present and not empty only if any of the other specified fields are present.
  1586. func requiredWith(fl FieldLevel) bool {
  1587. params := parseOneOfParam2(fl.Param())
  1588. for _, param := range params {
  1589. if !requireCheckFieldKind(fl, param, true) {
  1590. return hasValue(fl)
  1591. }
  1592. }
  1593. return true
  1594. }
  1595. // excludedWithAll is the validation function
  1596. // The field under validation must not be present or is empty if all of the other specified fields are present.
  1597. func excludedWithAll(fl FieldLevel) bool {
  1598. params := parseOneOfParam2(fl.Param())
  1599. for _, param := range params {
  1600. if requireCheckFieldKind(fl, param, true) {
  1601. return true
  1602. }
  1603. }
  1604. return !hasValue(fl)
  1605. }
  1606. // requiredWithAll is the validation function
  1607. // The field under validation must be present and not empty only if all of the other specified fields are present.
  1608. func requiredWithAll(fl FieldLevel) bool {
  1609. params := parseOneOfParam2(fl.Param())
  1610. for _, param := range params {
  1611. if requireCheckFieldKind(fl, param, true) {
  1612. return true
  1613. }
  1614. }
  1615. return hasValue(fl)
  1616. }
  1617. // excludedWithout is the validation function
  1618. // The field under validation must not be present or is empty when any of the other specified fields are not present.
  1619. func excludedWithout(fl FieldLevel) bool {
  1620. if requireCheckFieldKind(fl, strings.TrimSpace(fl.Param()), true) {
  1621. return !hasValue(fl)
  1622. }
  1623. return true
  1624. }
  1625. // requiredWithout is the validation function
  1626. // The field under validation must be present and not empty only when any of the other specified fields are not present.
  1627. func requiredWithout(fl FieldLevel) bool {
  1628. if requireCheckFieldKind(fl, strings.TrimSpace(fl.Param()), true) {
  1629. return hasValue(fl)
  1630. }
  1631. return true
  1632. }
  1633. // excludedWithoutAll is the validation function
  1634. // The field under validation must not be present or is empty when all of the other specified fields are not present.
  1635. func excludedWithoutAll(fl FieldLevel) bool {
  1636. params := parseOneOfParam2(fl.Param())
  1637. for _, param := range params {
  1638. if !requireCheckFieldKind(fl, param, true) {
  1639. return true
  1640. }
  1641. }
  1642. return !hasValue(fl)
  1643. }
  1644. // requiredWithoutAll is the validation function
  1645. // The field under validation must be present and not empty only when all of the other specified fields are not present.
  1646. func requiredWithoutAll(fl FieldLevel) bool {
  1647. params := parseOneOfParam2(fl.Param())
  1648. for _, param := range params {
  1649. if !requireCheckFieldKind(fl, param, true) {
  1650. return true
  1651. }
  1652. }
  1653. return hasValue(fl)
  1654. }
  1655. // 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.
  1656. func isGteField(fl FieldLevel) bool {
  1657. field := fl.Field()
  1658. kind := field.Kind()
  1659. currentField, currentKind, ok := fl.GetStructFieldOK()
  1660. if !ok || currentKind != kind {
  1661. return false
  1662. }
  1663. switch kind {
  1664. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1665. return field.Int() >= currentField.Int()
  1666. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1667. return field.Uint() >= currentField.Uint()
  1668. case reflect.Float32, reflect.Float64:
  1669. return field.Float() >= currentField.Float()
  1670. case reflect.Struct:
  1671. fieldType := field.Type()
  1672. if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {
  1673. t := currentField.Convert(timeType).Interface().(time.Time)
  1674. fieldTime := field.Convert(timeType).Interface().(time.Time)
  1675. return fieldTime.After(t) || fieldTime.Equal(t)
  1676. }
  1677. // Not Same underlying type i.e. struct and time
  1678. if fieldType != currentField.Type() {
  1679. return false
  1680. }
  1681. }
  1682. // default reflect.String
  1683. return len(field.String()) >= len(currentField.String())
  1684. }
  1685. // isGtField is the validation function for validating if the current field's value is greater than the field specified by the param's value.
  1686. func isGtField(fl FieldLevel) bool {
  1687. field := fl.Field()
  1688. kind := field.Kind()
  1689. currentField, currentKind, ok := fl.GetStructFieldOK()
  1690. if !ok || currentKind != kind {
  1691. return false
  1692. }
  1693. switch kind {
  1694. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1695. return field.Int() > currentField.Int()
  1696. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1697. return field.Uint() > currentField.Uint()
  1698. case reflect.Float32, reflect.Float64:
  1699. return field.Float() > currentField.Float()
  1700. case reflect.Struct:
  1701. fieldType := field.Type()
  1702. if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {
  1703. t := currentField.Convert(timeType).Interface().(time.Time)
  1704. fieldTime := field.Convert(timeType).Interface().(time.Time)
  1705. return fieldTime.After(t)
  1706. }
  1707. // Not Same underlying type i.e. struct and time
  1708. if fieldType != currentField.Type() {
  1709. return false
  1710. }
  1711. }
  1712. // default reflect.String
  1713. return len(field.String()) > len(currentField.String())
  1714. }
  1715. // isGte is the validation function for validating if the current field's value is greater than or equal to the param's value.
  1716. func isGte(fl FieldLevel) bool {
  1717. field := fl.Field()
  1718. param := fl.Param()
  1719. switch field.Kind() {
  1720. case reflect.String:
  1721. p := asInt(param)
  1722. return int64(utf8.RuneCountInString(field.String())) >= p
  1723. case reflect.Slice, reflect.Map, reflect.Array:
  1724. p := asInt(param)
  1725. return int64(field.Len()) >= p
  1726. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1727. p := asIntFromType(field.Type(), param)
  1728. return field.Int() >= p
  1729. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1730. p := asUint(param)
  1731. return field.Uint() >= p
  1732. case reflect.Float32:
  1733. p := asFloat32(param)
  1734. return field.Float() >= p
  1735. case reflect.Float64:
  1736. p := asFloat64(param)
  1737. return field.Float() >= p
  1738. case reflect.Struct:
  1739. if field.Type().ConvertibleTo(timeType) {
  1740. now := time.Now().UTC()
  1741. t := field.Convert(timeType).Interface().(time.Time)
  1742. return t.After(now) || t.Equal(now)
  1743. }
  1744. }
  1745. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1746. }
  1747. // isGt is the validation function for validating if the current field's value is greater than the param's value.
  1748. func isGt(fl FieldLevel) bool {
  1749. field := fl.Field()
  1750. param := fl.Param()
  1751. switch field.Kind() {
  1752. case reflect.String:
  1753. p := asInt(param)
  1754. return int64(utf8.RuneCountInString(field.String())) > p
  1755. case reflect.Slice, reflect.Map, reflect.Array:
  1756. p := asInt(param)
  1757. return int64(field.Len()) > p
  1758. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1759. p := asIntFromType(field.Type(), param)
  1760. return field.Int() > p
  1761. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1762. p := asUint(param)
  1763. return field.Uint() > p
  1764. case reflect.Float32:
  1765. p := asFloat32(param)
  1766. return field.Float() > p
  1767. case reflect.Float64:
  1768. p := asFloat64(param)
  1769. return field.Float() > p
  1770. case reflect.Struct:
  1771. if field.Type().ConvertibleTo(timeType) {
  1772. return field.Convert(timeType).Interface().(time.Time).After(time.Now().UTC())
  1773. }
  1774. }
  1775. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1776. }
  1777. // hasLengthOf is the validation function for validating if the current field's value is equal to the param's value.
  1778. func hasLengthOf(fl FieldLevel) bool {
  1779. field := fl.Field()
  1780. param := fl.Param()
  1781. switch field.Kind() {
  1782. case reflect.String:
  1783. p := asInt(param)
  1784. return int64(utf8.RuneCountInString(field.String())) == p
  1785. case reflect.Slice, reflect.Map, reflect.Array:
  1786. p := asInt(param)
  1787. return int64(field.Len()) == p
  1788. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1789. p := asIntFromType(field.Type(), param)
  1790. return field.Int() == p
  1791. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1792. p := asUint(param)
  1793. return field.Uint() == p
  1794. case reflect.Float32:
  1795. p := asFloat32(param)
  1796. return field.Float() == p
  1797. case reflect.Float64:
  1798. p := asFloat64(param)
  1799. return field.Float() == p
  1800. }
  1801. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1802. }
  1803. // hasMinOf is the validation function for validating if the current field's value is greater than or equal to the param's value.
  1804. func hasMinOf(fl FieldLevel) bool {
  1805. return isGte(fl)
  1806. }
  1807. // 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.
  1808. func isLteField(fl FieldLevel) bool {
  1809. field := fl.Field()
  1810. kind := field.Kind()
  1811. currentField, currentKind, ok := fl.GetStructFieldOK()
  1812. if !ok || currentKind != kind {
  1813. return false
  1814. }
  1815. switch kind {
  1816. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1817. return field.Int() <= currentField.Int()
  1818. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1819. return field.Uint() <= currentField.Uint()
  1820. case reflect.Float32, reflect.Float64:
  1821. return field.Float() <= currentField.Float()
  1822. case reflect.Struct:
  1823. fieldType := field.Type()
  1824. if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {
  1825. t := currentField.Convert(timeType).Interface().(time.Time)
  1826. fieldTime := field.Convert(timeType).Interface().(time.Time)
  1827. return fieldTime.Before(t) || fieldTime.Equal(t)
  1828. }
  1829. // Not Same underlying type i.e. struct and time
  1830. if fieldType != currentField.Type() {
  1831. return false
  1832. }
  1833. }
  1834. // default reflect.String
  1835. return len(field.String()) <= len(currentField.String())
  1836. }
  1837. // isLtField is the validation function for validating if the current field's value is less than the field specified by the param's value.
  1838. func isLtField(fl FieldLevel) bool {
  1839. field := fl.Field()
  1840. kind := field.Kind()
  1841. currentField, currentKind, ok := fl.GetStructFieldOK()
  1842. if !ok || currentKind != kind {
  1843. return false
  1844. }
  1845. switch kind {
  1846. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1847. return field.Int() < currentField.Int()
  1848. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1849. return field.Uint() < currentField.Uint()
  1850. case reflect.Float32, reflect.Float64:
  1851. return field.Float() < currentField.Float()
  1852. case reflect.Struct:
  1853. fieldType := field.Type()
  1854. if fieldType.ConvertibleTo(timeType) && currentField.Type().ConvertibleTo(timeType) {
  1855. t := currentField.Convert(timeType).Interface().(time.Time)
  1856. fieldTime := field.Convert(timeType).Interface().(time.Time)
  1857. return fieldTime.Before(t)
  1858. }
  1859. // Not Same underlying type i.e. struct and time
  1860. if fieldType != currentField.Type() {
  1861. return false
  1862. }
  1863. }
  1864. // default reflect.String
  1865. return len(field.String()) < len(currentField.String())
  1866. }
  1867. // isLte is the validation function for validating if the current field's value is less than or equal to the param's value.
  1868. func isLte(fl FieldLevel) bool {
  1869. field := fl.Field()
  1870. param := fl.Param()
  1871. switch field.Kind() {
  1872. case reflect.String:
  1873. p := asInt(param)
  1874. return int64(utf8.RuneCountInString(field.String())) <= p
  1875. case reflect.Slice, reflect.Map, reflect.Array:
  1876. p := asInt(param)
  1877. return int64(field.Len()) <= p
  1878. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1879. p := asIntFromType(field.Type(), param)
  1880. return field.Int() <= p
  1881. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1882. p := asUint(param)
  1883. return field.Uint() <= p
  1884. case reflect.Float32:
  1885. p := asFloat32(param)
  1886. return field.Float() <= p
  1887. case reflect.Float64:
  1888. p := asFloat64(param)
  1889. return field.Float() <= p
  1890. case reflect.Struct:
  1891. if field.Type().ConvertibleTo(timeType) {
  1892. now := time.Now().UTC()
  1893. t := field.Convert(timeType).Interface().(time.Time)
  1894. return t.Before(now) || t.Equal(now)
  1895. }
  1896. }
  1897. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1898. }
  1899. // isLt is the validation function for validating if the current field's value is less than the param's value.
  1900. func isLt(fl FieldLevel) bool {
  1901. field := fl.Field()
  1902. param := fl.Param()
  1903. switch field.Kind() {
  1904. case reflect.String:
  1905. p := asInt(param)
  1906. return int64(utf8.RuneCountInString(field.String())) < p
  1907. case reflect.Slice, reflect.Map, reflect.Array:
  1908. p := asInt(param)
  1909. return int64(field.Len()) < p
  1910. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1911. p := asIntFromType(field.Type(), param)
  1912. return field.Int() < p
  1913. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1914. p := asUint(param)
  1915. return field.Uint() < p
  1916. case reflect.Float32:
  1917. p := asFloat32(param)
  1918. return field.Float() < p
  1919. case reflect.Float64:
  1920. p := asFloat64(param)
  1921. return field.Float() < p
  1922. case reflect.Struct:
  1923. if field.Type().ConvertibleTo(timeType) {
  1924. return field.Convert(timeType).Interface().(time.Time).Before(time.Now().UTC())
  1925. }
  1926. }
  1927. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  1928. }
  1929. // hasMaxOf is the validation function for validating if the current field's value is less than or equal to the param's value.
  1930. func hasMaxOf(fl FieldLevel) bool {
  1931. return isLte(fl)
  1932. }
  1933. // isTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
  1934. func isTCP4AddrResolvable(fl FieldLevel) bool {
  1935. if !isIP4Addr(fl) {
  1936. return false
  1937. }
  1938. _, err := net.ResolveTCPAddr("tcp4", fl.Field().String())
  1939. return err == nil
  1940. }
  1941. // isTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
  1942. func isTCP6AddrResolvable(fl FieldLevel) bool {
  1943. if !isIP6Addr(fl) {
  1944. return false
  1945. }
  1946. _, err := net.ResolveTCPAddr("tcp6", fl.Field().String())
  1947. return err == nil
  1948. }
  1949. // isTCPAddrResolvable is the validation function for validating if the field's value is a resolvable tcp address.
  1950. func isTCPAddrResolvable(fl FieldLevel) bool {
  1951. if !isIP4Addr(fl) && !isIP6Addr(fl) {
  1952. return false
  1953. }
  1954. _, err := net.ResolveTCPAddr("tcp", fl.Field().String())
  1955. return err == nil
  1956. }
  1957. // isUDP4AddrResolvable is the validation function for validating if the field's value is a resolvable udp4 address.
  1958. func isUDP4AddrResolvable(fl FieldLevel) bool {
  1959. if !isIP4Addr(fl) {
  1960. return false
  1961. }
  1962. _, err := net.ResolveUDPAddr("udp4", fl.Field().String())
  1963. return err == nil
  1964. }
  1965. // isUDP6AddrResolvable is the validation function for validating if the field's value is a resolvable udp6 address.
  1966. func isUDP6AddrResolvable(fl FieldLevel) bool {
  1967. if !isIP6Addr(fl) {
  1968. return false
  1969. }
  1970. _, err := net.ResolveUDPAddr("udp6", fl.Field().String())
  1971. return err == nil
  1972. }
  1973. // isUDPAddrResolvable is the validation function for validating if the field's value is a resolvable udp address.
  1974. func isUDPAddrResolvable(fl FieldLevel) bool {
  1975. if !isIP4Addr(fl) && !isIP6Addr(fl) {
  1976. return false
  1977. }
  1978. _, err := net.ResolveUDPAddr("udp", fl.Field().String())
  1979. return err == nil
  1980. }
  1981. // isIP4AddrResolvable is the validation function for validating if the field's value is a resolvable ip4 address.
  1982. func isIP4AddrResolvable(fl FieldLevel) bool {
  1983. if !isIPv4(fl) {
  1984. return false
  1985. }
  1986. _, err := net.ResolveIPAddr("ip4", fl.Field().String())
  1987. return err == nil
  1988. }
  1989. // isIP6AddrResolvable is the validation function for validating if the field's value is a resolvable ip6 address.
  1990. func isIP6AddrResolvable(fl FieldLevel) bool {
  1991. if !isIPv6(fl) {
  1992. return false
  1993. }
  1994. _, err := net.ResolveIPAddr("ip6", fl.Field().String())
  1995. return err == nil
  1996. }
  1997. // isIPAddrResolvable is the validation function for validating if the field's value is a resolvable ip address.
  1998. func isIPAddrResolvable(fl FieldLevel) bool {
  1999. if !isIP(fl) {
  2000. return false
  2001. }
  2002. _, err := net.ResolveIPAddr("ip", fl.Field().String())
  2003. return err == nil
  2004. }
  2005. // isUnixAddrResolvable is the validation function for validating if the field's value is a resolvable unix address.
  2006. func isUnixAddrResolvable(fl FieldLevel) bool {
  2007. _, err := net.ResolveUnixAddr("unix", fl.Field().String())
  2008. return err == nil
  2009. }
  2010. func isIP4Addr(fl FieldLevel) bool {
  2011. val := fl.Field().String()
  2012. if idx := strings.LastIndex(val, ":"); idx != -1 {
  2013. val = val[0:idx]
  2014. }
  2015. ip := net.ParseIP(val)
  2016. return ip != nil && ip.To4() != nil
  2017. }
  2018. func isIP6Addr(fl FieldLevel) bool {
  2019. val := fl.Field().String()
  2020. if idx := strings.LastIndex(val, ":"); idx != -1 {
  2021. if idx != 0 && val[idx-1:idx] == "]" {
  2022. val = val[1 : idx-1]
  2023. }
  2024. }
  2025. ip := net.ParseIP(val)
  2026. return ip != nil && ip.To4() == nil
  2027. }
  2028. func isHostnameRFC952(fl FieldLevel) bool {
  2029. return hostnameRegexRFC952().MatchString(fl.Field().String())
  2030. }
  2031. func isHostnameRFC1123(fl FieldLevel) bool {
  2032. return hostnameRegexRFC1123().MatchString(fl.Field().String())
  2033. }
  2034. func isFQDN(fl FieldLevel) bool {
  2035. val := fl.Field().String()
  2036. if val == "" {
  2037. return false
  2038. }
  2039. return fqdnRegexRFC1123().MatchString(val)
  2040. }
  2041. // isDir is the validation function for validating if the current field's value is a valid existing directory.
  2042. func isDir(fl FieldLevel) bool {
  2043. field := fl.Field()
  2044. if field.Kind() == reflect.String {
  2045. fileInfo, err := os.Stat(field.String())
  2046. if err != nil {
  2047. return false
  2048. }
  2049. return fileInfo.IsDir()
  2050. }
  2051. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2052. }
  2053. // isDirPath is the validation function for validating if the current field's value is a valid directory.
  2054. func isDirPath(fl FieldLevel) bool {
  2055. var exists bool
  2056. var err error
  2057. field := fl.Field()
  2058. // If it exists, it obviously is valid.
  2059. // This is done first to avoid code duplication and unnecessary additional logic.
  2060. if exists = isDir(fl); exists {
  2061. return true
  2062. }
  2063. // It does not exist but may still be a valid path.
  2064. switch field.Kind() {
  2065. case reflect.String:
  2066. // Every OS allows for whitespace, but none
  2067. // let you use a dir with no name (to my knowledge).
  2068. // Unless you're dealing with raw inodes, but I digress.
  2069. if strings.TrimSpace(field.String()) == "" {
  2070. return false
  2071. }
  2072. if _, err = os.Stat(field.String()); err != nil {
  2073. switch t := err.(type) {
  2074. case *fs.PathError:
  2075. if t.Err == syscall.EINVAL {
  2076. // It's definitely an invalid character in the path.
  2077. return false
  2078. }
  2079. // It could be a permission error, a does-not-exist error, etc.
  2080. // Out-of-scope for this validation, though.
  2081. // Lastly, we make sure it is a directory.
  2082. if strings.HasSuffix(field.String(), string(os.PathSeparator)) {
  2083. return true
  2084. } else {
  2085. return false
  2086. }
  2087. default:
  2088. // Something went *seriously* wrong.
  2089. /*
  2090. Per https://pkg.go.dev/os#Stat:
  2091. "If there is an error, it will be of type *PathError."
  2092. */
  2093. panic(err)
  2094. }
  2095. }
  2096. // We repeat the check here to make sure it is an explicit directory in case the above os.Stat didn't trigger an error.
  2097. if strings.HasSuffix(field.String(), string(os.PathSeparator)) {
  2098. return true
  2099. } else {
  2100. return false
  2101. }
  2102. }
  2103. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2104. }
  2105. // isJSON is the validation function for validating if the current field's value is a valid json string.
  2106. func isJSON(fl FieldLevel) bool {
  2107. field := fl.Field()
  2108. switch field.Kind() {
  2109. case reflect.String:
  2110. val := field.String()
  2111. return json.Valid([]byte(val))
  2112. case reflect.Slice:
  2113. fieldType := field.Type()
  2114. if fieldType.ConvertibleTo(byteSliceType) {
  2115. b := field.Convert(byteSliceType).Interface().([]byte)
  2116. return json.Valid(b)
  2117. }
  2118. }
  2119. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2120. }
  2121. // isJWT is the validation function for validating if the current field's value is a valid JWT string.
  2122. func isJWT(fl FieldLevel) bool {
  2123. return jWTRegex().MatchString(fl.Field().String())
  2124. }
  2125. // isHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
  2126. func isHostnamePort(fl FieldLevel) bool {
  2127. val := fl.Field().String()
  2128. host, port, err := net.SplitHostPort(val)
  2129. if err != nil {
  2130. return false
  2131. }
  2132. // Port must be a iny <= 65535.
  2133. if portNum, err := strconv.ParseInt(
  2134. port, 10, 32,
  2135. ); err != nil || portNum > 65535 || portNum < 1 {
  2136. return false
  2137. }
  2138. // If host is specified, it should match a DNS name
  2139. if host != "" {
  2140. return hostnameRegexRFC1123().MatchString(host)
  2141. }
  2142. return true
  2143. }
  2144. // IsPort validates if the current field's value represents a valid port
  2145. func isPort(fl FieldLevel) bool {
  2146. val := fl.Field().Uint()
  2147. return val >= 1 && val <= 65535
  2148. }
  2149. // isLowercase is the validation function for validating if the current field's value is a lowercase string.
  2150. func isLowercase(fl FieldLevel) bool {
  2151. field := fl.Field()
  2152. if field.Kind() == reflect.String {
  2153. if field.String() == "" {
  2154. return false
  2155. }
  2156. return field.String() == strings.ToLower(field.String())
  2157. }
  2158. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2159. }
  2160. // isUppercase is the validation function for validating if the current field's value is an uppercase string.
  2161. func isUppercase(fl FieldLevel) bool {
  2162. field := fl.Field()
  2163. if field.Kind() == reflect.String {
  2164. if field.String() == "" {
  2165. return false
  2166. }
  2167. return field.String() == strings.ToUpper(field.String())
  2168. }
  2169. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2170. }
  2171. // isDatetime is the validation function for validating if the current field's value is a valid datetime string.
  2172. func isDatetime(fl FieldLevel) bool {
  2173. field := fl.Field()
  2174. param := fl.Param()
  2175. if field.Kind() == reflect.String {
  2176. _, err := time.Parse(param, field.String())
  2177. return err == nil
  2178. }
  2179. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2180. }
  2181. // isTimeZone is the validation function for validating if the current field's value is a valid time zone string.
  2182. func isTimeZone(fl FieldLevel) bool {
  2183. field := fl.Field()
  2184. if field.Kind() == reflect.String {
  2185. // empty value is converted to UTC by time.LoadLocation but disallow it as it is not a valid time zone name
  2186. if field.String() == "" {
  2187. return false
  2188. }
  2189. // Local value is converted to the current system time zone by time.LoadLocation but disallow it as it is not a valid time zone name
  2190. if strings.ToLower(field.String()) == "local" {
  2191. return false
  2192. }
  2193. _, err := time.LoadLocation(field.String())
  2194. return err == nil
  2195. }
  2196. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2197. }
  2198. // isIso3166Alpha2 is the validation function for validating if the current field's value is a valid iso3166-1 alpha-2 country code.
  2199. func isIso3166Alpha2(fl FieldLevel) bool {
  2200. _, ok := iso3166_1_alpha2[fl.Field().String()]
  2201. return ok
  2202. }
  2203. // isIso3166Alpha2EU is the validation function for validating if the current field's value is a valid iso3166-1 alpha-2 European Union country code.
  2204. func isIso3166Alpha2EU(fl FieldLevel) bool {
  2205. _, ok := iso3166_1_alpha2_eu[fl.Field().String()]
  2206. return ok
  2207. }
  2208. // isIso3166Alpha3 is the validation function for validating if the current field's value is a valid iso3166-1 alpha-3 country code.
  2209. func isIso3166Alpha3(fl FieldLevel) bool {
  2210. _, ok := iso3166_1_alpha3[fl.Field().String()]
  2211. return ok
  2212. }
  2213. // isIso3166Alpha3EU is the validation function for validating if the current field's value is a valid iso3166-1 alpha-3 European Union country code.
  2214. func isIso3166Alpha3EU(fl FieldLevel) bool {
  2215. _, ok := iso3166_1_alpha3_eu[fl.Field().String()]
  2216. return ok
  2217. }
  2218. // isIso3166AlphaNumeric is the validation function for validating if the current field's value is a valid iso3166-1 alpha-numeric country code.
  2219. func isIso3166AlphaNumeric(fl FieldLevel) bool {
  2220. field := fl.Field()
  2221. var code int
  2222. switch field.Kind() {
  2223. case reflect.String:
  2224. i, err := strconv.Atoi(field.String())
  2225. if err != nil {
  2226. return false
  2227. }
  2228. code = i % 1000
  2229. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  2230. code = int(field.Int() % 1000)
  2231. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  2232. code = int(field.Uint() % 1000)
  2233. default:
  2234. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2235. }
  2236. _, ok := iso3166_1_alpha_numeric[code]
  2237. return ok
  2238. }
  2239. // isIso3166AlphaNumericEU is the validation function for validating if the current field's value is a valid iso3166-1 alpha-numeric European Union country code.
  2240. func isIso3166AlphaNumericEU(fl FieldLevel) bool {
  2241. field := fl.Field()
  2242. var code int
  2243. switch field.Kind() {
  2244. case reflect.String:
  2245. i, err := strconv.Atoi(field.String())
  2246. if err != nil {
  2247. return false
  2248. }
  2249. code = i % 1000
  2250. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  2251. code = int(field.Int() % 1000)
  2252. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  2253. code = int(field.Uint() % 1000)
  2254. default:
  2255. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2256. }
  2257. _, ok := iso3166_1_alpha_numeric_eu[code]
  2258. return ok
  2259. }
  2260. // isIso31662 is the validation function for validating if the current field's value is a valid iso3166-2 code.
  2261. func isIso31662(fl FieldLevel) bool {
  2262. _, ok := iso3166_2[fl.Field().String()]
  2263. return ok
  2264. }
  2265. // isIso4217 is the validation function for validating if the current field's value is a valid iso4217 currency code.
  2266. func isIso4217(fl FieldLevel) bool {
  2267. _, ok := iso4217[fl.Field().String()]
  2268. return ok
  2269. }
  2270. // isIso4217Numeric is the validation function for validating if the current field's value is a valid iso4217 numeric currency code.
  2271. func isIso4217Numeric(fl FieldLevel) bool {
  2272. field := fl.Field()
  2273. var code int
  2274. switch field.Kind() {
  2275. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  2276. code = int(field.Int())
  2277. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  2278. code = int(field.Uint())
  2279. default:
  2280. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2281. }
  2282. _, ok := iso4217_numeric[code]
  2283. return ok
  2284. }
  2285. // isBCP47LanguageTag is the validation function for validating if the current field's value is a valid BCP 47 language tag, as parsed by language.Parse
  2286. func isBCP47LanguageTag(fl FieldLevel) bool {
  2287. field := fl.Field()
  2288. if field.Kind() == reflect.String {
  2289. _, err := language.Parse(field.String())
  2290. return err == nil
  2291. }
  2292. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2293. }
  2294. // isIsoBicFormat is the validation function for validating if the current field's value is a valid Business Identifier Code (SWIFT code), defined in ISO 9362
  2295. func isIsoBicFormat(fl FieldLevel) bool {
  2296. bicString := fl.Field().String()
  2297. return bicRegex().MatchString(bicString)
  2298. }
  2299. // isSemverFormat is the validation function for validating if the current field's value is a valid semver version, defined in Semantic Versioning 2.0.0
  2300. func isSemverFormat(fl FieldLevel) bool {
  2301. semverString := fl.Field().String()
  2302. return semverRegex().MatchString(semverString)
  2303. }
  2304. // isCveFormat is the validation function for validating if the current field's value is a valid cve id, defined in CVE mitre org
  2305. func isCveFormat(fl FieldLevel) bool {
  2306. cveString := fl.Field().String()
  2307. return cveRegex().MatchString(cveString)
  2308. }
  2309. // isDnsRFC1035LabelFormat is the validation function
  2310. // for validating if the current field's value is
  2311. // a valid dns RFC 1035 label, defined in RFC 1035.
  2312. func isDnsRFC1035LabelFormat(fl FieldLevel) bool {
  2313. val := fl.Field().String()
  2314. size := len(val)
  2315. if size > 63 {
  2316. return false
  2317. }
  2318. return dnsRegexRFC1035Label().MatchString(val)
  2319. }
  2320. // digitsHaveLuhnChecksum returns true if and only if the last element of the given digits slice is the Luhn checksum of the previous elements
  2321. func digitsHaveLuhnChecksum(digits []string) bool {
  2322. size := len(digits)
  2323. sum := 0
  2324. for i, digit := range digits {
  2325. value, err := strconv.Atoi(digit)
  2326. if err != nil {
  2327. return false
  2328. }
  2329. if size%2 == 0 && i%2 == 0 || size%2 == 1 && i%2 == 1 {
  2330. v := value * 2
  2331. if v >= 10 {
  2332. sum += 1 + (v % 10)
  2333. } else {
  2334. sum += v
  2335. }
  2336. } else {
  2337. sum += value
  2338. }
  2339. }
  2340. return (sum % 10) == 0
  2341. }
  2342. // isMongoDBObjectId is the validation function for validating if the current field's value is valid MongoDB ObjectID
  2343. func isMongoDBObjectId(fl FieldLevel) bool {
  2344. val := fl.Field().String()
  2345. return mongodbIdRegex().MatchString(val)
  2346. }
  2347. // isMongoDBConnectionString is the validation function for validating if the current field's value is valid MongoDB Connection String
  2348. func isMongoDBConnectionString(fl FieldLevel) bool {
  2349. val := fl.Field().String()
  2350. return mongodbConnectionRegex().MatchString(val)
  2351. }
  2352. // isSpiceDB is the validation function for validating if the current field's value is valid for use with Authzed SpiceDB in the indicated way
  2353. func isSpiceDB(fl FieldLevel) bool {
  2354. val := fl.Field().String()
  2355. param := fl.Param()
  2356. switch param {
  2357. case "permission":
  2358. return spicedbPermissionRegex().MatchString(val)
  2359. case "type":
  2360. return spicedbTypeRegex().MatchString(val)
  2361. case "id", "":
  2362. return spicedbIDRegex().MatchString(val)
  2363. }
  2364. panic("Unrecognized parameter: " + param)
  2365. }
  2366. // isCreditCard is the validation function for validating if the current field's value is a valid credit card number
  2367. func isCreditCard(fl FieldLevel) bool {
  2368. val := fl.Field().String()
  2369. var creditCard bytes.Buffer
  2370. segments := strings.Split(val, " ")
  2371. for _, segment := range segments {
  2372. if len(segment) < 3 {
  2373. return false
  2374. }
  2375. creditCard.WriteString(segment)
  2376. }
  2377. ccDigits := strings.Split(creditCard.String(), "")
  2378. size := len(ccDigits)
  2379. if size < 12 || size > 19 {
  2380. return false
  2381. }
  2382. return digitsHaveLuhnChecksum(ccDigits)
  2383. }
  2384. // hasLuhnChecksum is the validation for validating if the current field's value has a valid Luhn checksum
  2385. func hasLuhnChecksum(fl FieldLevel) bool {
  2386. field := fl.Field()
  2387. var str string // convert to a string which will then be split into single digits; easier and more readable than shifting/extracting single digits from a number
  2388. switch field.Kind() {
  2389. case reflect.String:
  2390. str = field.String()
  2391. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  2392. str = strconv.FormatInt(field.Int(), 10)
  2393. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  2394. str = strconv.FormatUint(field.Uint(), 10)
  2395. default:
  2396. panic(fmt.Sprintf("Bad field type %T", field.Interface()))
  2397. }
  2398. size := len(str)
  2399. if size < 2 { // there has to be at least one digit that carries a meaning + the checksum
  2400. return false
  2401. }
  2402. digits := strings.Split(str, "")
  2403. return digitsHaveLuhnChecksum(digits)
  2404. }
  2405. // isCron is the validation function for validating if the current field's value is a valid cron expression
  2406. func isCron(fl FieldLevel) bool {
  2407. cronString := fl.Field().String()
  2408. return cronRegex().MatchString(cronString)
  2409. }
  2410. // isEIN is the validation function for validating if the current field's value is a valid U.S. Employer Identification Number (EIN)
  2411. func isEIN(fl FieldLevel) bool {
  2412. field := fl.Field()
  2413. if field.Len() != 10 {
  2414. return false
  2415. }
  2416. return einRegex().MatchString(field.String())
  2417. }