xvalid

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultErrorMsgs

func SetDefaultErrorMsgs(msgs map[string]string)

替换默认的错误提示为指定的自定义提示 主要作用: 1、便于多语言错误提示设置; 2、默认错误提示信息不满意;

Types

type CustomMsg

type CustomMsg = map[string]interface{}

自定义错误信息: map[键名] => 字符串|map[规则]错误信息

type TValidError

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

校验错误对象, 实现error接口

func Check

func Check(value interface{}, rules string, msgs interface{}, params ...interface{}) *TValidError

检测单条数据的规则:

1. value为需要校验的数据,可以为任意基本数据类型;

2. msgs为自定义错误信息,由于同一条数据的校验规则可能存在多条,为方便调用,参数类型支持 string/map/struct/*struct, 允许传递多个自定义的错误信息,如果类型为string,那么中间使用"|"符号分隔多个自定义错误;

3. params参数为联合校验参数,支持任意的map/struct/*struct类型,对于需要联合校验的规则有效,如:required-*、same、different;

func CheckMap

func CheckMap(params interface{}, rules interface{}, msgs ...CustomMsg) *TValidError

检测键值对参数Map, rules参数支持 []string / map[string]string 类型,前面一种类型支持返回校验结果顺序(具体格式参考struct tag),后一种不支持; rules参数中得 map[string]string 是一个2维的关联数组,第一维键名为参数键名,第二维为带有错误的校验规则名称,值为错误信息。

func CheckStruct

func CheckStruct(object interface{}, rules interface{}, msgs ...CustomMsg) *TValidError

校验struct对象属性,object参数也可以是一个指向对象的指针,返回值同CheckMap方法。 struct的数据校验结果信息是顺序的。

func (*TValidError) Error

func (e *TValidError) Error() string

func (*TValidError) FirstItem

func (e *TValidError) FirstItem() (key string, msgs map[string]string)

只获取第一个键名的校验错误项

func (*TValidError) FirstRule

func (e *TValidError) FirstRule() (rule string, err string)

只获取第一个校验错误项的规则及错误信息

func (*TValidError) FirstString

func (e *TValidError) FirstString() (err string)

只获取第一个校验错误项的错误信息

func (*TValidError) Map

func (e *TValidError) Map() map[string]string

获得规则与错误信息的map; 当校验结果为多条数据校验时,返回第一条错误map(此时类似FirstItem)

func (*TValidError) Maps

func (e *TValidError) Maps() TValidErrorMap

获得原始校验结果TValidErrorMap

func (*TValidError) String

func (e *TValidError) String() string

将所有错误信息构建称字符串,多个错误信息字符串使用"; "符号分隔

func (*TValidError) Strings

func (e *TValidError) Strings() (errs []string)

只返回错误信息,构造成字符串数组返回

type TValidErrorMap

type TValidErrorMap map[string]map[string]string

校验错误信息: map[键名]map[规则名]错误信息

Jump to

Keyboard shortcuts

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