validator

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

validator-go

go form validator like php laravel framework validator go版表单验证器,类似php框架laravel中的validator, 用于对web常用表单数据验证。

使用方法

1. 简单使用 (simple use)
// 需要验证的数据, go语言net包获取的表单数据为map[string][]string类型,因此验证器保持一致,方便直接验证form表单数据
data := map[string][]string{
		"name" : {"banana"},
		"mobile" : {"13800138000"},
}
rules := map[string]string{
    "name" : "min:1|max:10",
    "mobile" : "mobile",
}
valid, err := validator.New(data, rules)
if err != nil {
    // err 仅仅返回验证器中第一条错误验证信息
    fmt.Println(err.Error())
    // ValidErrors() 可以获取到本次验证所有验证错误字段信息
    fmt.Println(valid.ValidErrors())
}
2. 自定义错误提示信息 (custom valid msg)

该验证器支持自定义错误信息,方便大家再具体场景自定义错误描述内容,只需要在使用时传入第三个参数即可,错误提示的格式为map[string]string类型:

// 验证数据
data := map[string][]string{
		"name" : {"banana"},
		"mobile" : {"13800138000"},
}

// 验证规则
rules := map[string]string{
    "name" : "min:1|max:10",
    "mobile" : "mobile",
}

// 自定义错误提示
msg := map[string]string {
    "name" : "用户名格式不正确",
    "mobile" : "手机号格式不正确"
}

valid, err := validator.New(data, rules, msg)
if err != nil {
    // err 仅仅返回验证器中第一条错误验证信息
    fmt.Println(err.Error())
    // ValidErrors() 可以获取到本次验证所有验证错误字段信息
    fmt.Println(valid.ValidErrors())
}

错误消息自定可以针对单个验证规则,也可以针对整个需要验证的字段,例如:

msg := map[string]string {
    "name.min"  : "用户名至少一个字符",
    "name.max"  : "用户名不超过10个字符长度"
    "name"      : "用户名不正确"
}
3. 内置可以验证规则如下(rules)

默认情况下,传入验证器的所有数据验证都是required类型数据,如果需要对某个字段做可选项验证,那么可以添加nullable验证,即:

rules := map[string]string{
    "mobile" : "nullable|mobile", // 表示,如果验证数据中存在mobile且值不为空,那么就验证,否则跳过
}
名称 描述
required 验证默认即required, 一般不需要配置
min 验证字符串最小长度,支持多字节字符,例如中文
max 验证字符串最大长度,支持多字节字符,例如中文
regex 正则表达式验证,如果正则表达式中包含"
int 验证数据是否为整数
numeric 验证数据是否为数字串
nullable 验证数据可选,如果验证数据不存在或为空值,则跳过后续验证
email 验证数据是否为合法邮箱
url 验证数据是否为合法url地址
mobile 大陆11位手机号验证
3.1 正则验证规则使用注意

一般来说正则验证规则和其他验证规则类似,例如下面验证mobile字段为有效手机号的正则:

rules := map[string]string{
    "mobile" : "min:1|regex:^1[0-9]{10}$",
}

但是,如果一个正则验证中包含|符号时,则比较特殊,因为验证器本身是基于|来切分验证规则的,如果正则表达式中包含改符号会引起验证器错误拆分验证规则问题,那么此时,我们需要把所有验证规则放在一个slice切片中即可:

rules := map[string][]string{
    "mobile" : {"min:1", "regex:^1[0-9]{10}$"}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomMsgElem

type CustomMsgElem map[string]string

type ValidError

type ValidError struct {
	Field  string
	Errors map[string]string
}

单个验证字段错误提示

type Validator

type Validator struct {
	ValidErrors []ValidError // 验证错误
	// contains filtered or unexported fields
}

func New

func New(data map[string][]string, rules interface{}, args ...map[string]string) (*Validator, error)

*

  • 不带自定义错误验证 *
  • @param data map[string][]string 验证的值
  • @param rules map[string]string 验证规则
  • @return Validator, error 默认返回验证错误第一项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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