上篇博文的表述是有问题,整个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 | 字符串 | 复杂正则不展示 | |
字符串 | 复杂正则不展示 | `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"` |
oneof | OneOfString | 对应规则按需验证 | `validate:"oneof=red green"` |
oneof | OneOfInt | 对应规则按需验证 | `validate:"oneof=5 63"` |
unique | UniqueSlice | 对应规则按需验证 | `validate:"unique"` |
unique | UniqueArray | 对应规则按需验证 | `validate:"unique"` |
unique | UniqueMap | 对应规则按需验证 | `validate:"unique"` |
json | JSONString | 对应规则按需验证 | `validate:"json"` |
lowercase | LowercaseString | 对应规则按需验证 | `validate:"lowercase"` |
uppercase | UppercaseString | 对应规则按需验证 | `validate:"uppercase"` |
datetime | Datetime | 对应规则按需验证 | `validate:"datetime=2006-01-02"` |
----- 接下来的工作 重写swagger-models的生成过程, 目前来看swagger-go 的生成过程对于models的处理只针对definetion的处理。加上go语言的特性使allof 以及 discriminator: "type" 是冲突的,所以我将重写这一部分让models 关注models
------ 整体入参无论是 path ,query , header , form 中可获取参数 跟随者models一起 定义在 具体的operation的Handle入参之中。 以完成校验。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)