validator

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2023 License: LGPL-3.0, MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
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

func AddCustomRule(name string, fn func(field string, value interface{}, rule string) error)

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 IsAlpha

func IsAlpha(str string) bool

IsAlpha check the input is letters (a-z,A-Z) or not

func IsAlphaDash

func IsAlphaDash(str string) bool

IsAlphaDash check the input is letters, number with dash and underscore

func IsAlphaNumeric

func IsAlphaNumeric(str string) bool

IsAlphaNumeric check the input is alpha numeric or not

func IsBoolean

func IsBoolean(str string) bool

IsBoolean check the input contains boolean type values in this case: "0", "1", "true", "false", "True", "False"

func IsCSSColor

func IsCSSColor(str string) bool

IsCSSColor is a valid CSS color value (hex, rgb, rgba, hsl, hsla) etc like #909, #00aaff, rgb(255,122,122)

func IsCoordinate

func IsCoordinate(str string) bool

IsCoordinate is a valid Coordinate or not

func IsCreditCard

func IsCreditCard(card string) bool

IsCreditCard check the provided card number is a valid

Visa, MasterCard, American Express, Diners Club, Discover or JCB card

func IsDate

func IsDate(date string) bool

IsDate check the date string is valid or not

func IsDateDDMMYY

func IsDateDDMMYY(date string) bool

IsDateDDMMYY check the date string is valid or not

func IsEmail

func IsEmail(email string) bool

IsEmail check a email is valid or not

func IsFloat

func IsFloat(str string) bool

IsFloat check the input string is a float or not

func IsIP

func IsIP(str string) bool

IsIP check the provided input string is a valid IP address or not

func IsIPV4

func IsIPV4(str string) bool

IsIPV4 check the provided input string is a valid IP address version 4 or not Ref: https://en.wikipedia.org/wiki/IPv4

func IsIPV6

func IsIPV6(str string) bool

IsIPV6 check the provided input string is a valid IP address version 6 or not Ref: https://en.wikipedia.org/wiki/IPv6

func IsIn

func IsIn(haystack []string, niddle string) bool

IsIn check if the niddle exist in the haystack

func IsJSON

func IsJSON(str string) bool

IsJSON check wheather the input string is a valid json or not

func IsLatitude

func IsLatitude(str string) bool

IsLatitude check the provided input string is a valid latitude or not

func IsLongitude

func IsLongitude(str string) bool

IsLongitude check the provided input string is a valid longitude or not

func IsMatchedRegex

func IsMatchedRegex(rxStr, str string) bool

IsMatchedRegex match the regular expression string provided in first argument with second argument which is also a string

func IsNumeric

func IsNumeric(str string) bool

IsNumeric check the provided input string is numeric or not

func IsURL

func IsURL(url string) bool

IsURL check a URL is valid or not

func IsUUID

func IsUUID(str string) bool

IsUUID check the provided string is valid UUID or not

func IsUUID3

func IsUUID3(str string) bool

IsUUID3 check the provided string is valid UUID version 3 or not

func IsUUID4

func IsUUID4(str string) bool

IsUUID4 check the provided string is valid UUID version 4 or not

func IsUUID5

func IsUUID5(str string) bool

IsUUID5 check the provided string is valid UUID version 5 or not

Types

type MapData

type MapData map[string][]string

MapData represents basic data structure for govalidator Rules and Messages

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 New

func New(opts Options) *Validator

New return a new validator object using provided options

func (*Validator) SetDefaultRequired

func (v *Validator) SetDefaultRequired(required bool)

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

func (v *Validator) SetUniqueKey(unique bool)

SetUniqueKey represents struct field name with prefix of struct name helps to stop collission between same field name in embeded struct

func (*Validator) Validate

func (v *Validator) Validate() url.Values

Validate validate request data like form-data, x-www-form-urlencoded and query params see example in README.md file

func (*Validator) ValidateMapJSON

func (v *Validator) ValidateMapJSON() url.Values

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

func (v *Validator) ValidateStructJSON() url.Values

ValidateStructJSON validate request data from JSON body to Go struct see example in README.md file

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL