102 lines
9.5 KiB
Go
102 lines
9.5 KiB
Go
|
package validator
|
||
|
|
||
|
import "regexp"
|
||
|
|
||
|
const (
|
||
|
alphaRegexString = "^[a-zA-Z]+$"
|
||
|
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
|
||
|
alphaUnicodeRegexString = "^[\\p{L}]+$"
|
||
|
alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
|
||
|
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
|
||
|
numberRegexString = "^[0-9]+$"
|
||
|
hexadecimalRegexString = "^(0[xX])?[0-9a-fA-F]+$"
|
||
|
hexcolorRegexString = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
|
||
|
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*\\)$"
|
||
|
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-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|[\\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}])))\\.?$"
|
||
|
e164RegexString = "^\\+[1-9]?[0-9]{7,14}$"
|
||
|
base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
|
||
|
base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
|
||
|
iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
|
||
|
iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$"
|
||
|
uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
||
|
uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
|
||
|
uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
|
||
|
uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
||
|
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}$"
|
||
|
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}$"
|
||
|
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}$"
|
||
|
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}$"
|
||
|
aSCIIRegexString = "^[\x00-\x7F]*$"
|
||
|
printableASCIIRegexString = "^[\x20-\x7E]*$"
|
||
|
multibyteRegexString = "[^\x00-\x7F]"
|
||
|
dataURIRegexString = `^data:((?:\w+\/(?:([^;]|;[^;]).)+)?)`
|
||
|
latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
|
||
|
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
|
||
|
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])$`
|
||
|
hostnameRegexStringRFC952 = `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
|
||
|
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
|
||
|
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 '.')
|
||
|
btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
|
||
|
btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
|
||
|
btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
|
||
|
ethAddressRegexString = `^0x[0-9a-fA-F]{40}$`
|
||
|
ethAddressUpperRegexString = `^0x[0-9A-F]{40}$`
|
||
|
ethAddressLowerRegexString = `^0x[0-9a-f]{40}$`
|
||
|
uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
|
||
|
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(>)|(<)|(")|(&)+[;]?`
|
||
|
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
|
||
|
splitParamsRegexString = `'[^']*'|\S+`
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
alphaRegex = regexp.MustCompile(alphaRegexString)
|
||
|
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
|
||
|
alphaUnicodeRegex = regexp.MustCompile(alphaUnicodeRegexString)
|
||
|
alphaUnicodeNumericRegex = regexp.MustCompile(alphaUnicodeNumericRegexString)
|
||
|
numericRegex = regexp.MustCompile(numericRegexString)
|
||
|
numberRegex = regexp.MustCompile(numberRegexString)
|
||
|
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
|
||
|
hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
|
||
|
rgbRegex = regexp.MustCompile(rgbRegexString)
|
||
|
rgbaRegex = regexp.MustCompile(rgbaRegexString)
|
||
|
hslRegex = regexp.MustCompile(hslRegexString)
|
||
|
hslaRegex = regexp.MustCompile(hslaRegexString)
|
||
|
e164Regex = regexp.MustCompile(e164RegexString)
|
||
|
emailRegex = regexp.MustCompile(emailRegexString)
|
||
|
base64Regex = regexp.MustCompile(base64RegexString)
|
||
|
base64URLRegex = regexp.MustCompile(base64URLRegexString)
|
||
|
iSBN10Regex = regexp.MustCompile(iSBN10RegexString)
|
||
|
iSBN13Regex = regexp.MustCompile(iSBN13RegexString)
|
||
|
uUID3Regex = regexp.MustCompile(uUID3RegexString)
|
||
|
uUID4Regex = regexp.MustCompile(uUID4RegexString)
|
||
|
uUID5Regex = regexp.MustCompile(uUID5RegexString)
|
||
|
uUIDRegex = regexp.MustCompile(uUIDRegexString)
|
||
|
uUID3RFC4122Regex = regexp.MustCompile(uUID3RFC4122RegexString)
|
||
|
uUID4RFC4122Regex = regexp.MustCompile(uUID4RFC4122RegexString)
|
||
|
uUID5RFC4122Regex = regexp.MustCompile(uUID5RFC4122RegexString)
|
||
|
uUIDRFC4122Regex = regexp.MustCompile(uUIDRFC4122RegexString)
|
||
|
aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
|
||
|
printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
|
||
|
multibyteRegex = regexp.MustCompile(multibyteRegexString)
|
||
|
dataURIRegex = regexp.MustCompile(dataURIRegexString)
|
||
|
latitudeRegex = regexp.MustCompile(latitudeRegexString)
|
||
|
longitudeRegex = regexp.MustCompile(longitudeRegexString)
|
||
|
sSNRegex = regexp.MustCompile(sSNRegexString)
|
||
|
hostnameRegexRFC952 = regexp.MustCompile(hostnameRegexStringRFC952)
|
||
|
hostnameRegexRFC1123 = regexp.MustCompile(hostnameRegexStringRFC1123)
|
||
|
fqdnRegexRFC1123 = regexp.MustCompile(fqdnRegexStringRFC1123)
|
||
|
btcAddressRegex = regexp.MustCompile(btcAddressRegexString)
|
||
|
btcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32)
|
||
|
btcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32)
|
||
|
ethAddressRegex = regexp.MustCompile(ethAddressRegexString)
|
||
|
ethaddressRegexUpper = regexp.MustCompile(ethAddressUpperRegexString)
|
||
|
ethAddressRegexLower = regexp.MustCompile(ethAddressLowerRegexString)
|
||
|
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
|
||
|
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
|
||
|
hTMLRegex = regexp.MustCompile(hTMLRegexString)
|
||
|
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
|
||
|
)
|