vdutil

package
v2.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package vdutil helps to do data validation.

It defines an abstract Rule interface to run validating rules, some commonly used rules are implemented in this package. User can define their own validating rules, for example wrapping "github.com/go-playground/validator" and "github.com/bytedance/go-tagexpr" to validate structs, or validating data passed through context.Context and save the result to Result.Data for further accessing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableRegexpCache added in v2.10.0

func EnableRegexpCache(cache lru.Interface[string, *regexp.Regexp])

EnableRegexpCache sets an LRU cache to cache compiled regular expressions.

Types

type IntegerOrString added in v2.10.0

type IntegerOrString interface {
	constraints.Integer | ~string
}

type RangeMode

type RangeMode int

RangeMode tells InRangeMode how to handle lower and upper bound when validating a value against a range.

const (
	GtAndLte  RangeMode = iota // min < x && x <= max
	GtAndLt                    // min < x && x < max
	GteAndLte                  // min <= x && x <= max
	GteAndLt                   // min <= x && x < max
)

type RegexpOrString added in v2.10.0

type RegexpOrString interface {
	*regexp.Regexp | string
}

type Result

type Result struct {
	// Data allows validating rules to pass data to the caller.
	Data ezmap.Map

	// Validating rules can optionally add detail information to ErrDetails
	// to inform the caller.
	ErrDetails []any

	// IsValidationError tells whether the error returned by Validate
	// is a ValidationError or not.
	IsValidationError bool
}

Result can be used to pass data from validating rules to the caller.

func Validate

func Validate(ctx context.Context, rules ...Rule) (result *Result, err error)

Validate runs validating rules, it returns a Result and the first error returned from the rules. If a rule returns an error, it returns and the remaining rules are not executed.

type Rule

type Rule interface {
	Validate(ctx context.Context, result *Result) (any, error)
}

Rule represents a validating rule.

func UseResult added in v2.14.1

func UseResult(result *Result) Rule

UseResult tells validating rules to use the given result.

type RuleFunc

type RuleFunc func(ctx context.Context, result *Result) (any, error)

RuleFunc implements the interface Rule.

func AllElementsGreaterThanZero added in v2.14.1

func AllElementsGreaterThanZero[T IntegerOrString](name string, slice []T, save bool) RuleFunc

AllElementsGreaterThanZero validates that all elements in slice are greater than zero. If save is true, the result integer slice will be saved to Result.Data using name as key.

func AllElementsNotZero added in v2.14.1

func AllElementsNotZero[T comparable](name string, slice []T) RuleFunc

AllElementsNotZero validates that all elements in slice are not equal to the zero value of type T.

func GreaterThanZero

func GreaterThanZero[T IntegerOrString](name string, value T, save bool) RuleFunc

GreaterThanZero validates that value is greater than zero. value can be either an integer or a string. If save is true, the result integer value will be saved to Result.Data using name as key.

func InRange

func InRange[T constraints.Ordered](name string, min, max T, value T) RuleFunc

InRange validates that value >= min and value <= max.

func InRangeMode

func InRangeMode[T constraints.Ordered](name string, mode RangeMode, min, max T, value T) RuleFunc

InRangeMode validates that value is in range of min and max, according to RangeMode.

func LessThanOrEqual

func LessThanOrEqual[T constraints.Ordered](name string, limit, value T) RuleFunc

LessThanOrEqual validates that value <= limit.

func MatchRegexp added in v2.10.0

func MatchRegexp[T RegexpOrString](name string, pattern T, value string) RuleFunc

MatchRegexp validates value match the regular expression pattern. pattern can be either a string or a compiled *regexp.Regexp.

If pattern is a string and cache is enabled by calling EnableRegexpCache, the compiled regular expression will be cached for reuse.

func NotNil

func NotNil(name string, value any) RuleFunc

NotNil validates value is not nil (e.g. nil pointer, nil slice, nil map).

func ParseStrsToInt64Map added in v2.10.0

func ParseStrsToInt64Map[T ~string](name string, values []T) RuleFunc

ParseStrsToInt64Map validates all elements in values are valid integer and convert values to be a map[int64]bool, the result map will be saved to Result.Data using name as key.

func ParseStrsToInt64Slice added in v2.10.0

func ParseStrsToInt64Slice[T ~string](name string, values []T) RuleFunc

ParseStrsToInt64Slice validates all elements in values are valid integer and convert values to be an []int64 slice, the result slice will be saved to Result.Data using name as key.

func (RuleFunc) Validate

func (f RuleFunc) Validate(ctx context.Context, result *Result) (any, error)

type ValidationError added in v2.10.0

type ValidationError struct {
	Name string
	Err  error
}

ValidationError indicates that the data is invalid. Name and Err tells which data and underlying error returned by a Rule. Note, a Rule should not return this error on programming error or internal system error.

func (*ValidationError) Error added in v2.10.0

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap added in v2.10.0

func (e *ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

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