validation

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 4 Imported by: 1

README

validation

Go Go Report Card License Go version codecov PkgGoDev

数据验证

包含了以下类型的数据验证:

  • GB11643 身份证验证和分析;
  • GB32100 统一信用代码验证和分析;
  • 银行卡卡号验证;
  • ISBN 验证;
  • IPv4/IPv6 验证;
  • 中国区手机/电话号码验证;
  • 域名验证;
  • Email 验证;
import (
    "github.com/issue9/validation"
    "github.com/issue9/validation/validator"
    "golang.org/x/text/language"
    "golang.org/x/text/message"
)

type Object {
    Age int
    Name string
}

o := &Object{}

p := message.NewPrinter(language.MustParse("cmn-Hans"))
v := validation.New(validation.ContinueAtError, p, ".")
messages := v.NewField(&o.Age, "age", validator.Min(18).Message("必须大于 18")).
    NewField(&o.Name, "name", validator.Required(false).Message("不能为空")).
    Messages()

本地化

本地化采用 golang.org/x/text 包

import (
    "github.com/issue9/validation"
    "github.com/issue9/validation/validator"
    "golang.org/x/text/language"
    "golang.org/x/text/message"
    "golang.org/x/text/message/catalog"
)

type Object {
    Age int
    Name string
}

builder := catalog.NewBuilder()
builder.SetString(language.SimplifiedChinese, "lang", "chn")
builder.SetString(language.TraditionalChinese, "lang", "cht")

o := &Object{}

p := message.NewPrinter(language.SimplifiedChinese, message.Catalog(builder))
v := validation.New(validation.ContinueAtError, p, ".")
messages := v.NewField(&o.Age, "age", validator.Min(18).Message("lang")). // 根据 p 的不同,会输出不同内容
    NewField(&o.Name, "name", validator.Required(false).Message("不能为空")).
    Messages()

版权

本项目采用 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(*Validation)
}

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 (msg Messages) Add(key string, val ...string)

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

func (Messages) Empty added in v0.6.0

func (msg Messages) Empty() bool

func (Messages) Merge added in v0.4.0

func (msg Messages) Merge(m Messages)

Merge 将另一个 Messages 内容合并到当前实例

func (Messages) Set

func (msg 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 实例

func (*Rule) AsSlice added in v0.2.0

func (rule *Rule) AsSlice() *Rule

AsSlice 以数组的形式验证数据

如果指定此属性,则所有 kind 值为 reflect.Array 和 reflect.Slice 的都将被当作数组处理, 包括 []byte 和 []rune 等。 其它类型继续以正常元素处理。

如果未指定此属性,则所有类型的元素都被当作一个值进行验证,即使是数组。

type ValidateFunc

type ValidateFunc func(interface{}) bool

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

func (ValidateFunc) IsValid

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

IsValid 将当前函数作为 Validator 使用

func (ValidateFunc) Message added in v0.2.0

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

Message 当前当前的验证函数转换为 Rule 实例

参数作为翻译项,在出错时,按要求输出指定的本地化错误信息。

type Validation

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

Validation 验证器

func New

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

New 返回 Validation 对象

separator 用于指定字段名称上下级元素名称之间的连接符。比如在返回 xml 元素时, 可能会采用 root/element 的格式表示上下级,此时 separator 应设置为 /。 而在 json 中,可能会被转换成 root.element 的格式。

func (*Validation) Messages

func (v *Validation) Messages() Messages

Messages 返回验证结果

func (*Validation) NewField

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

NewField 验证新的字段

val 表示需要被验证的值,如果是一个对象且需要验证子字段,那么让对象实现 FieldsValidator 接口, 则会自动调用该方法验证子项,将会将验证完的信息返回给当前的 Validation 实例; name 表示当前字段的名称; rules 表示验证的规则,按顺序依次验证。

type Validator

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

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

Directories

Path Synopsis
is
Package is 包提供了一系列的判断函数
Package is 包提供了一系列的判断函数
gb11643
Package gb11643 解析身分证详情
Package gb11643 解析身分证详情
gb32100
Package gb32100 统一信用代码校验 GB32100—2015
Package gb32100 统一信用代码校验 GB32100—2015
luhn
Package luhn 模 10 校验算法 https://en.wikipedia.org/wiki/Luhn_algorithm 1.
Package luhn 模 10 校验算法 https://en.wikipedia.org/wiki/Luhn_algorithm 1.
Package validator 提供各类验证器
Package validator 提供各类验证器

Jump to

Keyboard shortcuts

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