Documentation ¶
Overview ¶
Package tovalidate contains helpful utility functions and ozzo-validator-compatible rules and rule generators for validating data in the Traffic Ops API.
Actually, the functionality shouldn't be specific to Traffic Ops (usually), but that is the only place that uses it at the time of this writing.
Index ¶
- func IsAlphanumericDash(str string) bool
- func IsAlphanumericUnderscoreDash(str string) bool
- func IsGreaterThanZero(value interface{}) error
- func IsOneOfString(set ...string) func(string) bool
- func IsOneOfStringICase(set ...string) func(string) bool
- func IsPtrToSliceOfUniqueStringersICase(set ...string) func(interface{}) error
- func IsValidIPv6CIDROrAddress(value interface{}) error
- func IsValidPortNumber(value interface{}) error
- func NoLineBreaks(str string) bool
- func NoPeriods(str string) bool
- func NoSpaces(str string) bool
- func StringIsValidFloat() *validation.StringRule
- func ToError(err map[string]error) error
- func ToErrors(err map[string]error) []error
- type DBExistsRule
- type DBUniqueRule
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAlphanumericDash ¶
IsAlphanumericDash returns true if the string consists of only alphanumeric or dash characters.
func IsAlphanumericUnderscoreDash ¶
IsAlphanumericUnderscoreDash returns true if the string consists of only alphanumeric, underscore, or dash characters.
func IsGreaterThanZero ¶
func IsGreaterThanZero(value interface{}) error
IsGreaterThanZero returns an error if the given value is not nil and is not a pointer to a value that is strictly greater than zero.
The argument to this function must be a pointer to an int or a float64 - other types will cause it to return an error (not panic).
func IsOneOfString ¶
IsOneOfString generates a validator function returning whether a passed string is in the given set of strings.
func IsOneOfStringICase ¶
IsOneOfStringICase is a case-insensitive version of IsOneOfString.
func IsPtrToSliceOfUniqueStringersICase ¶
IsPtrToSliceOfUniqueStringersICase returns a validator function which returns an error if the argument is a non-nil pointer to a slice of fmt.Stringers whose String() values are not in the set of strings or if the argument contains more than one entry that produce the same value from their String() method.
func IsValidIPv6CIDROrAddress ¶
func IsValidIPv6CIDROrAddress(value interface{}) error
IsValidIPv6CIDROrAddress returns an error if the given value is not a pointer to a string that is a valid IPv6 address with optional CIDR-notation network prefix.
The argument to this function must be a pointer to a string - other types will cause it to return an error (not panic).
func IsValidPortNumber ¶
func IsValidPortNumber(value interface{}) error
IsValidPortNumber returns an error if the given value is not nil and is not a valid network port number or a pointer to a valid network port number.
The argument to this function must be either an int or a pointer to an int float64 - other types will cause it to return an error (not panic).
func NoLineBreaks ¶
NoLineBreaks returns true if the string has no line breaks.
func StringIsValidFloat ¶
func StringIsValidFloat() *validation.StringRule
StringIsValidFloat returns a reference to a validation.StringRule function that only returns true if the string value given string argument can be parsed to a 64-bit float that is not NaN.
func ToError ¶
ToError converts a map of strings to errors into a single error.
Because multiple errors are collapsed, errors cannot be wrapped and therefore error identity cannot be preserved.
Example ¶
errs := map[string]error{ "propA": errors.New("bad value"), "propB": errors.New("cannot be blank"), } err := ToError(errs).Error() // Iteration order of Go maps is random, so this is the best we can do. fmt.Println( err == "'propA' bad value, 'propB' cannot be blank" || err == "'propB' cannot be blank, 'propA' bad value", )
Output: true
func ToErrors ¶
ToErrors converts a map of strings to errors into an array of errors.
This is accomplished using `fmt.Errorf("'%v' %v", key, value)` where 'key' is the map key and 'value' is the error value to which it points - this means that error identity is NOT preserved. For example:
errMap := map[string]error{ "sql.ErrNoRows": sql.ErrNoRows, } errs := ToErrors(errMap) if errors.Is(errs[0], sql.ErrNoRows) { fmt.Println("true") } else { fmt.Println("false") }
... will output 'false'.
Types ¶
type DBExistsRule ¶
type DBExistsRule struct {
// contains filtered or unexported fields
}
DBExistsRule is a rule used to check if a given value is in a certain column of a specific table.
DBExistsRule is not known to be used anywhere, and likely only has meaning for Traffic Ops internals even if it was. Therefore, its use is discouraged as it may be removed/relocated in the future.
func NewDBExistsRule ¶
func NewDBExistsRule(db *sqlx.DB, table string, column string) *DBExistsRule
NewDBExistsRule creates a new validation rule that checks if a value is in the given column of the given table.
func (*DBExistsRule) Error ¶
func (r *DBExistsRule) Error(message string) *DBExistsRule
Error sets the error message for the rule.
func (*DBExistsRule) Validate ¶
func (r *DBExistsRule) Validate(value interface{}) error
Validate checks if the given value is valid or not according to this rule.
type DBUniqueRule ¶
type DBUniqueRule struct {
// contains filtered or unexported fields
}
DBUniqueRule is a rule used to check if a given value is in a certain column of a specific table, and that there is exactly one row containing that value in said table.
DBUniqueRule is not known to be used anywhere, and likely only has meaning for Traffic Ops internals even if it was. Therefore, its use is discouraged as it may be removed/relocated in the future.
func NewDBUniqueRule ¶
func NewDBUniqueRule(db *sqlx.DB, table string, column string, idCheck func(int) bool) *DBUniqueRule
NewDBUniqueRule creates a validation rule that checks if a value is in the given column of the given table, and that there is exactly one row containing that value in said table.
The idCheck function must be given and must be capable of determining uniqueness of the single numeric ID parameter given to it. Note that the DBUniqueRule is, therefore, incapable of verifying the uniqueness of a value in a table that uses non-numeric and/or compound keys.
func (*DBUniqueRule) Error ¶
func (r *DBUniqueRule) Error(message string) *DBUniqueRule
Error sets the error message for the rule.
func (*DBUniqueRule) Validate ¶
func (r *DBUniqueRule) Validate(value interface{}) error
Validate returns an error if the value already exists in the table in this column.