rules

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package rules interfaces for rules and rule sets as well as standard implementations for "any" rule sets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyRuleSet

type AnyRuleSet struct {
	NoConflict[any]
	// contains filtered or unexported fields
}

AnyRuleSet implements RuleSet for the "any" interface. Use when you don't care about the date type passed in and want to return it unaltered from the Validate method.

See also: WrapAny which also implements the "any" interface and wraps another RuleSet.

func Any

func Any() *AnyRuleSet

Any creates a new Any rule set.

func (*AnyRuleSet) Any

func (v *AnyRuleSet) Any() RuleSet[any]

Any is an identity function for this implementation and returns the current rule set.

func (*AnyRuleSet) Apply added in v0.3.0

func (v *AnyRuleSet) Apply(ctx context.Context, input, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the value to the output or a ValidationErrorCollection.

func (*AnyRuleSet) Evaluate added in v0.2.0

Evaluate performs a validation of a RuleSet against a value and returns a value of the same type as the wrapped RuleSet or a ValidationErrorCollection. The wrapped rules are called before any rules added directly to the WrapAnyRuleSet.

For WrapAny, Evaluate is identical to ValidateWithContext except for the argument order.

func (*AnyRuleSet) Required

func (v *AnyRuleSet) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*AnyRuleSet) String added in v0.2.0

func (ruleSet *AnyRuleSet) String() string

String returns a string representation of the rule set suitable for debugging.

func (*AnyRuleSet) WithForbidden

func (v *AnyRuleSet) WithForbidden() *AnyRuleSet

WithForbidden returns a new child rule set with the forbidden flag set. Use WithForbidden when a value is expected to always be nil or omitted.

func (*AnyRuleSet) WithRequired

func (v *AnyRuleSet) WithRequired() *AnyRuleSet

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

func (*AnyRuleSet) WithRule

func (v *AnyRuleSet) WithRule(rule Rule[any]) *AnyRuleSet

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface explicitly for the "any" interface.

Use this when implementing custom rules.

func (*AnyRuleSet) WithRuleFunc

func (v *AnyRuleSet) WithRuleFunc(rule RuleFunc[any]) *AnyRuleSet

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function explicitly for the "any" interface.

Use this when implementing custom rules.

type Conditional added in v0.3.0

type Conditional[T any, TK comparable] interface {
	Rule[T]
	KeyRules() []Rule[TK] // Return all key rules that the rule depends on
}

Conditional interface must be implemented for rules that are passed into WithConditionalKey. They must implement all of the standard rule methods as well as a method Keys which should return an array of all the keys names that must be present and error free for the rule to evaluate.

ObjectRuleSet[T] implements this interface out of the box.

type ConstantRuleSet added in v0.3.0

type ConstantRuleSet[T comparable] struct {
	// contains filtered or unexported fields
}

ConstantRuleSet implements RuleSet that returns an error for any value that does not match the constant.

This is primary used for conditional To test a constant of a specific type it is usually best to use that type.

func Constant added in v0.3.0

func Constant[T comparable](value T) *ConstantRuleSet[T]

Constant creates a new Constant rule set for the specified value. This function will return the same Rule Set when called multiple times with the same value.

func (*ConstantRuleSet[T]) Any added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) Any() RuleSet[any]

Any is an identity function for this implementation and returns the current rule set.

func (*ConstantRuleSet[T]) Apply added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) Apply(ctx context.Context, input, output any) errors.ValidationErrorCollection

Apply validates a RuleSet against an input value and assigns the validated value to output. It returns a ValidationErrorCollection.

func (*ConstantRuleSet[T]) Conflict added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) Conflict(other Rule[T]) bool

Conflict returns true for all rules since by definition no rule can be a superset of it.

func (*ConstantRuleSet[T]) Evaluate added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against a value and returns any errors.

func (*ConstantRuleSet[T]) Required added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*ConstantRuleSet[T]) String added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*ConstantRuleSet[T]) Value added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) Value() T

Value returns the constant value in the correct type.

func (*ConstantRuleSet[T]) WithRequired added in v0.3.0

func (ruleSet *ConstantRuleSet[T]) WithRequired() *ConstantRuleSet[T]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

type FloatRuleSet added in v0.3.0

type FloatRuleSet[T floating] struct {
	NoConflict[T]
	// contains filtered or unexported fields
}

Implementation of RuleSet for floats.

func Float32 added in v0.3.0

func Float32() *FloatRuleSet[float32]

Float32 creates a new float32 RuleSet.

func Float64 added in v0.3.0

func Float64() *FloatRuleSet[float64]

Float64 creates a new float64 RuleSet.

func (*FloatRuleSet[T]) Any added in v0.3.0

