validations

package
v0.0.0-...-294a506 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AddressLength = 20
)

Variables

View Source
var (
	ErrInvalidBase64           = errors.New("invalid base64 encoding")
	ErrInvalidBase64URL        = errors.New("invalid base64url encoding")
	ErrInvalidBase64RawURL     = errors.New("invalid base64rawurl encoding")
	ErrInvalidBIC              = errors.New("invalid BIC")
	ErrInvalidBCP47LanguageTag = errors.New("invalid BCP47 language tag")
	// ErrInvalidCreditCard is returned when the provided credit card number is invalid
	ErrInvalidCreditCard = errors.New("invalid credit card number")
	// ErrInvalidMongoID is returned when the provided string is not a valid MongoDB object ID.
	ErrInvalidMongoID         = errors.New("invalid MongoDB object ID")
	ErrInvalidCron            = errors.New("invalid CRON expression")
	ErrInvalidDatetime        = errors.New("invalid Datetime")
	ErrInvalidE164PhoneNumber = errors.New("invalid E.164 phone number")
	ErrInvalidEmail           = errors.New("invalid email address")
)
View Source
var (
	// ErrEmptyIPAddress is returned when IP address is empty.
	ErrEmptyIPAddress = errors.New("IP address cannot be empty")
	// ErrInvalidIPAddress is returned when IP address is invalid.
	ErrInvalidIPAddress = errors.New("invalid IP address")
	// ErrEmptyHostname is returned when hostname is empty.
	ErrEmptyHostname = errors.New("hostname cannot be empty")
	// ErrHostnameTooLong is returned when hostname is too long.
	ErrHostnameTooLong = errors.New("hostname is too long")
	// ErrInvalidHostname is returned when hostname is invalid.
	ErrInvalidHostname = errors.New("invalid hostname")
	// ErrInvalidIPv6Address is returned when IPv6 address is invalid.
	ErrInvalidIPv6Address = errors.New("invalid IPv6 address")
	// ErrExpectedIPv6Address is returned when IPv6 address is expected.
	ErrExpectedIPv6Address = errors.New("IPv6 address expected")
	// ErrInvalidIPv4Address is returned when IPv4 address is invalid.
	ErrInvalidIPv4Address = errors.New("invalid IPv4 address")
	// ErrExpectedIPv4Address is returned when IPv4 address is expected.
	ErrExpectedIPv4Address = errors.New("IPv4 address expected")
	// ErrEmptyMACAddress is returned when MAC address is empty.
	ErrEmptyMACAddress = errors.New("MAC address cannot be empty")
	// ErrInvalidMACAddress is returned when MAC address is invalid.
	ErrInvalidMACAddress = errors.New("invalid MAC address")
	// ErrEmptyURL is returned when URL is empty.
	ErrEmptyURL = errors.New("URL cannot be empty")
	// ErrInvalidURL is returned when URL is invalid.
	ErrInvalidURL = errors.New("invalid URL")
	// ErrInvalidCIDRv4 is returned when a CIDRv4 address is invalid.
	ErrInvalidCIDRv4 = errors.New("invalid CIDRv4 address")
	// ErrInvalidCIDRv6 is returned when a CIDRv6 address is invalid.
	ErrInvalidCIDRv6 = errors.New("invalid CIDRv6 address")
	// ErrInvalidDataURL is returned when a data URL is invalid.
	ErrInvalidDataURL = errors.New("invalid data URL")
	// ErrInvalidFQDN is returned when a fully qualified domain name is invalid.
	ErrInvalidFQDN = errors.New("invalid FQDN")
	// ErrInvalidRFC952Hostname is returned when an RFC 952 hostname is invalid.
	ErrInvalidRFC952Hostname = errors.New("invalid RFC 952 hostname")
	// ErrInvalidTCP4Addr is returned when ValidateTCP4Addr is given an invalid TCPv4 address.
	ErrInvalidTCP4Addr = errors.New("invalid TCPv4 address")
	// ErrInvalidTCP6Addr is returned when ValidateTCP6Addr is given an invalid TCPv6 address.
	ErrInvalidTCP6Addr = errors.New("invalid TCPv6 address")
	// ErrInvalidTCPAddr is returned when ValidateTCPAddr is given an invalid TCP address.
	ErrInvalidTCPAddr = errors.New("invalid TCP address")
	// ErrInvalidUDP4Addr is returned when ValidateUDP4Addr is given an invalid UDPv4 address.
	ErrInvalidUDP4Addr = errors.New("invalid UDPv4 address")
	// ErrInvalidUDP6Addr is returned when ValidateUDP6Addr is given an invalid UDPv6 address.
	ErrInvalidUDP6Addr = errors.New("invalid UDPv6 address")
	// ErrInvalidUDPAddr is returned when ValidateUDPAddr is given an invalid UDP address.
	ErrInvalidUDPAddr = errors.New("invalid UDP address")
	// ErrInvalidUnixAddr is returned when ValidateUnixAddr is given an invalid Unix domain socket address.
	ErrInvalidUnixAddr = errors.New("invalid Unix domain socket address")
	// ErrInvalidURI is returned when ValidateURI is given an invalid URI.
	ErrInvalidURI = errors.New("invalid URI")
	// ErrInvalidHTTPURL is returned when ValidateHTTPURL is given an invalid HTTP or HTTPS URL.
	ErrInvalidHTTPURL = errors.New("invalid HTTP or HTTPS URL")
	// ErrInvalidURLEncoded is returned when ValidateURLEncoded is given invalid URL-encoded data.
	ErrInvalidURLEncoded = errors.New("invalid URL-encoded data")
	// ErrInvalidURNRFC2141 is returned when ValidateURNRFC2141 is given an invalid URN according to RFC 2141.
	ErrInvalidURNRFC2141 = errors.New("invalid URN according to RFC 2141")
)
View Source
var (
	ErrNotAlpha               = errors.New("string is not alpha")
	ErrNotAlphanumeric        = errors.New("string is not alphanumeric")
	ErrNotAlphanumericUnicode = errors.New("string is not alphanumeric unicode")
	ErrNotAlphaUnicode        = errors.New("string is not alpha unicode")
	ErrNotASCIICode           = errors.New("string is not ASCII code")
	ErrNotBoolean             = errors.New("string is not a boolean")
	ErrDoesNotContain         = errors.New("string does not contain the substring")
	ErrDoesNotContainAny      = errors.New("string does not contain any of the substrings")
	ErrDoesNotContainRune     = errors.New("string does not contain the rune")
	ErrEndsWith               = errors.New("string ends with the substring")
	ErrEndsNotWith            = errors.New("string does not end with the substring")
	ErrExcludes               = errors.New("string includes the substring")
	ErrExcludesAll            = errors.New("string does not include all of the substrings")
	ErrIncludesAll            = errors.New("string does not include all of the substrings")
	ErrExcludesRune           = errors.New("string includes the rune")
	ErrNotLowerCase           = errors.New("string is not lowercase")
	ErrNotUpercase            = errors.New("string is not uppercase")
	ErrNotMultibyte           = errors.New("string does not contain one or more multibyte characters")
	ErrNotNumeric             = errors.New("string is not numeric")
	ErrNotPrintableASCII      = errors.New("string contains non-printable ASCII characters")
	ErrStartsWith             = errors.New("string starts with the substring")
	ErrNotStartsWith          = errors.New("string does not start with the substring")
)

