Documentation ¶
Index ¶
- Constants
- func AddCustomRule(name string, fn func(field string, value interface{}, rule string) error)
- func IsAlpha(str string) bool
- func IsAlphaDash(str string) bool
- func IsAlphaNumeric(str string) bool
- func IsBoolean(str string) bool
- func IsCSSColor(str string) bool
- func IsCoordinate(str string) bool
- func IsCreditCard(card string) bool
- func IsDate(date string) bool
- func IsDateDDMMYY(date string) bool
- func IsEmail(email string) bool
- func IsFloat(str string) bool
- func IsIP(str string) bool
- func IsIPV4(str string) bool
- func IsIPV6(str string) bool
- func IsIn(haystack []string, niddle string) bool
- func IsJSON(str string) bool
- func IsLatitude(str string) bool
- func IsLongitude(str string) bool
- func IsMatchedRegex(rxStr, str string) bool
- func IsNumeric(str string) bool
- func IsURL(url string) bool
- func IsUUID(str string) bool
- func IsUUID3(str string) bool
- func IsUUID4(str string) bool
- func IsUUID5(str string) bool
- type MapData
- type Options
- type Validator
Constants ¶
const ( // Alpha represents regular expression for alpha chartacters Alpha string = "^[a-zA-Z]+$" // AlphaDash represents regular expression for alpha chartacters with underscore and ash AlphaDash string = "^[a-zA-Z0-9_-]+$" // AlphaNumeric represents regular expression for alpha numeric chartacters AlphaNumeric string = "^[a-zA-Z0-9]+$" // CreditCard represents regular expression for credit cards like (Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards). Ref: https://stackoverflow.com/questions/9315647/regex-credit-card-number-tests CreditCard string = "" /* 154-byte string literal not displayed */ // Coordinate represents latitude and longitude regular expression Coordinate string = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?),\\s*[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$" // Ref: https://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates // CSSColor represents css valid color code with hex, rgb, rgba, hsl, hsla etc. Ref: http://www.regexpal.com/97509 CSSColor string = "^(#([\\da-f]{3}){1,2}|(rgb|hsl)a\\((\\d{1,3}%?,\\s?){3}(1|0?\\.\\d+)\\)|(rgb|hsl)\\(\\d{1,3}%?(,\\s?\\d{1,3}%?){2}\\))$" // Date represents regular expression for valid date like: yyyy-mm-dd Date string = "" /* 231-byte string literal not displayed */ // DateDDMMYY represents regular expression for valid date of format dd/mm/yyyy , dd-mm-yyyy etc.Ref: http://regexr.com/346hf DateDDMMYY string = "^(0?[1-9]|[12][0-9]|3[01])[\\/\\-](0?[1-9]|1[012])[\\/\\-]\\d{4}$" // Email represents regular expression for email Email string = "" /* 133-byte string literal not displayed */ // Float represents regular expression for finding fload number Float string = "^[+-]?([0-9]*[.])?[0-9]+$" // IP represents regular expression for ip address IP string = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" // IPV4 represents regular expression for ip address version 4 IPV4 string = "^([0-9]{1,3}\\.){3}[0-9]{1,3}(\\/([0-9]|[1-2][0-9]|3[0-2]))?$" // IPV6 represents regular expression for ip address version 6 IPV6 string = `` /* 1057-byte string literal not displayed */ // Latitude represents latitude regular expression Latitude string = "^(\\+|-)?(?:90(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\\.[0-9]{1,6})?))$" // Longitude represents longitude regular expression Longitude string = "^(\\+|-)?(?:180(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\\.[0-9]{1,6})?))$" // Numeric represents regular expression for numeric Numeric string = "^[0-9]+$" // URL represents regular expression for url URL string = "^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$" // Ref: https://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex // UUID represents regular expression for UUID UUID string = "^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$" // UUID3 represents regular expression for UUID version 3 UUID3 string = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$" // UUID4 represents regular expression for UUID version 4 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 represents regular expression for UUID version 5 UUID5 string = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" )
Variables ¶
This section is empty.
Functions ¶
func AddCustomRule ¶
AddCustomRule help to add custom rules for validator First argument it takes the rule name and second arg a func Second arg must have this signature below fn func(fieldName string, fieldValue string, rule string) error
func IsAlphaDash ¶
IsAlphaDash check the input is letters, number with dash and underscore
func IsAlphaNumeric ¶
IsAlphaNumeric check the input is alpha numeric or not
func IsBoolean ¶
IsBoolean check the input contains boolean type values in this case: "0", "1", "true", "false", "True", "False"
func IsCSSColor ¶
IsCSSColor is a valid CSS color value (hex, rgb, rgba, hsl, hsla) etc like #909, #00aaff, rgb(255,122,122)
func IsCreditCard ¶
IsCreditCard check the provided card number is a valid
Visa, MasterCard, American Express, Diners Club, Discover or JCB card
func IsDateDDMMYY ¶
IsDateDDMMYY check the date string is valid or not
func IsIPV4 ¶
IsIPV4 check the provided input string is a valid IP address version 4 or not Ref: https://en.wikipedia.org/wiki/IPv4
func IsIPV6 ¶
IsIPV6 check the provided input string is a valid IP address version 6 or not Ref: https://en.wikipedia.org/wiki/IPv6
func IsLatitude ¶
IsLatitude check the provided input string is a valid latitude or not
func IsLongitude ¶
IsLongitude check the provided input string is a valid longitude or not
func IsMatchedRegex ¶
IsMatchedRegex match the regular expression string provided in first argument with second argument which is also a string
Types ¶
type Options ¶
type Options struct { Data interface{} // Data represents structure for JSON body Request *http.Request RequiredDefault bool // RequiredDefault represents if all the fields are by default required or not UniqueKey bool // UniqueKey set prefix (type name) in field name for ValidateJSON Rules MapData // Rules represents rules for form-data/x-url-encoded/query params data Messages MapData // Messages represents custom/localize message for rules }
Options describes configuration option for validator
type Validator ¶
type Validator struct {
Opts Options // Opts contains all the options for validator
}
Validator represents a validator with options
func (*Validator) SetDefaultRequired ¶
SetDefaultRequired change the required behavior of fields Default value if false If SetDefaultRequired set to true then it will mark all the field in the rules list as required
func (*Validator) SetUniqueKey ¶
SetUniqueKey represents struct field name with prefix of struct name helps to stop collission between same field name in embeded struct
func (*Validator) Validate ¶
Validate validate request data like form-data, x-www-form-urlencoded and query params see example in README.md file
func (*Validator) ValidateMapJSON ¶
ValidateMapJSON validate request data from JSON body to Go map[string]interface{} interface{} can not be arrray, map, slice or struct it can be string, bool, number e.g: data := map[string]interface{}{"name": "John Doe", "age": 30, "single": false}
func (*Validator) ValidateStructJSON ¶
ValidateStructJSON validate request data from JSON body to Go struct see example in README.md file