rule

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyBind  = "binding"
	KeyMsg   = "msg"
	KeyRegex = "pattern"
)
View Source
const DefaultFieldMsg = "the param %s not valid"
View Source
const (
	NumericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
)

Variables

View Source
var (
	TimeType = reflect.TypeOf(time.Time{})

	ValidatorFunc = map[string]Func{
		"required":         hasValue,
		"required_if":      requiredIf,
		"len":              hasLengthOf,
		"min":              hasMinOf,
		"max":              hasMaxOf,
		"eq":               isEq,
		"ne":               isNe,
		"lt":               isLt,
		"lte":              isLte,
		"gt":               isGt,
		"gte":              isGte,
		"eqfield":          isEqField,
		"eqcsfield":        isEqCrossStructField,
		"necsfield":        isNeCrossStructField,
		"gtcsfield":        isGtCrossStructField,
		"gtecsfield":       isGteCrossStructField,
		"ltcsfield":        isLtCrossStructField,
		"ltecsfield":       isLteCrossStructField,
		"nefield":          isNeField,
		"gtefield":         isGteField,
		"gtfield":          isGtField,
		"ltefield":         isLteField,
		"ltfield":          isLtField,
		"fieldcontains":    fieldContains,
		"fieldexcludes":    fieldExcludes,
		"alpha":            isAlpha,
		"alphanum":         isAlphanum,
		"alphaunicode":     isAlphaUnicode,
		"alphanumunicode":  isAlphanumUnicode,
		"boolean":          isBoolean,
		"numeric":          isNumeric,
		"number":           isNumber,
		"email":            isEmail,
		"url":              isURL,
		"uri":              isURI,
		"urn_rfc2141":      isUrnRFC2141,
		"file":             isFile,
		"base64":           isBase64,
		"base64url":        isBase64URL,
		"contains":         contains,
		"containsany":      containsAny,
		"containsrune":     containsRune,
		"excludes":         excludes,
		"excludesall":      excludesAll,
		"excludesrune":     excludesRune,
		"startswith":       startsWith,
		"endswith":         endsWith,
		"startsnotwith":    startsNotWith,
		"endsnotwith":      endsNotWith,
		"md5":              isMD5,
		"sha256":           isSHA256,
		"ipv4":             isIPv4,
		"ipv6":             isIPv6,
		"ip":               isIP,
		"tcp4_addr":        isTCP4AddrResolvable,
		"tcp6_addr":        isTCP6AddrResolvable,
		"tcp_addr":         isTCPAddrResolvable,
		"udp4_addr":        isUDP4AddrResolvable,
		"udp6_addr":        isUDP6AddrResolvable,
		"udp_addr":         isUDPAddrResolvable,
		"ip4_addr":         isIP4AddrResolvable,
		"ip6_addr":         isIP6AddrResolvable,
		"ip_addr":          isIPAddrResolvable,
		"unix_addr":        isUnixAddrResolvable,
		"mac":              isMAC,
		"hostname":         isHostnameRFC952,
		"hostname_rfc1123": isHostnameRFC1123,
		"unique":           isUnique,
		"oneof":            isOneOf,
		"html":             isHTML,
		"html_encoded":     isHTMLEncoded,
		"url_encoded":      isURLEncoded,
		"json":             isJSON,
		"jwt":              isJWT,
		"hostname_port":    isHostnamePort,
		"lowercase":        isLowercase,
		"uppercase":        isUppercase,
		"datetime":         isDatetime,
		"timezone":         isTimeZone,

		"idcard": isIdCard,
		"mobile": isMobile,
		"regex":  isRegexMatch,
	}
)

Functions

func DefaultVal

func DefaultVal(tag reflect.StructTag, param string) (string, bool)

func FieldTimeVal

func FieldTimeVal(val string, tag reflect.StructTag) (time.Time, error)
func Head(str, sep string) (head string, tail string)

func Param

func Param(tag reflect.StructTag, param string) string

Types

type BaseFiled

type BaseFiled struct {
	CurParam      string            // filed form CurParam value
	CurKey        string            // current struct key name
	CurRule       Info              // current valid rule
	CurTag        reflect.StructTag // current valid field struct CurTag
	CurInheritTag reflect.StructTag // when dive valid parent has CurTag
	Track         []string          // current data index path
	CurDataIdx    string            // current data index string
	ValidTagName  string            // valid tag name: form、json、header
}

func (*BaseFiled) CustomMsg

func (f *BaseFiled) CustomMsg() (string, bool)

func (*BaseFiled) Err

func (f *BaseFiled) Err() error

func (*BaseFiled) PopTrack

func (f *BaseFiled) PopTrack()

func (*BaseFiled) PushTrack

func (f *BaseFiled) PushTrack(elem string)

func (*BaseFiled) WrapperErr

func (f *BaseFiled) WrapperErr(err error) error

type Field

type Field interface {
	// Root root element
	Root() reflect.Type

	// Parent  parent element
	Parent() reflect.Type

	// Self  wanted type
	Self() reflect.Type

	// Key Struct Key Name, self is struct key or inherit parent key name
	Key() string

	// StructField find relative another field key
	StructField(valType reflect.Type, namespace string) (Field, bool)

	// RuleName 当前验证的规则名称
	RuleName() string

	// RuleVal 当前验证规则值,可能不存在。
	RuleVal() string

	// CurDataIndex current data index
	CurDataIndex() string

	// SetDataIdx update data cur idx
	SetDataIdx(idx string)

	// Tag get current field CurTag
	Tag() reflect.StructTag

	// InheritTag inherit CurTag when dive valid
	InheritTag() reflect.StructTag

	// DefaultValExits 默认值是否存在
	DefaultValExits() bool

	// DefaultVal 默认值
	DefaultVal() string

	// Exist data exist or not
	Exist() bool

	// IsBool data is bool value or not
	IsBool() bool

	// IsArray data is array type or not, not support default value
	IsArray() bool

	// IsObject data is map or struct
	IsObject() bool

	// IsNumeric data is numeric
	IsNumeric() bool

	// Str try to get str data
	Str() (string, bool)

	// MustStr return str ignore err
	MustStr() string

	// Int try to get int data
	Int() (int64, bool)

	// MustInt return int val will ignore err or not exist
	MustInt() int64

	// Uint try to get uint data
	Uint() (uint64, bool)

	// MustUint return the confirmed val
	MustUint() uint64

	// Float try to get float data
	Float() (float64, bool)

	// MustFloat return float val will ignore the val not exist
	MustFloat() float64

	// Len try to get data len
	Len() int

	// Bool return data bool value
	Bool() (bool, bool)

	// MustBool return bool value ignore the error or the val not exist
	MustBool() bool

	// Raw get the data raw string
	Raw() string

	// ParamKey the value just is one of: json, form, header
	ParamKey() string

	// MapKeys json may keys array, them json map key only support string
	MapKeys() []string

	// ToString try to convert the value type to string
	ToString() string
}

type Func

type Func func(fl Field) bool

Func accepts a Field interface for all validation needs. The return value should be true when validation succeeds.

type Info

type Info struct {
	Name  string
	Val   string
	Level []string
}

type Inherits

type Inherits []Info

func (Inherits) Contains

func (i Inherits) Contains(key string) bool

func (Inherits) IndexOf

func (i Inherits) IndexOf(key string) int

type NumericT

type NumericT interface {
	float64 | int64 | int
}

Jump to

Keyboard shortcuts

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