Functions

func FloatIsGreaterThan

func FloatIsGreaterThan[T float64](value T, min T) bool

func FloatIsGreaterThanOrEqualTo

func FloatIsGreaterThanOrEqualTo[T float64](value T, min T) bool

func FloatIsInRange

func FloatIsInRange[T float64](value T, min T, max T) bool

func FloatIsLessThan

func FloatIsLessThan[T float64](value T, max T) bool

func FloatIsLessThanOrEqualTo

func FloatIsLessThanOrEqualTo[T float64](value T, max T) bool

func FloatIsMultipleOf

func FloatIsMultipleOf[T float64](value T, multiple T) bool

func FloatIsNegative

func FloatIsNegative[T float64](value T) bool

func FloatIsNonZero

func FloatIsNonZero[T float64](value T) bool

func FloatIsPositive

func FloatIsPositive[T float64](value T) bool

func FloatIsZero

func FloatIsZero[T float64](value T) bool

func IsBCP47LanguageTag

func IsBCP47LanguageTag(str string) error

IsBCP47LanguageTag checks if the given string is a valid BCP47 language tag.

func IsBIC

func IsBIC(str string) error

IsBIC checks if the given string is a valid BIC (Bank Identifier Code).

func IsBTCAddress

func IsBTCAddress(str string) error

