validators

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package validators holds the internal, RV-native implementations of the stateless built-in validators. They consume the cached value carrier (*fieldval.FieldValue) so the same value is not repeatedly re-reflected (reflect.ValueOf) inside each validator.

Dependency direction is one-way: root -> internal/validators -> internal/fieldval, internal/reflectx (and goutil). This package never imports the validate root package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Between

func Between(fl *fieldval.FieldValue, minVal, maxVal any) bool

Between value in the given range (inclusive). only check for: int(X), uint(X), float(X), string(lexicographic).

func Enum

func Enum(fl *fieldval.FieldValue, enum any) bool

Enum value(int(X),string) should be in the given enum(strings, ints, uints). RV 版。

逐行对照原 Enum:复用载体缓存 RV() 经 ConvToBasicTypeRV 取基础值;字符串走 []string 包含判定,其余按 int64 走 ToInt64s/Int64sHas。

A slice or array value (eg. a multi-select field) is in the enum when every element is in it, so `in:go,php` accepts ["go","php"] and rejects ["go","java"].

func EnumUnsupportedKind added in v2.0.3

func EnumUnsupportedKind(rv reflect.Value) (reflect.Kind, bool)

EnumUnsupportedKind reports the kind the enum rules cannot compare, when the value (or one of its slice elements) has one: only a map is reported, because it has nothing to compare with the given list and used to fail with the rule message, which reads like the value is simply not in the list. Any other value stays with the old behaviour, including a struct reached through a nil nested pointer, which the rules already treat as an unset value.

func Gt

func Gt(fl *fieldval.FieldValue, dstVal any) bool

Gt check value greater dst value. only check for: int(X), uint(X), float(X), string(lexicographic).

func Gte

func Gte(fl *fieldval.FieldValue, dstVal any) bool

Gte check value greater or equal dst value. only check for: int(X), uint(X), float(X), string(lexicographic).

func IsArray

func IsArray(fl *fieldval.FieldValue, strict ...bool) bool

IsArray check value is array or slice. RV 版,支持 strict 变参。

public = if val==nil return false; reflect.Indirect(reflect.ValueOf(val)) 后判 Kind。 复现:rv := reflect.Indirect(fl.RealV()) —— fl.RealV() 已解一层,再 Indirect 一层 =匹配 public 的 ValueOf+Indirect 链(含 **T 双指针)。public 的 val==nil 早退对应 fl.Src==nil, 但注意 reflect.Call 路径里 public 实际收到 RealV().Interface()(nil 字段为 NilObject{} 非 nil), 故等价契约下不在 Src==nil 处早退,而是让 NilObject{}(struct) 自然走到 Kind 判定返回 false。

func IsBool

func IsBool(fl *fieldval.FieldValue) bool

IsBool check. allow: bool, string. RV 版。

public = IndirectValue(val) 后 val.(bool) / val.(string)→strutil.ToBool。复现:对 fl.RealV()(已预解一层)再做一次 IndirectValue(=第二层),得到的动态类型断言 .(bool) /.(string)。⚠️ 用动态类型断言而非 Kind:命名类型 type MyBool bool 会判 false,与 public 的 val.(bool) 一致。

func IsInt

func IsInt(fl *fieldval.FieldValue, minAndMax ...int64) bool

IsInt check, and support length check. RV 版。

原 = if val == nil return false; val = IndirectValue(val); mathutil.StrictInt(val); 然后按 minAndMax 个数做长度判定。nil 判定在 Indirect 之前(与原函数顺序一致)。

func IsMap

func IsMap(fl *fieldval.FieldValue) bool

IsMap check. RV 版。

public = if val==nil return false; reflect.Indirect(reflect.ValueOf(val)).Kind()==Map。 复现同 IsArray:rv := reflect.Indirect(fl.RealV()) 后判 Kind==Map。

func IsSlice

func IsSlice(fl *fieldval.FieldValue) bool

IsSlice check value is slice type. RV 版,复用载体缓存 RV() 免二次 reflect.ValueOf。

原 = if val == nil return false; reflect.Indirect(reflect.ValueOf(val)).Kind() == Slice

func IsString

func IsString(fl *fieldval.FieldValue, minAndMaxLen ...int) bool

IsString check and support length check. RV 版。

原 = val = IndirectValue(val); if val == nil return false; str,isStr := val.(string); 然后按 minAndMaxLen 个数做长度判定。nil 判定在 Indirect 之后(与原函数顺序一致)。

func IsUint

func IsUint(fl *fieldval.FieldValue) bool

IsUint check, allow: intX, uintX, string. RV 版。

public 无 indirection,对 fl.RealV().Interface() 直接做 type-switch。RealV 已完成 reflect.Call 路径的那一次预解引用,这里不再二次解引用 —— 与 public(RealV().Interface()) 逐分支一致。注意用动态类型 type-switch(命名类型 type MyInt int 会落到 int case)。

func Length

func Length(fl *fieldval.FieldValue, wantLen int) bool

Length equal check for string, array, slice, map. RV 版。

func Lt

func Lt(fl *fieldval.FieldValue, dstVal any) bool

Lt less than dst value. only check for: int(X), uint(X), float(X), string(lexicographic).

func Lte

func Lte(fl *fieldval.FieldValue, dstVal any) bool

Lte less than or equal dst value. only check for: int(X), uint(X), float(X), string(lexicographic).

func Max

func Max(fl *fieldval.FieldValue, dstVal any) bool

Max less than or equal dst value, alias Lte(). only check for: int(X), uint(X), float(X), string(lexicographic). NOTICE: use maxLength/max_len for the length of a string.

func MaxLength

func MaxLength(fl *fieldval.FieldValue, maxLen int) bool

MaxLength check for string, array, slice, map. RV 版。

func Min

func Min(fl *fieldval.FieldValue, dstVal any) bool

Min check value greater or equal dst value, alias Gte(). only check for: int(X), uint(X), float(X), string(lexicographic).

NOTICE: a string field is compared with the rule argument as a string, not by its length - `min:3` on "ab" passes and on "1" fails. Use minLength/min_len for a length check.

func MinLength

func MinLength(fl *fieldval.FieldValue, minLen int) bool

MinLength check for string, array, slice, map. RV 版。

func NotIn

func NotIn(fl *fieldval.FieldValue, enum any) bool

NotIn value should be not in the given enum(strings, ints, uints). RV 版。

For a slice or array value, no element may be in the enum, so `not_in:go,php` rejects ["go","java"] but accepts ["java","rust"].

Types

This section is empty.

Jump to

Keyboard shortcuts

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