...
1Package validator
2=================
3<img align="right" src="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.18.0-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 returns 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://pkg.go.dev/github.com/go-playground/validator/v10 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### Special Notes:
71- If new to using validator it is highly recommended to initialize it using the `WithRequiredStructEnabled` option which is opt-in to new behaviour that will become the default behaviour in v11+. See documentation for more details.
72```go
73validate := validator.New(validator.WithRequiredStructEnabled())
74```
75
76### Fields:
77
78| Tag | Description |
79| - | - |
80| eqcsfield | Field Equals Another Field (relative)|
81| eqfield | Field Equals Another Field |
82| fieldcontains | Check the indicated characters are present in the Field |
83| fieldexcludes | Check the indicated characters are not present in the field |
84| gtcsfield | Field Greater Than Another Relative Field |
85| gtecsfield | Field Greater Than or Equal To Another Relative Field |
86| gtefield | Field Greater Than or Equal To Another Field |
87| gtfield | Field Greater Than Another Field |
88| ltcsfield | Less Than Another Relative Field |
89| ltecsfield | Less Than or Equal To Another Relative Field |
90| ltefield | Less Than or Equal To Another Field |
91| ltfield | Less Than Another Field |
92| necsfield | Field Does Not Equal Another Field (relative) |
93| nefield | Field Does Not Equal Another Field |
94
95### Network:
96
97| Tag | Description |
98| - | - |
99| cidr | Classless Inter-Domain Routing CIDR |
100| cidrv4 | Classless Inter-Domain Routing CIDRv4 |
101| cidrv6 | Classless Inter-Domain Routing CIDRv6 |
102| datauri | Data URL |
103| fqdn | Full Qualified Domain Name (FQDN) |
104| hostname | Hostname RFC 952 |
105| hostname_port | HostPort |
106| hostname_rfc1123 | Hostname RFC 1123 |
107| ip | Internet Protocol Address IP |
108| ip4_addr | Internet Protocol Address IPv4 |
109| ip6_addr | Internet Protocol Address IPv6 |
110| ip_addr | Internet Protocol Address IP |
111| ipv4 | Internet Protocol Address IPv4 |
112| ipv6 | Internet Protocol Address IPv6 |
113| mac | Media Access Control Address MAC |
114| tcp4_addr | Transmission Control Protocol Address TCPv4 |
115| tcp6_addr | Transmission Control Protocol Address TCPv6 |
116| tcp_addr | Transmission Control Protocol Address TCP |
117| udp4_addr | User Datagram Protocol Address UDPv4 |
118| udp6_addr | User Datagram Protocol Address UDPv6 |
119| udp_addr | User Datagram Protocol Address UDP |
120| unix_addr | Unix domain socket end point Address |
121| uri | URI String |
122| url | URL String |
123| http_url | HTTP URL String |
124| url_encoded | URL Encoded |
125| urn_rfc2141 | Urn RFC 2141 String |
126
127### Strings:
128
129| Tag | Description |
130| - | - |
131| alpha | Alpha Only |
132| alphanum | Alphanumeric |
133| alphanumunicode | Alphanumeric Unicode |
134| alphaunicode | Alpha Unicode |
135| ascii | ASCII |
136| boolean | Boolean |
137| contains | Contains |
138| containsany | Contains Any |
139| containsrune | Contains Rune |
140| endsnotwith | Ends Not With |
141| endswith | Ends With |
142| excludes | Excludes |
143| excludesall | Excludes All |
144| excludesrune | Excludes Rune |
145| lowercase | Lowercase |
146| multibyte | Multi-Byte Characters |
147| number | Number |
148| numeric | Numeric |
149| printascii | Printable ASCII |
150| startsnotwith | Starts Not With |
151| startswith | Starts With |
152| uppercase | Uppercase |
153
154### Format:
155| Tag | Description |
156| - | - |
157| base64 | Base64 String |
158| base64url | Base64URL String |
159| base64rawurl | Base64RawURL String |
160| bic | Business Identifier Code (ISO 9362) |
161| bcp47_language_tag | Language tag (BCP 47) |
162| btc_addr | Bitcoin Address |
163| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
164| credit_card | Credit Card Number |
165| mongodb | MongoDB ObjectID |
166| cron | Cron |
167| spicedb | SpiceDb ObjectID/Permission/Type |
168| datetime | Datetime |
169| e164 | e164 formatted phone number |
170| email | E-mail String
171| eth_addr | Ethereum Address |
172| hexadecimal | Hexadecimal String |
173| hexcolor | Hexcolor String |
174| hsl | HSL String |
175| hsla | HSLA String |
176| html | HTML Tags |
177| html_encoded | HTML Encoded |
178| isbn | International Standard Book Number |
179| isbn10 | International Standard Book Number 10 |
180| isbn13 | International Standard Book Number 13 |
181| issn | International Standard Serial Number |
182| iso3166_1_alpha2 | Two-letter country code (ISO 3166-1 alpha-2) |
183| iso3166_1_alpha3 | Three-letter country code (ISO 3166-1 alpha-3) |
184| iso3166_1_alpha_numeric | Numeric country code (ISO 3166-1 numeric) |
185| iso3166_2 | Country subdivision code (ISO 3166-2) |
186| iso4217 | Currency code (ISO 4217) |
187| json | JSON |
188| jwt | JSON Web Token (JWT) |
189| latitude | Latitude |
190| longitude | Longitude |
191| luhn_checksum | Luhn Algorithm Checksum (for strings and (u)int) |
192| postcode_iso3166_alpha2 | Postcode |
193| postcode_iso3166_alpha2_field | Postcode |
194| rgb | RGB String |
195| rgba | RGBA String |
196| ssn | Social Security Number SSN |
197| timezone | Timezone |
198| uuid | Universally Unique Identifier UUID |
199| uuid3 | Universally Unique Identifier UUID v3 |
200| uuid3_rfc4122 | Universally Unique Identifier UUID v3 RFC4122 |
201| uuid4 | Universally Unique Identifier UUID v4 |
202| uuid4_rfc4122 | Universally Unique Identifier UUID v4 RFC4122 |
203| uuid5 | Universally Unique Identifier UUID v5 |
204| uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
205| uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
206| md4 | MD4 hash |
207| md5 | MD5 hash |
208| sha256 | SHA256 hash |
209| sha384 | SHA384 hash |
210| sha512 | SHA512 hash |
211| ripemd128 | RIPEMD-128 hash |
212| ripemd128 | RIPEMD-160 hash |
213| tiger128 | TIGER128 hash |
214| tiger160 | TIGER160 hash |
215| tiger192 | TIGER192 hash |
216| semver | Semantic Versioning 2.0.0 |
217| ulid | Universally Unique Lexicographically Sortable Identifier ULID |
218| cve | Common Vulnerabilities and Exposures Identifier (CVE id) |
219
220### Comparisons:
221| Tag | Description |
222| - | - |
223| eq | Equals |
224| eq_ignore_case | Equals ignoring case |
225| gt | Greater than|
226| gte | Greater than or equal |
227| lt | Less Than |
228| lte | Less Than or Equal |
229| ne | Not Equal |
230| ne_ignore_case | Not Equal ignoring case |
231
232### Other:
233| Tag | Description |
234| - | - |
235| dir | Existing Directory |
236| dirpath | Directory Path |
237| file | Existing File |
238| filepath | File Path |
239| image | Image |
240| isdefault | Is Default |
241| len | Length |
242| max | Maximum |
243| min | Minimum |
244| oneof | One Of |
245| required | Required |
246| required_if | Required If |
247| required_unless | Required Unless |
248| required_with | Required With |
249| required_with_all | Required With All |
250| required_without | Required Without |
251| required_without_all | Required Without All |
252| excluded_if | Excluded If |
253| excluded_unless | Excluded Unless |
254| excluded_with | Excluded With |
255| excluded_with_all | Excluded With All |
256| excluded_without | Excluded Without |
257| excluded_without_all | Excluded Without All |
258| unique | Unique |
259
260#### Aliases:
261| Tag | Description |
262| - | - |
263| iscolor | hexcolor\|rgb\|rgba\|hsl\|hsla |
264| country_code | iso3166_1_alpha2\|iso3166_1_alpha3\|iso3166_1_alpha_numeric |
265
266Benchmarks
267------
268###### Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64
269```go
270go version go1.21.0 darwin/arm64
271goos: darwin
272goarch: arm64
273pkg: github.com/go-playground/validator/v10
274BenchmarkFieldSuccess-8 33142266 35.94 ns/op 0 B/op 0 allocs/op
275BenchmarkFieldSuccessParallel-8 200816191 6.568 ns/op 0 B/op 0 allocs/op
276BenchmarkFieldFailure-8 6779707 175.1 ns/op 200 B/op 4 allocs/op
277BenchmarkFieldFailureParallel-8 11044147 108.4 ns/op 200 B/op 4 allocs/op
278BenchmarkFieldArrayDiveSuccess-8 6054232 194.4 ns/op 97 B/op 5 allocs/op
279BenchmarkFieldArrayDiveSuccessParallel-8 12523388 94.07 ns/op 97 B/op 5 allocs/op
280BenchmarkFieldArrayDiveFailure-8 3587043 334.3 ns/op 300 B/op 10 allocs/op
281BenchmarkFieldArrayDiveFailureParallel-8 5816665 200.8 ns/op 300 B/op 10 allocs/op
282BenchmarkFieldMapDiveSuccess-8 2217910 540.1 ns/op 288 B/op 14 allocs/op
283BenchmarkFieldMapDiveSuccessParallel-8 4446698 258.7 ns/op 288 B/op 14 allocs/op
284BenchmarkFieldMapDiveFailure-8 2392759 504.6 ns/op 376 B/op 13 allocs/op
285BenchmarkFieldMapDiveFailureParallel-8 4244199 286.9 ns/op 376 B/op 13 allocs/op
286BenchmarkFieldMapDiveWithKeysSuccess-8 2005857 592.1 ns/op 288 B/op 14 allocs/op
287BenchmarkFieldMapDiveWithKeysSuccessParallel-8 4400850 296.9 ns/op 288 B/op 14 allocs/op
288BenchmarkFieldMapDiveWithKeysFailure-8 1850227 643.8 ns/op 553 B/op 16 allocs/op
289BenchmarkFieldMapDiveWithKeysFailureParallel-8 3293233 375.1 ns/op 553 B/op 16 allocs/op
290BenchmarkFieldCustomTypeSuccess-8 12174412 98.25 ns/op 32 B/op 2 allocs/op
291BenchmarkFieldCustomTypeSuccessParallel-8 34389907 35.49 ns/op 32 B/op 2 allocs/op
292BenchmarkFieldCustomTypeFailure-8 7582524 156.6 ns/op 184 B/op 3 allocs/op
293BenchmarkFieldCustomTypeFailureParallel-8 13019902 92.79 ns/op 184 B/op 3 allocs/op
294BenchmarkFieldOrTagSuccess-8 3427260 349.4 ns/op 16 B/op 1 allocs/op
295BenchmarkFieldOrTagSuccessParallel-8 15144128 81.25 ns/op 16 B/op 1 allocs/op
296BenchmarkFieldOrTagFailure-8 5913546 201.9 ns/op 216 B/op 5 allocs/op
297BenchmarkFieldOrTagFailureParallel-8 9810212 113.7 ns/op 216 B/op 5 allocs/op
298BenchmarkStructLevelValidationSuccess-8 13456327 87.66 ns/op 16 B/op 1 allocs/op
299BenchmarkStructLevelValidationSuccessParallel-8 41818888 27.77 ns/op 16 B/op 1 allocs/op
300BenchmarkStructLevelValidationFailure-8 4166284 272.6 ns/op 264 B/op 7 allocs/op
301BenchmarkStructLevelValidationFailureParallel-8 7594581 152.1 ns/op 264 B/op 7 allocs/op
302BenchmarkStructSimpleCustomTypeSuccess-8 6508082 182.6 ns/op 32 B/op 2 allocs/op
303BenchmarkStructSimpleCustomTypeSuccessParallel-8 23078605 54.78 ns/op 32 B/op 2 allocs/op
304BenchmarkStructSimpleCustomTypeFailure-8 3118352 381.0 ns/op 416 B/op 9 allocs/op
305BenchmarkStructSimpleCustomTypeFailureParallel-8 5300738 224.1 ns/op 432 B/op 10 allocs/op
306BenchmarkStructFilteredSuccess-8 4761807 251.1 ns/op 216 B/op 5 allocs/op
307BenchmarkStructFilteredSuccessParallel-8 8792598 128.6 ns/op 216 B/op 5 allocs/op
308BenchmarkStructFilteredFailure-8 5202573 232.1 ns/op 216 B/op 5 allocs/op
309BenchmarkStructFilteredFailureParallel-8 9591267 121.4 ns/op 216 B/op 5 allocs/op
310BenchmarkStructPartialSuccess-8 5188512 231.6 ns/op 224 B/op 4 allocs/op
311BenchmarkStructPartialSuccessParallel-8 9179776 123.1 ns/op 224 B/op 4 allocs/op
312BenchmarkStructPartialFailure-8 3071212 392.5 ns/op 440 B/op 9 allocs/op
313BenchmarkStructPartialFailureParallel-8 5344261 223.7 ns/op 440 B/op 9 allocs/op
314BenchmarkStructExceptSuccess-8 3184230 375.0 ns/op 424 B/op 8 allocs/op
315BenchmarkStructExceptSuccessParallel-8 10090130 108.9 ns/op 208 B/op 3 allocs/op
316BenchmarkStructExceptFailure-8 3347226 357.7 ns/op 424 B/op 8 allocs/op
317BenchmarkStructExceptFailureParallel-8 5654923 209.5 ns/op 424 B/op 8 allocs/op
318BenchmarkStructSimpleCrossFieldSuccess-8 5232265 229.1 ns/op 56 B/op 3 allocs/op
319BenchmarkStructSimpleCrossFieldSuccessParallel-8 17436674 64.75 ns/op 56 B/op 3 allocs/op
320BenchmarkStructSimpleCrossFieldFailure-8 3128613 383.6 ns/op 272 B/op 8 allocs/op
321BenchmarkStructSimpleCrossFieldFailureParallel-8 6994113 168.8 ns/op 272 B/op 8 allocs/op
322BenchmarkStructSimpleCrossStructCrossFieldSuccess-8 3506487 340.9 ns/op 64 B/op 4 allocs/op
323BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8 13431300 91.77 ns/op 64 B/op 4 allocs/op
324BenchmarkStructSimpleCrossStructCrossFieldFailure-8 2410566 500.9 ns/op 288 B/op 9 allocs/op
325BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8 6344510 188.2 ns/op 288 B/op 9 allocs/op
326BenchmarkStructSimpleSuccess-8 8922726 133.8 ns/op 0 B/op 0 allocs/op
327BenchmarkStructSimpleSuccessParallel-8 55291153 23.63 ns/op 0 B/op 0 allocs/op
328BenchmarkStructSimpleFailure-8 3171553 378.4 ns/op 416 B/op 9 allocs/op
329BenchmarkStructSimpleFailureParallel-8 5571692 212.0 ns/op 416 B/op 9 allocs/op
330BenchmarkStructComplexSuccess-8 1683750 714.5 ns/op 224 B/op 5 allocs/op
331BenchmarkStructComplexSuccessParallel-8 4578046 257.0 ns/op 224 B/op 5 allocs/op
332BenchmarkStructComplexFailure-8 481585 2547 ns/op 3041 B/op 48 allocs/op
333BenchmarkStructComplexFailureParallel-8 965764 1577 ns/op 3040 B/op 48 allocs/op
334BenchmarkOneof-8 17380881 68.50 ns/op 0 B/op 0 allocs/op
335BenchmarkOneofParallel-8 8084733 153.5 ns/op 0 B/op 0 allocs/op
336```
337
338Complementary Software
339----------------------
340
341Here is a list of software that complements using this library either pre or post validation.
342
343* [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.
344* [mold](https://github.com/go-playground/mold) - A general library to help modify or set data within data structures and other objects
345
346How to Contribute
347------
348
349Make a pull request...
350
351License
352-------
353Distributed under MIT License, please see license file within the code for more details.
354
355Maintainers
356-----------
357This project has grown large enough that more than one person is required to properly support the community.
358If you are interested in becoming a maintainer please reach out to me https://github.com/deankarn
View as plain text