validator

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSlice       = errors.Base("invalid slice")
	ErrNotCanonicalisable = errors.Base("value cannot be canonicalised")
	ErrNotComparable      = errors.Base("value cannot be compared")
)
View Source
var (
	ErrInvalidRegexp = errors.Base("invalid regexp")
	ErrRegexpParse   = errors.Base("error parsing regexp")
)
View Source
var (
	ErrFailInvoked = errors.Base("fail action was invoked")
)
View Source
var (
	ErrValueNotString = errors.Base("value is not a string")
)

Functions

func BuildPredicateDict

func BuildPredicateDict() dag.PredicateDict

func KindToString

func KindToString(kind FieldKind) string

Types

type Bindings

type Bindings struct {
	Bindings map[reflect.Type]*StructDescriptor
	// contains filtered or unexported fields
}

func NewBindings

func NewBindings() *Bindings

func (*Bindings) Bind

func (b *Bindings) Bind(object Reflectable) (*BoundObject, bool)

func (*Bindings) BindWithReflection

func (b *Bindings) BindWithReflection(object any) (*BoundObject, bool)

func (*Bindings) Build

func (b *Bindings) Build(object Reflectable) (*StructDescriptor, bool)

func (*Bindings) BuildWithReflection

func (b *Bindings) BuildWithReflection(object any) (*StructDescriptor, bool)

func (*Bindings) Register

func (b *Bindings) Register(object *StructDescriptor) bool

type BoundObject

type BoundObject struct {
	Descriptor *StructDescriptor
	Binding    any
}

func (*BoundObject) Get

func (bo *BoundObject) Get(key string) (any, bool)

func (*BoundObject) GetValue

func (bo *BoundObject) GetValue(key string) (any, bool)

Get the value for the given key from the bound object.

This works by using the accessor obtained via reflection during the predicate building phase.

func (*BoundObject) Keys

func (bo *BoundObject) Keys() []string

func (*BoundObject) Set

func (bo *BoundObject) Set(_ string, _ any) bool

func (*BoundObject) String

func (bo *BoundObject) String() string

type FTEQBuilder

type FTEQBuilder struct{}

func (*FTEQBuilder) Build

func (bld *FTEQBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FTEQBuilder) Token

func (bld *FTEQBuilder) Token() string

type FTEQPredicate

type FTEQPredicate struct {
	MetaPredicate
}

Field Type Equality.

This predicate compares the type of the structure's field. If it is equal then the predicate returns true.

func (*FTEQPredicate) Debug

func (pred *FTEQPredicate) Debug() string

func (*FTEQPredicate) Eval

func (pred *FTEQPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FTEQPredicate) Instruction

func (pred *FTEQPredicate) Instruction() string

func (*FTEQPredicate) String

func (pred *FTEQPredicate) String() string

func (*FTEQPredicate) Token

func (pred *FTEQPredicate) Token() string

type FTINBuilder

type FTINBuilder struct{}

func (*FTINBuilder) Build

func (bld *FTINBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FTINBuilder) Token

func (bld *FTINBuilder) Token() string

type FTINPredicate

type FTINPredicate struct {
	MetaPredicate
	// contains filtered or unexported fields
}

Field Type In.

This predicate returns true of the type of a field in the input structure is one of the provided values in the predicate.

func (*FTINPredicate) Debug

func (pred *FTINPredicate) Debug() string

func (*FTINPredicate) Eval

func (pred *FTINPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FTINPredicate) Instruction

func (pred *FTINPredicate) Instruction() string

func (*FTINPredicate) String

func (pred *FTINPredicate) String() string

func (*FTINPredicate) Token

func (pred *FTINPredicate) Token() string

type FVEQBuilder

type FVEQBuilder struct{}

func (*FVEQBuilder) Build

func (bld *FVEQBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVEQBuilder) Token

func (bld *FVEQBuilder) Token() string

type FVEQPredicate

type FVEQPredicate struct {
	MetaPredicate
}

