binding

package
v1.0.24 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: BSD-2-Clause Imports: 10 Imported by: 0

README

required 必填

eq 等于

in 包含

not-in 不包含

gt 大于

gte 大于等于

lt 小于

lte 小于等于

len 长度等于

min 长度最小

max 长度最大

match 正则式匹配

mobile 手机号码

tel 座机

qq QQ号

postcode 邮编

account 账号

password 密码

password2 中度密码

password3 重度密码

email 邮箱

url url地址

domain 域名

mac MAC地址

ip ip地址

ip4 ip4地址

ip6 ip6地址

id-number 身份证号码

date 日期

datetime 日期时间

hour-minute 时间(小时分钟)

luhn 银行卡号

no-repeat 数组不重复

自定义关键词 参考下方自定义验证使用方法

binding.RegValidater("自定义验证", func(val interface{}, arg string) bool{ //val = 调用参数绑定时传入的数据 , 例如 me //arg = 验证参数设置的自定义条件 ,例如 me=123,那么123就是传入的参数 }) 定时任务

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(ptr interface{}, m map[string]interface{}) *bind

map绑定器

func HttpBindCookie

func HttpBindCookie(r *http.Request, objectPtr interface{}) error

func HttpBindForm

func HttpBindForm(r *http.Request, objectPtr interface{}) error

func HttpBindHead

func HttpBindHead(r *http.Request, objectPtr interface{}) error

func HttpBindJSON

func HttpBindJSON(r *http.Request, objectPtr interface{}) error

func HttpBindQuery

func HttpBindQuery(r *http.Request, objectPtr interface{}) error

http数据自动绑定

func IsAccount

func IsAccount(val interface{}) bool

帐号规则(字母开头,只能包含字母、数字和下划线,长度在6~18之间) tag使用方法 account

func IsArrayRepeat

func IsArrayRepeat(val interface{}) bool

数组是否有重复

func IsDate

func IsDate(val interface{}) bool

日期 格式 2006-01-02 tag使用方法 date

func IsDatetime

func IsDatetime(val interface{}) bool

日期时间 格式 2006-01-02 15:04:05 tag使用方法 datetime

func IsDomain

func IsDomain(val interface{}) bool

domain tag使用方法 domain

func IsEmail

func IsEmail(val interface{}) bool

邮件 tag使用方法 email

func IsEq

func IsEq(val interface{}, arg string) bool

等于(对数字、字符、布尔类型有效) tag使用方法 eq=10

func IsFloat added in v1.0.17

func IsFloat(val interface{}) bool

是否为浮点型

func IsGreatThan

func IsGreatThan(val interface{}, a float64) bool

大于(对数字类型有效) tag使用方法 gt=10

func IsGreatThanAndEqual

func IsGreatThanAndEqual(val interface{}, a float64) bool

大于等于(对数字类型有效) tag使用方法 gte=10

func IsHourMinute

func IsHourMinute(val interface{}) bool

时间

func IsIDNumber

func IsIDNumber(val interface{}) bool
公民身份证号
xxxxxx yyyy MM dd 375 0     十八位
xxxxxx   yy MM dd  75 0     十五位

地区:[1-9]\d{5}
年的前两位:(18|19|([23]\d))      1800-2399
年的后两位:\d{2}
月份:((0[1-9])|(10|11|12))
天数:(([0-2][1-9])|10|20|30|31) 闰年不能禁止29+

三位顺序码:\d{3}
两位顺序码:\d{2}
校验码:   [0-9Xx]

十八位:^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
十五位:^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$

总:
(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)

tag使用方法 id-number

func IsIP

func IsIP(val interface{}) bool

IP地址(ip4和ip6均可) tag使用方法 ip

func IsIP4

func IsIP4(val interface{}) bool

IP4地址 tag使用方法 ip4

func IsIP6

func IsIP6(val interface{}) bool

IP6地址 tag使用方法 ip6

func IsIn

func IsIn(val interface{}, arg string) bool

在范围内(对数字、字符、布尔类型有效) tag使用方法 in=a|b|c,多个规则使用|分隔

func IsInt added in v1.0.17

func IsInt(val interface{}) bool

是否为整型

func IsLen

func IsLen(val interface{}, l int) bool

长度验证(对数字、字符、切片类型有效) tag使用方法 len=10

func IsLessThan

func IsLessThan(val interface{}, a float64) bool

小于(对数字类型有效) tag使用方法 lt=10

func IsLessThanAndEuqal

func IsLessThanAndEuqal(val interface{}, a float64) bool

小于等于(对数字类型有效) tag使用方法 lte=10

func IsLuHn

func IsLuHn(val interface{}) bool

LUHN规则(银行卡号验证规则) tag使用方法 luhn

func IsMac

func IsMac(val interface{}) bool

MAC地址 tag使用方法 mac

func IsMatch

func IsMatch(val interface{}, arg string) bool

正则式处理 arg=正则表达式 tag使用方法 match=^\d+$ 注意:不要在正则式中间加:

func IsMax

func IsMax(val interface{}, l int) bool

最大长度度(对字符、数字、切片类型有效) tag使用方法 max=10

func IsMin

func IsMin(val interface{}, l int) bool

最小长度(对字符、数字、切片类型有效) tag使用方法 min=10

func IsMobile

func IsMobile(val interface{}) bool
  • 验证所给手机号码是否符合手机号的格式.
  • 移动: 134、135、136、137、138、139、150、151、152、157、158、159、182、183、184、187、188、178(4G)、147(上网卡);
  • 联通: 130、131、132、155、156、185、186、176(4G)、145(上网卡)、175;
  • 电信: 133、153、180、181、189 、177(4G);
  • 卫星通信: 1349
  • 虚拟运营商: 170、171,173
  • 2018新增: 16x, 19x

tag使用方法 mobile

func IsNotIn

func IsNotIn(val interface{}, arg string) bool

不在范围内(对数字、字符、布尔类型有效) tag使用方法 not-in=a|b|c,多个规则使用|分隔

func IsNotNull

func IsNotNull(val interface{}) bool

为空验证(对字符、切片、对象类型有效)

func IsPassword

func IsPassword(val interface{}) bool

通用密码(任意可见字符,长度在6~18之间) tag使用方法 password

func IsPassword2

func IsPassword2(val interface{}) bool

中等强度密码(在弱密码的基础上,必须包含大小写字母和数字) tag使用方法 password2

func IsPassword3

func IsPassword3(val interface{}) bool

强等强度密码(在弱密码的基础上,必须包含大小写字母、数字和特殊字符) tag使用方法 password3

func IsPostcode

func IsPostcode(val interface{}) bool

邮政编码 tag使用方法 postcode

func IsQQ

func IsQQ(val interface{}) bool

腾讯QQ号,从10000开始 tag使用方法 tel

func IsTel

func IsTel(val interface{}) bool

国内座机电话号码:"XXXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"、"XXXXXXXX" tag使用方法 tel

func IsUrl

func IsUrl(val interface{}) bool

URL tag使用方法 url

func Marshal

func Marshal(obj interface{}) string

json反序列化

func ParseFloat

func ParseFloat(s string) float64

字符串转换为浮点数

func ParseInt

func ParseInt(s string) int64

字符串转换为整数

func RegValidater

func RegValidater(name string, fn Handler)

添加验证器

func ToBool

func ToBool(val interface{}) (bool, error)

转换为bool类型,大于0的数字=true,小于0的数字=false

func ToFloat64

func ToFloat64(val interface{}) (float64, error)

转换为float类型

func ToInt64

func ToInt64(val interface{}) (int64, error)

转换为int64

func ToMap

func ToMap(val interface{}) (map[string]interface{}, error)

转换为map

func ToSlice

func ToSlice(val interface{}) ([]interface{}, error)

转换为slice

func ToString

func ToString(val interface{}) (str string, err error)

转换为string

func ToTime

func ToTime(val interface{}, layout string) (time.Time, error)

转换为日期

func ToUint64

func ToUint64(val interface{}) (uint64, error)

转换为uint64

Types

type BindError

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

绑定错误

func (*BindError) Error

func (this *BindError) Error() string

func (*BindError) Field

func (this *BindError) Field() string

func (*BindError) Msg

func (this *BindError) Msg() string

type Handler

type Handler func(val interface{}, arg string) bool

验证方法 val=值,arg=参数

type SerializeInterface

type SerializeInterface interface {
	UnmarshalJSON(data []byte) error
	MarshalJSON() ([]byte, error)
}

json序列化接口

Jump to

Keyboard shortcuts

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