forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-ozzo/ozzo-validation/is/rules.go

309 lines
10 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2016 Qiang Xue. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a MIT-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Package is provides a list of commonly used string validation rules.
2024-02-18 10:42:21 +00:00
package is
import (
"regexp"
"unicode"
"github.com/asaskevich/govalidator"
"github.com/go-ozzo/ozzo-validation"
)
var (
2024-02-18 10:42:21 +00:00
// Email validates if a string is an email or not.
2024-02-18 10:42:21 +00:00
Email = validation.NewStringRule(govalidator.IsEmail, "must be a valid email address")
2024-02-18 10:42:21 +00:00
// URL validates if a string is a valid URL
2024-02-18 10:42:21 +00:00
URL = validation.NewStringRule(govalidator.IsURL, "must be a valid URL")
2024-02-18 10:42:21 +00:00
// RequestURL validates if a string is a valid request URL
2024-02-18 10:42:21 +00:00
RequestURL = validation.NewStringRule(govalidator.IsRequestURL, "must be a valid request URL")
2024-02-18 10:42:21 +00:00
// RequestURI validates if a string is a valid request URI
2024-02-18 10:42:21 +00:00
RequestURI = validation.NewStringRule(govalidator.IsRequestURI, "must be a valid request URI")
2024-02-18 10:42:21 +00:00
// Alpha validates if a string contains English letters only (a-zA-Z)
2024-02-18 10:42:21 +00:00
Alpha = validation.NewStringRule(govalidator.IsAlpha, "must contain English letters only")
2024-02-18 10:42:21 +00:00
// Digit validates if a string contains digits only (0-9)
2024-02-18 10:42:21 +00:00
Digit = validation.NewStringRule(isDigit, "must contain digits only")
2024-02-18 10:42:21 +00:00
// Alphanumeric validates if a string contains English letters and digits only (a-zA-Z0-9)
2024-02-18 10:42:21 +00:00
Alphanumeric = validation.NewStringRule(govalidator.IsAlphanumeric, "must contain English letters and digits only")
2024-02-18 10:42:21 +00:00
// UTFLetter validates if a string contains unicode letters only
2024-02-18 10:42:21 +00:00
UTFLetter = validation.NewStringRule(govalidator.IsUTFLetter, "must contain unicode letter characters only")
2024-02-18 10:42:21 +00:00
// UTFDigit validates if a string contains unicode decimal digits only
2024-02-18 10:42:21 +00:00
UTFDigit = validation.NewStringRule(govalidator.IsUTFDigit, "must contain unicode decimal digits only")
2024-02-18 10:42:21 +00:00
// UTFLetterNumeric validates if a string contains unicode letters and numbers only
2024-02-18 10:42:21 +00:00
UTFLetterNumeric = validation.NewStringRule(govalidator.IsUTFLetterNumeric, "must contain unicode letters and numbers only")
2024-02-18 10:42:21 +00:00
// UTFNumeric validates if a string contains unicode number characters (category N) only
2024-02-18 10:42:21 +00:00
UTFNumeric = validation.NewStringRule(isUTFNumeric, "must contain unicode number characters only")
2024-02-18 10:42:21 +00:00
// LowerCase validates if a string contains lower case unicode letters only
2024-02-18 10:42:21 +00:00
LowerCase = validation.NewStringRule(govalidator.IsLowerCase, "must be in lower case")
2024-02-18 10:42:21 +00:00
// UpperCase validates if a string contains upper case unicode letters only
2024-02-18 10:42:21 +00:00
UpperCase = validation.NewStringRule(govalidator.IsUpperCase, "must be in upper case")
2024-02-18 10:42:21 +00:00
// Hexadecimal validates if a string is a valid hexadecimal number
2024-02-18 10:42:21 +00:00
Hexadecimal = validation.NewStringRule(govalidator.IsHexadecimal, "must be a valid hexadecimal number")
2024-02-18 10:42:21 +00:00
// HexColor validates if a string is a valid hexadecimal color code
2024-02-18 10:42:21 +00:00
HexColor = validation.NewStringRule(govalidator.IsHexcolor, "must be a valid hexadecimal color code")
2024-02-18 10:42:21 +00:00
// RGBColor validates if a string is a valid RGB color in the form of rgb(R, G, B)
2024-02-18 10:42:21 +00:00
RGBColor = validation.NewStringRule(govalidator.IsRGBcolor, "must be a valid RGB color code")
2024-02-18 10:42:21 +00:00
// Int validates if a string is a valid integer number
2024-02-18 10:42:21 +00:00
Int = validation.NewStringRule(govalidator.IsInt, "must be an integer number")
2024-02-18 10:42:21 +00:00
// Float validates if a string is a floating point number
2024-02-18 10:42:21 +00:00
Float = validation.NewStringRule(govalidator.IsFloat, "must be a floating point number")
2024-02-18 10:42:21 +00:00
// UUIDv3 validates if a string is a valid version 3 UUID
2024-02-18 10:42:21 +00:00
UUIDv3 = validation.NewStringRule(govalidator.IsUUIDv3, "must be a valid UUID v3")
2024-02-18 10:42:21 +00:00
// UUIDv4 validates if a string is a valid version 4 UUID
2024-02-18 10:42:21 +00:00
UUIDv4 = validation.NewStringRule(govalidator.IsUUIDv4, "must be a valid UUID v4")
2024-02-18 10:42:21 +00:00
// UUIDv5 validates if a string is a valid version 5 UUID
2024-02-18 10:42:21 +00:00
UUIDv5 = validation.NewStringRule(govalidator.IsUUIDv5, "must be a valid UUID v5")
2024-02-18 10:42:21 +00:00
// UUID validates if a string is a valid UUID
2024-02-18 10:42:21 +00:00
UUID = validation.NewStringRule(govalidator.IsUUID, "must be a valid UUID")
2024-02-18 10:42:21 +00:00
// CreditCard validates if a string is a valid credit card number
2024-02-18 10:42:21 +00:00
CreditCard = validation.NewStringRule(govalidator.IsCreditCard, "must be a valid credit card number")
2024-02-18 10:42:21 +00:00
// ISBN10 validates if a string is an ISBN version 10
2024-02-18 10:42:21 +00:00
ISBN10 = validation.NewStringRule(govalidator.IsISBN10, "must be a valid ISBN-10")
2024-02-18 10:42:21 +00:00
// ISBN13 validates if a string is an ISBN version 13
2024-02-18 10:42:21 +00:00
ISBN13 = validation.NewStringRule(govalidator.IsISBN13, "must be a valid ISBN-13")
2024-02-18 10:42:21 +00:00
// ISBN validates if a string is an ISBN (either version 10 or 13)
2024-02-18 10:42:21 +00:00
ISBN = validation.NewStringRule(isISBN, "must be a valid ISBN")
2024-02-18 10:42:21 +00:00
// JSON validates if a string is in valid JSON format
2024-02-18 10:42:21 +00:00
JSON = validation.NewStringRule(govalidator.IsJSON, "must be in valid JSON format")
2024-02-18 10:42:21 +00:00
// ASCII validates if a string contains ASCII characters only
2024-02-18 10:42:21 +00:00
ASCII = validation.NewStringRule(govalidator.IsASCII, "must contain ASCII characters only")
2024-02-18 10:42:21 +00:00
// PrintableASCII validates if a string contains printable ASCII characters only
2024-02-18 10:42:21 +00:00
PrintableASCII = validation.NewStringRule(govalidator.IsPrintableASCII, "must contain printable ASCII characters only")
2024-02-18 10:42:21 +00:00
// Multibyte validates if a string contains multibyte characters
2024-02-18 10:42:21 +00:00
Multibyte = validation.NewStringRule(govalidator.IsMultibyte, "must contain multibyte characters")
2024-02-18 10:42:21 +00:00
// FullWidth validates if a string contains full-width characters
2024-02-18 10:42:21 +00:00
FullWidth = validation.NewStringRule(govalidator.IsFullWidth, "must contain full-width characters")
2024-02-18 10:42:21 +00:00
// HalfWidth validates if a string contains half-width characters
2024-02-18 10:42:21 +00:00
HalfWidth = validation.NewStringRule(govalidator.IsHalfWidth, "must contain half-width characters")
2024-02-18 10:42:21 +00:00
// VariableWidth validates if a string contains both full-width and half-width characters
2024-02-18 10:42:21 +00:00
VariableWidth = validation.NewStringRule(govalidator.IsVariableWidth, "must contain both full-width and half-width characters")
2024-02-18 10:42:21 +00:00
// Base64 validates if a string is encoded in Base64
2024-02-18 10:42:21 +00:00
Base64 = validation.NewStringRule(govalidator.IsBase64, "must be encoded in Base64")
2024-02-18 10:42:21 +00:00
// DataURI validates if a string is a valid base64-encoded data URI
2024-02-18 10:42:21 +00:00
DataURI = validation.NewStringRule(govalidator.IsDataURI, "must be a Base64-encoded data URI")
2024-02-18 10:42:21 +00:00
// E164 validates if a string is a valid ISO3166 Alpha 2 country code
2024-02-18 10:42:21 +00:00
E164 = validation.NewStringRule(isE164Number, "must be a valid E164 number")
2024-02-18 10:42:21 +00:00
// CountryCode2 validates if a string is a valid ISO3166 Alpha 2 country code
2024-02-18 10:42:21 +00:00
CountryCode2 = validation.NewStringRule(govalidator.IsISO3166Alpha2, "must be a valid two-letter country code")
2024-02-18 10:42:21 +00:00
// CountryCode3 validates if a string is a valid ISO3166 Alpha 3 country code
2024-02-18 10:42:21 +00:00
CountryCode3 = validation.NewStringRule(govalidator.IsISO3166Alpha3, "must be a valid three-letter country code")
2024-02-18 10:42:21 +00:00
// CurrencyCode validates if a string is a valid IsISO4217 currency code.
2024-02-18 10:42:21 +00:00
CurrencyCode = validation.NewStringRule(govalidator.IsISO4217, "must be valid ISO 4217 currency code")
2024-02-18 10:42:21 +00:00
// DialString validates if a string is a valid dial string that can be passed to Dial()
2024-02-18 10:42:21 +00:00
DialString = validation.NewStringRule(govalidator.IsDialString, "must be a valid dial string")
2024-02-18 10:42:21 +00:00
// MAC validates if a string is a MAC address
2024-02-18 10:42:21 +00:00
MAC = validation.NewStringRule(govalidator.IsMAC, "must be a valid MAC address")
2024-02-18 10:42:21 +00:00
// IP validates if a string is a valid IP address (either version 4 or 6)
2024-02-18 10:42:21 +00:00
IP = validation.NewStringRule(govalidator.IsIP, "must be a valid IP address")
2024-02-18 10:42:21 +00:00
// IPv4 validates if a string is a valid version 4 IP address
2024-02-18 10:42:21 +00:00
IPv4 = validation.NewStringRule(govalidator.IsIPv4, "must be a valid IPv4 address")
2024-02-18 10:42:21 +00:00
// IPv6 validates if a string is a valid version 6 IP address
2024-02-18 10:42:21 +00:00
IPv6 = validation.NewStringRule(govalidator.IsIPv6, "must be a valid IPv6 address")
2024-02-18 10:42:21 +00:00
// Subdomain validates if a string is valid subdomain
2024-02-18 10:42:21 +00:00
Subdomain = validation.NewStringRule(isSubdomain, "must be a valid subdomain")
2024-02-18 10:42:21 +00:00
// Domain validates if a string is valid domain
2024-02-18 10:42:21 +00:00
Domain = validation.NewStringRule(isDomain, "must be a valid domain")
2024-02-18 10:42:21 +00:00
// DNSName validates if a string is valid DNS name
2024-02-18 10:42:21 +00:00
DNSName = validation.NewStringRule(govalidator.IsDNSName, "must be a valid DNS name")
2024-02-18 10:42:21 +00:00
// Host validates if a string is a valid IP (both v4 and v6) or a valid DNS name
2024-02-18 10:42:21 +00:00
Host = validation.NewStringRule(govalidator.IsHost, "must be a valid IP address or DNS name")
2024-02-18 10:42:21 +00:00
// Port validates if a string is a valid port number
2024-02-18 10:42:21 +00:00
Port = validation.NewStringRule(govalidator.IsPort, "must be a valid port number")
2024-02-18 10:42:21 +00:00
// MongoID validates if a string is a valid Mongo ID
2024-02-18 10:42:21 +00:00
MongoID = validation.NewStringRule(govalidator.IsMongoID, "must be a valid hex-encoded MongoDB ObjectId")
2024-02-18 10:42:21 +00:00
// Latitude validates if a string is a valid latitude
2024-02-18 10:42:21 +00:00
Latitude = validation.NewStringRule(govalidator.IsLatitude, "must be a valid latitude")
2024-02-18 10:42:21 +00:00
// Longitude validates if a string is a valid longitude
2024-02-18 10:42:21 +00:00
Longitude = validation.NewStringRule(govalidator.IsLongitude, "must be a valid longitude")
2024-02-18 10:42:21 +00:00
// SSN validates if a string is a social security number (SSN)
2024-02-18 10:42:21 +00:00
SSN = validation.NewStringRule(govalidator.IsSSN, "must be a valid social security number")
2024-02-18 10:42:21 +00:00
// Semver validates if a string is a valid semantic version
2024-02-18 10:42:21 +00:00
Semver = validation.NewStringRule(govalidator.IsSemver, "must be a valid semantic version")
)
var (
reDigit = regexp.MustCompile("^[0-9]+$")
2024-02-18 10:42:21 +00:00
// Subdomain regex source: https://stackoverflow.com/a/7933253
2024-02-18 10:42:21 +00:00
reSubdomain = regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$`)
2024-02-18 10:42:21 +00:00
// E164 regex source: https://stackoverflow.com/a/23299989
2024-02-18 10:42:21 +00:00
reE164 = regexp.MustCompile(`^\+?[1-9]\d{1,14}$`)
2024-02-18 10:42:21 +00:00
// Domain regex source: https://stackoverflow.com/a/7933253
2024-02-18 10:42:21 +00:00
// Slightly modified: Removed 255 max length validation since Go regex does not
2024-02-18 10:42:21 +00:00
// support lookarounds. More info: https://stackoverflow.com/a/38935027
2024-02-18 10:42:21 +00:00
reDomain = regexp.MustCompile(`^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z]{1,63}| xn--[a-z0-9]{1,59})$`)
)
func isISBN(value string) bool {
2024-02-18 10:42:21 +00:00
return govalidator.IsISBN(value, 10) || govalidator.IsISBN(value, 13)
2024-02-18 10:42:21 +00:00
}
func isDigit(value string) bool {
2024-02-18 10:42:21 +00:00
return reDigit.MatchString(value)
2024-02-18 10:42:21 +00:00
}
func isE164Number(value string) bool {
2024-02-18 10:42:21 +00:00
return reE164.MatchString(value)
2024-02-18 10:42:21 +00:00
}
func isSubdomain(value string) bool {
2024-02-18 10:42:21 +00:00
return reSubdomain.MatchString(value)
2024-02-18 10:42:21 +00:00
}
func isDomain(value string) bool {
2024-02-18 10:42:21 +00:00
if len(value) > 255 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
return reDomain.MatchString(value)
2024-02-18 10:42:21 +00:00
}
func isUTFNumeric(value string) bool {
2024-02-18 10:42:21 +00:00
for _, c := range value {
2024-02-18 10:42:21 +00:00
if unicode.IsNumber(c) == false {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}