validation

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoComparisonFuncRegistered     = errors.New("no comparison func registered")
	ErrTypeConversionFailed           = errors.New("type conversion failed")
	ErrComparisonFailed               = errors.New("comparison failed")
	ErrUnexpectedComparisonTargetType = errors.New("unexpected comparison target value type")
	ErrUnexpectedComparisonActualType = errors.New("unexpected comparison actual value type")
)

Functions

func CompareAttrValues

func CompareAttrValues(av attr.Value, op CompareOp, target interface{}, meta ...interface{}) error

CompareAttrValues attempts to execute a comparison between the provided attribute value and the targeted value.

If there is no comparison function registered for the target type, an ErrNoComparisonFuncRegistered is returned.

If a function is registered and the comparison fails, an ErrComparisonFailed error will be returned

func ComparisonFailedError added in v1.0.0

func ComparisonFailedError(actual interface{}, op CompareOp, target interface{}) error

func DefaultComparisonFuncs

func DefaultComparisonFuncs() map[string]ComparisonFunc

DefaultComparisonFuncs returns the complete list of default comparison functions

func IsComparisonFailedError added in v1.0.0

func IsComparisonFailedError(err error) bool

func IsNoComparisonFuncRegisteredError added in v1.0.0

func IsNoComparisonFuncRegisteredError(err error) bool

func IsTypeConversionFailedError added in v1.0.0

func IsTypeConversionFailedError(err error) bool

func IsUnexpectedAttributeValueTypeError added in v1.0.0

func IsUnexpectedAttributeValueTypeError(err error) bool

func IsUnexpectedComparisonActualType added in v1.0.0

func IsUnexpectedComparisonActualType(err error) bool

func NewValidator

func NewValidator(desc, md string, fn TestFunc, skipNull, skipUnknown bool) *baseAttributeValidator

func NoComparisonFuncRegisteredError added in v1.0.0

func NoComparisonFuncRegisteredError(op CompareOp, t interface{}) error

func SetComparisonFunc

func SetComparisonFunc(targetType interface{}, fn ComparisonFunc)

SetComparisonFunc sets a comparison function to use for comparing attribute values to values of the specified type

func TypeConversionFailedError added in v1.0.0

func TypeConversionFailedError(err error) error

func UnexpectedComparisonActualTypeError added in v1.0.0

func UnexpectedComparisonActualTypeError(scope string, actualActual interface{}, op CompareOp, expectedActual interface{}, err error) error

func UnexpectedComparisonTargetTypeError added in v1.0.0

func UnexpectedComparisonTargetTypeError(scope string, actualTarget interface{}, op CompareOp, expectedTarget interface{}, err error) error

Types

type AttributeValidator

type AttributeValidator interface {
	tfsdk.AttributeValidator
	WithDescription(string) AttributeValidator
	WithMarkdownDescription(string) AttributeValidator
}

func Compare

func Compare(op CompareOp, target interface{}, meta ...interface{}) AttributeValidator

Compare executes the specified comparison to the target value for an attribute.

Type comparisons

func EnvVarValued

func EnvVarValued() AttributeValidator

EnvVarValued returns a validator that asserts a given attribute's value is the name of an environment variable that is valued at runtime

func FileIsReadable

func FileIsReadable() AttributeValidator

FileIsReadable returns a validator that asserts a given attribute's value is a local file that is readable.

func IsDurationString

func IsDurationString() AttributeValidator

IsDurationString returns a validator that asserts a given attribute's value is a valid time.Duration string

func IsURL

func IsURL() AttributeValidator

IsURL returns a validator that asserts a given attribute value is parseable as a URL

func Length

func Length(minL, maxL int) AttributeValidator

Length asserts an attribute's length is limited to the specified bounds. The allowed types are:

  • string
  • set
  • map
  • list

func MutuallyExclusiveSibling

func MutuallyExclusiveSibling(siblingAttr string) AttributeValidator

MutuallyExclusiveSibling returns a validator that ensures that an attribute only carries a value when another sibling attribute does not.

Sibling is defined as another attribute that is at the same step depth as the source attribute

func MutuallyInclusiveSibling

