• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H17-Oct-2020-3718

_examples/H17-Oct-2020-552354

non-standard/validators/H17-Oct-2020-9273

testdata/H17-Oct-2020-21

translations/H17-Oct-2020-24,38520,796

.gitignoreH A D17-Oct-2020312 3125

.travis.ymlH A D17-Oct-2020655 3024

LICENSEH A D17-Oct-20201.1 KiB2317

MakefileH A D17-Oct-2020415 1813

README.mdH A D17-Oct-202017.5 KiB300264

baked_in.goH A D17-Oct-202062 KiB2,2861,523

benchmarks_test.goH A D17-Oct-202020.1 KiB1,100907

cache.goH A D17-Oct-20207.4 KiB323248

country_codes.goH A D17-Oct-20209.2 KiB163157

doc.goH A D17-Oct-202032.8 KiB1,3091

errors.goH A D17-Oct-20207.2 KiB276114

field_level.goH A D17-Oct-20204.7 KiB12046

go.modH A D17-Oct-2020298 129

go.sumH A D17-Oct-20202.6 KiB2928

regexes.goH A D17-Oct-20209.5 KiB10298

struct_level.goH A D17-Oct-20205.3 KiB17689

translations.goH A D17-Oct-2020425 124

util.goH A D17-Oct-20206.4 KiB289197

validator.goH A D17-Oct-202010.9 KiB478355

validator_instance.goH A D17-Oct-202020.8 KiB620356

validator_test.goH A D17-Oct-2020285.7 KiB11,0368,685

README.md