Field Value Equality.

This predicate compares the value to that in the structure. If they are equal then the predicate returns true.

The predicate will take various circumstances into consideration while checking the value:

If the field is `any` then the comparison will match just the type of the value rather than using the type of the field along with the value.

If the field is integer, then the structure's field must have a bit width large enough to hold the value.

func (*FVEQPredicate) Debug

func (pred *FVEQPredicate) Debug() string

func (*FVEQPredicate) Eval

func (pred *FVEQPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVEQPredicate) Instruction

func (pred *FVEQPredicate) Instruction() string

func (*FVEQPredicate) String

func (pred *FVEQPredicate) String() string

func (*FVEQPredicate) Token

func (pred *FVEQPredicate) Token() string

type FVFALSEBuilder

type FVFALSEBuilder struct{}

func (*FVFALSEBuilder) Build

func (bld *FVFALSEBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVFALSEBuilder) Token

func (bld *FVFALSEBuilder) Token() string

type FVFALSEPredicate

type FVFALSEPredicate struct {
	MetaPredicate
}

Field Value is Logically False.

This predicate returns true if the value of the filtered field is logically false.

A logical false value is any value that is empty or zero. The following are examples of this:

"" string, 0 numeric, [] array

Logical falsehood is not the same as `nil`, so if you are looking for nil values then you should look at `FVNIL` instead.

Structures are a special case. They are never logically false. This is because the validator does not recurse into structures. If you wish to deal with structures within structures, then those sub-structures require validation by themselves. How you do that is up to you.

Interfaces are also a special case. An interface can be considered logically false if it is `nil`, but it can also be considered logically false if the wrapped value is zero or empty.

func (*FVFALSEPredicate) Debug

func (pred *FVFALSEPredicate) Debug() string

func (*FVFALSEPredicate) Eval

func (pred *FVFALSEPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVFALSEPredicate) Instruction

func (pred *FVFALSEPredicate) Instruction() string

func (*FVFALSEPredicate) String

func (pred *FVFALSEPredicate) String() string

func (*FVFALSEPredicate) Token

func (pred *FVFALSEPredicate) Token() string

type FVGTBuilder

type FVGTBuilder struct{}

func (*FVGTBuilder) Build

func (bld *FVGTBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVGTBuilder) Token

func (bld *FVGTBuilder) Token() string

type FVGTEBuilder

type FVGTEBuilder struct{}

func (*FVGTEBuilder) Build

func (bld *FVGTEBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVGTEBuilder) Token

func (bld *FVGTEBuilder) Token() string

type FVGTEPredicate

type FVGTEPredicate struct {
	MetaPredicate
}

This predicate returns true if the value in the structure is greater than or equal to the value in the predicate.

func (*FVGTEPredicate) Debug

func (pred *FVGTEPredicate) Debug() string

func (*FVGTEPredicate) Eval

func (pred *FVGTEPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVGTEPredicate) Instruction

func (pred *FVGTEPredicate) Instruction() string

func (*FVGTEPredicate) String

func (pred *FVGTEPredicate) String() string

func (*FVGTEPredicate) Token

func (pred *FVGTEPredicate) Token() string

type FVGTPredicate

type FVGTPredicate struct {
	MetaPredicate
}

This predicate returns true if the value in the structure is greater than the value in the predicate.

func (*FVGTPredicate) Debug

func (pred *FVGTPredicate) Debug() string

func (*FVGTPredicate) Eval

func (pred *FVGTPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVGTPredicate) Instruction

func (pred *FVGTPredicate) Instruction() string

func (*FVGTPredicate) String

func (pred *FVGTPredicate) String() string

func (*FVGTPredicate) Token

func (pred *FVGTPredicate) Token() string

type FVINBuilder

type FVINBuilder struct{}

func (*FVINBuilder) Build

func (bld *FVINBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVINBuilder) Token

func (bld *FVINBuilder) Token() string

type FVINPredicate

