v

package module
v0.0.0-...-a14e2d9 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(validations ...Validatable) error

Check 逐条执行验证器,一旦验证未通过,立即返回

func Map

func Map(data map[string]any) func(name, label string) *Valuer

Map 通过 map 构建值验证器

func SetDefaultTranslator

func SetDefaultTranslator(translator Translator)

SetDefaultTranslator 设置默认翻译函数

func Validate

func Validate(validations ...Validatable) error

Validate 执行多个验证器

Types

type Checker

type Checker func() error

Checker 功能验证函数签名

func Every

func Every(validators ...Validatable) Checker

Every 每一项都要验证通过

func IndexBy

func IndexBy(index *int, values [][]any, options ...ErrorOption) Checker

IndexBy 分组验证,只要其中一组验证通过就返回

func Some

func Some(validators ...Validatable) Checker

Some 任意一项验证通过即可

func Wrap

func Wrap(value any, check func(any) error) Checker

Wrap 将值和值的验证函数包装成验证器

func (Checker) Validate

func (c Checker) Validate() error

Validate 实现 Validatable 接口

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error 验证错误结构体

func NewError

func NewError(code string, options ...ErrorOption) *Error

NewError 创建错误实例

func (*Error) Code

func (e *Error) Code() string

Code 返回错误代码

func (*Error) Error

func (e *Error) Error() string

Error 实现内置错误接口(优先使用内部错误)

func (*Error) Field

func (e *Error) Field() string

Field 返回字段名

func (*Error) Format

func (e *Error) Format() string

Format 返回错误格式化模板

func (*Error) Label

func (e *Error) Label() string

Label 返回错误标签

func (*Error) Params

func (e *Error) Params() map[string]any

Params 返回错误格式化蚕食

func (*Error) String

func (e *Error) String() string

String 实现 fmt.Stringer 接口,返回格式化后的字符串

func (*Error) Value

func (e *Error) Value() any

Value 返回用于验证的值

type ErrorOption

type ErrorOption func(*Error)

ErrorOption 错误配置函数签名

func ErrorCode

func ErrorCode(code string) ErrorOption

ErrorCode 设置错误代码

func ErrorFormat

func ErrorFormat(format string) ErrorOption

ErrorFormat 设置错误格式化字符串

func ErrorParam

func ErrorParam(key string, value any) ErrorOption

ErrorParam 设置验证参数

type Errors

type Errors struct {
	// contains filtered or unexported fields
}

Errors 错误集

func (*Errors) Add

func (e *Errors) Add(err error)

Add 添加一个错误

func (*Errors) All

func (e *Errors) All() []*Error

func (*Errors) Error

func (e *Errors) Error() string

func (*Errors) First

func (e *Errors) First() *Error

First 返回第一个错误实例,如果不存在则返回 nil

func (*Errors) Get

func (e *Errors) Get(field string) []*Error

Get 获取指定标签的错误列表,如果不存在将返回 nil

func (*Errors) IsEmpty

func (e *Errors) IsEmpty() bool

IsEmpty 是否存在错误

func (*Errors) String

func (e *Errors) String() string

func (*Errors) ToMap

func (e *Errors) ToMap() map[string][]*Error

ToMap 根据错误标签分组

type Item

type Item struct {
	Key   any
	Index int
	Value any
}

type Matcher

type Matcher struct {
	// contains filtered or unexported fields
}

func Match

func Match(value any, field, label string) *Matcher

func (*Matcher) Branch

func (m *Matcher) Branch(value any, handle func(valuer *Valuer) error) *Matcher

func (*Matcher) Fallback

func (m *Matcher) Fallback(handle func(valuer *Valuer) error) *Matcher

func (*Matcher) Validate

func (m *Matcher) Validate() error

type Ruler

type Ruler func(any) error

Ruler 规则验证函数签名

type Translator

type Translator func(message string, params map[string]any) string

Translator 翻译函数签名

func DefaultTranslator

func DefaultTranslator() Translator

DefaultTranslator 返回默认翻译函数

type Validatable

type Validatable interface {
	Validate() error
}

Validatable 验证功能接口

type Valuer

type Valuer struct {
	// contains filtered or unexported fields
}

Valuer 基本值验证器

func Value

func Value(value any, field, label string) *Valuer

Value 创建一条验证器

func (*Valuer) Between

func (v *Valuer) Between(min, max any, options ...ErrorOption) *Valuer

func (*Valuer) Contains

func (v *Valuer) Contains(substr string, options ...ErrorOption) *Valuer

func (*Valuer) ContainsAny

func (v *Valuer) ContainsAny(chars string, options ...ErrorOption) *Valuer

func (*Valuer) ContainsRune

func (v *Valuer) ContainsRune(rune rune, options ...ErrorOption) *Valuer

func (*Valuer) Custom

func (v *Valuer) Custom(code string, check func(val any) any, options ...ErrorOption) *Valuer