func (v *FloatRuleSet[T]) Any() RuleSet[any]

Any returns a new RuleSet that wraps the number RuleSet in any Any rule set which can then be used in nested validation.

func (*FloatRuleSet[T]) Apply added in v0.3.0

func (v *FloatRuleSet[T]) Apply(ctx context.Context, input any, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the result to the output parameter. It returns a ValidationErrorCollection if any validation errors occur.

func (*FloatRuleSet[T]) Evaluate added in v0.3.0

func (v *FloatRuleSet[T]) Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against a float value and returns a float value of the same type or a ValidationErrorCollection.

func (*FloatRuleSet[T]) Required added in v0.3.0

func (v *FloatRuleSet[T]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*FloatRuleSet[T]) String added in v0.3.0

func (ruleSet *FloatRuleSet[T]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*FloatRuleSet[T]) WithMax added in v0.3.0

func (v *FloatRuleSet[T]) WithMax(max T) *FloatRuleSet[T]

WithMax returns a new child RuleSet that is constrained to the provided maximum value.

func (*FloatRuleSet[T]) WithMin added in v0.3.0

func (v *FloatRuleSet[T]) WithMin(min T) *FloatRuleSet[T]

WithMin returns a new child RuleSet that is constrained to the provided minimum value.

func (*FloatRuleSet[T]) WithRequired added in v0.3.0

func (v *FloatRuleSet[T]) WithRequired() *FloatRuleSet[T]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

func (*FloatRuleSet[T]) WithRounding added in v0.3.0

func (v *FloatRuleSet[T]) WithRounding(rounding Rounding, precision int) *FloatRuleSet[T]

WithRounding returns a new child RuleSet with the rounding rule set to the supplied value.

Standard warnings for floating point numbers apply: - Some numbers cannot be represented precisely with floating points. - Sometimes the rounded result may have additional precision when the rounded number cannot be exactly represented. - For best results, consider using int for your math and data storage/transfer.

func (*FloatRuleSet[T]) WithRule added in v0.3.0

func (ruleSet *FloatRuleSet[T]) WithRule(rule Rule[T]) *FloatRuleSet[T]

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface for the given number type.

Use this when implementing custom rules.

func (*FloatRuleSet[T]) WithRuleFunc added in v0.3.0

func (v *FloatRuleSet[T]) WithRuleFunc(rule RuleFunc[T]) *FloatRuleSet[T]

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function for the given number type.

Use this when implementing custom rules.

func (*FloatRuleSet[T]) WithStrict added in v0.3.0

func (v *FloatRuleSet[T]) WithStrict() *FloatRuleSet[T]

WithStrict returns a new child RuleSet with the strict flag applied. A strict rule will only validate if the value is already the correct type.

With number types, any type will work in strict mode as long as it can be converted deterministically and without loss.

type IntRuleSet added in v0.3.0

type IntRuleSet[T integer] struct {
	NoConflict[T]
	// contains filtered or unexported fields
}

Implementation of RuleSet for integers.

func Int added in v0.3.0

func Int() *IntRuleSet[int]

Int creates a new integer RuleSet.

func Int16 added in v0.3.0

func Int16() *IntRuleSet[int16]

Int16 creates a new 16 bit integer RuleSet.

func Int32 added in v0.3.0

func Int32() *IntRuleSet[int32]

Int32 creates a new 32 bit integer RuleSet.

func Int64 added in v0.3.0

func Int64() *IntRuleSet[int64]

Int64 creates a new int64 RuleSet.

func Int8 added in v0.3.0

func Int8() *IntRuleSet[int8]

Int8 creates a new 8 bit integer RuleSet.

func Uint added in v0.3.0

func Uint() *IntRuleSet[uint]

Uint creates a new unsigned integer RuleSet.

func Uint16 added in v0.3.0

func Uint16() *IntRuleSet[uint16]

Uint16 creates a new unsigned 16 bit integer RuleSet.

func Uint32 added in v0.3.0

func Uint32() *IntRuleSet[uint32]

Uint32 creates a new unsigned 32 bit integer RuleSet.

func Uint64 added in v0.3.0

func Uint64() *IntRuleSet[uint64]

Uint64 creates a new unsigned 64 bit integer RuleSet.

func Uint8 added in v0.3.0

func Uint8() *IntRuleSet[uint8]

Uint8 creates a new unsigned 8 bit integer RuleSet.

func (*IntRuleSet[T]) Any added in v0.3.0

func (v *IntRuleSet[T]) Any() RuleSet[any]

Any returns a new RuleSet that wraps the number RuleSet in any Any rule set which can then be used in nested validation.

func (*IntRuleSet[T]) Apply added in v0.3.0

func (ruleSet *IntRuleSet[T]) Apply(ctx context.Context, input any, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the result to the output parameter. It returns a ValidationErrorCollection if any validation errors occur.

func (*IntRuleSet[T]) Evaluate added in v0.3.0

func (v *IntRuleSet[T]) Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against an integer value and returns an integer value of the same type or a ValidationErrorCollection.

func (*IntRuleSet[T]) Required added in v0.3.0

func (v *IntRuleSet[T]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*IntRuleSet[T]) String added in v0.3.0

func (ruleSet *IntRuleSet[T]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*IntRuleSet[T]) WithAllowedValues added in v0.3.0

func (ruleSet *IntRuleSet[T]) WithAllowedValues(value T, rest ...T) *IntRuleSet[T]

WithAllowedValues returns a new child RuleSet that is checked against the provided list of allowed values.

This method can be called more than once and the allowed values are cumulative. Allowed values must still pass all other rules.

func (*IntRuleSet[T]) WithBase added in v0.3.0

func (v *IntRuleSet[T]) WithBase(base int) *IntRuleSet[T]

WithBase returns a new child rule set with the number base set. The base will be used to convert strings to numbers. The base has no effect if the RuleSet is strict since strict sets will not convert types.

The default is base 10.

func (*IntRuleSet[T]) WithMax added in v0.3.0

func (v *IntRuleSet[T]) WithMax(max T) *IntRuleSet[T]

WithMax returns a new child RuleSet that is constrained to the provided maximum value.

func (*IntRuleSet[T]) WithMin added in v0.3.0

func (v *IntRuleSet[T]) WithMin(min T) *IntRuleSet[T]

WithMin returns a new child RuleSet that is constrained to the provided minimum value.

func (*IntRuleSet[T]) WithRejectedValues added in v0.3.0

func (ruleSet *IntRuleSet[T]) WithRejectedValues(value T, rest ...T) *IntRuleSet[T]

WithRejectedValues returns a new child RuleSet that is checked against the provided list of values hat should be rejected. This method can be called more than once.

Rejected values will always be rejected even if they are in the allowed values list.

func (*IntRuleSet[T]) WithRequired added in v0.3.0

func (v *IntRuleSet[T]) WithRequired() *IntRuleSet[T]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

func (*IntRuleSet[T]) WithRounding added in v0.3.0

func (v *IntRuleSet[T]) WithRounding(rounding Rounding) *IntRuleSet[T]

WithRounding returns a new child RuleSet with the rounding rule set to the supplied value.

Notes on floating point numbers: The RuleSet will attempt to convert floating point numbers to integers even if rounding is not enabled. If the number is not within tolerance (1e-9) of a whole number, an error will be returned.

func (*IntRuleSet[T]) WithRule added in v0.3.0

func (ruleSet *IntRuleSet[T]) WithRule(rule Rule[T]) *IntRuleSet[T]

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface for the given number type.

Use this when implementing custom rules.

func (*IntRuleSet[T]) WithRuleFunc added in v0.3.0

func (v *IntRuleSet[T]) WithRuleFunc(rule RuleFunc[T]) *IntRuleSet[T]

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function for the given number type.

Use this when implementing custom rules.

func (*IntRuleSet[T]) WithStrict added in v0.3.0

func (v *IntRuleSet[T]) WithStrict() *IntRuleSet[T]

WithStrict returns a new child RuleSet with the strict flag applied. A strict rule will only validate if the value is already the correct type.

With number types, any type will work in strict mode as long as it can be converted deterministically and without loss.

type InterfaceRuleSet added in v0.3.0

type InterfaceRuleSet[T any] struct {
	NoConflict[T]
	// contains filtered or unexported fields
}

InterfaceRuleSet implements RuleSet for the a generic interface.

func Interface added in v0.3.0

func Interface[T any]() *InterfaceRuleSet[T]

Interface creates a new Interface rule set.

func (*InterfaceRuleSet[T]) Any added in v0.3.0

func (v *InterfaceRuleSet[T]) Any() RuleSet[any]

Interface is an identity function for this implementation and returns the current rule set.

func (*InterfaceRuleSet[T]) Apply added in v0.3.0

func (ruleSet *InterfaceRuleSet[T]) Apply(ctx context.Context, input any, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the result to the output parameter. It returns a ValidationErrorCollection if any validation errors occur.

func (*InterfaceRuleSet[T]) Evaluate added in v0.3.0

func (v *InterfaceRuleSet[T]) Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against all the defined rules.

func (*InterfaceRuleSet[T]) Required added in v0.3.0

func (v *InterfaceRuleSet[T]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*InterfaceRuleSet[T]) String added in v0.3.0

func (ruleSet *InterfaceRuleSet[T]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*InterfaceRuleSet[T]) WithCast added in v0.3.0

func (v *InterfaceRuleSet[T]) WithCast(fn func(ctx context.Context, value any) (T, errors.ValidationErrorCollection)) *InterfaceRuleSet[T]

WithCast creates a new Interface rule set that has the set cast function. The cast function should take "any" and return a value of the appropriate interface type. Run will always try to directly cast the value. Adding a function is useful for when the value may need to be wrapped in another type in order to satisfy the interface.

Cast functions are stacking, You may call this function as many times as you need in order to cast from different type. Newly defined cast functions take priority. Execution will stop at the first function to return a non-nil value or an error collection.

A third boolean return value is added to differentiate between a successful cast to a nil value and

func (*InterfaceRuleSet[T]) WithRequired added in v0.3.0

func (v *InterfaceRuleSet[T]) WithRequired() *InterfaceRuleSet[T]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

func (*InterfaceRuleSet[T]) WithRule added in v0.3.0

func (v *InterfaceRuleSet[T]) WithRule(rule Rule[T]) *InterfaceRuleSet[T]

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface explicitly for the "any" interface.

Use this when implementing custom rules.

func (*InterfaceRuleSet[T]) WithRuleFunc added in v0.3.0

func (v *InterfaceRuleSet[T]) WithRuleFunc(rule RuleFunc[T]) *InterfaceRuleSet[T]

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function explicitly for the "any" interface.

Use this when implementing custom rules.

type NoConflict added in v0.2.0

type NoConflict[T any] struct {
}

func (NoConflict[T]) Conflict added in v0.2.0

func (_ NoConflict[T]) Conflict(_ Rule[T]) bool

type ObjectRuleSet added in v0.3.0

type ObjectRuleSet[T any, TK comparable, TV any] struct {
	NoConflict[T]
	// contains filtered or unexported fields
}

Implementation of RuleSet for objects and maps.

func Map added in v0.3.0

func Map[TK comparable, TV any]() *ObjectRuleSet[map[TK]TV, TK, TV]

NewObjectMap returns a new RuleSet that can be used to validate maps with strings as the keys and the specified data type (which can be "any") as the values.

func StringMap added in v0.3.0

func StringMap[T any]() *ObjectRuleSet[map[string]T, string, T]

StringMap returns a new RuleSet that can be used to validate maps with strings as the keys and the specified data type (which can be "any") as the values.

func Struct added in v0.3.0

func Struct[T any]() *ObjectRuleSet[T, string, any]

Struct returns a RuleSet that can be used to validate an struct of an arbitrary type.

Using the "validate" annotation you can may input values to different properties of the object. This is useful for converting unstructured maps created from Json and converting to an object.

func (*ObjectRuleSet[T, TK, TV]) Any added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) Any() RuleSet[any]

Any returns a new RuleSet that wraps the object RuleSet in any Any rule set which can then be used in nested validation.

func (*ObjectRuleSet[T, TK, TV]) Apply added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) Apply(ctx context.Context, value any, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the result to the output parameter. It returns a ValidationErrorCollection if any validation errors occur.

func (*ObjectRuleSet[T, TK, TV]) Evaluate added in v0.3.0

func (ruleSet *ObjectRuleSet[T, TK, TV]) Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against a value of the object type and returns a ValidationErrorCollection.

func (*ObjectRuleSet[T, TK, TV]) Key deprecated added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) Key(key TK, ruleSet RuleSet[TV]) *ObjectRuleSet[T, TK, TV]

Deprecated: Key is deprecated and will be removed in v1.0.0. Use WithKey instead.

func (*ObjectRuleSet[T, TK, TV]) KeyRules added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) KeyRules() []Rule[TK]

