constraint

package
v0.0.0-...-8f66fda Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EQ  ComparisonType = "EQ"
	NEQ                = "NEQ"
	GT                 = "GT"
	GTE                = "GTE"
	LT                 = "LT"
	LTE                = "LTE"
)

Variables

View Source
var (
	Empty    = length("Empty", nil, nil, &uintZero)
	NonEmpty = length("NonEmpty", nil, &uintOne, nil)
)
View Source
var (
	IsNil    = nilConstraint("IsNil", false)
	IsNotNil = nilConstraint("IsNotNil", true)
)
View Source
var (
	IsZero    = zeroConstraint("IsZero", false)
	IsNotZero = zeroConstraint("IsNotZero", true)
)
View Source
var CamelCaseTargetNamer = TransformFieldName(bind.CamelCase)

CamelCaseTargetNamer is a common alternative to rendering the field name in case of a validation error.

View Source
var DefaultValidationTool = NewValidationTool(structs.StructTagDirectiveProvider{"validate"})
View Source
var IsRegexp = stringTest("IsRegexp", nil, func(s string) bool {
	_, err := regexp.Compile(s)
	return err == nil
})
View Source
var Required = New("Required", nil, func(toTest interface{}) gomerr.Gomerr {
	ttv, ok := flect.ReadableIndirectValue(toTest)
	if !ok || ttv.IsZero() {
		return NotSatisfied(toTest)
	}
	return nil
})
View Source
var UseBracketsForContainedTargets = false

UseBracketsForContainedTargets specifies whether the generated target value is in 'json' or 'Gomer' format. The former is consistent w/ the representation emitted by the JsonSchema validation. The latter conforms to the fields directives format. Examples of target values when UseBracketsForContainedTargets is true or false:

type SomeStruct struct {
  Foo struct {
    S []Bar          // true: Foo.S[3]; false: Foo.S.3
    M map[string]Bar // true: Foo.M[cat]; false: Foo.M.cat
  }
}

Note that when UseBracketsForContainedTargets is false, both the key and value will have the same target (e.g. Foo.M.Cat). If the value is true, the key value will be Foo.M.cat and the latter will be Foo.M[cat].

Functions

func Gte

func Gte(ft reflect.Type, compareTo *interface{})

func NewValidationTool

func NewValidationTool(dp structs.DirectiveProvider, optional ...TargetNamer) *structs.Tool

func Register

func Register(name string, constraintOrBuilder interface{}) gomerr.Gomerr

func RegisterEach

func RegisterEach(constraintsAndBuilders map[string]interface{}) gomerr.Gomerr

func Validate

func Validate(v interface{}, validationTool *structs.Tool, optional ...*structs.ToolContext) gomerr.Gomerr

Types

type ComparisonType

type ComparisonType = string

type Constraint

type Constraint interface {
	Type() string
	Parameters() interface{}
	Validate(target string, toTest interface{}) gomerr.Gomerr
	Test(toTest interface{}) gomerr.Gomerr
	String() string
}

func And

func And(constraints ...Constraint) Constraint

func ConfigurationError

func ConfigurationError(problem string) Constraint

func Elements

func Elements(constraint Constraint) Constraint

func EndsWith

func EndsWith(suffix *string) Constraint

func Entries

func Entries(entryConstraint Constraint) Constraint

func Equals

func Equals(value interface{}) Constraint

func Fail

func Fail(msg string) Constraint

func FloatBetween

func FloatBetween(lower, upper *float64) Constraint

FloatBetween determines whether the provided value is (inclusively) between the lower and upper values provided. Stated explicitly, this tests for lower <= value <= upper.

func FloatCompare

func FloatCompare(comparisonType ComparisonType, compareTo *float64) Constraint

FloatCompare compares a tested value to compareTo. While compareTo is an float64, the tested value can be either float32/float64. If the value is not a float type, the constraint will fail.

func IntBetween

func IntBetween(lower, upper *int64) Constraint

IntBetween determines whether the provided value is (inclusively) between the lower and upper values provided. Stated explicitly, this tests for lower <= value <= upper.

func IntCompare

func IntCompare(comparisonType ComparisonType, compareTo *int64) Constraint

