vString

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

String Validators

Index

Constants

View Source
const (
	Type = "string"

	MandatoryErrorFormat    = "Must not be left empty."
	PatternErrorFormat      = "Does not match pattern '{{.pattern}}'."
	MinRuneErrorFormat      = "Must have more than '{{.min}}' characters."
	MaxRuneErrorFormat      = "Must have less than '{{.max}}' characters."
	BetweenRuneErrorFormat  = "Must be between {{.min}} and {{.max}} characters."
	MustMatchErrorFormat    = "Field does not match '{{.fieldName}}'."
	MatchesErrorFormat      = "Is not in the list, valid matches are '{{.matches|join \"and\"}}'."
	AlphaErrorFormat        = "Should only be alpha characters."
	AlphaDashErrorFormat    = "Should only be alpha characters with underscore and dash."
	AlphaNumericErrorFormat = "Should only be alpha numeric characters."
	CreditCardErrorFormat   = "Not a valid credit card number."
	CSSColorErrorFormat     = "Not valid as css color."
	EmailErrorFormat        = "Not a valid email address."
	IPErrorFormat           = "Not a valid IP address."
	IPV4ErrorFormat         = "Not a valid IPv4 address."
	IPV6ErrorFormat         = "Not a valid IPv6 address."
	URLErrorFormat          = "Not a valid URL."
	UUIDErrorFormat         = "Not a valid UUID."
	UUID3ErrorFormat        = "Not a valid UUID (version 3)."
	UUID4ErrorFormat        = "Not a valid UUID (version 4)."
	UUID5ErrorFormat        = "Not a valid UUID (version 5)."
)
View Source
const (
	// AlphaPattern represents regular expression for alpha characters
	AlphaPattern = "^[a-zA-Z]+$"
	// AlphaDashPattern represents regular expression for alpha characters with underscore and dash
	AlphaDashPattern = "^[a-zA-Z0-9_-]+$"
	// AlphaNumericPattern represents regular expression for alpha numeric characters
	AlphaNumericPattern = "^[a-zA-Z0-9]+$"
	// CreditCardPattern 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
	CreditCardPattern = "" /* 154-byte string literal not displayed */
	// CSSColorPattern represents css valid color code with hex, rgb, rgba, hsl, hsla etc. Ref: http://www.regexpal.com/97509
	CSSColorPattern = "^(#([\\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}\\))$"
	// EmailPattern represents regular expression for email
	EmailPattern = "" /* 133-byte string literal not displayed */
	// IPPattern represents regular expression for ip address
	IPPattern = "^(([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])$"
	// IPV4Pattern represents regular expression for ip address version 4
	IPV4Pattern = "^([0-9]{1,3}\\.){3}[0-9]{1,3}(\\/([0-9]|[1-2][0-9]|3[0-2]))?$"
	// IPV6Pattern represents regular expression for ip address version 6
	IPV6Pattern = `` /* 1057-byte string literal not displayed */
	// URLPattern represents regular expression for url
	URLPattern = "^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$" // Ref: https://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex
	// UUIDPattern represents regular expression for UUID
	UUIDPattern = "^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$"
	// UUID3Pattern represents regular expression for UUID version 3
	UUID3Pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
	// UUID4Pattern represents regular expression for UUID version 4
	UUID4Pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
	// UUID5Pattern represents regular expression for UUID version 5
	UUID5Pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
)

Borrowed the rules from https://github.com/thedevsaddam/govalidator

Variables

This section is empty.

Functions

func Must

func Must(value string, err error) string

func MustMultiple

func MustMultiple(values []string, err error) []string

func Validate

func Validate(value string, rules ...ValidationRule) (string, error)

func ValidateMultiple

func ValidateMultiple(values []string, rules ...ValidationRule) ([]string, error)

Types

type ValidationRule

type ValidationRule func(value *string, hasError bool) error

func Alpha

func Alpha() ValidationRule

Validate Alpha

func AlphaDash

func AlphaDash() ValidationRule

Validate AlphaDash

func AlphaNumeric

func AlphaNumeric() ValidationRule

Validate AlphaNumeric

func BetweenRune

func BetweenRune(min, max int) ValidationRule

Validate minimum and maximum number of characters

func CSSColor

func CSSColor() ValidationRule

Validate CSSColor

func CreditCard

func CreditCard() ValidationRule

Validate CreditCard

func Email

func Email() ValidationRule

Validate Email

func IP

func IP() ValidationRule

Validate IP

func IPV4

func IPV4() ValidationRule

Validate IPV4

func IPV6

func IPV6() ValidationRule

Validate IPV6

func Mandatory

func Mandatory() ValidationRule

Make sure value is set, if not set the rule return a validation error

func Matches

func Matches(matches ...string) ValidationRule

Check for matches, return error if matches is not found

func MaxRune

func MaxRune(max int) ValidationRule

Validate maximum number of characters

func MinRune

func MinRune(min int) ValidationRule

Validate minimum number of characters

func MustMatch

func MustMatch(mustMatch, fieldName string) ValidationRule

Must match field.

func Optional

func Optional(rules ...ValidationRule) ValidationRule

Optional Value, if value is not set, return nil, otherwise go though the validation rules.

func OverrideErrorMsg

func OverrideErrorMsg(validationError vError.ValidationError, rules ...ValidationRule) ValidationRule

Override Error Message

func Pattern

func Pattern(pattern *regexp.Regexp) ValidationRule

Validate Pattern

func URL

func URL() ValidationRule

Validate URL

func UUID

func UUID() ValidationRule

Validate UUID

func UUID3

func UUID3() ValidationRule

Validate UUID3

func UUID4

func UUID4() ValidationRule

Validate UUID4

func UUID5

func UUID5() ValidationRule

Validate UUID5

Jump to

Keyboard shortcuts

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