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

..03-May-2022-

.github/H09-Feb-2020-

_examples/H09-Feb-2020-

non-standard/validators/H09-Feb-2020-

testdata/H09-Feb-2020-

translations/H09-Feb-2020-

.gitignoreH A D09-Feb-2020307

.travis.ymlH A D09-Feb-2020654

LICENSEH A D09-Feb-20201.1 KiB

MakefileH A D09-Feb-2020415

README.mdH A D09-Feb-202013 KiB

baked_in.goH A D09-Feb-202055 KiB

benchmarks_test.goH A D09-Feb-202020.1 KiB

cache.goH A D09-Feb-20207.4 KiB

doc.goH A D09-Feb-202028.8 KiB

errors.goH A D09-Feb-20207.1 KiB

field_level.goH A D09-Feb-20204.7 KiB

go.modH A D09-Feb-2020242

go.sumH A D09-Feb-20201.9 KiB

regexes.goH A D09-Feb-20209.1 KiB

struct_level.goH A D09-Feb-20205.3 KiB

translations.goH A D09-Feb-2020425

util.goH A D09-Feb-20205.8 KiB

validator.goH A D09-Feb-202010.9 KiB

validator_instance.goH A D09-Feb-202020.6 KiB

validator_test.goH A D09-Feb-2020228.8 KiB

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.1.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://godoc.org/github.com/go-playground/validator)
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
67Benchmarks
68------
69###### Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64
70```go
71goos: darwin
72goarch: amd64
73pkg: github.com/go-playground/validator
74BenchmarkFieldSuccess-8                                         20000000                83.6 ns/op             0 B/op          0 allocs/op
75BenchmarkFieldSuccessParallel-8                                 50000000                26.8 ns/op             0 B/op          0 allocs/op
76BenchmarkFieldFailure-8                                          5000000               291 ns/op             208 B/op          4 allocs/op
77BenchmarkFieldFailureParallel-8                                 20000000               107 ns/op             208 B/op          4 allocs/op
78BenchmarkFieldArrayDiveSuccess-8                                 2000000               623 ns/op             201 B/op         11 allocs/op
79BenchmarkFieldArrayDiveSuccessParallel-8                        10000000               237 ns/op             201 B/op         11 allocs/op
80BenchmarkFieldArrayDiveFailure-8                                 2000000               859 ns/op             412 B/op         16 allocs/op
81BenchmarkFieldArrayDiveFailureParallel-8                         5000000               335 ns/op             413 B/op         16 allocs/op
82BenchmarkFieldMapDiveSuccess-8                                   1000000              1292 ns/op             432 B/op         18 allocs/op
83BenchmarkFieldMapDiveSuccessParallel-8                           3000000               467 ns/op             432 B/op         18 allocs/op
84BenchmarkFieldMapDiveFailure-8                                   1000000              1082 ns/op             512 B/op         16 allocs/op
85BenchmarkFieldMapDiveFailureParallel-8                           5000000               425 ns/op             512 B/op         16 allocs/op
86BenchmarkFieldMapDiveWithKeysSuccess-8                           1000000              1539 ns/op             480 B/op         21 allocs/op
87BenchmarkFieldMapDiveWithKeysSuccessParallel-8                   3000000               613 ns/op             480 B/op         21 allocs/op
88BenchmarkFieldMapDiveWithKeysFailure-8                           1000000              1413 ns/op             721 B/op         21 allocs/op
89BenchmarkFieldMapDiveWithKeysFailureParallel-8                   3000000               575 ns/op             721 B/op         21 allocs/op
90BenchmarkFieldCustomTypeSuccess-8                               10000000               216 ns/op              32 B/op          2 allocs/op
91BenchmarkFieldCustomTypeSuccessParallel-8                       20000000                82.2 ns/op            32 B/op          2 allocs/op
92BenchmarkFieldCustomTypeFailure-8                                5000000               274 ns/op             208 B/op          4 allocs/op
93BenchmarkFieldCustomTypeFailureParallel-8                       20000000               116 ns/op             208 B/op          4 allocs/op
94BenchmarkFieldOrTagSuccess-8                                     2000000               740 ns/op              16 B/op          1 allocs/op
95BenchmarkFieldOrTagSuccessParallel-8                             3000000               474 ns/op              16 B/op          1 allocs/op
96BenchmarkFieldOrTagFailure-8                                     3000000               471 ns/op             224 B/op          5 allocs/op
97BenchmarkFieldOrTagFailureParallel-8                             3000000               414 ns/op             224 B/op          5 allocs/op
98BenchmarkStructLevelValidationSuccess-8                         10000000               213 ns/op              32 B/op          2 allocs/op
99BenchmarkStructLevelValidationSuccessParallel-8                 20000000                91.8 ns/op            32 B/op          2 allocs/op
100BenchmarkStructLevelValidationFailure-8                          3000000               473 ns/op             304 B/op          8 allocs/op
101BenchmarkStructLevelValidationFailureParallel-8                 10000000               234 ns/op             304 B/op          8 allocs/op
102BenchmarkStructSimpleCustomTypeSuccess-8                         5000000               385 ns/op              32 B/op          2 allocs/op
103BenchmarkStructSimpleCustomTypeSuccessParallel-8                10000000               161 ns/op              32 B/op          2 allocs/op
104BenchmarkStructSimpleCustomTypeFailure-8                         2000000               640 ns/op             424 B/op          9 allocs/op
105BenchmarkStructSimpleCustomTypeFailureParallel-8                 5000000               318 ns/op             440 B/op         10 allocs/op
106BenchmarkStructFilteredSuccess-8                                 2000000               597 ns/op             288 B/op          9 allocs/op
107BenchmarkStructFilteredSuccessParallel-8                        10000000               266 ns/op             288 B/op          9 allocs/op
108BenchmarkStructFilteredFailure-8                                 3000000               454 ns/op             256 B/op          7 allocs/op
109BenchmarkStructFilteredFailureParallel-8                        10000000               214 ns/op             256 B/op          7 allocs/op
110BenchmarkStructPartialSuccess-8                                  3000000               502 ns/op             256 B/op          6 allocs/op
111BenchmarkStructPartialSuccessParallel-8                         10000000               225 ns/op             256 B/op          6 allocs/op
112BenchmarkStructPartialFailure-8                                  2000000               702 ns/op             480 B/op         11 allocs/op
113BenchmarkStructPartialFailureParallel-8                          5000000               329 ns/op             480 B/op         11 allocs/op
114BenchmarkStructExceptSuccess-8                                   2000000               793 ns/op             496 B/op         12 allocs/op
115BenchmarkStructExceptSuccessParallel-8                          10000000               193 ns/op             240 B/op          5 allocs/op
116BenchmarkStructExceptFailure-8                                   2000000               639 ns/op             464 B/op         10 allocs/op
117BenchmarkStructExceptFailureParallel-8                           5000000               300 ns/op             464 B/op         10 allocs/op
118BenchmarkStructSimpleCrossFieldSuccess-8                         3000000               417 ns/op              72 B/op          3 allocs/op
119BenchmarkStructSimpleCrossFieldSuccessParallel-8                10000000               163 ns/op              72 B/op          3 allocs/op
120BenchmarkStructSimpleCrossFieldFailure-8                         2000000               645 ns/op             304 B/op          8 allocs/op
121BenchmarkStructSimpleCrossFieldFailureParallel-8                 5000000               285 ns/op             304 B/op          8 allocs/op
122BenchmarkStructSimpleCrossStructCrossFieldSuccess-8              3000000               588 ns/op              80 B/op          4 allocs/op
123BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8     10000000               221 ns/op              80 B/op          4 allocs/op
124BenchmarkStructSimpleCrossStructCrossFieldFailure-8              2000000               868 ns/op             320 B/op          9 allocs/op
125BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8      5000000               337 ns/op             320 B/op          9 allocs/op
126BenchmarkStructSimpleSuccess-8                                   5000000               260 ns/op               0 B/op          0 allocs/op
127BenchmarkStructSimpleSuccessParallel-8                          20000000                90.6 ns/op             0 B/op          0 allocs/op
128BenchmarkStructSimpleFailure-8                                   2000000               619 ns/op             424 B/op          9 allocs/op
129BenchmarkStructSimpleFailureParallel-8                           5000000               296 ns/op             424 B/op          9 allocs/op
130BenchmarkStructComplexSuccess-8                                  1000000              1454 ns/op             128 B/op          8 allocs/op
131BenchmarkStructComplexSuccessParallel-8                          3000000               579 ns/op             128 B/op          8 allocs/op
132BenchmarkStructComplexFailure-8                                   300000              4140 ns/op            3041 B/op         53 allocs/op
133BenchmarkStructComplexFailureParallel-8                          1000000              2127 ns/op            3041 B/op         53 allocs/op
134BenchmarkOneof-8                                                10000000               140 ns/op               0 B/op          0 allocs/op
135BenchmarkOneofParallel-8                                        20000000                70.1 ns/op             0 B/op          0 allocs/op
136```
137
138Complementary Software
139----------------------
140
141Here is a list of software that complements using this library either pre or post validation.
142
143* [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.
144* [mold](https://github.com/go-playground/mold) - A general library to help modify or set data within data structures and other objects
145
146How to Contribute
147------
148
149Make a pull request...
150
151License
152------
153Distributed under MIT License, please see license file within the code for more details.
154