Keys returns the keys names that have rule sets associated with them. This will not return keys that don't have rule sets (even if they do have a mapping).

It also will not return keys that are referenced WithRule or WithRuleFund. To get around this you may want to consider moving your rule set to WithKey or putting a simple permissive validator inside WithKey.

The results are not sorted. You should not depend on the order of the results.

func (*ObjectRuleSet[T, TK, TV]) Required added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*ObjectRuleSet[T, TK, TV]) String added in v0.3.0

func (ruleSet *ObjectRuleSet[T, TK, TV]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*ObjectRuleSet[T, TK, TV]) WithConditionalDynamicBucket added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithConditionalDynamicBucket(keyRule Rule[TK], condition Conditional[T, TK], bucket TK) *ObjectRuleSet[T, TK, TV]

WithConditionalDynamicBucket behaves like WithDynamicBucket except the value is not sorted into the bucket unless the condition is met.

If the only dynamic rules are conditional, the key will be considered unknown if no conditions match.

func (*ObjectRuleSet[T, TK, TV]) WithConditionalKey added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithConditionalKey(key TK, condition Conditional[T, TK], ruleSet RuleSet[TV]) *ObjectRuleSet[T, TK, TV]

WithConditionalKey returns a new Rule with a validation rule for the specified key.