func (*Valuer) EndsNotWith

func (v *Valuer) EndsNotWith(suffix string, options ...ErrorOption) *Valuer

func (*Valuer) EndsWith

func (v *Valuer) EndsWith(suffix string, options ...ErrorOption) *Valuer

func (*Valuer) Equal

func (v *Valuer) Equal(another any, options ...ErrorOption) *Valuer

func (*Valuer) Every

func (v *Valuer) Every(handle func(item *Item) any, options ...ErrorOption) *Valuer

func (*Valuer) Excludes

func (v *Valuer) Excludes(substr string, options ...ErrorOption) *Valuer

func (*Valuer) ExcludesAll

func (v *Valuer) ExcludesAll(chars string, options ...ErrorOption) *Valuer

func (*Valuer) ExcludesRune

func (v *Valuer) ExcludesRune(rune rune, options ...ErrorOption) *Valuer

func (*Valuer) GreaterEqualThan

func (v *Valuer) GreaterEqualThan(n any, options ...ErrorOption) *Valuer

func (*Valuer) GreaterThan

func (v *Valuer) GreaterThan(min any, options ...ErrorOption) *Valuer

func (*Valuer) IsAlpha

func (v *Valuer) IsAlpha(options ...ErrorOption) *Valuer

func (*Valuer) IsAlphaUnicode

func (v *Valuer) IsAlphaUnicode(options ...ErrorOption) *Valuer

func (*Valuer) IsAlphanumeric

func (v *Valuer) IsAlphanumeric(options ...ErrorOption) *Valuer

func (*Valuer) IsAlphanumericUnicode

func (v *Valuer) IsAlphanumericUnicode(options ...ErrorOption) *Valuer

func (*Valuer) IsAscii

func (v *Valuer) IsAscii(options ...ErrorOption) *Valuer

func (*Valuer) IsBase64

func (v *Valuer) IsBase64(options ...ErrorOption) *Valuer

func (*Valuer) IsBase64URL

func (v *Valuer) IsBase64URL(options ...ErrorOption) *Valuer

func (*Valuer) IsBool

func (v *Valuer) IsBool(options ...ErrorOption) *Valuer

func (*Valuer) IsColor

func (v *Valuer) IsColor(options ...ErrorOption) *Valuer

func (*Valuer) IsDatetime

func (v *Valuer) IsDatetime(layout string, options ...ErrorOption) *Valuer

func (*Valuer) IsDir

func (v *Valuer) IsDir(options ...ErrorOption) *Valuer

func (*Valuer) IsE164

func (v *Valuer) IsE164(options ...ErrorOption) *Valuer

func (*Valuer) IsEmail

func (v *Valuer) IsEmail(options ...ErrorOption) *Valuer

func (*Valuer) IsFile

func (v *Valuer) IsFile(options ...ErrorOption) *Valuer

func (*Valuer) IsHTML

func (v *Valuer) IsHTML(options ...ErrorOption) *Valuer

func (*Valuer) IsHTMLEncoded

func (v *Valuer) IsHTMLEncoded(options ...ErrorOption) *Valuer

func (*Valuer) IsHexColor

func (v *Valuer) IsHexColor(options ...ErrorOption) *Valuer

func (*Valuer) IsHexadecimal

func (v *Valuer) IsHexadecimal(options ...ErrorOption) *Valuer

func (*Valuer) IsHsl

func (v *Valuer) IsHsl(options ...ErrorOption) *Valuer

func (*Valuer) IsHsla

func (v *Valuer) IsHsla(options ...ErrorOption) *Valuer

func (*Valuer) IsIP

func (v *Valuer) IsIP(options ...ErrorOption) *Valuer

func (*Valuer) IsIPv4

func (v *Valuer) IsIPv4(options ...ErrorOption) *Valuer

func (*Valuer) IsIPv6

func (v *Valuer) IsIPv6(options ...ErrorOption) *Valuer

func (*Valuer) IsJson

func (v *Valuer) IsJson(options ...ErrorOption) *Valuer

func (*Valuer) IsJwt

func (v *Valuer) IsJwt(options ...ErrorOption) *Valuer

func (*Valuer) IsLabel

func (v *Valuer) IsLabel(options ...ErrorOption) *Valuer

func (*Valuer) IsLatitude

func (v *Valuer) IsLatitude(options ...ErrorOption) *Valuer

func (*Valuer) IsLongitude

func (v *Valuer) IsLongitude(options ...ErrorOption) *Valuer

func (*Valuer) IsLower

func (v *Valuer) IsLower(options ...ErrorOption) *Valuer

func (*Valuer) IsMAC

func (v *Valuer) IsMAC(options ...ErrorOption) *Valuer

func (*Valuer) IsMD4

func (v *Valuer) IsMD4(options ...ErrorOption) *Valuer