IsBTCAddress checks if the given string is a valid Bitcoin address.

func IsBase64

func IsBase64(str string) error

IsBase64 checks if the given string is a valid base64 encoding.

func IsBase64RawURL

func IsBase64RawURL(str string) error

IsBase64RawURL checks if the given string is a valid base64rawurl encoding.

func IsBase64URL

func IsBase64URL(str string) error

IsBase64URL checks if the given string is a valid base64url encoding.

func IsDivisibleBy

func IsDivisibleBy[T int](value T, divisor T) bool

func IsEthAddress

func IsEthAddress(s string) bool

IsEthAddress checks if the given string is a valid etherum address

func IsEven

func IsEven[T int](value T) bool

func IsGreaterThan

func IsGreaterThan[T int](value T, min T) bool

func IsGreaterThanOrEqualTo

func IsGreaterThanOrEqualTo[T int](value T, min T) bool

func IsInRange

func IsInRange[T int](value T, min T, max T) bool

func IsLessThan

func IsLessThan[T int](value T, max T) bool

func IsLessThanOrEqualTo

func IsLessThanOrEqualTo[T int](value T, max T) bool

func IsMultipleOf

func IsMultipleOf[T int](value T, multiple T) bool

func IsNegative

func IsNegative[T int](value T) bool

func IsNonZero

func IsNonZero[T int](value T) bool

func IsOdd

func IsOdd[T int](value T) bool

func IsValidCreditCard

func IsValidCreditCard(str string) error

func IsValidCron

func IsValidCron(str string) error

IsValidCron checks if the given string is a valid CRON expression.

func IsValidDatetime

func IsValidDatetime(str string) error

IsValidDatetime checks if the given string is a valid datetime in the format of "YYYY-MM-DD HH:MM:SS".

func IsValidE164PhoneNumber

func IsValidE164PhoneNumber(str string) error

IsValidE164PhoneNumber checks if the given string is a valid E.164 phone number.

func IsValidEmail

func IsValidEmail(str string) error

IsValidEmail checks if the given string is a valid email address.

func IsValidMongoID

func IsValidMongoID(str string) error

IsValidMongoID checks if the given string is a valid MongoDB object ID.

func IsZero

func IsZero[T int](value T) bool

func SliceIsPresent

func SliceIsPresent(value []interface{}) bool

func SliceIsUnique

func SliceIsUnique[T comparable](value []T, f func(T) interface{}) bool

SliceIsUnique checks if all elements in a slice are unique by applying a function to each element and comparing the result.

Example:

SliceIsUnique([]string{"a", "b", "c"}, func(s string) interface{} { return s })
SliceIsUnique([]int{1, 2, 3}, func(i int) interface{} { return i })
SliceIsUnique([]struct{ A, B int }{{1, 2}, {3, 4}, {5, 6}}, func(s struct{ A, B int }) interface{} { return s.A })

Note: The function f must return a comparable type. See https://golang.org/ref/spec#Comparison_operators

func StringContains

func StringContains(s, substr string) error

StringContains checks if a string contains a substring.

func StringContainsAny

func StringContainsAny(s string, substrs ...string) error

StringContainsAny checks if a string contains any of the substrings.

func StringContainsRune

func StringContainsRune(s string, r rune) error

StringContainsRune checks if a string contains a rune.

func StringEndsNotWith

func StringEndsNotWith(s, substr string) error

StringEndsNotWith checks if a string does not end with a substring.

func StringEndsWith

func StringEndsWith(s, substr string) error

StringEndsWith checks if a string ends with a substring.

func StringExcludes

func StringExcludes(s, substr string) error

StringExcludes checks if a string excludes a substring.

func StringExcludesAll

func StringExcludesAll(s string, substrs ...string) error

StringExcludesAll checks if a string excludes all of the substrings.

func StringExcludesRune

func StringExcludesRune(s string, r rune) error

StringExcludesRune checks if a string excludes a rune.

func StringIsASCIICode

func StringIsASCIICode(s string) error

StringIsASCIICode checks if a string contains only ASCII characters.

func StringIsAlpha

func StringIsAlpha(s string) error

StringIsAlpha checks if a string contains only letters.

func StringIsAlphaUnicode

func StringIsAlphaUnicode(s string) error

StringIsAlphaUnicode checks if a string contains only unicode letters.

func StringIsAlphanumeric