It takes as an argument a Rule that is used to evaluate the entire object or map. If it returns a nil error then the conditional key Rule will be evaluated.

Errors returned from the conditional Rule are not considered validation failures and will not be returned from the Validate / Evaluate functions. Errors in the conditional are only used to determine if the Rule should be evaluated.

Conditional rules will be run any time after all fields they depend on are evaluated. For example if the conditional rule set looks for keys X and Y then the conditional will not be evaluated until all the rules for both X and Y have also been evaluated. This includes conditional So if X is also dependent on Z then Z will also need to be complete.

If one or more of the fields has an error then the conditional rule will not be run.

WithRule and WithRuleFunc are both evaluated after any keys or conditional keys. Because of this, it is not possible to have a conditional key that is dependent on data that is modified in those

If nil is passed in as the conditional then this method behaves identical to WithKey.

This method will panic immediately if a circular dependency is detected.

func (*ObjectRuleSet[T, TK, TV]) WithDynamicBucket added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithDynamicBucket(keyRule Rule[TK], bucket TK) *ObjectRuleSet[T, TK, TV]

WithDynamicBucket tells the Rule Set to put matching keys into specific buckets. A bucket is expected to be a map with the key type (string for structs targets or variable for map) and a value type that matches the expected value.

To avoid runtime errors it is usually best to also add a validation rule for the key using WithDynamic key to ensure the value is the correct type.

