Categorygithub.com/ghp3000/validator
modulepackage
0.0.0-20240407070430-cd18cd06e963
Repository: https://github.com/ghp3000/validator.git
Documentation: pkg.go.dev

# README

代码复制自gin项目,做了一些使用便利性修改

比较规则

规则描述
eq等于
eq_ignore_case等于,忽略大小写
ne不等于
ne_ignore_case不等于,忽略大小写
gt大于
gte大于等于
lt小于
lte小于等于

字段规则

规则描述
eqfield当前字段必须等于指定字段
nefield当前字段必须不等于指定字段
gtfield当前字段必须大于指定字段
gtefield当前字段必须大于等于指定字段
ltfield当前字段必须小于指定字段
ltefield当前字段必须小于等于指定字段
fieldcontains当前字段必须包含指定字段,只能用于字符串类型
fieldexcludes当前字段必须不包含指定字段,只能用于字符串类型
eqcsfield当前字段必须等于指定结构体的字段
necsfield当前字段必须不等于指定结构体的字段
gtcsfield当前字段必须大于指定结构体的字段
gtecsfield当前字段必须大于等于指定结构体的字段
ltcsfield当前字段必须小于指定结构体的字段
ltecsfield当前字段必须小于等于指定结构体的字段

字符串规则

TagDescription
alpha仅限字母
alphanum仅限字母、数字
alphanumunicode仅限字母、数字和 Unicode
alphaunicode字母和 Unicode
asciiASCII码字符
boolean当前字段必须是能够被 strconv.ParseBool 解析为字符串的值
contains当前字段必须包含指定字符串
containsanyContains Any
containsruneContains Rune
endsnotwith当前字段必须不是以指定字符串结尾
endswith当前字段必须以指定字符串结尾
excludes不包含
excludesallExcludes All
excludesruneExcludes Rune
lowercase当前字段的字母必须是小写,可包含数字,不能为空
multibyteMulti-Byte Characters
number数字
numericNumeric
printascii可打印的ASCII码字符
startsnotwith当前字段必须不是以指定字符串开始
startswith当前字段必须以指定字符串开头
uppercase当前字段的字母必须是大写,可包含数字,不能为空

字段规则

规则描述
base64当前字段必须是有效的 Base64 值,不能为空
base64url当前字段必须是包含根据 RFC4648 规范的有效 base64 URL 安全值。
json正确的 JSON 串
rgb正确的 RGB 字符串
rgba正确的 RGBA 字符串
dir指定字段的值必须是已存在的目录
dirpath指定字段的值必须是合法的目录路径
file指定字段的值必须是已存在的文件
filepath指定字段的值必须是合法的文件路径
len当前字段的长度必须指定值,可用于 string、slice 等
max当前字段的最大值必须是指定值
min当前字段的最小值必须是指定值
required当前字段为必填项,且不能为零值
required_if当指定字段等于给定值时,当前字段使用 required 验证。如 required_if=Field1 foo Field2 bar
required_unless当指定字段不等于给定值时,当前字段使用 required 验证。如
required_with当任一指定字段不为零值时,当前字段使用 required 验证。如 required_with=Field1 Field2
required_with_all当所有指定字段不为零值时,当前字段使用 required 验证。

网络类规则

规则描述
cidr点分格式的ip地址.比如:192.168.1.1或fe80::ba44:827f:4389:6c4b%3
cidrv4点分格式的ipv4地址.比如:192.168.1.1
cidrv6点分格式的ipv4地址.比如:fe80::ba44:827f:4389:6c4b%3
datauriData URL
fqdnFull Qualified Domain Name (FQDN)
hostname符合RFC 952规范的主机名
hostname_port主机的端口号. 0-65535
hostname_rfc1123符合 RFC 1123 规范的主机名
ipInternet Protocol Address IP
ip4_addrInternet Protocol Address IPv4
ip6_addrInternet Protocol Address IPv6
ip_addrInternet Protocol Address IP
ipv4Internet Protocol Address IPv4
ipv6Internet Protocol Address IPv6
macMedia Access Control Address MAC
tcp4_addrTransmission Control Protocol Address TCPv4
tcp6_addrTransmission Control Protocol Address TCPv6
tcp_addrTransmission Control Protocol Address TCP
udp4_addrUser Datagram Protocol Address UDPv4
udp6_addrUser Datagram Protocol Address UDPv6
udp_addrUser Datagram Protocol Address UDP
unix_addrUnix domain socket end point Address
uriURI String
urlURL String
http_urlHTTP URL String
url_encodedURL Encoded
urn_rfc2141Urn RFC 2141 String

