isdef

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: Apache-2.0 Imports: 9 Imported by: 126

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IsDuration = Is("is a duration", func(path llpath.Path, v interface{}) *llresult.Results {
	if _, ok := v.(time.Duration); ok {
		return llresult.ValidResult(path)
	}
	return llresult.SimpleResult(
		path,
		false,
		fmt.Sprintf("Expected a time.duration, got '%v' which is a %T", v, v),
	)
})

IsDuration tests that the given value is a duration.

View Source
var IsNil = Is("is nil", func(path llpath.Path, v interface{}) *llresult.Results {
	if v == nil {
		return llresult.ValidResult(path)
	}
	return llresult.SimpleResult(
		path,
		false,
		fmt.Sprintf("Value %#v is not nil", v),
	)
})

IsNil tests that a value is nil.

View Source
var IsNonEmptyString = Is("is a non-empty string", func(path llpath.Path, v interface{}) *llresult.Results {
	strV, errorResults := isStrCheck(path, v)
	if errorResults != nil {
		return errorResults
	}

	if len(strV) == 0 {
		return llresult.SimpleResult(path, false, "String '%s' should not be empty", strV)
	}

	return llresult.ValidResult(path)
})

IsNonEmptyString checks that the given value is a string and has a length > 1.

View Source
var IsString = Is("is a string", func(path llpath.Path, v interface{}) *llresult.Results {
	_, errorResults := isStrCheck(path, v)
	if errorResults != nil {
		return errorResults
	}

	return llresult.ValidResult(path)
})

IsString checks that the given value is a string.

View Source
var KeyMissing = IsDef{Name: "check key not present", CheckKeyMissing: true}

KeyMissing checks that the given key is not present defined.

View Source
var KeyPresent = IsDef{Name: "check key present"}

KeyPresent checks that the given key is in the map, even if it has a nil value.

Functions

func MustRegisterEqual

func MustRegisterEqual(fn interface{})

MustRegisterEqual is the panic-ing equivalent of RegisterEqual.

func RegisterEqual

func RegisterEqual(fn interface{}) error

RegisterEqual takes a function of the form fn(v someType) IsDef and registers it to check equality for that type.

Types

type InvalidEqualFnError

type InvalidEqualFnError struct {
	// contains filtered or unexported fields
}

InvalidEqualFnError is the error type returned by RegisterEqual when there is an issue with the given function.

func (InvalidEqualFnError) Error

func (e InvalidEqualFnError) Error() string

type IsDef

type IsDef struct {
	Name            string
	Checker         ValueValidator
	Optional        bool
	CheckKeyMissing bool
}

An IsDef defines the type of Check to do. Generally only Name and Checker are set. Optional and CheckKeyMissing are needed for weird checks like key presence.

func Is

func Is(name string, checker ValueValidator) IsDef

Is creates a named IsDef with the given Checker.

func IsAny

func IsAny(of ...IsDef) IsDef

IsAny takes a variable number of IsDef's and combines them with a logical OR. If any single definition matches the key will be marked as valid.

func IsDeepEqual

func IsDeepEqual(to interface{}) IsDef

IsDeepEqual checks equality using reflect.DeepEqual.

func IsEqual

func IsEqual(to interface{}) IsDef

IsEqual tests that the given object is equal to the actual object.

func IsEqualToTime

func IsEqualToTime(to time.Time) IsDef

IsEqualToTime ensures that the actual value is the given time, regardless of zone.

func IsIntGt

func IsIntGt(than int) IsDef

IsIntGt tests that a value is an int greater than.

func IsSliceOf

func IsSliceOf(validator validator.Validator) IsDef

IsSliceOf validates that the array at the given key is an array of objects all validatable via the given validator.Validator.

func IsStringContaining

func IsStringContaining(needle string) IsDef

IsStringContaining validates that the the actual value contains the specified substring.

func IsStringMatching

func IsStringMatching(regexp *regexp.Regexp) IsDef

IsStringMatching checks whether a value matches the given regexp.

func IsUnique

func IsUnique() IsDef

IsUnique instances are used in multiple spots, flagging a value as being in error if it's seen across invocations. To use it, assign IsUnique to a variable, then use that variable multiple times in a map[string]interface{}.

func Optional

func Optional(id IsDef) IsDef

Optional wraps an IsDef to mark the field's presence as Optional.

func (IsDef) Check

func (id IsDef) Check(path llpath.Path, v interface{}, keyExists bool) *llresult.Results

Check runs the IsDef at the given value at the given path

type UniqScopeTracker

type UniqScopeTracker map[interface{}]string

UniqScopeTracker is represents the tracking data for invoking IsUniqueTo.

func ScopedIsUnique

func ScopedIsUnique() UniqScopeTracker

ScopedIsUnique returns a new scope for uniqueness checks.

func (UniqScopeTracker) IsUniqueTo

func (ust UniqScopeTracker) IsUniqueTo(namespace string) IsDef

IsUniqueTo validates that the given value is only ever seen within a single namespace.

type ValueValidator

type ValueValidator func(path llpath.Path, v interface{}) *llresult.Results

A ValueValidator is used to validate a value in an interface{}.

Jump to

Keyboard shortcuts

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