You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-35Lines changed: 27 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@ Package validator
2
2
================
3
3
4
4
[](https://gitter.im/bluesuncorp/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
// returns nil or ValidationErrors ( map[string]*FieldError )
87
92
errs:= validate.Struct(user)
88
93
89
94
if errs != nil {
90
95
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"]
111
99
fmt.Println(err.Field) // output: City
112
100
fmt.Println(err.Tag) // output: required
113
101
fmt.Println(err.Kind) // output: string
@@ -126,14 +114,18 @@ func main() {
126
114
Benchmarks
127
115
------
128
116
###### 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
Copy file name to clipboardExpand all lines: doc.go
+20-85Lines changed: 20 additions & 85 deletions
Original file line number
Diff line number
Diff line change
@@ -1,59 +1,9 @@
1
1
/*
2
-
Package validator implements value validations for structs and individual fields based on tags. It can also handle Cross Field and Cross Struct validation for nested structs.
2
+
Package validator implements value validations for structs and individual fields based on tags.
3
+
It can also handle Cross Field and Cross Struct validation for nested structs and has the ability
// returns nil or ValidationErrors ( map[string]*FieldError )
49
54
errs:=validate.Struct(user)
50
55
51
56
iferrs!=nil {
52
57
53
-
// err will be of type *FieldError
54
-
err:=errs.Errors["Age"]
55
-
fmt.Println(err.Error()) // output: Field validation for "Age" failed on the "lte" tag
56
-
fmt.Println(err.Field) // output: Age
57
-
fmt.Println(err.Tag) // output: lte
58
-
fmt.Println(err.Kind) // output: uint8
59
-
fmt.Println(err.Type) // output: uint8
60
-
fmt.Println(err.Param) // output: 130
61
-
fmt.Println(err.Value) // output: 135
62
-
63
-
// or if you prefer you can use the Flatten function
64
-
// NOTE: I find this usefull when using a more hard static approach of checking field errors.
65
-
// The above, is best for passing to some generic code to say parse the errors. i.e. I pass errs
66
-
// to a routine which loops through the errors, creates and translates the error message into the
67
-
// users locale and returns a map of map[string]string // field and error which I then use
68
-
// within the HTML rendering.
69
-
70
-
flat:=errs.Flatten()
71
-
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]
72
-
err=flat["Addresses[0].Address.City"]
58
+
fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag
59
+
// Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
0 commit comments