gin-binding --- go-playgroundvalidator 的常用关键字

gin-binding --- go-playgroundvalidator 的常用关键字,第1张

上篇博文的表述是有问题,整个binding并不是 gin工程的其依然是使用了go-playground/validator 的校验系统。

 

package binding

import (
	"reflect"
	"sync"

	"github.com/go-playground/validator/v10"
)

type defaultValidator struct {
	once     sync.Once
	validate *validator.Validate
}

var _ StructValidator = &defaultValidator{}

// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
	value := reflect.ValueOf(obj)
	valueType := value.Kind()
	if valueType == reflect.Ptr {
		valueType = value.Elem().Kind()
	}
	if valueType == reflect.Struct {
		v.lazyinit()
		if err := v.validate.Struct(obj); err != nil {
			return err
		}
	}
	return nil
}

所以我们整理也是应当用go-playground/validator 的源代码整理。代码中的go文件为 pt.go ,整理如下。

 

关键字针对对象功能示例
required属性,结构,文件标示必须存在`validate:"required"`
len字符串,数组,时间间隔,文件长度标示长度,size,间隔,大小`validate:"len=1"`
min字符串,数字,数组,时间间隔标示最小`validate:"min=1"`
max字符串,数字,数组,时间标示最大`validate:"max=7"`
eq字符串,数组,时间间隔,布尔值标示相等,正对数组是长度`validate:"eq=3"`
ne字符串,数组,时间间隔,布尔值标示不相等`validate:"ne="`
lt字符串,数字,数组,时间小于`validate:"lt=3"`
lte字符串,数字,数组,时间小于等于`validate:"lte=3"`
gt字符串,数字,数组,时间大于`validate:"gt=3"`
gte字符串,数字,数组,时间大于等于`validate:"gte=3"`
eqfield同级属性等于`validate:"eqfield=MaxString"`
eqcsfield内部属性等于`validate:"eqcsfield=Inner.EqCSFieldString"`
necsfield内部属性不等于`validate:"necsfield=Inner.NeCSFieldString"`
gtcsfield内部属性大于`validate:"gtcsfield=Inner.GtCSFieldString"`
ltcsfield内部属性小于`validate:"ltcsfield=Inner.LtCSFieldString"`
ltecsfield内部属性小于等于`validate:"ltecsfield=Inner.LteCSFieldString"`
nefield同级属性不等于`validate:"nefield=EqFieldString"`
gtfield同级属性大于`validate:"gtfield=MaxString"`
gtefield同级属性大于等于`validate:"gtefield=MaxString"`
ltfield同级属性小于`validate:"ltfield=MaxString"`
ltefield同级属性小于等于`validate:"ltefield=MaxString"`
alpha字符串"^[a-zA-Z]+$"`validate:"alpha"`
alphanum字符串"^[a-zA-Z0-9]+$"`validate:"alphanum"`
numeric字符串"^[-+]?[0-9]+(?:\.[0-9]+)?$"`validate:"numeric"`
number字符串 "^[0-9]+$"`validate:"number"`
hexadecimal字符串"^(0[xX])?[0-9a-fA-F]+$"`validate:"hexadecimal"`
hexcolor字符串"^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"`validate:"hexcolor"`
rgb字符串复杂正则不展示`validate:"rgb"`
rgba字符串复杂正则不展示 
hsl字符串复杂正则不展示 
hsla字符串复杂正则不展示 
email字符串复杂正则不展示`validate:"email"`
url字符串url规则`validate:"url"`
uri字符串uri规则`validate:"uri"`
base64字符串"^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$"`validate:"base64"`
contains字符串包含`validate:"contains=purpose"`
containsany字符串包含任意一个`validate:"containsany=!@#$"`
excludes字符串不包含`validate:"excludes=text"`
excludesall字符串不包含任意一个`validate:"excludesall=!@#$"`
excludesrune字符串不包含某个rune类型`validate:"excludesrune=☻"`
isbn字符串两个isbn`validate:"isbn"`
isbn10字符串"^(?:[0-9]{9}X|[0-9]{10})$"`validate:"isbn10"`
isbn13字符串^(?:(?:97(?:8|9))[0-9]{10})$"`validate:"isbn13"`
uuid字符串"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"`validate:"uuid"`
uuid3字符串"^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"`validate:"uuid3"`
uuid4字符串"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"`validate:"uuid4"`
uuid5字符串"^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"`validate:"uuid5"`
ascii字符串"^[\x00-\x7F]*$"`validate:"ascii"`
printascii字符串^[\x20-\x7E]*$"`validate:"printascii"`
multibyte字符串"[^\x00-\x7F]"`validate:"multibyte"`
datauri字符串`^data:((?:\w+\/(?:([^;]|;[^;]).)+)?)``validate:"datauri"`
latitude字符串"^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$"`validate:"latitude"`
longitude字符串 "^[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$"`validate:"longitude"`
ssn字符串^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$``validate:"ssn"`
ip字符串ip规则`validate:"ip"`
ipv4字符串ipv4规则`validate:"ipv4"`
ipv6字符串ipv6规则`validate:"ipv6"`
cidr字符串ip规则`validate:"cidr"`
cidrv4字符串ipv4规则`validate:"cidrv4"`
cidrv6字符串ipv6规则`validate:"cidrv6"`
tcp_addr字符串对应规则按需验证`validate:"tcp_addr"`
tcp4_addr字符串对应规则按需验证`validate:"tcp4_addr"`
tcp6_addr字符串对应规则按需验证`validate:"tcp6_addr"`
udp_addr字符串对应规则按需验证`validate:"udp_addr"`
udp4_addr字符串对应规则按需验证`validate:"udp4_addr"`
udp6_addr字符串对应规则按需验证`validate:"udp6_addr"`
ip_addr字符串对应规则按需验证`validate:"ip_addr"`
ip4_addr字符串对应规则按需验证`validate:"ip4_addr"`
ip6_addr字符串对应规则按需验证`validate:"ip6_addr"`
unix_addr字符串对应规则按需验证`validate:"unix_addr"`
mac字符串对应规则按需验证`validate:"mac"`
iscolor字符串颜色校验所有颜色规则`validate:"iscolor"`
oneofOneOfString对应规则按需验证`validate:"oneof=red green"`
oneofOneOfInt对应规则按需验证`validate:"oneof=5 63"`
uniqueUniqueSlice对应规则按需验证`validate:"unique"`
uniqueUniqueArray对应规则按需验证`validate:"unique"`
uniqueUniqueMap对应规则按需验证`validate:"unique"`
jsonJSONString对应规则按需验证`validate:"json"`
lowercaseLowercaseString对应规则按需验证`validate:"lowercase"`
uppercaseUppercaseString对应规则按需验证`validate:"uppercase"`
datetimeDatetime对应规则按需验证`validate:"datetime=2006-01-02"`

----- 接下来的工作  重写swagger-models的生成过程, 目前来看swagger-go 的生成过程对于models的处理只针对definetion的处理。加上go语言的特性使allof 以及  discriminator: "type" 是冲突的,所以我将重写这一部分让models 关注models

------ 整体入参无论是  path  ,query , header , form 中可获取参数 跟随者models一起  定义在 具体的operation的Handle入参之中。 以完成校验。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995967.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存