func MutuallyInclusiveSibling(siblingAttr string) AttributeValidator

func RegexpMatch

func RegexpMatch(r string) AttributeValidator

RegexpMatch returns a validator that asserts an attribute's value matches the provided expression

func RegexpNotMatch

func RegexpNotMatch(r string) AttributeValidator

RegexpNotMatch returns a validator that asserts an attribute's value does not match the provided expression

func Required

func Required() AttributeValidator

Required returns a validator that asserts a field is configured with any value at all.

type CompareOp

type CompareOp string
const (
	Equal                CompareOp = "=="
	LessThan             CompareOp = "<"
	LessThanOrEqualTo    CompareOp = "<="
	GreaterThan          CompareOp = ">"
	GreaterThanOrEqualTo CompareOp = ">="
	NotEqual             CompareOp = "<>"
	OneOf                CompareOp = "|"
	NotOneOf             CompareOp = "^|"
)

func (CompareOp) Name added in v1.0.0

func (op CompareOp) Name() string

func (CompareOp) String

func (op CompareOp) String() string

type ComparisonFunc

type ComparisonFunc func(av attr.Value, op CompareOp, target interface{}, meta ...interface{}) error

ComparisonFunc executes a specific comparison of an attribute value to the targeted value. You are guaranteed that the target type will be the type or one of the types the function was registered with. If you register a single func with more than type target type, you must perform type assertion / conversion yourself.

The returned error is expected to be testable for type:

nil 					- Comparison succeeded
ErrComparisonFailed 	- Must be returned when any comparison operation fails
ErrTypeConversionFailed - Must be returned if the function performs an internal type conversion before comparison that errored
any other error			- Treated as unhandled error

To see the default list of functions, see DefaultComparisonFuncs.

To register a new function or overwrite an existing function, see SetComparisonFunc

func GetComparisonFunc

func GetComparisonFunc(targetType interface{}) (ComparisonFunc, bool)

GetComparisonFunc attempts to return a previously registered comparison function for a specified op : type combination

type TestFunc

func CompareTest

func CompareTest(op CompareOp, target interface{}, meta ...interface{}) TestFunc

CompareTest executes a registered comparison function against the target attribute's value

func LengthTest

func LengthTest(minL, maxL int) TestFunc

LengthTest is an AttributeValidator implementation that attempts to find a length value appropriate for the attribute value type, asserting that it is within the specified bounds.

If either min or max is set to -1, then that value is unbounded and thus not verified.

func MutuallyExclusiveSiblingTest

func MutuallyExclusiveSiblingTest(siblingAttr string) TestFunc

MutuallyExclusiveSiblingTest ensures that a given attribute is not valued when another one is, and vice versa.

func MutuallyInclusiveSiblingTest

func MutuallyInclusiveSiblingTest(siblingAttr string) TestFunc

MutuallyInclusiveSiblingTest ensures that a given attribute is valued when a sibling attribute is also valued

func RegexpMatchTest

func RegexpMatchTest(r string) TestFunc

RegexpMatchTest is an AttributeValidator implementation that will first attempt to convert the value of a field to a string, then see if that resulting string matches the provided expression.

func RegexpNotMatchTest

func RegexpNotMatchTest(r string) TestFunc

RegexpNotMatchTest is an AttributeValidator implementation that will first attempt to convert the value of a field to a string, then see if that resulting string matches the provided expression.

func RequiredTest

func RequiredTest() TestFunc

RequiredTest is an AttributeValidator implementation that will register an error if the attribute has no value of any kind

func TestEnvVarValued

func TestEnvVarValued() TestFunc

TestEnvVarValued asserts that a given attribute value is the name of an environment variable that is valued at runtime

func TestFileIsReadable

func TestFileIsReadable() TestFunc

TestFileIsReadable attempts to open and subsequently read a single byte from the file at the path specified by the attribute value.

func TestIsDurationString

func TestIsDurationString() TestFunc

TestIsDurationString asserts that a given attribute's value is a valid time.Duration string

func TestIsURL

func TestIsURL() TestFunc

TestIsURL asserts that the provided value can be parsed by url.Parse()

Jump to

Keyboard shortcuts

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