1Package validator
2================
3<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)
4![Project status](https://img.shields.io/badge/version-10.4.1-green.svg)
5[![Build Status](https://travis-ci.org/go-playground/validator.svg?branch=master)](https://travis-ci.org/go-playground/validator)
6[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-playground/validator?branch=master)
7[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)
8[![GoDoc](https://godoc.org/github.com/go-playground/validator?status.svg)](https://pkg.go.dev/github.com/go-playground/validator/v10)
9![License](https://img.shields.io/dub/l/vibe-d.svg)
10
11Package validator implements value validations for structs and individual fields based on tags.
12
13It has the following **unique** features:
14
15-   Cross Field and Cross Struct validations by using validation tags or custom validators.
16-   Slice, Array and Map diving, which allows any or all levels of a multidimensional field to be validated.
17-   Ability to dive into both map keys and values for validation
18-   Handles type interface by determining it's underlying type prior to validation.
19-   Handles custom field types such as sql driver Valuer see [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29)
20-   Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs
21-   Extraction of custom defined Field Name e.g. can specify to extract the JSON name while validating and have it available in the resulting FieldError
22-   Customizable i18n aware error messages.
23-   Default validator for the [gin](https://github.com/gin-gonic/gin) web framework; upgrading from v8 to v9 in gin see [here](https://github.com/go-playground/validator/tree/master/_examples/gin-upgrading-overriding)
24
25Installation
26------------
27
28Use go get.
29
30	go get github.com/go-playground/validator/v10
31
32Then import the validator package into your own code.
33
34	import "github.com/go-playground/validator/v10"
35
36Error Return Value
37-------
38
39Validation functions return type error
40
41They return type error to avoid the issue discussed in the following, where err is always != nil:
42
43* http://stackoverflow.com/a/29138676/3158232
44* https://github.com/go-playground/validator/issues/134
45
46Validator only InvalidValidationError for bad validation input, nil or ValidationErrors as type error; so, in your code all you need to do is check if the error returned is not nil, and if it's not check if error is InvalidValidationError ( if necessary, most of the time it isn't ) type cast it to type ValidationErrors like so:
47
48```go
49err := validate.Struct(mystruct)
50validationErrors := err.(validator.ValidationErrors)
51 ```
52
53Usage and documentation
54------
55
56Please see https://godoc.org/github.com/go-playground/validator for detailed usage docs.
57
58##### Examples:
59
60- [Simple](https://github.com/go-playground/validator/blob/master/_examples/simple/main.go)
61- [Custom Field Types](https://github.com/go-playground/validator/blob/master/_examples/custom/main.go)
62- [Struct Level](https://github.com/go-playground/validator/blob/master/_examples/struct-level/main.go)
63- [Translations & Custom Errors](https://github.com/go-playground/validator/blob/master/_examples/translations/main.go)
64- [Gin upgrade and/or override validator](https://github.com/go-playground/validator/tree/v9/_examples/gin-upgrading-overriding)
65- [wash - an example application putting it all together](https://github.com/bluesuncorp/wash)
66
67Baked-in Validations
68------
69
70### Fields:
71
72| Tag | Description |
73| - | - |
74| eqcsfield | Field Equals Another Field (relative)|
75| eqfield | Field Equals Another Field |
76| fieldcontains | NOT DOCUMENTED IN doc.go |
77| fieldexcludes | NOT DOCUMENTED IN doc.go |
78| gtcsfield | Field Greater Than Another Relative Field |
79| gtecsfield | Field Greater Than or Equal To Another Relative Field |
80| gtefield | Field Greater Than or Equal To Another Field |
81| gtfield | Field Greater Than Another Field |
82| ltcsfield | Less Than Another Relative Field |
83| ltecsfield | Less Than or Equal To Another Relative Field |
84| ltefield | Less Than or Equal To Another Field |
85| ltfield | Less Than Another Field |
86| necsfield | Field Does Not Equal Another Field (relative) |
87| nefield | Field Does Not Equal Another Field |
88
89### Network:
90
91| Tag | Description |
92| - | - |
93| cidr | Classless Inter-Domain Routing CIDR |
94| cidrv4 | Classless Inter-Domain Routing CIDRv4 |
95| cidrv6 | Classless Inter-Domain Routing CIDRv6 |
96| datauri | Data URL |
97| fqdn | Full Qualified Domain Name (FQDN) |
98| hostname | Hostname RFC 952 |
99| hostname_port | HostPort |
100| hostname_rfc1123 | Hostname RFC 1123 |
101| ip | Internet Protocol Address IP |
102| ip4_addr | Internet Protocol Address IPv4 |
103| ip6_addr |Internet Protocol Address IPv6 |
104| ip_addr | Internet Protocol Address IP |
105| ipv4 | Internet Protocol Address IPv4 |
106| ipv6 | Internet Protocol Address IPv6 |
107| mac | Media Access Control Address MAC |
108| tcp4_addr | Transmission Control Protocol Address TCPv4 |
109| tcp6_addr | Transmission Control Protocol Address TCPv6 |
110| tcp_addr | Transmission Control Protocol Address TCP |
111| udp4_addr | User Datagram Protocol Address UDPv4 |
112| udp6_addr | User Datagram Protocol Address UDPv6 |
113| udp_addr | User Datagram Protocol Address UDP |
114| unix_addr | Unix domain socket end point Address |
115| uri | URI String |
116| url | URL String |
117| url_encoded | URL Encoded |
118| urn_rfc2141 | Urn RFC 2141 String |
119
120### Strings:
121
122| Tag | Description |
123| - | - |
124| alpha | Alpha Only |
125| alphanum | Alphanumeric |
126| alphanumunicode | Alphanumeric Unicode |
127| alphaunicode | Alpha Unicode |
128| ascii | ASCII |
129| contains | Contains |
130| containsany | Contains Any |
131| containsrune | Contains Rune |
132| endswith | Ends With |
133| lowercase | Lowercase |
134| multibyte | Multi-Byte Characters |
135| number | NOT DOCUMENTED IN doc.go |
136| numeric | Numeric |
137| printascii | Printable ASCII |
138| startswith | Starts With |
139| uppercase | Uppercase |
140
141### Format:
142| Tag | Description |
143| - | - |
144| base64 | Base64 String |
145| base64url | Base64URL String |
146| btc_addr | Bitcoin Address |
147| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
148| datetime | Datetime |
149| e164 | e164 formatted phone number |
150| email | E-mail String
151| eth_addr | Ethereum Address |
152| hexadecimal | Hexadecimal String |
153| hexcolor | Hexcolor String |
154| hsl | HSL String |
155| hsla | HSLA String |
156| html | HTML Tags |
157| html_encoded | HTML Encoded |
158| isbn | International Standard Book Number |
159| isbn10 | International Standard Book Number 10 |
160| isbn13 | International Standard Book Number 13 |
161| json | JSON |
162| latitude | Latitude |
163| longitude | Longitude |
164| rgb | RGB String |
165| rgba | RGBA String |
166| ssn | Social Security Number SSN |
167| uuid | Universally Unique Identifier UUID |
168| uuid3 | Universally Unique Identifier UUID v3 |
169| uuid3_rfc4122 | Universally Unique Identifier UUID v3 RFC4122 |
170| uuid4 | Universally Unique Identifier UUID v4 |
171| uuid4_rfc4122 | Universally Unique Identifier UUID v4 RFC4122 |
172| uuid5 | Universally Unique Identifier UUID v5 |
173| uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
174| uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
175
176### Comparisons:
177| Tag | Description |
178| - | - |
179| eq | Equals |
180| gt | Greater than|
181| gte |Greater than or equal |
182| lt | Less Than |
183| lte | Less Than or Equal |
184| ne | Not Equal |
185
186### Other:
187| Tag | Description |
188| - | - |
189| dir | Directory |
190| endswith | Ends With |
191| excludes | Excludes |
192| excludesall | Excludes All |
193| excludesrune | Excludes Rune |
194| file | File path |
195| isdefault | Is Default |
196| len | Length |
197| max | Maximum |
198| min | Minimum |
199| oneof | One Of |
200| required | Required |
201| required_if | Required If |
202| required_unless | Required Unless |
203| required_with | Required With |
204| required_with_all | Required With All |
205| required_without | Required Without |
206| required_without_all | Required Without All |
207| excluded_with | Excluded With |
208| excluded_with_all | Excluded With All |
209| excluded_without | Excluded Without |
210| excluded_without_all | Excluded Without All |
211| unique | Unique |
212
213Benchmarks
214------
215###### Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64
216```go
217goos: darwin
218goarch: amd64
219pkg: github.com/go-playground/validator
220BenchmarkFieldSuccess-8                                         20000000                83.6 ns/op             0 B/op          0 allocs/op
221BenchmarkFieldSuccessParallel-8                                 50000000                26.8 ns/op             0 B/op          0 allocs/op
222BenchmarkFieldFailure-8                                          5000000               291 ns/op             208 B/op          4 allocs/op
223BenchmarkFieldFailureParallel-8                                 20000000               107 ns/op             208 B/op          4 allocs/op
224BenchmarkFieldArrayDiveSuccess-8                                 2000000               623 ns/op             201 B/op         11 allocs/op
225BenchmarkFieldArrayDiveSuccessParallel-8                        10000000               237 ns/op             201 B/op         11 allocs/op
226BenchmarkFieldArrayDiveFailure-8                                 2000000               859 ns/op             412 B/op         16 allocs/op
227BenchmarkFieldArrayDiveFailureParallel-8                         5000000               335 ns/op             413 B/op         16 allocs/op
228BenchmarkFieldMapDiveSuccess-8                                   1000000              1292 ns/op             432 B/op         18 allocs/op
229BenchmarkFieldMapDiveSuccessParallel-8                           3000000               467 ns/op             432 B/op         18 allocs/op
230BenchmarkFieldMapDiveFailure-8                                   1000000              1082 ns/op             512 B/op         16 allocs/op
231BenchmarkFieldMapDiveFailureParallel-8                           5000000               425 ns/op             512 B/op         16 allocs/op
232BenchmarkFieldMapDiveWithKeysSuccess-8                           1000000              1539 ns/op             480 B/op         21 allocs/op
233BenchmarkFieldMapDiveWithKeysSuccessParallel-8                   3000000               613 ns/op             480 B/op         21 allocs/op
234BenchmarkFieldMapDiveWithKeysFailure-8                           1000000              1413 ns/op             721 B/op         21 allocs/op
235BenchmarkFieldMapDiveWithKeysFailureParallel-8                   3000000               575 ns/op             721 B/op         21 allocs/op
236BenchmarkFieldCustomTypeSuccess-8                               10000000               216 ns/op              32 B/op          2 allocs/op
237BenchmarkFieldCustomTypeSuccessParallel-8                       20000000                82.2 ns/op            32 B/op          2 allocs/op
238BenchmarkFieldCustomTypeFailure-8                                5000000               274 ns/op             208 B/op          4 allocs/op
239BenchmarkFieldCustomTypeFailureParallel-8                       20000000               116 ns/op             208 B/op          4 allocs/op
240BenchmarkFieldOrTagSuccess-8                                     2000000               740 ns/op              16 B/op          1 allocs/op
241BenchmarkFieldOrTagSuccessParallel-8                             3000000               474 ns/op              16 B/op          1 allocs/op
242BenchmarkFieldOrTagFailure-8                                     3000000               471 ns/op             224 B/op          5 allocs/op
243BenchmarkFieldOrTagFailureParallel-8                             3000000               414 ns/op             224 B/op          5 allocs/op
244BenchmarkStructLevelValidationSuccess-8                         10000000               213 ns/op              32 B/op          2 allocs/op
245BenchmarkStructLevelValidationSuccessParallel-8                 20000000                91.8 ns/op            32 B/op          2 allocs/op
246BenchmarkStructLevelValidationFailure-8                          3000000               473 ns/op             304 B/op          8 allocs/op
247BenchmarkStructLevelValidationFailureParallel-8                 10000000               234 ns/op             304 B/op          8 allocs/op
248BenchmarkStructSimpleCustomTypeSuccess-8                         5000000               385 ns/op              32 B/op          2 allocs/op
249BenchmarkStructSimpleCustomTypeSuccessParallel-8                10000000               161 ns/op              32 B/op          2 allocs/op
250BenchmarkStructSimpleCustomTypeFailure-8                         2000000               640 ns/op             424 B/op          9 allocs/op
251BenchmarkStructSimpleCustomTypeFailureParallel-8                 5000000               318 ns/op             440 B/op         10 allocs/op
252BenchmarkStructFilteredSuccess-8                                 2000000               597 ns/op             288 B/op          9 allocs/op
253BenchmarkStructFilteredSuccessParallel-8                        10000000               266 ns/op             288 B/op          9 allocs/op
254BenchmarkStructFilteredFailure-8                                 3000000               454 ns/op             256 B/op          7 allocs/op
255BenchmarkStructFilteredFailureParallel-8                        10000000               214 ns/op             256 B/op          7 allocs/op
256BenchmarkStructPartialSuccess-8                                  3000000               502 ns/op             256 B/op          6 allocs/op
257BenchmarkStructPartialSuccessParallel-8                         10000000               225 ns/op             256 B/op          6 allocs/op
258BenchmarkStructPartialFailure-8                                  2000000               702 ns/op             480 B/op         11 allocs/op
259BenchmarkStructPartialFailureParallel-8                          5000000               329 ns/op             480 B/op         11 allocs/op
260BenchmarkStructExceptSuccess-8                                   2000000               793 ns/op             496 B/op         12 allocs/op
261BenchmarkStructExceptSuccessParallel-8                          10000000               193 ns/op             240 B/op          5 allocs/op
262BenchmarkStructExceptFailure-8                                   2000000               639 ns/op             464 B/op         10 allocs/op
263BenchmarkStructExceptFailureParallel-8                           5000000               300 ns/op             464 B/op         10 allocs/op
264BenchmarkStructSimpleCrossFieldSuccess-8                         3000000               417 ns/op              72 B/op          3 allocs/op
265BenchmarkStructSimpleCrossFieldSuccessParallel-8                10000000               163 ns/op              72 B/op          3 allocs/op
266BenchmarkStructSimpleCrossFieldFailure-8                         2000000               645 ns/op             304 B/op          8 allocs/op
267BenchmarkStructSimpleCrossFieldFailureParallel-8                 5000000               285 ns/op             304 B/op          8 allocs/op
268BenchmarkStructSimpleCrossStructCrossFieldSuccess-8              3000000               588 ns/op              80 B/op          4 allocs/op
269BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8     10000000               221 ns/op              80 B/op          4 allocs/op
270BenchmarkStructSimpleCrossStructCrossFieldFailure-8              2000000               868 ns/op             320 B/op          9 allocs/op
271BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8      5000000               337 ns/op             320 B/op          9 allocs/op
272BenchmarkStructSimpleSuccess-8                                   5000000               260 ns/op               0 B/op          0 allocs/op
273BenchmarkStructSimpleSuccessParallel-8                          20000000                90.6 ns/op             0 B/op          0 allocs/op
274BenchmarkStructSimpleFailure-8                                   2000000               619 ns/op             424 B/op          9 allocs/op
275BenchmarkStructSimpleFailureParallel-8                           5000000               296 ns/op             424 B/op          9 allocs/op
276BenchmarkStructComplexSuccess-8                                  1000000              1454 ns/op             128 B/op          8 allocs/op
277BenchmarkStructComplexSuccessParallel-8                          3000000               579 ns/op             128 B/op          8 allocs/op
278BenchmarkStructComplexFailure-8                                   300000              4140 ns/op            3041 B/op         53 allocs/op
279BenchmarkStructComplexFailureParallel-8                          1000000              2127 ns/op            3041 B/op         53 allocs/op
280BenchmarkOneof-8                                                10000000               140 ns/op               0 B/op          0 allocs/op
281BenchmarkOneofParallel-8                                        20000000                70.1 ns/op             0 B/op          0 allocs/op
282```
283
284Complementary Software
285----------------------
286
287Here is a list of software that complements using this library either pre or post validation.
288
289* [form](https://github.com/go-playground/form) - Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.
290* [mold](https://github.com/go-playground/mold) - A general library to help modify or set data within data structures and other objects
291
292How to Contribute
293------
294
295Make a pull request...
296
297License
298------
299Distributed under MIT License, please see license file within the code for more details.
300