This method is designed for unknown and dynamic keys only. If you have any explicit rules for your key, it will not be put into the dynamic bucket.

If a key matches the dynamic bucket key rules then it will no longer be considered "unknown" and will not trigger an unknown key error. You are encouraged to add additional validation rules for the values.

If a key belongs to more than one bucket it will be included in all of them.

For structs:

When WithDynamicBucket is called this function will panic if the bucket property does not exist on the struct or
bucket property is not a map.
The value of the property will be nil until at least one key matches.

For maps:

Running the rule set will panic if the value type is not "any" since any other type of value will not allow the bucket
map to be created.
The value of the bucket key in the map will not exist unless at least one key matches.

func (*ObjectRuleSet[T, TK, TV]) WithDynamicKey added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithDynamicKey(keyRule Rule[TK], ruleSet RuleSet[TV]) *ObjectRuleSet[T, TK, TV]

WithDynamicKey returns a new RuleSet with a validation rule for any key that matches the key rule. Dynamic rules are run even if they match a key that has an already defined rule. Mappings are not applied to dynamic

If more than one call is made with the same key or overlapping dynamic rules, than all will be evaluated. However, the order in which they are run is not guaranteed.

Multiple rule sets may run in parallel but only one will run a time for each key since rule sets can return a mutated value. This is true even for constant value keys and other dynamic rules if the patterns overlap.

If a key matches the key rules of any unconditional dynamic rule it will no longer be considered an "unknown" key.

With maps, the dynamic keys are directly set on the output map. For structs you must set a dynamic key bucket using WithDynamicBucket.

func (*ObjectRuleSet[T, TK, TV]) WithJson added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithJson() *ObjectRuleSet[T, TK, TV]

WithJson allows the input to be a Json encoded string.

func (*ObjectRuleSet[T, TK, TV]) WithKey added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithKey(key TK, ruleSet RuleSet[TV]) *ObjectRuleSet[T, TK, TV]

WithKey returns a new RuleSet with a validation rule for the specified key.

If more than one call is made with the same key than all will be evaluated. However, the order in which they are run is not guaranteed.

Multiple rule sets may run in parallel but only one will run a time for each key since rule sets can return a mutated value.

func (*ObjectRuleSet[T, TK, TV]) WithRequired added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithRequired() *ObjectRuleSet[T, TK, TV]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

func (*ObjectRuleSet[T, TK, TV]) WithRule added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithRule(rule Rule[T]) *ObjectRuleSet[T, TK, TV]

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface for the given object type.

Use this when implementing custom

func (*ObjectRuleSet[T, TK, TV]) WithRuleFunc added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithRuleFunc(rule RuleFunc[T]) *ObjectRuleSet[T, TK, TV]

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function for the given object type.

Use this when implementing custom

func (*ObjectRuleSet[T, TK, TV]) WithUnknown added in v0.3.0

