validation

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2020 License: MIT Imports: 6 Imported by: 1

README

validation Go Go version Go Report Card license codecov PkgGoDev

数据验证

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package validation 数据验证相关功能

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandling

type ErrorHandling int8

ErrorHandling 当验证出错时的处理方式

const (
	ContinueAtError  ErrorHandling = iota // 碰到错误不中断验证
	ExitAtError                           // 碰到错误中断验证
	ExitFieldAtError                      // 碰到错误中断当前字段的验证
)

当验证出错时的几种可用处理方式

type FieldsValidator

type FieldsValidator interface {
	ValidateFields(ErrorHandling, *message.Printer) Messages
}

FieldsValidator 验证子项接口

一般用在自定义类型上,用于验证自身的子项数据。

凡实现此接口的对象,在 NewField 中会自动调用此接口的方法进行额外验证。

type IfExpr

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

IfExpr 根据 if 条件选择不同的验证规则

func If

func If(expr bool, rule ...*Rule) *IfExpr

If 返回 IfExpr 表达式

func (*IfExpr) Else

func (expr *IfExpr) Else(rule ...*Rule) *IfExpr

Else 指定条件不成言的验证规则

调用多次,则以最后一次指定为准,如果最后一次为空,则取消 Else 的内容。

func (*IfExpr) Rules

func (expr *IfExpr) Rules() []*Rule

Rules 返回当前表达式最后使用的验证规则

type Messages

type Messages map[string][]string

Messages 表示一组错误信息的集合

键名查询参数名称,键值则为在解析和验证过种中返回的错误信息。

func (Messages) Add

func (err Messages) Add(key string, val ...string)

Add 为查询参数 key 添加一条新的错误信息

func (Messages) Set

func (err Messages) Set(key string, val ...string)

Set 将查询参数 key 的错误信息改为 val

type Rule

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

Rule 验证规则需要实现的接口

func NewRule

func NewRule(validator Validator, key message.Reference, v ...interface{}) *Rule

NewRule 返回 Rule 实例

type ValidateFunc

type ValidateFunc func(interface{}) bool

ValidateFunc 用于验证指定数据的合法性

func In

func In(element ...interface{}) ValidateFunc

In 声明枚举类型的验证规则

要求验证的值必须包含在 element 元素中,如果不存在,则返回 msg 的内容。

func Length

func Length(min, max int64) ValidateFunc

Length 声明判断内容长度的验证规则

如果 min 和 max 有值为 -1,表示忽略该值的比较,都为 -1 表示不限制长度。

只能验证类型为 string、Map、Slice 和 Array 的数据。

func Match

func Match(exp *regexp.Regexp) ValidateFunc

Match 定义正则匹配的验证规则

func Max

func Max(max float64) ValidateFunc

Max 声明判断数值不大于 max 的验证规则

func MaxLength

func MaxLength(max int64) ValidateFunc

MaxLength 声明判断内容长度不大于 max 的验证规则

func Min

func Min(min float64) ValidateFunc

Min 声明判断数值不小于 min 的验证规则

func MinLength

func MinLength(min int64) ValidateFunc

MinLength 声明判断内容长度不小于 min 的验证规则

func NotIn

func NotIn(element ...interface{}) ValidateFunc

NotIn 声明不在枚举中的验证规则

func Range

func Range(min, max float64) ValidateFunc

Range 声明判断数值大小的验证规则

只能验证类型为 int、int8、int16、int32、int64、uint、uint8、uint16、uint32、uint64、float32 和 float64 类型的值。

min 和 max 可以分别采用 math.Inf(-1) 和 math.Inf(1) 表示其最大的值范围。

func Required

func Required(skipNil bool) ValidateFunc

Required 判断值是否必须为非空的规则

skipNil 表示当前值为指针时,如果指向 nil,是否跳过非空检测规则。 如果 skipNil 为 false,则 nil 被当作空值处理。

具体判断规则可参考 github.com/issue9/is.Empty

func (ValidateFunc) IsValid

func (f ValidateFunc) IsValid(v interface{}) bool

IsValid 将当前函数作为 Validator 使用

func (ValidateFunc) Rule

func (f ValidateFunc) Rule(key message.Reference, v ...interface{}) *Rule

Rule 将当前函数转换成 Rule 实例

type Validation

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

Validation 验证器

func New

func New(errHandling ErrorHandling, p *message.Printer) *Validation

New 返回 Validation 对象

func (*Validation) Messages

func (v *Validation) Messages() Messages

Messages 返回验证结果

func (*Validation) NewField

func (v *Validation) NewField(val interface{}, name string, rules ...*Rule) *Validation

NewField 验证新的字段

type Validator

type Validator interface {
	// 验证 v 是否符合当前的规则
	IsValid(v interface{}) bool
}

Validator 用于验证指定数据的合法性

Jump to

Keyboard shortcuts

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