type FVINPredicate struct {
	MetaPredicate
	// contains filtered or unexported fields
}

Field Value In.

This predicate returns true if the value in the structure is one of the provided values in the predicate.

func (*FVINPredicate) Debug

func (pred *FVINPredicate) Debug() string

func (*FVINPredicate) Eval

func (pred *FVINPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVINPredicate) Instruction

func (pred *FVINPredicate) Instruction() string

func (*FVINPredicate) String

func (pred *FVINPredicate) String() string

func (*FVINPredicate) Token

func (pred *FVINPredicate) Token() string

type FVLTBuilder

type FVLTBuilder struct{}

func (*FVLTBuilder) Build

func (bld *FVLTBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVLTBuilder) Token

func (bld *FVLTBuilder) Token() string

type FVLTEBuilder

type FVLTEBuilder struct{}

func (*FVLTEBuilder) Build

func (bld *FVLTEBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVLTEBuilder) Token

func (bld *FVLTEBuilder) Token() string

type FVLTEPredicate

type FVLTEPredicate struct {
	MetaPredicate
}

This predicate returns true if the value in the structure is lesser than or equal to the value in the predicate.

func (*FVLTEPredicate) Debug

func (pred *FVLTEPredicate) Debug() string

func (*FVLTEPredicate) Eval

func (pred *FVLTEPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVLTEPredicate) Instruction

func (pred *FVLTEPredicate) Instruction() string

func (*FVLTEPredicate) String

func (pred *FVLTEPredicate) String() string

func (*FVLTEPredicate) Token

func (pred *FVLTEPredicate) Token() string

type FVLTPredicate

type FVLTPredicate struct {
	MetaPredicate
}

This predicate returns true if the value in the structure is lesser than the value in the predicate.

func (*FVLTPredicate) Debug

func (pred *FVLTPredicate) Debug() string

func (*FVLTPredicate) Eval

func (pred *FVLTPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVLTPredicate) Instruction

func (pred *FVLTPredicate) Instruction() string

func (*FVLTPredicate) String

func (pred *FVLTPredicate) String() string

func (*FVLTPredicate) Token

func (pred *FVLTPredicate) Token() string

type FVNEQBuilder

type FVNEQBuilder struct{}

func (*FVNEQBuilder) Build

func (bld *FVNEQBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVNEQBuilder) Token

func (bld *FVNEQBuilder) Token() string

type FVNEQPredicate

type FVNEQPredicate struct {
	FVEQPredicate
}

Field Valie Inequality.

This predicate compares the value to that in the structure. If they are not equal then the predicate returns true.

The predicate will take various circumstances into consideration while checking the value:

If the field is `any` then the comparison will match just the type of the value rather than using the type of the field along with the value.

If the field is integer then the structure's field must have a bit width large enough to hold the value.

func (*FVNEQPredicate) Debug

func (pred *FVNEQPredicate) Debug() string

func (*FVNEQPredicate) Eval

func (pred *FVNEQPredicate) Eval(ctx context.Context, input dag.Filterable) bool

func (*FVNEQPredicate) Instruction

func (pred *FVNEQPredicate) Instruction() string

func (*FVNEQPredicate) String

func (pred *FVNEQPredicate) String() string

func (*FVNEQPredicate) Token

func (pred *FVNEQPredicate) Token() string

type FVNILBuilder

type FVNILBuilder struct{}

func (*FVNILBuilder) Build

func (bld *FVNILBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVNILBuilder) Token

func (bld *FVNILBuilder) Token() string

type FVNILPredicate

type FVNILPredicate struct {
	MetaPredicate
}

Field Value Is Nil.

This predicate returns true if and only if the **reference** value of the filtered field is `nil`.

If the field value is a concrete type (e.g. string, int, float, bool etc), then the predicate will return false.

It only applies to types that can be `nil` in Go--e.g. pointers, slices, maps, interfaces, et al. If you're looking to test whether a field is "logically nil" (e.g. zero, false, empty) then consider using `FVFALSE` instead.

