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 ¶
- func EnableRegexpCache(cache lru.Interface[string, *regexp.Regexp])
- type IntegerOrString
- type RangeMode
- type RegexpOrString
- type Result
- type Rule
- type RuleFunc
- func AllElementsGreaterThanZero[T IntegerOrString](name string, slice []T, save bool) RuleFunc
- func AllElementsNotZero[T comparable](name string, slice []T) RuleFunc
- func GreaterThanZero[T IntegerOrString](name string, value T, save bool) RuleFunc
- func InRange[T constraints.Ordered](name string, min, max T, value T) RuleFunc
- func InRangeMode[T constraints.Ordered](name string, mode RangeMode, min, max T, value T) RuleFunc
- func LessThanOrEqual[T constraints.Ordered](name string, limit, value T) RuleFunc
- func MatchRegexp[T RegexpOrString](name string, pattern T, value string) RuleFunc
- func NotNil(name string, value any) RuleFunc
- func ParseStrsToInt64Map[T ~string](name string, values []T) RuleFunc
- func ParseStrsToInt64Slice[T ~string](name string, values []T) RuleFunc
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
type RegexpOrString ¶ added in v2.10.0
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.
type RuleFunc ¶
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 ParseStrsToInt64Map ¶ added in v2.10.0
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
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.
type ValidationError ¶ added in v2.10.0
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