func (v *ObjectRuleSet[T, TK, TV]) WithUnknown() *ObjectRuleSet[T, TK, TV]

WithUnknown returns a new RuleSet with the "unknown" flag set.

By default if the validator fines an unknown key on a map it will return an error. Setting the unknown flag will allow keys that aren't defined to be present in the map. This is useful for parsing arbitrary Json where additional keys may be included.

type Rounding added in v0.3.0

type Rounding int

Rounding type is used to specify how a floating point number should be converted to a number with lower precision.

These values are not guaranteed to be unchanged between versions. Don't use for serialization or cross-process communication.

const (
	RoundingNone     Rounding = iota // Default. No rounding will be performed and return an error if the number is not already rounded.
	RoundingUp                       // Ceil. Always round up.
	RoundingDown                     // Floor. Always round down.
	RoundingHalfEven                 // "Bankers rounding." Round to the nearest even number.
	RoundingHalfUp                   // Always round half values up.
)

func (Rounding) String added in v0.3.0

func (r Rounding) String() string

String returns the string value for the rounding. Useful for debugging.

type Rule

type Rule[T any] interface {
	// Evaluate takes in a context and value and returns any validation errors.
	Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

	// Conflict returns true if two rules should not co-exist.
	// It may be used to remove duplicate rules when the new rule conflicts the existing rule.
	// The new rule should be kept and the old rule should be disabled.
	//
	// For example, if minimum is called a second time with a higher value, the new value should be taken.
	// If both rules are kept then the effective minimum is the smaller of the two.
	//
	// This method may return false if the previous rule should never be discarded.
	//
	// This method should be called even if the two rule types are not the same.
	Conflict(Rule[T]) bool

	// Returns the string representation of the rule for debugging.
	String() string
}

type RuleFunc

type RuleFunc[T any] func(ctx context.Context, value T) errors.ValidationErrorCollection

RuleFunc implements the Rule interface for functions.

func (RuleFunc[T]) Conflict added in v0.2.0

func (rule RuleFunc[T]) Conflict(_ Rule[T]) bool

Conflict always returns false for rule functions. To perform deduplication, implement the interface instead.

func (RuleFunc[T]) Evaluate

func (rule RuleFunc[T]) Evaluate(ctx context.Context, value T) errors.ValidationErrorCollection

Evaluate calls the rule function and returns the results.

func (RuleFunc[T]) String added in v0.2.0

func (rule RuleFunc[T]) String() string

String always returns WithRule(func()) for function rules. To use a different string, implement the interface instead.

type RuleSet

type RuleSet[T any] interface {
	Rule[T]
	Apply(ctx context.Context, value any, out any) errors.ValidationErrorCollection // Apply attempts to coerce the value into the correct type and evaluates all rules in the rule set, then assigns the results to an interface.
	Any() RuleSet[any]                                                              // Any returns an implementation of rule sets for the "any" type that wraps a typed RuleSet so that the set can be used in nested objects and arrays.
	Required() bool                                                                 // Returns true if the value is not allowed to be omitted when nested under other rule sets.
	String() string                                                                 // Converts the rule set to a string for printing and debugging.
}

RuleSet interface is used to define a collection of Rules that logically apply to the same value.

type SliceRuleSet added in v0.3.0

type SliceRuleSet[T any] struct {
	NoConflict[[]T]
	// contains filtered or unexported fields
}

Implementation of RuleSet for arrays of a given type.

func Slice added in v0.3.0

func Slice[T any]() *SliceRuleSet[T]

NewInt creates a new array RuleSet.

func (*SliceRuleSet[T]) Any added in v0.3.0

func (v *SliceRuleSet[T]) Any() RuleSet[any]

Any returns a new RuleSet that wraps the array RuleSet in any Any rule set which can then be used in nested validation.

func (*SliceRuleSet[T]) Apply added in v0.3.0

