Skip to content

Commit fbc15ff

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #114 from joeybloggs/v6-development
V6 development
2 parents ed304c7 + 41b4a43 commit fbc15ff

File tree

9 files changed

+1487
-2561
lines changed

9 files changed

+1487
-2561
lines changed

README.md

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ Package validator
22
================
33

44
[![Join the chat at https://gitter.im/bluesuncorp/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bluesuncorp/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5-
[![Build Status](https://travis-ci.org/bluesuncorp/validator.svg?branch=v5.1)](https://travis-ci.org/bluesuncorp/validator)
6-
[![Coverage Status](https://coveralls.io/repos/bluesuncorp/validator/badge.svg?branch=v5)](https://coveralls.io/r/bluesuncorp/validator?branch=v5)
7-
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v5?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.v5)
5+
[![Build Status](https://travis-ci.org/bluesuncorp/validator.svg?branch=v6)](https://travis-ci.org/bluesuncorp/validator)
6+
[![Coverage Status](https://coveralls.io/repos/bluesuncorp/validator/badge.svg?branch=v6)](https://coveralls.io/r/bluesuncorp/validator?branch=v6)
7+
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v6?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.v6)
88

99
Package validator implements value validations for structs and individual fields based on tags.
1010

@@ -19,20 +19,20 @@ Installation
1919

2020
Use go get.
2121

22-
go get gopkg.in/bluesuncorp/validator.v5
22+
go get gopkg.in/bluesuncorp/validator.v6
2323

2424
or to update
2525

26-
go get -u gopkg.in/bluesuncorp/validator.v5
26+
go get -u gopkg.in/bluesuncorp/validator.v6
2727

2828
Then import the validator package into your own code.
2929

30-
import "gopkg.in/bluesuncorp/validator.v5"
30+
import "gopkg.in/bluesuncorp/validator.v6"
3131

3232
Usage and documentation
3333
------
3434

35-
Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage docs.
35+
Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v6 for detailed usage docs.
3636

3737
##### Example:
3838
```go
@@ -41,7 +41,7 @@ package main
4141
import (
4242
"fmt"
4343

44-
"gopkg.in/bluesuncorp/validator.v5"
44+
"gopkg.in/bluesuncorp/validator.v6"
4545
)
4646

4747
// User contains user information
@@ -66,7 +66,12 @@ var validate *validator.Validate
6666

6767
func main() {
6868

69-
validate = validator.New("validate", validator.BakedInValidators)
69+
config := validator.Config{
70+
TagName: "validate",
71+
ValidationFuncs: validator.BakedInValidators,
72+
}
73+
74+
validate = validator.New(config)
7075

7176
address := &Address{
7277
Street: "Eavesdown Docks",
@@ -83,31 +88,14 @@ func main() {
8388
Addresses: []*Address{address},
8489
}
8590

86-
// returns nil or *StructErrors
91+
// returns nil or ValidationErrors ( map[string]*FieldError )
8792
errs := validate.Struct(user)
8893

8994
if errs != nil {
9095

91-
// err will be of type *FieldError
92-
err := errs.Errors["Age"]
93-
fmt.Println(err.Error()) // output: Field validation for "Age" failed on the "lte" tag
94-
fmt.Println(err.Field) // output: Age
95-
fmt.Println(err.Tag) // output: lte
96-
fmt.Println(err.Kind) // output: uint8
97-
fmt.Println(err.Type) // output: uint8
98-
fmt.Println(err.Param) // output: 130
99-
fmt.Println(err.Value) // output: 135
100-
101-
// or if you prefer you can use the Flatten function
102-
// NOTE: I find this usefull when using a more hard static approach of checking field errors.
103-
// The above, is best for passing to some generic code to say parse the errors. i.e. I pass errs
104-
// to a routine which loops through the errors, creates and translates the error message into the
105-
// users locale and returns a map of map[string]string // field and error which I then use
106-
// within the HTML rendering.
107-
108-
flat := errs.Flatten()
109-
fmt.Println(flat) // output: map[Age:Field validation for "Age" failed on the "lte" tag Addresses[0].Address.City:Field validation for "City" failed on the "required" tag]
110-
err = flat["Addresses[0].Address.City"]
96+
fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag
97+
// Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
98+
err := errs["User.Addresses[0].City"]
11199
fmt.Println(err.Field) // output: City
112100
fmt.Println(err.Tag) // output: required
113101
fmt.Println(err.Kind) // output: string
@@ -126,14 +114,18 @@ func main() {
126114
Benchmarks
127115
------
128116
###### Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3
117+
NOTE: allocations for structs are up from v5, however ns/op for parallel operations are way down.
118+
It was a decicion not to cache struct info because although it reduced allocation to v5 levels, it
119+
hurt parallel performance too much.
129120
```go
130121
$ go test -cpu=4 -bench=. -benchmem=true
131122
PASS
132-
BenchmarkValidateField-4 3000000 429 ns/op 192 B/op 2 allocs/op
133-
BenchmarkValidateStructSimple-4 500000 2877 ns/op 657 B/op 10 allocs/op
134-
BenchmarkTemplateParallelSimple-4 500000 3097 ns/op 657 B/op 10 allocs/op
135-
BenchmarkValidateStructLarge-4 100000 15228 ns/op 4350 B/op 62 allocs/op
136-
BenchmarkTemplateParallelLarge-4 100000 14257 ns/op 4354 B/op 62 allocs/op
123+
BenchmarkField-4 5000000 314 ns/op 16 B/op 1 allocs/op
124+
BenchmarkFieldOrTag-4 500000 2425 ns/op 20 B/op 2 allocs/op
125+
BenchmarkStructSimple-4 500000 3117 ns/op 553 B/op 14 allocs/op
126+
BenchmarkStructSimpleParallel-4 1000000 1149 ns/op 553 B/op 14 allocs/op
127+
BenchmarkStructComplex-4 100000 19580 ns/op 3230 B/op 102 allocs/op
128+
BenchmarkStructComplexParallel-4 200000 6686 ns/op 3232 B/op 102 allocs/op
137129
```
138130

139131
How to Contribute

0 commit comments

Comments
 (0)