123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410 |
- package validator
- import (
- "fmt"
- "net"
- "net/url"
- "reflect"
- "strings"
- "time"
- "unicode/utf8"
- )
- // BakedInAliasValidators is a default mapping of a single validationstag that
- // defines a common or complex set of validation(s) to simplify
- // adding validation to structs. i.e. set key "_ageok" and the tags
- // are "gt=0,lte=130" or key "_preferredname" and tags "omitempty,gt=0,lte=60"
- var bakedInAliasValidators = map[string]string{
- "iscolor": "hexcolor|rgb|rgba|hsl|hsla",
- }
- // BakedInValidators is the default map of ValidationFunc
- // you can add, remove or even replace items to suite your needs,
- // or even disregard and use your own map if so desired.
- var bakedInValidators = map[string]Func{
- "required": HasValue,
- "len": HasLengthOf,
- "min": HasMinOf,
- "max": HasMaxOf,
- "eq": IsEq,
- "ne": IsNe,
- "lt": IsLt,
- "lte": IsLte,
- "gt": IsGt,
- "gte": IsGte,
- "eqfield": IsEqField,
- "eqcsfield": IsEqCrossStructField,
- "necsfield": IsNeCrossStructField,
- "gtcsfield": IsGtCrossStructField,
- "gtecsfield": IsGteCrossStructField,
- "ltcsfield": IsLtCrossStructField,
- "ltecsfield": IsLteCrossStructField,
- "nefield": IsNeField,
- "gtefield": IsGteField,
- "gtfield": IsGtField,
- "ltefield": IsLteField,
- "ltfield": IsLtField,
- "alpha": IsAlpha,
- "alphanum": IsAlphanum,
- "numeric": IsNumeric,
- "number": IsNumber,
- "hexadecimal": IsHexadecimal,
- "hexcolor": IsHEXColor,
- "rgb": IsRGB,
- "rgba": IsRGBA,
- "hsl": IsHSL,
- "hsla": IsHSLA,
- "email": IsEmail,
- "url": IsURL,
- "uri": IsURI,
- "base64": IsBase64,
- "contains": Contains,
- "containsany": ContainsAny,
- "containsrune": ContainsRune,
- "excludes": Excludes,
- "excludesall": ExcludesAll,
- "excludesrune": ExcludesRune,
- "isbn": IsISBN,
- "isbn10": IsISBN10,
- "isbn13": IsISBN13,
- "uuid": IsUUID,
- "uuid3": IsUUID3,
- "uuid4": IsUUID4,
- "uuid5": IsUUID5,
- "ascii": IsASCII,
- "printascii": IsPrintableASCII,
- "multibyte": HasMultiByteCharacter,
- "datauri": IsDataURI,
- "latitude": IsLatitude,
- "longitude": IsLongitude,
- "ssn": IsSSN,
- "ipv4": IsIPv4,
- "ipv6": IsIPv6,
- "ip": IsIP,
- "cidrv4": IsCIDRv4,
- "cidrv6": IsCIDRv6,
- "cidr": IsCIDR,
- "tcp4_addr": IsTCP4AddrResolvable,
- "tcp6_addr": IsTCP6AddrResolvable,
- "tcp_addr": IsTCPAddrResolvable,
- "udp4_addr": IsUDP4AddrResolvable,
- "udp6_addr": IsUDP6AddrResolvable,
- "udp_addr": IsUDPAddrResolvable,
- "ip4_addr": IsIP4AddrResolvable,
- "ip6_addr": IsIP6AddrResolvable,
- "ip_addr": IsIPAddrResolvable,
- "unix_addr": IsUnixAddrResolvable,
- "mac": IsMAC,
- }
- // IsMAC is the validation function for validating if the field's value is a valid MAC address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsMAC(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- _, err := net.ParseMAC(field.String())
- return err == nil
- }
- // IsCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsCIDRv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- ip, _, err := net.ParseCIDR(field.String())
- return err == nil && ip.To4() != nil
- }
- // IsCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsCIDRv6(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- ip, _, err := net.ParseCIDR(field.String())
- return err == nil && ip.To4() == nil
- }
- // IsCIDR is the validation function for validating if the field's value is a valid v4 or v6 CIDR address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsCIDR(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- _, _, err := net.ParseCIDR(field.String())
- return err == nil
- }
- // IsIPv4 is the validation function for validating if a value is a valid v4 IP address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsIPv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- ip := net.ParseIP(field.String())
- return ip != nil && ip.To4() != nil
- }
- // IsIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsIPv6(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- ip := net.ParseIP(field.String())
- return ip != nil && ip.To4() == nil
- }
- // IsIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsIP(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- ip := net.ParseIP(field.String())
- return ip != nil
- }
- // IsSSN is the validation function for validating if the field's value is a valid SSN.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsSSN(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if field.Len() != 11 {
- return false
- }
- return sSNRegex.MatchString(field.String())
- }
- // IsLongitude is the validation function for validating if the field's value is a valid longitude coordinate.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLongitude(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return longitudeRegex.MatchString(field.String())
- }
- // IsLatitude is the validation function for validating if the field's value is a valid latitude coordinate.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLatitude(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return latitudeRegex.MatchString(field.String())
- }
- // IsDataURI is the validation function for validating if the field's value is a valid data URI.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsDataURI(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- uri := strings.SplitN(field.String(), ",", 2)
- if len(uri) != 2 {
- return false
- }
- if !dataURIRegex.MatchString(uri[0]) {
- return false
- }
- fld := reflect.ValueOf(uri[1])
- return IsBase64(v, topStruct, currentStructOrField, fld, fld.Type(), fld.Kind(), param)
- }
- // HasMultiByteCharacter is the validation function for validating if the field's value has a multi byte character.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func HasMultiByteCharacter(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if field.Len() == 0 {
- return true
- }
- return multibyteRegex.MatchString(field.String())
- }
- // IsPrintableASCII is the validation function for validating if the field's value is a valid printable ASCII character.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsPrintableASCII(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return printableASCIIRegex.MatchString(field.String())
- }
- // IsASCII is the validation function for validating if the field's value is a valid ASCII character.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsASCII(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return aSCIIRegex.MatchString(field.String())
- }
- // IsUUID5 is the validation function for validating if the field's value is a valid v5 UUID.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUUID5(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return uUID5Regex.MatchString(field.String())
- }
- // IsUUID4 is the validation function for validating if the field's value is a valid v4 UUID.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUUID4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return uUID4Regex.MatchString(field.String())
- }
- // IsUUID3 is the validation function for validating if the field's value is a valid v3 UUID.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUUID3(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return uUID3Regex.MatchString(field.String())
- }
- // IsUUID is the validation function for validating if the field's value is a valid UUID of any version.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUUID(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return uUIDRegex.MatchString(field.String())
- }
- // IsISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsISBN(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return IsISBN10(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) || IsISBN13(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // IsISBN13 is the validation function for validating if the field's value is a valid v13 ISBN.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsISBN13(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- s := strings.Replace(strings.Replace(field.String(), "-", "", 4), " ", "", 4)
- if !iSBN13Regex.MatchString(s) {
- return false
- }
- var checksum int32
- var i int32
- factor := []int32{1, 3}
- for i = 0; i < 12; i++ {
- checksum += factor[i%2] * int32(s[i]-'0')
- }
- return (int32(s[12]-'0'))-((10-(checksum%10))%10) == 0
- }
- // IsISBN10 is the validation function for validating if the field's value is a valid v10 ISBN.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsISBN10(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- s := strings.Replace(strings.Replace(field.String(), "-", "", 3), " ", "", 3)
- if !iSBN10Regex.MatchString(s) {
- return false
- }
- var checksum int32
- var i int32
- for i = 0; i < 9; i++ {
- checksum += (i + 1) * int32(s[i]-'0')
- }
- if s[9] == 'X' {
- checksum += 10 * 10
- } else {
- checksum += 10 * int32(s[9]-'0')
- }
- return checksum%11 == 0
- }
- // ExcludesRune is the validation function for validating that the field's value does not contain the rune specified within the param.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func ExcludesRune(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return !ContainsRune(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // ExcludesAll is the validation function for validating that the field's value does not contain any of the characters specified within the param.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func ExcludesAll(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return !ContainsAny(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // Excludes is the validation function for validating that the field's value does not contain the text specified within the param.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func Excludes(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return !Contains(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // ContainsRune is the validation function for validating that the field's value contains the rune specified within the param.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func ContainsRune(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- r, _ := utf8.DecodeRuneInString(param)
- return strings.ContainsRune(field.String(), r)
- }
- // ContainsAny is the validation function for validating that the field's value contains any of the characters specified within the param.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func ContainsAny(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return strings.ContainsAny(field.String(), param)
- }
- // Contains is the validation function for validating that the field's value contains the text specified within the param.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func Contains(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return strings.Contains(field.String(), param)
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsNeField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
- if !ok || currentKind != fieldKind {
- return true
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() != currentField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() != currentField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() != currentField.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(field.Len()) != int64(currentField.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != currentField.Type() {
- return true
- }
- if fieldType == timeType {
- t := currentField.Interface().(time.Time)
- fieldTime := field.Interface().(time.Time)
- return !fieldTime.Equal(t)
- }
- }
- // default reflect.String:
- return field.String() != currentField.String()
- }
- // IsNe is the validation function for validating that the field's value does not equal the provided param value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsNe(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return !IsEq(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
- if !ok || topKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() <= topField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() <= topField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() <= topField.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(field.Len()) <= int64(topField.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != topField.Type() {
- return false
- }
- if fieldType == timeType {
- fieldTime := field.Interface().(time.Time)
- topTime := topField.Interface().(time.Time)
- return fieldTime.Before(topTime) || fieldTime.Equal(topTime)
- }
- }
- // default reflect.String:
- return field.String() <= topField.String()
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
- if !ok || topKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() < topField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() < topField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() < topField.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(field.Len()) < int64(topField.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != topField.Type() {
- return false
- }
- if fieldType == timeType {
- fieldTime := field.Interface().(time.Time)
- topTime := topField.Interface().(time.Time)
- return fieldTime.Before(topTime)
- }
- }
- // default reflect.String:
- return field.String() < topField.String()
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsGteCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
- if !ok || topKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() >= topField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() >= topField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() >= topField.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(field.Len()) >= int64(topField.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != topField.Type() {
- return false
- }
- if fieldType == timeType {
- fieldTime := field.Interface().(time.Time)
- topTime := topField.Interface().(time.Time)
- return fieldTime.After(topTime) || fieldTime.Equal(topTime)
- }
- }
- // default reflect.String:
- return field.String() >= topField.String()
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsGtCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
- if !ok || topKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() > topField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() > topField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() > topField.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(field.Len()) > int64(topField.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != topField.Type() {
- return false
- }
- if fieldType == timeType {
- fieldTime := field.Interface().(time.Time)
- topTime := topField.Interface().(time.Time)
- return fieldTime.After(topTime)
- }
- }
- // default reflect.String:
- return field.String() > topField.String()
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsNeCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- topField, currentKind, ok := v.GetStructFieldOK(topStruct, param)
- if !ok || currentKind != fieldKind {
- return true
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return topField.Int() != field.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return topField.Uint() != field.Uint()
- case reflect.Float32, reflect.Float64:
- return topField.Float() != field.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(topField.Len()) != int64(field.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != topField.Type() {
- return true
- }
- if fieldType == timeType {
- t := field.Interface().(time.Time)
- fieldTime := topField.Interface().(time.Time)
- return !fieldTime.Equal(t)
- }
- }
- // default reflect.String:
- return topField.String() != field.String()
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsEqCrossStructField(v *Validate, topStruct reflect.Value, current reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- topField, topKind, ok := v.GetStructFieldOK(topStruct, param)
- if !ok || topKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return topField.Int() == field.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return topField.Uint() == field.Uint()
- case reflect.Float32, reflect.Float64:
- return topField.Float() == field.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(topField.Len()) == int64(field.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != topField.Type() {
- return false
- }
- if fieldType == timeType {
- t := field.Interface().(time.Time)
- fieldTime := topField.Interface().(time.Time)
- return fieldTime.Equal(t)
- }
- }
- // default reflect.String:
- return topField.String() == field.String()
- }
- // IsEqField is the validation function for validating if the current field's value is equal to the field specified by the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsEqField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
- if !ok || currentKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() == currentField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() == currentField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() == currentField.Float()
- case reflect.Slice, reflect.Map, reflect.Array:
- return int64(field.Len()) == int64(currentField.Len())
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != currentField.Type() {
- return false
- }
- if fieldType == timeType {
- t := currentField.Interface().(time.Time)
- fieldTime := field.Interface().(time.Time)
- return fieldTime.Equal(t)
- }
- }
- // default reflect.String:
- return field.String() == currentField.String()
- }
- // IsEq is the validation function for validating if the current field's value is equal to the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsEq(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- return field.String() == param
- case reflect.Slice, reflect.Map, reflect.Array:
- p := asInt(param)
- return int64(field.Len()) == p
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- p := asInt(param)
- return field.Int() == p
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- p := asUint(param)
- return field.Uint() == p
- case reflect.Float32, reflect.Float64:
- p := asFloat(param)
- return field.Float() == p
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // IsBase64 is the validation function for validating if the current field's value is a valid base 64.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsBase64(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return base64Regex.MatchString(field.String())
- }
- // IsURI is the validation function for validating if the current field's value is a valid URI.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsURI(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- s := field.String()
- // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
- // emulate browser and strip the '#' suffix prior to validation. see issue-#237
- if i := strings.Index(s, "#"); i > -1 {
- s = s[:i]
- }
- if s == blank {
- return false
- }
- _, err := url.ParseRequestURI(s)
- return err == nil
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // IsURL is the validation function for validating if the current field's value is a valid URL.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsURL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- var i int
- s := field.String()
- // checks needed as of Go 1.6 because of change https://github.com/golang/go/commit/617c93ce740c3c3cc28cdd1a0d712be183d0b328#diff-6c2d018290e298803c0c9419d8739885L195
- // emulate browser and strip the '#' suffix prior to validation. see issue-#237
- if i = strings.Index(s, "#"); i > -1 {
- s = s[:i]
- }
- if s == blank {
- return false
- }
- url, err := url.ParseRequestURI(s)
- if err != nil || url.Scheme == blank {
- return false
- }
- return err == nil
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // IsEmail is the validation function for validating if the current field's value is a valid email address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsEmail(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return emailRegex.MatchString(field.String())
- }
- // IsHSLA is the validation function for validating if the current field's value is a valid HSLA color.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsHSLA(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return hslaRegex.MatchString(field.String())
- }
- // IsHSL is the validation function for validating if the current field's value is a valid HSL color.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsHSL(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return hslRegex.MatchString(field.String())
- }
- // IsRGBA is the validation function for validating if the current field's value is a valid RGBA color.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsRGBA(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return rgbaRegex.MatchString(field.String())
- }
- // IsRGB is the validation function for validating if the current field's value is a valid RGB color.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsRGB(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return rgbRegex.MatchString(field.String())
- }
- // IsHEXColor is the validation function for validating if the current field's value is a valid HEX color.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsHEXColor(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return hexcolorRegex.MatchString(field.String())
- }
- // IsHexadecimal is the validation function for validating if the current field's value is a valid hexadecimal.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsHexadecimal(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return hexadecimalRegex.MatchString(field.String())
- }
- // IsNumber is the validation function for validating if the current field's value is a valid number.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsNumber(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return numberRegex.MatchString(field.String())
- }
- // IsNumeric is the validation function for validating if the current field's value is a valid numeric value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsNumeric(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return numericRegex.MatchString(field.String())
- }
- // IsAlphanum is the validation function for validating if the current field's value is a valid alphanumeric value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsAlphanum(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return alphaNumericRegex.MatchString(field.String())
- }
- // IsAlpha is the validation function for validating if the current field's value is a valid alpha value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsAlpha(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return alphaRegex.MatchString(field.String())
- }
- // HasValue is the validation function for validating if the current field's value is not the default static value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func HasValue(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
- return !field.IsNil()
- default:
- return field.IsValid() && field.Interface() != reflect.Zero(fieldType).Interface()
- }
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsGteField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
- if !ok || currentKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() >= currentField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() >= currentField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() >= currentField.Float()
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != currentField.Type() {
- return false
- }
- if fieldType == timeType {
- t := currentField.Interface().(time.Time)
- fieldTime := field.Interface().(time.Time)
- return fieldTime.After(t) || fieldTime.Equal(t)
- }
- }
- // default reflect.String
- return len(field.String()) >= len(currentField.String())
- }
- // IsGtField is the validation function for validating if the current field's value is greater than the field specified by the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsGtField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
- if !ok || currentKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() > currentField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() > currentField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() > currentField.Float()
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != currentField.Type() {
- return false
- }
- if fieldType == timeType {
- t := currentField.Interface().(time.Time)
- fieldTime := field.Interface().(time.Time)
- return fieldTime.After(t)
- }
- }
- // default reflect.String
- return len(field.String()) > len(currentField.String())
- }
- // IsGte is the validation function for validating if the current field's value is greater than or equal to the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsGte(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- p := asInt(param)
- return int64(utf8.RuneCountInString(field.String())) >= p
- case reflect.Slice, reflect.Map, reflect.Array:
- p := asInt(param)
- return int64(field.Len()) >= p
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- p := asInt(param)
- return field.Int() >= p
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- p := asUint(param)
- return field.Uint() >= p
- case reflect.Float32, reflect.Float64:
- p := asFloat(param)
- return field.Float() >= p
- case reflect.Struct:
- if fieldType == timeType || fieldType == timePtrType {
- now := time.Now().UTC()
- t := field.Interface().(time.Time)
- return t.After(now) || t.Equal(now)
- }
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // IsGt is the validation function for validating if the current field's value is greater than the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsGt(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- p := asInt(param)
- return int64(utf8.RuneCountInString(field.String())) > p
- case reflect.Slice, reflect.Map, reflect.Array:
- p := asInt(param)
- return int64(field.Len()) > p
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- p := asInt(param)
- return field.Int() > p
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- p := asUint(param)
- return field.Uint() > p
- case reflect.Float32, reflect.Float64:
- p := asFloat(param)
- return field.Float() > p
- case reflect.Struct:
- if fieldType == timeType || fieldType == timePtrType {
- return field.Interface().(time.Time).After(time.Now().UTC())
- }
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // HasLengthOf is the validation function for validating if the current field's value is equal to the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func HasLengthOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- p := asInt(param)
- return int64(utf8.RuneCountInString(field.String())) == p
- case reflect.Slice, reflect.Map, reflect.Array:
- p := asInt(param)
- return int64(field.Len()) == p
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- p := asInt(param)
- return field.Int() == p
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- p := asUint(param)
- return field.Uint() == p
- case reflect.Float32, reflect.Float64:
- p := asFloat(param)
- return field.Float() == p
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // HasMinOf is the validation function for validating if the current field's value is greater than or equal to the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func HasMinOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return IsGte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // 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.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLteField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
- if !ok || currentKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() <= currentField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() <= currentField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() <= currentField.Float()
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != currentField.Type() {
- return false
- }
- if fieldType == timeType {
- t := currentField.Interface().(time.Time)
- fieldTime := field.Interface().(time.Time)
- return fieldTime.Before(t) || fieldTime.Equal(t)
- }
- }
- // default reflect.String
- return len(field.String()) <= len(currentField.String())
- }
- // IsLtField is the validation function for validating if the current field's value is less than the field specified by the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLtField(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- currentField, currentKind, ok := v.GetStructFieldOK(currentStructOrField, param)
- if !ok || currentKind != fieldKind {
- return false
- }
- switch fieldKind {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return field.Int() < currentField.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return field.Uint() < currentField.Uint()
- case reflect.Float32, reflect.Float64:
- return field.Float() < currentField.Float()
- case reflect.Struct:
- // Not Same underlying type i.e. struct and time
- if fieldType != currentField.Type() {
- return false
- }
- if fieldType == timeType {
- t := currentField.Interface().(time.Time)
- fieldTime := field.Interface().(time.Time)
- return fieldTime.Before(t)
- }
- }
- // default reflect.String
- return len(field.String()) < len(currentField.String())
- }
- // IsLte is the validation function for validating if the current field's value is less than or equal to the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLte(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- p := asInt(param)
- return int64(utf8.RuneCountInString(field.String())) <= p
- case reflect.Slice, reflect.Map, reflect.Array:
- p := asInt(param)
- return int64(field.Len()) <= p
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- p := asInt(param)
- return field.Int() <= p
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- p := asUint(param)
- return field.Uint() <= p
- case reflect.Float32, reflect.Float64:
- p := asFloat(param)
- return field.Float() <= p
- case reflect.Struct:
- if fieldType == timeType || fieldType == timePtrType {
- now := time.Now().UTC()
- t := field.Interface().(time.Time)
- return t.Before(now) || t.Equal(now)
- }
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // IsLt is the validation function for validating if the current field's value is less than the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsLt(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- switch fieldKind {
- case reflect.String:
- p := asInt(param)
- return int64(utf8.RuneCountInString(field.String())) < p
- case reflect.Slice, reflect.Map, reflect.Array:
- p := asInt(param)
- return int64(field.Len()) < p
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- p := asInt(param)
- return field.Int() < p
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- p := asUint(param)
- return field.Uint() < p
- case reflect.Float32, reflect.Float64:
- p := asFloat(param)
- return field.Float() < p
- case reflect.Struct:
- if fieldType == timeType || fieldType == timePtrType {
- return field.Interface().(time.Time).Before(time.Now().UTC())
- }
- }
- panic(fmt.Sprintf("Bad field type %T", field.Interface()))
- }
- // HasMaxOf is the validation function for validating if the current field's value is less than or equal to the param's value.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func HasMaxOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- return IsLte(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param)
- }
- // IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsTCP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveTCPAddr("tcp4", field.String())
- return err == nil
- }
- // IsTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsTCP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveTCPAddr("tcp6", field.String())
- return err == nil
- }
- // IsTCPAddrResolvable is the validation function for validating if the field's value is a resolvable tcp address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsTCPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) &&
- !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveTCPAddr("tcp", field.String())
- return err == nil
- }
- // IsUDP4AddrResolvable is the validation function for validating if the field's value is a resolvable udp4 address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUDP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveUDPAddr("udp4", field.String())
- return err == nil
- }
- // IsUDP6AddrResolvable is the validation function for validating if the field's value is a resolvable udp6 address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUDP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveUDPAddr("udp6", field.String())
- return err == nil
- }
- // IsUDPAddrResolvable is the validation function for validating if the field's value is a resolvable udp address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUDPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !isIP4Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) &&
- !isIP6Addr(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveUDPAddr("udp", field.String())
- return err == nil
- }
- // IsIP4AddrResolvable is the validation function for validating if the field's value is a resolvable ip4 address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsIP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !IsIPv4(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveIPAddr("ip4", field.String())
- return err == nil
- }
- // IsIP6AddrResolvable is the validation function for validating if the field's value is a resolvable ip6 address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsIP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !IsIPv6(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveIPAddr("ip6", field.String())
- return err == nil
- }
- // IsIPAddrResolvable is the validation function for validating if the field's value is a resolvable ip address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsIPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- if !IsIP(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
- return false
- }
- _, err := net.ResolveIPAddr("ip", field.String())
- return err == nil
- }
- // IsUnixAddrResolvable is the validation function for validating if the field's value is a resolvable unix address.
- // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
- func IsUnixAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- _, err := net.ResolveUnixAddr("unix", field.String())
- return err == nil
- }
- func isIP4Addr(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- val := field.String()
- if idx := strings.LastIndex(val, ":"); idx != -1 {
- val = val[0:idx]
- }
- if !IsIPv4(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
- return false
- }
- return true
- }
- func isIP6Addr(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
- val := field.String()
- if idx := strings.LastIndex(val, ":"); idx != -1 {
- if idx != 0 && val[idx-1:idx] == "]" {
- val = val[1 : idx-1]
- }
- }
- if !IsIPv6(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
- return false
- }
- return true
- }
|