Validate表单验证
Validate表单验证
摸鱼的二哈文档地址:https://pkg.go.dev/github.com/go-playground/validator/v10
多个条件使用英文逗号分隔 ,
内置规则
字段相关
Tag | Description |
---|---|
eqcsfield | Field Equals Another Field (relative) |
eqfield | Field Equals Another Field |
fieldcontains | NOT DOCUMENTED IN doc.go |
fieldexcludes | NOT DOCUMENTED IN doc.go |
gtcsfield | Field Greater Than Another Relative Field |
gtecsfield | Field Greater Than or Equal To Another Relative Field |
gtefield | Field Greater Than or Equal To Another Field |
gtfield | Field Greater Than Another Field |
ltcsfield | Less Than Another Relative Field |
ltecsfield | Less Than or Equal To Another Relative Field |
ltefield | Less Than or Equal To Another Field |
ltfield | Less Than Another Field |
necsfield | Field Does Not Equal Another Field (relative) |
nefield | Field Does Not Equal Another Field |
字符串
Tag | Description |
---|---|
alpha | Alpha Only |
alphanum | Alphanumeric |
alphanumunicode | Alphanumeric Unicode |
alphaunicode | Alpha Unicode |
ascii | ASCII |
contains | Contains |
containsany | Contains Any |
containsrune | Contains Rune |
endswith | Ends With |
lowercase | Lowercase |
multibyte | Multi-Byte Characters |
number | NOT DOCUMENTED IN doc.go |
numeric | Numeric |
printascii | Printable ASCII |
startswith | Starts With |
uppercase | Uppercase |
比较
Tag | Description |
---|---|
eq | Equals |
gt | Greater than |
gte | Greater than or equal |
lt | Less Than |
lte | Less Than or Equal |
ne | Not Equal |
格式化
base64 | Base64 String |
---|---|
base64url | Base64URL String |
btc_addr | Bitcoin Address |
btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
datetime | Datetime |
e164 | e164 formatted phone number |
E-mail String | |
eth_addr | Ethereum Address |
hexadecimal | Hexadecimal String |
hexcolor | Hexcolor String |
hsl | HSL String |
hsla | HSLA String |
html | HTML Tags |
html_encoded | HTML Encoded |
isbn | International Standard Book Number |
isbn10 | International Standard Book Number 10 |
isbn13 | International Standard Book Number 13 |
json | JSON |
latitude | Latitude |
longitude | Longitude |
rgb | RGB String |
rgba | RGBA String |
ssn | Social Security Number SSN |
uuid | Universally Unique Identifier UUID |
uuid3 | Universally Unique Identifier UUID v3 |
uuid3_rfc4122 | Universally Unique Identifier UUID v3 RFC4122 |
uuid4 | Universally Unique Identifier UUID v4 |
uuid4_rfc4122 | Universally Unique Identifier UUID v4 RFC4122 |
uuid5 | Universally Unique Identifier UUID v5 |
uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
其他
标签 | 说明 |
---|---|
dir | Directory |
endswith | Ends With |
excludes | Excludes |
excludesall | Excludes All |
excludesrune | Excludes Rune |
file | File path |
isdefault | Is Default |
len | Length |
max | 最大 可用于数字(值大小)、字符串(长度)、切片(长度)、数组、字典 max=10 |
min | Minimum |
oneof | 其中之一,对string、int、uint;值必须指定的其中一个值 值可以是字符串或数字。若要匹配包含空格的字符串,请在单引号之间包含目标字符串。 oneof=red green oneof=’red green’ ‘blue yellow’ oneof=5 7 9 |
required | 必须,不能为空 |
required_if | 当指定字段满足指定值时,当前验证字段为必须,不能为空required_if=Field1 1 // 只有在Field1=1时,该字段为必须可以多个字段 required_if=Field1 1 Field2 true 同一字段多个值 required_if=Field1 1,required_if=Field1 2 |
required_unless | 与 required_if 功能相似,当指定字段不为指定值时为必填 |
required_with | 如果指定字段不为空,则该字段也不能为空,多个字段 或 关系 如: required_with = Field1 Field2 如果Field1 或 Field2 字段不为空则当前字段也不能为空 |
required_with_all | 同上,多个字段 与 关系 |
required_without | 当指定的字段为空时,则当前字段为必填,多个字段为或关系 |
required_without_all | 当指定的字段为空时,则当前字段为必填,多个字段为与关系 |
excluded_with | Excluded With |
excluded_with_all | Excluded With All |
excluded_without | Excluded Without |
excluded_without_all | Excluded Without All |
unique | Unique |
omitempty | 为空不验证,不为空时,验证后续规则 |
网络相关
Tag | Description |
---|---|
cidr | Classless Inter-Domain Routing CIDR |
cidrv4 | Classless Inter-Domain Routing CIDRv4 |
cidrv6 | Classless Inter-Domain Routing CIDRv6 |
datauri | Data URL |
fqdn | Full Qualified Domain Name (FQDN) |
hostname | Hostname RFC 952 |
hostname_port | HostPort |
hostname_rfc1123 | Hostname RFC 1123 |
ip | Internet Protocol Address IP |
ip4_addr | Internet Protocol Address IPv4 |
ip6_addr | Internet Protocol Address IPv6 |
ip_addr | Internet Protocol Address IP |
ipv4 | Internet Protocol Address IPv4 |
ipv6 | Internet Protocol Address IPv6 |
mac | Media Access Control Address MAC |
tcp4_addr | Transmission Control Protocol Address TCPv4 |
tcp6_addr | Transmission Control Protocol Address TCPv6 |
tcp_addr | Transmission Control Protocol Address TCP |
udp4_addr | User Datagram Protocol Address UDPv4 |
udp6_addr | User Datagram Protocol Address UDPv6 |
udp_addr | User Datagram Protocol Address UDP |
unix_addr | Unix domain socket end point Address |
uri | URI String |
url | URL String |
url_encoded | URL Encoded |
urn_rfc2141 | Urn RFC 2141 String |