func (v *SliceRuleSet[T]) Apply(ctx context.Context, input any, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the result to the output parameter. It returns a ValidationErrorCollection if any validation errors occur.

func (*SliceRuleSet[T]) Evaluate added in v0.3.0

func (ruleSet *SliceRuleSet[T]) Evaluate(ctx context.Context, value []T) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against a the array/slice type and returns a value of the same type or a ValidationErrorCollection.

func (*SliceRuleSet[T]) Required added in v0.3.0

func (v *SliceRuleSet[T]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*SliceRuleSet[T]) String added in v0.3.0

func (ruleSet *SliceRuleSet[T]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*SliceRuleSet[T]) WithItemRuleSet added in v0.3.0

func (v *SliceRuleSet[T]) WithItemRuleSet(itemRules RuleSet[T]) *SliceRuleSet[T]

WithItemRuleSet takes a new rule set to use to validate array items and returns a new child rule set.

If this function is called more than once, only the most recent one will be used to validate the items. If you don't set an item rule set then the validator will attempt to cast the items to the correct type and perform no additional validation.

func (*SliceRuleSet[T]) WithMaxLen added in v0.3.0

func (v *SliceRuleSet[T]) WithMaxLen(max int) *SliceRuleSet[T]

WithMaxLen returns a new child RuleSet that is constrained to the provided maximum array/slice length.

func (*SliceRuleSet[T]) WithMinLen added in v0.3.0

func (v *SliceRuleSet[T]) WithMinLen(min int) *SliceRuleSet[T]

WithMinLen returns a new child RuleSet that is constrained to the provided minimum array/slice length.

func (*SliceRuleSet[T]) WithRequired added in v0.3.0

func (v *SliceRuleSet[T]) WithRequired() *SliceRuleSet[T]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted. Required has no effect on integer if the RuleSet is strict since nil is not a valid number.

func (*SliceRuleSet[T]) WithRule added in v0.3.0

func (v *SliceRuleSet[T]) WithRule(rule Rule[[]T]) *SliceRuleSet[T]

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface for the given array and item type.

Use this when implementing custom

func (*SliceRuleSet[T]) WithRuleFunc added in v0.3.0

func (v *SliceRuleSet[T]) WithRuleFunc(rule RuleFunc[[]T]) *SliceRuleSet[T]

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function for the given array and item type.

Use this when implementing custom

type StringRuleSet added in v0.3.0

type StringRuleSet struct {
	NoConflict[string]
	// contains filtered or unexported fields
}

Implementation of RuleSet for strings.

func String added in v0.3.0

func String() *StringRuleSet

String returns the base StringRuleSet.

func (*StringRuleSet) Any added in v0.3.0

func (v *StringRuleSet) Any() RuleSet[any]

Any returns a new RuleSet that wraps the string RuleSet in any Any rule set which can then be used in nested validation.

func (*StringRuleSet) Apply added in v0.3.0

func (v *StringRuleSet) Apply(ctx context.Context, value, output any) errors.ValidationErrorCollection

Apply performs a validation of a RuleSet against a value and assigns the resulting string to the output pointer a ValidationErrorCollection.

func (*StringRuleSet) Evaluate added in v0.3.0

Evaluate performs a validation of a RuleSet against a string value and returns a string value or a ValidationErrorCollection.

func (*StringRuleSet) Required added in v0.3.0

func (v *StringRuleSet) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*StringRuleSet) String added in v0.3.0

func (ruleSet *StringRuleSet) String() string

String returns a string representation of the rule set suitable for debugging.

func (*StringRuleSet) WithAllowedValues added in v0.3.0

func (ruleSet *StringRuleSet) WithAllowedValues(value string, rest ...string) *StringRuleSet

WithAllowedValues returns a new child RuleSet that is checked against the provided list of allowed values.

This method can be called more than once and the allowed values are cumulative. Allowed values must still pass all other rules.

func (*StringRuleSet) WithMaxLen added in v0.3.0

func (v *StringRuleSet) WithMaxLen(max int) *StringRuleSet

WithMaxLen returns a new child RuleSet that is constrained to the provided maximum string length.

func (*StringRuleSet) WithMinLen added in v0.3.0

func (v *StringRuleSet) WithMinLen(min int) *StringRuleSet

WithMinLen returns a new child RuleSet that is constrained to the provided minimum string length.

func (*StringRuleSet) WithRegexp added in v0.3.0

func (v *StringRuleSet) WithRegexp(exp *regexp.Regexp, errorMsg string) *StringRuleSet

WithRegexp returns a new child RuleSet that is constrained to the provided regular expression. The second parameter is the error text, which will be localized if a translation is available.

func (*StringRuleSet) WithRegexpString added in v0.3.0

func (v *StringRuleSet) WithRegexpString(exp, errorMsg string) *StringRuleSet

WithRegexpString returns a new child RuleSet that is constrained to the provided regular expression. The second parameter is the error text, which will be localized if a translation is available.

This method panics if the expression cannot be compiled.

func (*StringRuleSet) WithRejectedValues added in v0.3.0

func (ruleSet *StringRuleSet) WithRejectedValues(value string, rest ...string) *StringRuleSet

WithRejectedValues returns a new child RuleSet that is checked against the provided list of values hat should be rejected. This method can be called more than once.

Rejected values will always be rejected even if they are in the allowed values list.

func (*StringRuleSet) WithRequired added in v0.3.0

