Documentation ¶
Overview ¶
Package govalidator is package of string validators and sanitizers. ver. 0.0.1
Index ¶
- Constants
- Variables
- func BlackList(str, chars string) string
- func CamelCaseToUnderscore(str string) string
- func Contains(str, substring string) bool
- func Escape(str string) string
- func GetLine(s string, index int) (string, error)
- func GetLines(s string) []string
- func IsASCII(str string) bool
- func IsAlpha(str string) bool
- func IsAlphanumeric(str string) bool
- func IsBase64(str string) bool
- func IsByteLength(str string, min, max int) bool
- func IsCreditCard(str string) bool
- func IsDataURI(str string) bool
- func IsDivisibleBy(str, num string) bool
- func IsEmail(str string) bool
- func IsFloat(str string) bool
- func IsFullWidth(str string) bool
- func IsHalfWidth(str string) bool
- func IsHexadecimal(str string) bool
- func IsHexcolor(str string) bool
- func IsIP(str string, version int) bool
- func IsIPv4(str string) bool
- func IsIPv6(str string) bool
- func IsISBN(str string, version int) bool
- func IsISBN10(str string) bool
- func IsISBN13(str string) bool
- func IsInt(str string) bool
- func IsJSON(str string) bool
- func IsLatitude(str string) bool
- func IsLongitude(str string) bool
- func IsLowerCase(str string) bool
- func IsMAC(str string) bool
- func IsMultibyte(str string) bool
- func IsNull(str string) bool
- func IsNumeric(str string) bool
- func IsRGBcolor(str string) bool
- func IsURL(str string) bool
- func IsUUID(str string) bool
- func IsUUIDv3(str string) bool
- func IsUUIDv4(str string) bool
- func IsUUIDv5(str string) bool
- func IsUpperCase(str string) bool
- func IsVariableWidth(str string) bool
- func LeftTrim(str, chars string) string
- func Matches(str, pattern string) bool
- func RemoveTags(s string) string
- func ReplacePattern(str, pattern, replace string) string
- func Reverse(s string) string
- func RightTrim(str, chars string) string
- func SafeFileName(str string) string
- func StripLow(str string, keepNewLines bool) string
- func ToBoolean(str string) (bool, error)
- func ToFloat(str string) (float64, error)
- func ToInt(str string) (int64, error)
- func ToString(obj interface{}) (string, error)
- func Trim(str, chars string) string
- func UnderscoreToCamelCase(s string) string
- func ValidateStruct(s interface{}) (bool, error)
- func WhiteList(str, chars string) string
- type UnsupportedTypeError
- type Validator
Examples ¶
Constants ¶
const ( Email string = "" /* 1182-byte string literal not displayed */ CreditCard string = "" /* 151-byte string literal not displayed */ ISBN10 string = "^(?:[0-9]{9}X|[0-9]{10})$" ISBN13 string = "^(?:[0-9]{13})$" IPv4 string = "^(\\d?\\d?\\d)\\.(\\d?\\d?\\d)\\.(\\d?\\d?\\d)\\.(\\d?\\d?\\d)$" IPv6 string = "^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$" UUID3 string = "^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$" UUID4 string = "^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$" UUID5 string = "^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$" UUID string = "^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$" Alpha string = "^[a-zA-Z]+$" Alphanumeric string = "^[a-zA-Z0-9]+$" Numeric string = "^-?[0-9]+$" Int string = "^(?:-?(?:0|[1-9][0-9]*))$" Float string = "^(?:-?(?:[0-9]+))?(?:\\.[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$" Hexadecimal string = "^[0-9a-fA-F]+$" Hexcolor string = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$" RGBcolor string = "" /* 157-byte string literal not displayed */ ASCII string = "^[\x00-\x7F]+$" Multibyte string = "[^\x00-\x7F]" FullWidth string = "[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]" HalfWidth string = "[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]" Base64 string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$" DataURI string = "^data:.+\\/(.+);base64$" MAC string = "^([a-fA-F0-9]{2}[:-]){5}[a-fA-F0-9]{2}$" Latitude string = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$" Longitude string = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$" URL string = `` /* 301-byte string literal not displayed */ )
Basic regular expressions for validating strings
Variables ¶
var TagMap = map[string]Validator{ "email": IsEmail, "url": IsURL, "alpha": IsAlpha, "alphanum": IsAlphanumeric, "numeric": IsNumeric, "hexadecimal": IsHexadecimal, "hexcolor": IsHexcolor, "rgbcolor": IsRGBcolor, "lowercase": IsLowerCase, "uppercase": IsUpperCase, "int": IsInt, "float": IsFloat, "null": IsNull, "uuid": IsUUID, "uuidv3": IsUUIDv3, "uuidv4": IsUUIDv4, "uuidv5": IsUUIDv5, "creditcard": IsCreditCard, "isbn10": IsISBN10, "isbn13": IsISBN13, "json": IsJSON, "multibyte": IsMultibyte, "ascii": IsASCII, "fullwidth": IsFullWidth, "halfwidth": IsHalfWidth, "variablewidth": IsVariableWidth, "base64": IsBase64, "datauri": IsDataURI, "ipv4": IsIPv4, "ipv6": IsIPv6, "mac": IsMAC, "latitude": IsLatitude, "longitude": IsLongitude, }
TagMap is a map of functions, that can be used as tags for ValidateStruct function.
Functions ¶
func CamelCaseToUnderscore ¶
CamelCaseToUnderscore converts from camel case form to underscore separated form. Ex.: MyFunc => my_func
func IsAlphanumeric ¶
IsAlphanumeric check if the string contains only letters and numbers.
func IsByteLength ¶
IsByteLength check if the string's length (in bytes) falls in a range.
func IsCreditCard ¶
IsCreditCard check if the string is a credit card.
func IsDivisibleBy ¶
IsDivisibleBy check if the string is a number that's divisible by another. If second argument is not valid integer or zero, it's return false. Otherwise, if first argument is not valid integer or zero, it's return true (Invalid string converts to zero).
Example ¶
This small example illustrate how to work with IsDivisibleBy function.
println("1024 is divisible by 64: ", IsDivisibleBy("1024", "64"))
Output:
func IsFullWidth ¶
IsFullWidth check if the string contains any full-width chars.
func IsHalfWidth ¶
IsHalfWidth check if the string contains any half-width chars.
func IsHexadecimal ¶
IsHexadecimal check if the string is a hexadecimal number.
func IsHexcolor ¶
IsHexcolor check if the string is a hexadecimal color.
func IsIP ¶
IsIP check if the string is an IP (version 4 or 6). If version value is not equal to 6 or 4, it will be check both variants.
func IsISBN ¶
IsISBN check if the string is an ISBN (version 10 or 13). If version value is not equal to 10 or 13, it will be check both variants.
func IsLongitude ¶
IsLongitude check if a string is valid longitude.
func IsMAC ¶
IsMAC check if a string is valid MAC address. Possible MAC formats: 3D:F2:C9:A6:B3:4F 3D-F2-C9-A6-B3:4F 3d-f2-c9-a6-b3:4f
func IsMultibyte ¶
IsMultibyte check if the string contains one or more multibyte chars.
func IsRGBcolor ¶
IsRGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB).
func IsVariableWidth ¶
IsVariableWidth check if the string contains a mixture of full and half-width chars.
func LeftTrim ¶
LeftTrim trim characters from the left-side of the input. If second argument is empty, it's will be remove leading spaces.
func Matches ¶
Matches check if string matches the pattern (pattern is regular expression) In case of error return false
func ReplacePattern ¶
ReplacePattern replace regular expression pattern in string
Example ¶
This small example illustrate how to work with ReplacePattern function.
// Replace in "http123123ftp://git534543hub.comio" following (pattern "(ftp|io|[0-9]+)"): // - Sequence "ftp". // - Sequence "io". // - Sequence of digits. // with empty string. println(ReplacePattern("http123123ftp://git534543hub.comio", "(ftp|io|[0-9]+)", "") == "http://github.com")
Output:
func RightTrim ¶
RightTrim trim characters from the right-side of the input. If second argument is empty, it's will be remove spaces.
func SafeFileName ¶
SafeFileName return safe string that can be used in file names
func StripLow ¶
StripLow remove characters with a numerical value < 32 and 127, mostly control characters. If keep_new_lines is true, newline characters are preserved (\n and \r, hex 0xA and 0xD).
func Trim ¶
Trim trim characters from both sides of the input. If second argument is empty, it's will be remove spaces.
Example ¶
This small example illustrate how to work with Trim function.
// Remove from left and right spaces and "\r", "\n", "\t" characters println(Trim(" \r\r\ntext\r \t\n", "") == "text") // Remove from left and right characters that are between "1" and "8". // "1-8" is like full list "12345678". println(Trim("1234567890987654321", "1-8") == "909")
Output:
func UnderscoreToCamelCase ¶
UnderscoreToCamelCase converts from underscore separated form to camel case form. Ex.: my_func => MyFunc
func ValidateStruct ¶
ValidateStruct use tags for fields
Example ¶
type Post struct { Title string `valid:"alphanum,required"` Message string `valid:"duck,ascii"` AuthorIP string `valid:"ipv4"` } post := &Post{"My Example Post", "duck", "123.234.54.3"} //Add your own struct validation tags TagMap["duck"] = Validator(func(str string) bool { return str == "duck" }) result, err := ValidateStruct(post) if err != nil { println("error: " + err.Error()) } println(result)
Output:
func WhiteList ¶
WhiteList remove characters that do not appear in the whitelist.
Example ¶
This small example illustrate how to work with WhiteList function.
// Remove all characters from string ignoring characters between "a" and "z" println(WhiteList("a3a43a5a4a3a2a23a4a5a4a3a4", "a-z") == "aaaaaaaaaaaa")
Output:
Types ¶
type UnsupportedTypeError ¶
UnsupportedTypeError is a wrapper for reflect.Type
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
Error returns string equivalent for reflect.Type