func (*FVNILPredicate) Debug

func (pred *FVNILPredicate) Debug() string

func (*FVNILPredicate) Eval

func (pred *FVNILPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVNILPredicate) Instruction

func (pred *FVNILPredicate) Instruction() string

func (*FVNILPredicate) String

func (pred *FVNILPredicate) String() string

func (*FVNILPredicate) Token

func (pred *FVNILPredicate) Token() string

type FVREMBuilder

type FVREMBuilder struct{}

func (*FVREMBuilder) Build

func (bld *FVREMBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVREMBuilder) Token

func (bld *FVREMBuilder) Token() string

type FVREMPredicate

type FVREMPredicate struct {
	MetaPredicate
	// contains filtered or unexported fields
}

func (*FVREMPredicate) Debug

func (pred *FVREMPredicate) Debug() string

func (*FVREMPredicate) Eval

func (pred *FVREMPredicate) Eval(_ context.Context, input dag.Filterable) bool

func (*FVREMPredicate) Instruction

func (pred *FVREMPredicate) Instruction() string

func (*FVREMPredicate) String

func (pred *FVREMPredicate) String() string

func (*FVREMPredicate) Token

func (pred *FVREMPredicate) Token() string

type FVTRUEBuilder

type FVTRUEBuilder struct{}

func (*FVTRUEBuilder) Build

func (bld *FVTRUEBuilder) Build(key string, val any, lgr logger.Logger, dbg bool) (dag.Predicate, error)

func (*FVTRUEBuilder) Token

func (bld *FVTRUEBuilder) Token() string

type FVTRUEPredicate

type FVTRUEPredicate struct {
	FVFALSEPredicate
}

Field Value is Logically True.

This predicate returns true if the value of the filtered field is logically true.

A logical true value is any value that is not empty or zero.

For more details on how this works, see `FVFALSE`.

func (*FVTRUEPredicate) Debug

func (pred *FVTRUEPredicate) Debug() string

func (*FVTRUEPredicate) Eval

func (pred *FVTRUEPredicate) Eval(ctx context.Context, input dag.Filterable) bool

func (*FVTRUEPredicate) Instruction

func (pred *FVTRUEPredicate) Instruction() string

func (*FVTRUEPredicate) String

func (pred *FVTRUEPredicate) String() string

func (*FVTRUEPredicate) Token

func (pred *FVTRUEPredicate) Token() string

type FieldAccessorFn

type FieldAccessorFn func(any) any

type FieldInfo

type FieldInfo struct {
	Type        reflect.Type
	ElementType reflect.Type
	Accessor    FieldAccessorFn
	Name        string
	TypeName    string
	Tags        reflect.StructTag
	TypeKind    reflect.Kind
	Kind        FieldKind
	ElementKind FieldKind
}

func (*FieldInfo) Debug

func (fi *FieldInfo) Debug(params ...any) *debug.Debug

func (*FieldInfo) String

func (fi *FieldInfo) String() string

type FieldKind

type FieldKind int
const (
	KindPrimitive FieldKind = iota
	KindStruct
	KindSlice
	KindMap
	KindInterface
	KindUnknown
)

type MetaPredicate

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

func (*MetaPredicate) Debug

func (meta *MetaPredicate) Debug(isn, token string) string

func (*MetaPredicate) GetKeyAsFieldInfo

func (meta *MetaPredicate) GetKeyAsFieldInfo(input dag.Filterable) (*FieldInfo, bool)

Return the `Filterable`'s field information.

This is directed through to `BoundObject.Description.Fields`.

func (*MetaPredicate) GetKeyAsFloat64

func (meta *MetaPredicate) GetKeyAsFloat64(input dag.Filterable) (float64, bool)

func (*MetaPredicate) GetKeyAsString

func (meta *MetaPredicate) GetKeyAsString(input dag.Filterable) (string, bool)

func (*MetaPredicate) GetKeyAsValue

