tovalidate

package
v7.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 12 Imported by: 140

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAlphanumericDash

func IsAlphanumericDash(str string) bool

IsAlphanumericDash returns true if the string consists of only alphanumeric or dash characters.

func IsAlphanumericUnderscoreDash

func IsAlphanumericUnderscoreDash(str string) bool

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

func IsOneOfString(set ...string) func(string) bool

IsOneOfString generates a validator function returning whether a passed string is in the given set of strings.

func IsOneOfStringICase

func IsOneOfStringICase(set ...string) func(string) bool

IsOneOfStringICase is a case-insensitive version of IsOneOfString.

func IsPtrToSliceOfUniqueStringersICase

func IsPtrToSliceOfUniqueStringersICase(set ...string) func(interface{}) error

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

func NoLineBreaks(str string) bool

NoLineBreaks returns true if the string has no line breaks.

func NoPeriods

func NoPeriods(str string) bool

NoPeriods returns true if the string has no periods.

func NoSpaces

func NoSpaces(str string) bool

NoSpaces returns true if the string has no spaces.

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

func ToError(err map[string]error) error

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

func ToErrors(err map[string]error) []error

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.

Jump to

Keyboard shortcuts

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