func (v *StringRuleSet) WithRequired() *StringRuleSet

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

func (*StringRuleSet) WithRule added in v0.3.0

func (ruleSet *StringRuleSet) WithRule(rule Rule[string]) *StringRuleSet

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface for the string type.

Use this when implementing custom rules.

func (*StringRuleSet) WithRuleFunc added in v0.3.0

func (v *StringRuleSet) WithRuleFunc(rule RuleFunc[string]) *StringRuleSet

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function for the string type.

Use this when implementing custom rules.

func (*StringRuleSet) WithStrict added in v0.3.0

func (v *StringRuleSet) WithStrict() *StringRuleSet

WithStrict returns a new child RuleSet with the strict flag applied. A strict rule will only validate if the value is already a string.

type WrapAnyRuleSet

type WrapAnyRuleSet[T any] struct {
	NoConflict[any]
	// contains filtered or unexported fields
}

WrapAnyRuleSet implements RuleSet for the "any" interface and wraps around another type of rule set. Use it when you need to use a more specific RuleSet in a nested validator or to pass into a function.

Unless you are implementing a brand new RuleSet you probably want to use the .Any() method on the RuleSet itself instead, which usually returns this interface.

func WrapAny

func WrapAny[T any](inner RuleSet[T]) *WrapAnyRuleSet[T]

WrapAny wraps an existing RuleSet in an "Any" rule set which can then be used to pass into nested validators or any function where the type of RuleSet is not known ahead of time.

Unless you are implementing a brand new RuleSet you probably want to use the .Any() method on the RuleSet itself instead, which usually calls this function.

func (*WrapAnyRuleSet[T]) Any

func (v *WrapAnyRuleSet[T]) Any() RuleSet[any]

Any is an identity function for this implementation and returns the current rule set.

func (*WrapAnyRuleSet[T]) Apply added in v0.3.0

func (v *WrapAnyRuleSet[T]) Apply(ctx context.Context, input, output any) errors.ValidationErrorCollection

Run performs a validation of a RuleSet against a value and returns a value of the same type as the wrapped RuleSet or a ValidationErrorCollection. The wrapped rules are called before any rules added directly to the WrapAnyRuleSet.

func (*WrapAnyRuleSet[T]) Evaluate added in v0.2.0

func (ruleSet *WrapAnyRuleSet[T]) Evaluate(ctx context.Context, value any) errors.ValidationErrorCollection

Evaluate performs a validation of a RuleSet against a value of any type. If the input value implements the same type as the wrapped RuleSet then Evaluate is called directly, otherwise Apply is called. This approach is usually more efficient since it does not need to allocate an output variable.

func (*WrapAnyRuleSet[T]) Required

func (v *WrapAnyRuleSet[T]) Required() bool

Required returns a boolean indicating if the value is allowed to be omitted when included in a nested object.

func (*WrapAnyRuleSet[T]) String added in v0.2.0

func (ruleSet *WrapAnyRuleSet[T]) String() string

String returns a string representation of the rule set suitable for debugging.

func (*WrapAnyRuleSet[T]) WithRequired

func (v *WrapAnyRuleSet[T]) WithRequired() *WrapAnyRuleSet[T]

WithRequired returns a new child rule set with the required flag set. Use WithRequired when nesting a RuleSet and the a value is not allowed to be omitted.

Required defaults to the value of the wrapped RuleSet so if it is already required then there is no need to call this again.

func (*WrapAnyRuleSet[T]) WithRule

func (v *WrapAnyRuleSet[T]) WithRule(rule Rule[any]) *WrapAnyRuleSet[T]

WithRule returns a new child rule set with a rule added to the list of rules to evaluate. WithRule takes an implementation of the Rule interface explicitly for the "any" interface.

If you want to add a rule directly to the wrapped RuleSet you must do it before wrapping it.

Use this when implementing custom rules.

func (*WrapAnyRuleSet[T]) WithRuleFunc

func (v *WrapAnyRuleSet[T]) WithRuleFunc(rule RuleFunc[any]) *WrapAnyRuleSet[T]

WithRuleFunc returns a new child rule set with a rule added to the list of rules to evaluate. WithRuleFunc takes an implementation of the Rule function explicitly for the "any" interface.

If you want to add a rule directly to the wrapped RuleSet you must do it before wrapping it.

Use this when implementing custom rules.

Directories

Path Synopsis
Package net provides RuleSet implementations for dealing with networks and networked services.
Package net provides RuleSet implementations for dealing with networks and networked services.
Package net provides RuleSet implementations for dealing with time and dates.
Package net provides RuleSet implementations for dealing with time and dates.

Jump to

Keyboard shortcuts

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