IntCompare compares the tested value to compareTo. While compareTo is an int64, the tested value can be any of the integer types (e.g. int, int16, etc). If the tested value is not an integer type, the constraint will fail.

func Length

func Length(values ...*uint64) Constraint

Length determines whether the value's length is either between (inclusively) two provided values (a min and max) or a single value (internally: min = max). This tests for min <= len(value) <= max. The value's type can be one of Array, Chan, Map, Slice, or String. Any other type will result in a false value from the constraint. If min is greater than max or min is less than 0, this will return a Fail() constraint.

func Map

func Map(keyConstraint Constraint, valueConstraint Constraint) Constraint

func MapKeys

func MapKeys(keyConstraint Constraint) Constraint

func MapValues

func MapValues(valueConstraint Constraint) Constraint

func MaxLength

func MaxLength(max *uint64) Constraint

MaxLength determines whether the value's length is less than or equal to the max value provided. Stated explicitly, this tests for len(value) <= max. The value's type can be one of Array, Chan, Map, Slice, or String. Any other type will result in a false value from the constraint.

func MinLength

func MinLength(min *uint64) Constraint

MinLength determines whether the value's length is greater than or equal to the min value provided. Stated explicitly, this tests for min <= len(value). The value's type can be one of Array, Chan, Map, Slice, or String. Any other type will result in a false value from the constraint.

func New

func New(constraintType string, constraintParameters interface{}, testFn func(toTest interface{}) gomerr.Gomerr) Constraint

func Nil

func Nil(value *interface{}) Constraint

func Not

func Not(constraint Constraint) Constraint

func NotEquals

func NotEquals(value interface{}) Constraint

func NotNil

func NotNil(value *interface{}) Constraint

func NotZero

func NotZero(value *interface{}) Constraint

func OneOf

func OneOf(values ...interface{}) Constraint

func Or

func Or(constraints ...Constraint) Constraint

func Regexp

func Regexp(r string) Constraint

func RegexpMatch

func RegexpMatch(re *regexp.Regexp) Constraint

func StartsWith

func StartsWith(prefix *string) Constraint

func Struct

func Struct(validationTool *structs.Tool) Constraint

func Success

func Success(msg string) Constraint

func TimeBetween

func TimeBetween(lower, upper *time.Time) Constraint

TimeBetween determines whether the provided value is (inclusively) between the lower and upper values provided. Stated explicitly, this tests for lower <= value <= upper.

func TimeCompare

func TimeCompare(comparisonType ComparisonType, compareTo *time.Time) Constraint

TimeCompare compares a tested value to compareTo. If the tested value is not a time.Time, the constraint will fail.

func TypeOf

func TypeOf(value interface{}) Constraint

func UintBetween

func UintBetween(lower, upper *uint64) Constraint

UintBetween determines whether the provided value is (inclusively) between the lower and upper values provided. Stated explicitly, this tests for lower <= value <= upper.

func UintCompare

func UintCompare(comparisonType ComparisonType, compareTo *uint64) Constraint

UintCompare compares a tested value to compareTo. While compareTo is an uint64, the tested value can be any of the unsigned integer types (e.g. uint, uint16, etc). If the tested value is not an unsigned integer type, the constraint will fail.

func Zero

func Zero(value *interface{}) Constraint

type Entry

type Entry struct {
	Key   interface{}
	Value interface{}
}

type NotSatisfiedError

type NotSatisfiedError struct {
	gomerr.Gomerr
	ToTest     interface{} `gomerr:"include_type"` // Needs to be first (after gomerr.Gomerr) to populate properly via gomerr.Build()
	Target     string
	Constraint Constraint
}

func NotSatisfied

func NotSatisfied(toTest interface{}) *NotSatisfiedError

type TargetNamer

type TargetNamer func(reflect.Type, reflect.StructField) string

TargetNamer provides an alternative value for NotSatisfiedError.Target if an error occurs. By default, the value will be the field name, but one might want to have a camelCase value (using field.CamelCase) or pre-pend a value with an underscore include using field.CamelCase to change the casing on a field or pre-pending an underscore if desired.

func TransformFieldName

func TransformFieldName(transform func(string) string) TargetNamer

Jump to

Keyboard shortcuts

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