func (*Valuer) IsMD5

func (v *Valuer) IsMD5(options ...ErrorOption) *Valuer

func (*Valuer) IsNumber

func (v *Valuer) IsNumber(options ...ErrorOption) *Valuer

func (*Valuer) IsNumeric

func (v *Valuer) IsNumeric(options ...ErrorOption) *Valuer

func (*Valuer) IsPhoneNumber

func (v *Valuer) IsPhoneNumber(options ...ErrorOption) *Valuer

func (*Valuer) IsRgb

func (v *Valuer) IsRgb(options ...ErrorOption) *Valuer

func (*Valuer) IsRgba

func (v *Valuer) IsRgba(options ...ErrorOption) *Valuer

func (*Valuer) IsSHA256

func (v *Valuer) IsSHA256(options ...ErrorOption) *Valuer

func (*Valuer) IsSHA384

func (v *Valuer) IsSHA384(options ...ErrorOption) *Valuer

func (*Valuer) IsSHA512

func (v *Valuer) IsSHA512(options ...ErrorOption) *Valuer

func (*Valuer) IsSemver

func (v *Valuer) IsSemver(options ...ErrorOption) *Valuer

func (*Valuer) IsString

func (v *Valuer) IsString(options ...ErrorOption) *Valuer

func (*Valuer) IsTimezone

func (v *Valuer) IsTimezone(options ...ErrorOption) *Valuer

func (*Valuer) IsULID

func (v *Valuer) IsULID(options ...ErrorOption) *Valuer

func (*Valuer) IsURL

func (v *Valuer) IsURL(options ...ErrorOption) *Valuer

func (*Valuer) IsURLEncoded

func (v *Valuer) IsURLEncoded(options ...ErrorOption) *Valuer

func (*Valuer) IsUUID

func (v *Valuer) IsUUID(options ...ErrorOption) *Valuer

func (*Valuer) IsUUID3

func (v *Valuer) IsUUID3(options ...ErrorOption) *Valuer

func (*Valuer) IsUUID4

func (v *Valuer) IsUUID4(options ...ErrorOption) *Valuer

func (*Valuer) IsUUID5

func (v *Valuer) IsUUID5(options ...ErrorOption) *Valuer

func (*Valuer) IsUpper

func (v *Valuer) IsUpper(options ...ErrorOption) *Valuer

func (*Valuer) Length

func (v *Valuer) Length(n int, options ...ErrorOption) *Valuer

func (*Valuer) LengthBetween

func (v *Valuer) LengthBetween(min, max int, options ...ErrorOption) *Valuer

func (*Valuer) LessEqualThan

func (v *Valuer) LessEqualThan(max any, options ...ErrorOption) *Valuer

func (*Valuer) LessThan

func (v *Valuer) LessThan(max any, options ...ErrorOption) *Valuer

func (*Valuer) Match

func (v *Valuer) Match(handle func(m *Matcher)) *Valuer

func (*Valuer) MaxLength

func (v *Valuer) MaxLength(max int, options ...ErrorOption) *Valuer

func (*Valuer) MinLength

func (v *Valuer) MinLength(min int, options ...ErrorOption) *Valuer

func (*Valuer) NotBetween

func (v *Valuer) NotBetween(min, max any, options ...ErrorOption) *Valuer

func (*Valuer) NotEmpty

func (v *Valuer) NotEmpty(options ...ErrorOption) *Valuer

func (*Valuer) NotEqual

func (v *Valuer) NotEqual(another any, options ...ErrorOption) *Valuer

func (*Valuer) OneOf

func (v *Valuer) OneOf(items []any, options ...ErrorOption) *Valuer

func (*Valuer) Required

func (v *Valuer) Required(options ...ErrorOption) *Valuer

Required 值是否必须(值不为空)

func (*Valuer) RequiredIf

func (v *Valuer) RequiredIf(condition bool, options ...ErrorOption) *Valuer

RequiredIf 满足条件必须

func (*Valuer) RequiredWith

func (v *Valuer) RequiredWith(values []any, options ...ErrorOption) *Valuer

RequiredWith 依赖其它值判断是否必须

func (*Valuer) Some

func (v *Valuer) Some(handle func(item *Item) any, options ...ErrorOption) *Valuer

func (*Valuer) StartsNotWith

func (v *Valuer) StartsNotWith(prefix string, options ...ErrorOption) *Valuer

func (*Valuer) StartsWith

func (v *Valuer) StartsWith(prefix string, options ...ErrorOption) *Valuer

func (*Valuer) Typeof

func (v *Valuer) Typeof(kind reflect.Kind, options ...ErrorOption) *Valuer

func (*Valuer) Validate

func (v *Valuer) Validate() error

Validate 实现验证器接口

func (*Valuer) When

func (v *Valuer) When(condition bool, then func(*Valuer)) *Valuer

Jump to

Keyboard shortcuts

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