...

Source file src/github.com/go-playground/validator/v10/regexes.go

Documentation: github.com/go-playground/validator/v10

     1  package validator
     2  
     3  import "regexp"
     4  
     5  const (
     6  	alphaRegexString                 = "^[a-zA-Z]+$"
     7  	alphaNumericRegexString          = "^[a-zA-Z0-9]+$"
     8  	alphaUnicodeRegexString          = "^[\\p{L}]+$"
     9  	alphaUnicodeNumericRegexString   = "^[\\p{L}\\p{N}]+$"
    10  	numericRegexString               = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
    11  	numberRegexString                = "^[0-9]+$"
    12  	hexadecimalRegexString           = "^(0[xX])?[0-9a-fA-F]+$"
    13  	hexColorRegexString              = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
    14  	rgbRegexString                   = "^rgb\\(\\s*(?:(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])|(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%)\\s*\\)$"
    15  	rgbaRegexString                  = "^rgba\\(\\s*(?:(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])|(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
    16  	hslRegexString                   = "^hsl\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*\\)$"
    17  	hslaRegexString                  = "^hsla\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
    18  	emailRegexString                 = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
    19  	e164RegexString                  = "^\\+[1-9]?[0-9]{7,14}$"
    20  	base64RegexString                = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
    21  	base64URLRegexString             = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
    22  	base64RawURLRegexString          = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2,4})$"
    23  	iSBN10RegexString                = "^(?:[0-9]{9}X|[0-9]{10})$"
    24  	iSBN13RegexString                = "^(?:(?:97(?:8|9))[0-9]{10})$"
    25  	iSSNRegexString                  = "^(?:[0-9]{4}-[0-9]{3}[0-9X])$"
    26  	uUID3RegexString                 = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
    27  	uUID4RegexString                 = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    28  	uUID5RegexString                 = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    29  	uUIDRegexString                  = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
    30  	uUID3RFC4122RegexString          = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    31  	uUID4RFC4122RegexString          = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
    32  	uUID5RFC4122RegexString          = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
    33  	uUIDRFC4122RegexString           = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    34  	uLIDRegexString                  = "^[A-HJKMNP-TV-Z0-9]{26}$"
    35  	md4RegexString                   = "^[0-9a-f]{32}$"
    36  	md5RegexString                   = "^[0-9a-f]{32}$"
    37  	sha256RegexString                = "^[0-9a-f]{64}$"
    38  	sha384RegexString                = "^[0-9a-f]{96}$"
    39  	sha512RegexString                = "^[0-9a-f]{128}$"
    40  	ripemd128RegexString             = "^[0-9a-f]{32}$"
    41  	ripemd160RegexString             = "^[0-9a-f]{40}$"
    42  	tiger128RegexString              = "^[0-9a-f]{32}$"
    43  	tiger160RegexString              = "^[0-9a-f]{40}$"
    44  	tiger192RegexString              = "^[0-9a-f]{48}$"
    45  	aSCIIRegexString                 = "^[\x00-\x7F]*$"
    46  	printableASCIIRegexString        = "^[\x20-\x7E]*$"
    47  	multibyteRegexString             = "[^\x00-\x7F]"
    48  	dataURIRegexString               = `^data:((?:\w+\/(?:([^;]|;[^;]).)+)?)`
    49  	latitudeRegexString              = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
    50  	longitudeRegexString             = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
    51  	sSNRegexString                   = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$`
    52  	hostnameRegexStringRFC952        = `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`                                                                   // https://tools.ietf.org/html/rfc952
    53  	hostnameRegexStringRFC1123       = `^([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62}){1}(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?$`                                 // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123
    54  	fqdnRegexStringRFC1123           = `^([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?$` // same as hostnameRegexStringRFC1123 but must contain a non numerical TLD (possibly ending with '.')
    55  	btcAddressRegexString            = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$`                                                                             // bitcoin address
    56  	btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$`                                                                                   // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
    57  	btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$`                                                                                   // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
    58  	ethAddressRegexString            = `^0x[0-9a-fA-F]{40}$`
    59  	ethAddressUpperRegexString       = `^0x[0-9A-F]{40}$`
    60  	ethAddressLowerRegexString       = `^0x[0-9a-f]{40}$`
    61  	uRLEncodedRegexString            = `^(?:[^%]|%[0-9A-Fa-f]{2})*$`
    62  	hTMLEncodedRegexString           = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
    63  	hTMLRegexString                  = `<[/]?([a-zA-Z]+).*?>`
    64  	jWTRegexString                   = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
    65  	splitParamsRegexString           = `'[^']*'|\S+`
    66  	bicRegexString                   = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
    67  	semverRegexString                = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/
    68  	dnsRegexStringRFC1035Label       = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
    69  	cveRegexString                   = `^CVE-(1999|2\d{3})-(0[^0]\d{2}|0\d[^0]\d{1}|0\d{2}[^0]|[1-9]{1}\d{3,})$` // CVE Format Id https://cve.mitre.org/cve/identifiers/syntaxchange.html
    70  	mongodbRegexString               = "^[a-f\\d]{24}$"
    71  	cronRegexString                  = `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`
    72  	spicedbIDRegexString             = `^(([a-zA-Z0-9/_|\-=+]{1,})|\*)$`
    73  	spicedbPermissionRegexString     = "^([a-z][a-z0-9_]{1,62}[a-z0-9])?$"
    74  	spicedbTypeRegexString           = "^([a-z][a-z0-9_]{1,61}[a-z0-9]/)?[a-z][a-z0-9_]{1,62}[a-z0-9]$"
    75  )
    76  
    77  var (
    78  	alphaRegex                 = regexp.MustCompile(alphaRegexString)
    79  	alphaNumericRegex          = regexp.MustCompile(alphaNumericRegexString)
    80  	alphaUnicodeRegex          = regexp.MustCompile(alphaUnicodeRegexString)
    81  	alphaUnicodeNumericRegex   = regexp.MustCompile(alphaUnicodeNumericRegexString)
    82  	numericRegex               = regexp.MustCompile(numericRegexString)
    83  	numberRegex                = regexp.MustCompile(numberRegexString)
    84  	hexadecimalRegex           = regexp.MustCompile(hexadecimalRegexString)
    85  	hexColorRegex              = regexp.MustCompile(hexColorRegexString)
    86  	rgbRegex                   = regexp.MustCompile(rgbRegexString)
    87  	rgbaRegex                  = regexp.MustCompile(rgbaRegexString)
    88  	hslRegex                   = regexp.MustCompile(hslRegexString)
    89  	hslaRegex                  = regexp.MustCompile(hslaRegexString)
    90  	e164Regex                  = regexp.MustCompile(e164RegexString)
    91  	emailRegex                 = regexp.MustCompile(emailRegexString)
    92  	base64Regex                = regexp.MustCompile(base64RegexString)
    93  	base64URLRegex             = regexp.MustCompile(base64URLRegexString)
    94  	base64RawURLRegex          = regexp.MustCompile(base64RawURLRegexString)
    95  	iSBN10Regex                = regexp.MustCompile(iSBN10RegexString)
    96  	iSBN13Regex                = regexp.MustCompile(iSBN13RegexString)
    97  	iSSNRegex                  = regexp.MustCompile(iSSNRegexString)
    98  	uUID3Regex                 = regexp.MustCompile(uUID3RegexString)
    99  	uUID4Regex                 = regexp.MustCompile(uUID4RegexString)
   100  	uUID5Regex                 = regexp.MustCompile(uUID5RegexString)
   101  	uUIDRegex                  = regexp.MustCompile(uUIDRegexString)
   102  	uUID3RFC4122Regex          = regexp.MustCompile(uUID3RFC4122RegexString)
   103  	uUID4RFC4122Regex          = regexp.MustCompile(uUID4RFC4122RegexString)
   104  	uUID5RFC4122Regex          = regexp.MustCompile(uUID5RFC4122RegexString)
   105  	uUIDRFC4122Regex           = regexp.MustCompile(uUIDRFC4122RegexString)
   106  	uLIDRegex                  = regexp.MustCompile(uLIDRegexString)
   107  	md4Regex                   = regexp.MustCompile(md4RegexString)
   108  	md5Regex                   = regexp.MustCompile(md5RegexString)
   109  	sha256Regex                = regexp.MustCompile(sha256RegexString)
   110  	sha384Regex                = regexp.MustCompile(sha384RegexString)
   111  	sha512Regex                = regexp.MustCompile(sha512RegexString)
   112  	ripemd128Regex             = regexp.MustCompile(ripemd128RegexString)
   113  	ripemd160Regex             = regexp.MustCompile(ripemd160RegexString)
   114  	tiger128Regex              = regexp.MustCompile(tiger128RegexString)
   115  	tiger160Regex              = regexp.MustCompile(tiger160RegexString)
   116  	tiger192Regex              = regexp.MustCompile(tiger192RegexString)
   117  	aSCIIRegex                 = regexp.MustCompile(aSCIIRegexString)
   118  	printableASCIIRegex        = regexp.MustCompile(printableASCIIRegexString)
   119  	multibyteRegex             = regexp.MustCompile(multibyteRegexString)
   120  	dataURIRegex               = regexp.MustCompile(dataURIRegexString)
   121  	latitudeRegex              = regexp.MustCompile(latitudeRegexString)
   122  	longitudeRegex             = regexp.MustCompile(longitudeRegexString)
   123  	sSNRegex                   = regexp.MustCompile(sSNRegexString)
   124  	hostnameRegexRFC952        = regexp.MustCompile(hostnameRegexStringRFC952)
   125  	hostnameRegexRFC1123       = regexp.MustCompile(hostnameRegexStringRFC1123)
   126  	fqdnRegexRFC1123           = regexp.MustCompile(fqdnRegexStringRFC1123)
   127  	btcAddressRegex            = regexp.MustCompile(btcAddressRegexString)
   128  	btcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32)
   129  	btcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32)
   130  	ethAddressRegex            = regexp.MustCompile(ethAddressRegexString)
   131  	uRLEncodedRegex            = regexp.MustCompile(uRLEncodedRegexString)
   132  	hTMLEncodedRegex           = regexp.MustCompile(hTMLEncodedRegexString)
   133  	hTMLRegex                  = regexp.MustCompile(hTMLRegexString)
   134  	jWTRegex                   = regexp.MustCompile(jWTRegexString)
   135  	splitParamsRegex           = regexp.MustCompile(splitParamsRegexString)
   136  	bicRegex                   = regexp.MustCompile(bicRegexString)
   137  	semverRegex                = regexp.MustCompile(semverRegexString)
   138  	dnsRegexRFC1035Label       = regexp.MustCompile(dnsRegexStringRFC1035Label)
   139  	cveRegex                   = regexp.MustCompile(cveRegexString)
   140  	mongodbRegex               = regexp.MustCompile(mongodbRegexString)
   141  	cronRegex                  = regexp.MustCompile(cronRegexString)
   142  	spicedbIDRegex             = regexp.MustCompile(spicedbIDRegexString)
   143  	spicedbPermissionRegex     = regexp.MustCompile(spicedbPermissionRegexString)
   144  	spicedbTypeRegex           = regexp.MustCompile(spicedbTypeRegexString)
   145  )
   146  

View as plain text