func (meta *MetaPredicate) GetKeyAsValue(input dag.Filterable) (any, bool)

Get the value from the `Filterable`.

This equates to `BoundObject.Descriptor.Field[key].Accessor` being called with `BoundObject.Binding`.

See `BoundObject.GetValue` for more.

func (*MetaPredicate) GetValueAsAny

func (meta *MetaPredicate) GetValueAsAny() (any, bool)

func (*MetaPredicate) GetValueAsBool

func (meta *MetaPredicate) GetValueAsBool() (bool, bool)

func (*MetaPredicate) GetValueAsComplex128

func (meta *MetaPredicate) GetValueAsComplex128() (complex128, bool)

func (*MetaPredicate) GetValueAsFloat64

func (meta *MetaPredicate) GetValueAsFloat64() (float64, bool)

func (*MetaPredicate) GetValueAsInt64

func (meta *MetaPredicate) GetValueAsInt64() (int64, bool)

func (*MetaPredicate) GetValueAsString

func (meta *MetaPredicate) GetValueAsString() (string, bool)

Return the condition value as a string.

func (*MetaPredicate) GetValueAsUint64

func (meta *MetaPredicate) GetValueAsUint64() (uint64, bool)

func (*MetaPredicate) String

func (meta *MetaPredicate) String(token string) string

type Reflectable

type Reflectable interface {
	// Return the reflected type for a given object.
	//
	// An example of how this could work is:
	//
	// “`go
	//     var (
	//         typeForYourStruct reflect.Type
	//         onceForYourStruct sync.Once
	//     )
	//
	//     type YourStruct struct {
	//         // ...
	//     }
	//
	//     func (ys *YourStruct) ReflectType() reflect.Type {
	//         onceForYourStruct.Do(func() {
	//             typeForYourStruct = reflect.TypeOf(ys).Elem()
	//         })
	//
	//         return typeForYourStruct
	//     }
	// “`
	//
	// You might need to do things differently for non-pointer types.
	ReflectType() reflect.Type
}

type StructDescriptor

type StructDescriptor struct {
	Type     reflect.Type
	Fields   map[string]*FieldInfo
	TypeName string
}

func BuildDescriptor

func BuildDescriptor(typ reflect.Type) *StructDescriptor

func NewStructDescriptor

func NewStructDescriptor() *StructDescriptor

func (*StructDescriptor) Debug

func (sd *StructDescriptor) Debug(params ...any) *debug.Debug

func (*StructDescriptor) Find

func (sd *StructDescriptor) Find(what string) (any, bool)

func (*StructDescriptor) Get

func (sd *StructDescriptor) Get(key string) (any, bool)

func (*StructDescriptor) Keys

func (sd *StructDescriptor) Keys() []string

func (*StructDescriptor) String

func (sd *StructDescriptor) String() string

type Validator

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

Validator structure.

func NewValidator

func NewValidator(ctx context.Context) *Validator

Create a new validator with the default action set and predicate list.

func (*Validator) ClearFailures

func (v *Validator) ClearFailures()

Clear the list of failure messages.

func (*Validator) Compile

func (v *Validator) Compile(specs []dag.RuleSpec) []error

Compile a slice of rule specs into a DAG graph.

func (*Validator) CompileAction

func (v *Validator) CompileAction(spec dag.ActionSpec) (dag.ActionFn, error)

Compile an action from an action specification.

func (*Validator) CompileFailure

func (v *Validator) CompileFailure(spec dag.FailureSpec) (dag.ActionFn, error)

Compile a failure action from a failure specification.

func (*Validator) Evaluate

func (v *Validator) Evaluate(input dag.Filterable)

Evaluate an input against the validator.

func (*Validator) Export

func (v *Validator) Export(writer io.Writer)

Export the compiler's rulesets to GraphViz DOT format.

func (*Validator) Failures

func (v *Validator) Failures() []error

Return a list of failure messages (if any) generated during validation.

Jump to

Keyboard shortcuts

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