格式化

TagDescription
base64Base64 String
base64urlBase64URL String
base64rawurlBase64RawURL String
bicBusiness Identifier Code (ISO 9362)
bcp47_language_tagLanguage tag (BCP 47)
btc_addrBitcoin Address
btc_addr_bech32Bitcoin Bech32 Address (segwit)
credit_cardCredit Card Number
mongodbMongoDB ObjectID
cronCron
spicedbSpiceDb ObjectID/Permission/Type
datetimeDatetime
e164e164 formatted phone number
emailE-mail String
eth_addrEthereum Address
hexadecimalHexadecimal String
hexcolorHexcolor String
hslHSL String
hslaHSLA String
htmlHTML Tags
html_encodedHTML Encoded
isbnInternational Standard Book Number
isbn10International Standard Book Number 10
isbn13International Standard Book Number 13
issnInternational Standard Serial Number
iso3166_1_alpha2Two-letter country code (ISO 3166-1 alpha-2)
iso3166_1_alpha3Three-letter country code (ISO 3166-1 alpha-3)
iso3166_1_alpha_numericNumeric country code (ISO 3166-1 numeric)
iso3166_2Country subdivision code (ISO 3166-2)
iso4217Currency code (ISO 4217)
jsonJSON
jwtJSON Web Token (JWT)
latitudeLatitude
longitudeLongitude
luhn_checksumLuhn Algorithm Checksum (for strings and (u)int)
postcode_iso3166_alpha2Postcode
postcode_iso3166_alpha2_fieldPostcode
rgbRGB String
rgbaRGBA String
ssnSocial Security Number SSN
timezoneTimezone
uuidUniversally Unique Identifier UUID
uuid3Universally Unique Identifier UUID v3
uuid3_rfc4122Universally Unique Identifier UUID v3 RFC4122
uuid4Universally Unique Identifier UUID v4
uuid4_rfc4122Universally Unique Identifier UUID v4 RFC4122
uuid5Universally Unique Identifier UUID v5
uuid5_rfc4122Universally Unique Identifier UUID v5 RFC4122
uuid_rfc4122Universally Unique Identifier UUID RFC4122
md4MD4 hash
md5MD5 hash
sha256SHA256 hash
sha384SHA384 hash
sha512SHA512 hash
ripemd128RIPEMD-128 hash
ripemd128RIPEMD-160 hash
tiger128TIGER128 hash
tiger160TIGER160 hash
tiger192TIGER192 hash
semverSemantic Versioning 2.0.0
ulidUniversally Unique Lexicographically Sortable Identifier ULID
cveCommon Vulnerabilities and Exposures Identifier (CVE id)

其他

TagDescription
dir文件夹是否存在
dirpath文件路径
file文件是否存在
filepath文件路径
imageImage
isdefaultIs Default
len长度:len=10
max最大值:max=20
min最小值:min=1
oneof枚举值,数值或字符串,以空格分隔,如果字符串中有空格,则使用单引号包围。例如:oneof=beijing shanghai
required必须有
required_ifRequired If
required_unlessRequired Unless
required_withRequired With
required_with_allRequired With All
required_withoutRequired Without
required_without_allRequired Without All
excluded_ifExcluded If
excluded_unlessExcluded Unless
excluded_withExcluded With
excluded_with_allExcluded With All
excluded_withoutExcluded Without
excluded_without_allExcluded Without All
unique唯一值,一般用于slice ,约束slice的成员不重复

Aliases:

TagDescription
iscolor颜色值,支持:hexcolor|rgb|rgba|hsl|hsla
country_code国家或地区代码:iso3166_1_alpha2|iso3166_1_alpha3|iso3166_1_alpha_numeric

例子:

package main

import (
"fmt"
"github.com/ghp3000/validator"
)

type User struct {
User     string `validate:"min=6,max=32"`
Password string `validate:"min=12,max=32"`
}

func main() {
    err := validator.SetLanguage(validator.LangZh)
    if err != nil {
        fmt.Println(err)
    }
    u := User{User: "admin", Password: "123456"}
    err = validator.Struct(u)
    if err != nil {
        fmt.Println(err)
    }
    i := 10
    validator.SetLanguage(validator.LangEn)
    if err := validator.Var(&i, "min=1,max=9"); err != nil {
        fmt.Println(err)
    }
}

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author