🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Package validator ================= <img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ![Project status](https://img.shields.io/badge/version-10.11.1-green.svg) [![Build Status](https://travis-ci.org/go-playground/validator.svg?branch=master)](https://travis-ci.org/go-playground/validator) [![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-playground/validator?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator) [![GoDoc](https://godoc.org/github.com/go-playground/validator?status.svg)](https://pkg.go.dev/github.com/go-playground/validator/v10) ![License](https://img.shields.io/dub/l/vibe-d.svg) 包验证器根据标签对结构和单个字段进行值验证。 它具有以下**特色**功能:: - 使用验证标记或自定义验证器进行跨域和跨结构验证。 - 切片、阵列和映射潜水,允许验证多维字段的任何或所有级别。 - 能够深入地图键和值进行验证 - 通过在验证之前确定类型接口的基础类型来处理该接口。 - 处理自定义字段类型,如sql驱动程序Valuer请参阅 [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29) - 别名验证标记,允许将多个验证映射到单个标记,以便更容易地定义结构上的验证 - 提取自定义字段名,例如,可以指定在验证时提取JSON名称,并在生成的FieldError中提供该名称 - 可自定义的i18n感知错误消息。 - 的默认验证器 [gin](https://github.com/gin-gonic/gin) web框架;在gin see中从v8升级到v9 [here](https://github.com/go-playground/validator/tree/master/_examples/gin-upgrading-overriding) 安装 ------------ 使用 go get. go get github.com/go-playground/validator/v10 然后将验证器包导入到您自己的代码中。 import "github.com/go-playground/validator/v10" 错误返回值 ------- 验证函数返回类型错误 它们返回类型错误以避免下面讨论的问题,其中 err != nil: * http://stackoverflow.com/a/29138676/3158232 * https://github.com/go-playground/validator/issues/134 对于错误的验证输入,Validator仅返回InvalidValidationError,nil或ValidationErrors作为类型错误;因此,在代码中,您需要做的就是检查返回的错误是否不是nil,如果没有检查错误是否为InvalidValidationError(如果需要,大多数情况下不是),请将其类型转换为ValidationErrors,如下所示: ```go err := validate.Struct(mystruct) validationErrors := err.(validator.ValidationErrors) ``` 使用和文档 ------ 请参见 https://pkg.go.dev/github.com/go-playground/validator/v10 详细的使用文档。 ##### 例子: - [简单](https://github.com/go-playground/validator/blob/master/_examples/simple/main.go) - [自定义字段类型](https://github.com/go-playground/validator/blob/master/_examples/custom/main.go) - [结构层](https://github.com/go-playground/validator/blob/master/_examples/struct-level/main.go) - [翻译和自定义错误](https://github.com/go-playground/validator/blob/master/_examples/translations/main.go) - [Gin 升级和/或覆盖验证器](https://github.com/go-playground/validator/tree/v9/_examples/gin-upgrading-overriding) - [wash - 将所有内容组合在一起的示例应用程序](https://github.com/bluesuncorp/wash) 验证规则 ------ ### Fields: | Tag | 描述 | | - | - | | eqcsfield | 字段等于另一个字段 (相对)| | eqfield | 字段等于另一个字段 | | fieldcontains | NOT DOCUMENTED IN doc.go | | fieldexcludes | NOT DOCUMENTED IN doc.go | | gtcsfield | 大于另一个相关字段的字段 | | gtecsfield | 字段大于或等于另一个相关字段 | | gtefield | 字段大于或等于另一个字段 | | gtfield | 字段大于另一个字段 | | ltcsfield | 小于另一个相关字段 | | ltecsfield | 小于或等于另一个相关字段 | | ltefield | 小于或等于另一个字段 | | ltfield | 小于另一个字段 | | necsfield | 字段不等于另一个字段(相对) | | nefield | 字段不等于另一个字段 | ### Network: | Tag | 描述 | | - | - | | cidr | 无类域间路由 CIDR | | cidrv4 | 无类域间路由 CIDRv4 | | cidrv6 | 无类别域间路由CIDRv6 | | datauri | 资源URL | | fqdn | 完全限定域名 (FQDN) | | hostname | 主机名 RFC 952 | | hostname_port | 主机端口 | | hostname_rfc1123 | 主机名 RFC 1123 | | ip | 互联网协议地址 IP | | ip4_addr | 互联网协议地址 IPv4 | | ip6_addr | 互联网协议地址 IPv6 | | ip_addr | 互联网协议地址 IP | | ipv4 | 互联网协议地址 IPv4 | | ipv6 | 互联网协议地址 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 | ### Strings: | Tag | 描述 | | - | - | | alpha | 仅限字母 | | alphanum | 字母数字 | | alphanumunicode | 字母数字Unicode | | alphaunicode | 字母Unicode | | ascii | ASCII | | boolean | Boolean | | contains | 包含 | | containsany | 包含任何 | | containsrune | 包含 Rune | | endsnotwith | 结尾不是 | | endswith | Ends With | | excludes | 排除 | | excludesall | Excludes All | | excludesrune | Excludes Rune | | lowercase | 小写 | | multibyte | 多字节字符 | | number | NOT DOCUMENTED IN doc.go | | numeric | Numeric | | printascii | 可打印的 ASCII | | startsnotwith | Starts Not With | | startswith | Starts With | | uppercase | 大写 | ### Format: | Tag | 描述 | | - | - | | base64 | Base64 字符串 | | base64url | Base64URL String | | bic | 企业识别码 (ISO 9362) | | bcp47_language_tag | 语言标签 (BCP 47) | | btc_addr | 比特币地址 | | btc_addr_bech32 | 比特币 Bech32 地址(segwit | | credit_card | 信用卡号 | | datetime | Datetime | | e164 | e164格式电话号码 | | email | E-mail String | | eth_addr | 以太坊地址 | | hexadecimal | 十六进制字符串 | | hexcolor | 十六进制颜色字符串 | | hsl | HSL String | | hsla | HSLA String | | html | HTML Tags | | html_encoded | HTML编码 | | isbn | 国际标准书号 | | isbn10 | 国际标准图书编号10 | | isbn13 | 国际标准图书编号13 | | iso3166_1_alpha2 | 两个字母的国家代码 (ISO 3166-1 alpha-2) | | iso3166_1_alpha3 | 三字母国家代码 (ISO 3166-1 alpha-3) | | iso3166_1_alpha_numeric | 数字国家代码(ISO 3166-1 数字) | | iso3166_2 | 国家细分代码 (ISO 3166-2) | | iso4217 | 货币代码 (ISO 4217) | | json | JSON | | jwt | JSON Web Token (JWT) | | latitude | 纬度 | | longitude | 经度 | | postcode_iso3166_alpha2 | 邮政编码 | | postcode_iso3166_alpha2_field | 邮政编码 | | rgb | RGB String | | rgba | RGBA String | | ssn | 社会保障号码SSN | | timezone | 时区 | | uuid | 通用唯一标识符 UUID | | uuid3 | 通用唯一标识符 UUID v3 | | uuid3_rfc4122 | 通用唯一标识符 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 | | md4 | MD4 hash | | md5 | MD5 hash | | sha256 | SHA256 hash | | sha384 | SHA384 hash | | sha512 | SHA512 hash | | ripemd128 | RIPEMD-128 hash | | ripemd128 | RIPEMD-160 hash | | tiger128 | TIGER128 hash | | tiger160 | TIGER160 hash | | tiger192 | TIGER192 hash | | semver | 语义版本控制 2.0.0 | | ulid | 普遍唯一的字典排序标识符 ULID | ### Comparisons: | Tag | 描述 | | - | - | | eq | 等于 | | gt | 大于| | gte | 大于或等于 | | lt | 小于 | | lte | 小于或等于 | | ne | 不等于 | ### Other: | Tag | 描述 | | - | - | | dir | 目录 | | file | 文件路径 | | isdefault | 是默认的 | | len | 长度 | | max | 最大 | | min | 最小 | | oneof | One Of | | required | 必需的 | | required_if | 验证字段在指定字段等于某个值的情况才是必须的 | | required_unless | 验证字段在指定字段不等于某个值的情况才是必须的 | | required_with | 验证字段只有在任一其它指定字段存在的情况才是必须的 | | required_with_all | 验证字段只有在所有指定字段存在的情况下才是必须的 | | required_without | 验证字段只有当任一指定字段不存在的情况下才是必须的 | | required_without_all | 验证字段只有当所有指定字段不存在的情况下才是必须的 | | excluded_if | Excluded If | | excluded_unless | Excluded Unless | | excluded_with | Excluded With | | excluded_with_all | Excluded With All | | excluded_without | Excluded Without | | excluded_without_all | Excluded Without All | | unique | 唯一 | #### Aliases: | Tag | 描述 | | - | - | | iscolor | hexcolor\|rgb\|rgba\|hsl\|hsla | | country_code | iso3166_1_alpha2\|iso3166_1_alpha3\|iso3166_1_alpha_numeric | 基准 ------ ###### Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64 ```go goos: darwin goarch: amd64 pkg: github.com/go-playground/validator BenchmarkFieldSuccess-8 20000000 83.6 ns/op 0 B/op 0 allocs/op BenchmarkFieldSuccessParallel-8 50000000 26.8 ns/op 0 B/op 0 allocs/op BenchmarkFieldFailure-8 5000000 291 ns/op 208 B/op 4 allocs/op BenchmarkFieldFailureParallel-8 20000000 107 ns/op 208 B/op 4 allocs/op BenchmarkFieldArrayDiveSuccess-8 2000000 623 ns/op 201 B/op 11 allocs/op BenchmarkFieldArrayDiveSuccessParallel-8 10000000 237 ns/op 201 B/op 11 allocs/op BenchmarkFieldArrayDiveFailure-8 2000000 859 ns/op 412 B/op 16 allocs/op BenchmarkFieldArrayDiveFailureParallel-8 5000000 335 ns/op 413 B/op 16 allocs/op BenchmarkFieldMapDiveSuccess-8 1000000 1292 ns/op 432 B/op 18 allocs/op BenchmarkFieldMapDiveSuccessParallel-8 3000000 467 ns/op 432 B/op 18 allocs/op BenchmarkFieldMapDiveFailure-8 1000000 1082 ns/op 512 B/op 16 allocs/op BenchmarkFieldMapDiveFailureParallel-8 5000000 425 ns/op 512 B/op 16 allocs/op BenchmarkFieldMapDiveWithKeysSuccess-8 1000000 1539 ns/op 480 B/op 21 allocs/op BenchmarkFieldMapDiveWithKeysSuccessParallel-8 3000000 613 ns/op 480 B/op 21 allocs/op BenchmarkFieldMapDiveWithKeysFailure-8 1000000 1413 ns/op 721 B/op 21 allocs/op BenchmarkFieldMapDiveWithKeysFailureParallel-8 3000000 575 ns/op 721 B/op 21 allocs/op BenchmarkFieldCustomTypeSuccess-8 10000000 216 ns/op 32 B/op 2 allocs/op BenchmarkFieldCustomTypeSuccessParallel-8 20000000 82.2 ns/op 32 B/op 2 allocs/op BenchmarkFieldCustomTypeFailure-8 5000000 274 ns/op 208 B/op 4 allocs/op BenchmarkFieldCustomTypeFailureParallel-8 20000000 116 ns/op 208 B/op 4 allocs/op BenchmarkFieldOrTagSuccess-8 2000000 740 ns/op 16 B/op 1 allocs/op BenchmarkFieldOrTagSuccessParallel-8 3000000 474 ns/op 16 B/op 1 allocs/op BenchmarkFieldOrTagFailure-8 3000000 471 ns/op 224 B/op 5 allocs/op BenchmarkFieldOrTagFailureParallel-8 3000000 414 ns/op 224 B/op 5 allocs/op BenchmarkStructLevelValidationSuccess-8 10000000 213 ns/op 32 B/op 2 allocs/op BenchmarkStructLevelValidationSuccessParallel-8 20000000 91.8 ns/op 32 B/op 2 allocs/op BenchmarkStructLevelValidationFailure-8 3000000 473 ns/op 304 B/op 8 allocs/op BenchmarkStructLevelValidationFailureParallel-8 10000000 234 ns/op 304 B/op 8 allocs/op BenchmarkStructSimpleCustomTypeSuccess-8 5000000 385 ns/op 32 B/op 2 allocs/op BenchmarkStructSimpleCustomTypeSuccessParallel-8 10000000 161 ns/op 32 B/op 2 allocs/op BenchmarkStructSimpleCustomTypeFailure-8 2000000 640 ns/op 424 B/op 9 allocs/op BenchmarkStructSimpleCustomTypeFailureParallel-8 5000000 318 ns/op 440 B/op 10 allocs/op BenchmarkStructFilteredSuccess-8 2000000 597 ns/op 288 B/op 9 allocs/op BenchmarkStructFilteredSuccessParallel-8 10000000 266 ns/op 288 B/op 9 allocs/op BenchmarkStructFilteredFailure-8 3000000 454 ns/op 256 B/op 7 allocs/op BenchmarkStructFilteredFailureParallel-8 10000000 214 ns/op 256 B/op 7 allocs/op BenchmarkStructPartialSuccess-8 3000000 502 ns/op 256 B/op 6 allocs/op BenchmarkStructPartialSuccessParallel-8 10000000 225 ns/op 256 B/op 6 allocs/op BenchmarkStructPartialFailure-8 2000000 702 ns/op 480 B/op 11 allocs/op BenchmarkStructPartialFailureParallel-8 5000000 329 ns/op 480 B/op 11 allocs/op BenchmarkStructExceptSuccess-8 2000000 793 ns/op 496 B/op 12 allocs/op BenchmarkStructExceptSuccessParallel-8 10000000 193 ns/op 240 B/op 5 allocs/op BenchmarkStructExceptFailure-8 2000000 639 ns/op 464 B/op 10 allocs/op BenchmarkStructExceptFailureParallel-8 5000000 300 ns/op 464 B/op 10 allocs/op BenchmarkStructSimpleCrossFieldSuccess-8 3000000 417 ns/op 72 B/op 3 allocs/op BenchmarkStructSimpleCrossFieldSuccessParallel-8 10000000 163 ns/op 72 B/op 3 allocs/op BenchmarkStructSimpleCrossFieldFailure-8 2000000 645 ns/op 304 B/op 8 allocs/op BenchmarkStructSimpleCrossFieldFailureParallel-8 5000000 285 ns/op 304 B/op 8 allocs/op BenchmarkStructSimpleCrossStructCrossFieldSuccess-8 3000000 588 ns/op 80 B/op 4 allocs/op BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8 10000000 221 ns/op 80 B/op 4 allocs/op BenchmarkStructSimpleCrossStructCrossFieldFailure-8 2000000 868 ns/op 320 B/op 9 allocs/op BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8 5000000 337 ns/op 320 B/op 9 allocs/op BenchmarkStructSimpleSuccess-8 5000000 260 ns/op 0 B/op 0 allocs/op BenchmarkStructSimpleSuccessParallel-8 20000000 90.6 ns/op 0 B/op 0 allocs/op BenchmarkStructSimpleFailure-8 2000000 619 ns/op 424 B/op 9 allocs/op BenchmarkStructSimpleFailureParallel-8 5000000 296 ns/op 424 B/op 9 allocs/op BenchmarkStructComplexSuccess-8 1000000 1454 ns/op 128 B/op 8 allocs/op BenchmarkStructComplexSuccessParallel-8 3000000 579 ns/op 128 B/op 8 allocs/op BenchmarkStructComplexFailure-8 300000 4140 ns/op 3041 B/op 53 allocs/op BenchmarkStructComplexFailureParallel-8 1000000 2127 ns/op 3041 B/op 53 allocs/op BenchmarkOneof-8 10000000 140 ns/op 0 B/op 0 allocs/op BenchmarkOneofParallel-8 20000000 70.1 ns/op 0 B/op 0 allocs/op ``` 补充软件 ---------------------- 这是一个软件列表,可以在验证前或验证后使用该库进行补充。 * [form](https://github.com/go-playground/form) - 将 url.Values 解码为 Go 值并将 Go 值编码为 url.Values。双阵列和全地图支持。 * [mold](https://github.com/go-playground/mold) - 帮助修改或设置数据结构和其他对象中的数据的通用库 如何贡献 ------ Make a pull request... License ------- 在 MIT 许可证下分发,请参阅代码中的许可证文件以获取更多详细信息。 维护者 ----------- 这个项目已经发展到足够大,需要不止一个人来适当地支持社区。 如果你有兴趣成为维修人员,请联系我 https://github.com/deankarn