func StringIsAlphanumeric(s string) error

StringIsAlphanumeric checks if a string contains only numbers.

func StringIsAlphanumericUnicode

func StringIsAlphanumericUnicode(s string) error

StringIsAlphaUnicode checks if a string contains only unicode letters.

func StringIsBoolean

func StringIsBoolean(s string) error

StringIsBoolean checks if a string is a boolean.

func StringIsLowerCase

func StringIsLowerCase(s string) error

StringIsLowerCase checks if a string is lowercase.

func StringIsMultibyte

func StringIsMultibyte(s string) error

StringIsMultibyte checks if a string contains one or more multibyte characters.

func StringIsNumeric

func StringIsNumeric(s string) error

StringIsNumeric checks if a string contains only numbers.

func StringIsPrintableASCII

func StringIsPrintableASCII(s string) error

StringIsPrintableASCII checks if a string contains only printable ASCII characters.

func StringIsUpperCase

func StringIsUpperCase(s string) error

StringIsUpperCase checks if a string is uppercase.

func StringStartsNotWith

func StringStartsNotWith(s, substr string) error

StringStartsNotWith checks if a string does not start with a substring.

func StringStartsWith

func StringStartsWith(s, substr string) error

StringStartsWith checks if a string starts with a substring.

func ValidateCIDRv4

func ValidateCIDRv4(cidr string) error

ValidateCIDRv4 validates a CIDRv4 address.

func ValidateCIDRv6

func ValidateCIDRv6(cidr string) error

ValidateCIDRv6 validates a CIDRv6 address.

func ValidateDataURL

func ValidateDataURL(dataURL string) error

ValidateDataURL validates a data URL.

func ValidateFQDN

func ValidateFQDN(fqdn string) error

ValidateFQDN validates a fully qualified domain name.

func ValidateHTTPURL

func ValidateHTTPURL(urlStr string) error

ValidateHTTPURL checks if the given string is a valid HTTP or HTTPS URL.

func ValidateHostname

func ValidateHostname(hostname string) error

ValidateHostname validates a hostname.

func ValidateIPAddress

func ValidateIPAddress(ipAddress string) error

ValidateIPAddress validates an IPv4 or IPv6 address.

func ValidateIPv4Address

func ValidateIPv4Address(ipAddress string) error

ValidateIPv4Address validates an IPv4 address.

func ValidateIPv6Address

func ValidateIPv6Address(ipAddress string) error

ValidateIPv6Address validates an IPv6 address.

func ValidateMACAddress

func ValidateMACAddress(macAddress string) error

ValidateMACAddress validates a MAC address.

func ValidateRFC952

func ValidateRFC952(hostname string) error

ValidateRFC952 validates an RFC 952 hostname.

func ValidateTCP4Addr

func ValidateTCP4Addr(addr string) error

ValidateTCP4Addr checks if the given address is a valid TCPv4 address.

func ValidateTCP6Addr

func ValidateTCP6Addr(addr string) error

ValidateTCP6Addr checks if the given address is a valid TCPv6 address.

func ValidateTCPAddr

func ValidateTCPAddr(addr string) error

ValidateTCPAddr checks if the given address is a valid TCP address.

func ValidateUDP4Addr

func ValidateUDP4Addr(addr string) error

ValidateUDP4Addr checks if the given address is a valid UDPv4 address.

func ValidateUDP6Addr

func ValidateUDP6Addr(addr string) error

ValidateUDP6Addr checks if the given address is a valid UDPv6 address.

func ValidateUDPAddr

func ValidateUDPAddr(addr string) error

ValidateUDPAddr checks if the given address is a valid UDP address.

func ValidateURI

func ValidateURI(uri string) error

ValidateURI checks if the given string is a valid URI.

func ValidateURL

func ValidateURL(urlStr string) error

ValidateURL checks if the given string is a valid URL.

func ValidateURLEncoded

func ValidateURLEncoded(data string) error

ValidateURLEncoded checks if the given string is valid URL-encoded data.

func ValidateURNRFC2141

func ValidateURNRFC2141(urn string) error

ValidateURNRFC2141 checks if the given string is a valid URN according to RFC 2141.

func ValidateUnixAddr

func ValidateUnixAddr(addr string) error

ValidateUnixAddr checks if the given address is a valid Unix domain socket address.

Types

This section is empty.

Jump to

Keyboard shortcuts

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