Skip to content

Commit 048d7b8

Browse files
joeybloggsjoeybloggs
authored andcommitted
Merge branch 'v6-development' into v6
2 parents 790122c + 32910f8 commit 048d7b8

File tree

2 files changed

+149
-119
lines changed

2 files changed

+149
-119
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ func (v valuer) Value() (sql.Value, error) {
170170
return v.Name, nil
171171
}
172172

173+
// ValidateValuerType implements validator.CustomTypeFunc
174+
func ValidateValuerType(field reflect.Value) interface{} {
175+
if valuer, ok := field.Interface().(sql.Valuer); ok {
176+
val, err := valuer.Value()
177+
if err != nil {
178+
// handle the error how you want
179+
return nil
180+
}
181+
182+
return val
183+
}
184+
185+
return nil
186+
}
187+
173188
func main() {
174189

175190
customTypes := map[reflect.Type]validator.CustomTypeFunc{}

examples/simple.go

Lines changed: 134 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,165 @@
11
package main
22

3-
// import (
4-
// "errors"
5-
// "fmt"
6-
// "reflect"
7-
8-
// sql "database/sql/driver"
9-
10-
// "gopkg.in/bluesuncorp/validator.v6"
11-
// )
12-
13-
// // User contains user information
14-
// type User struct {
15-
// FirstName string `validate:"required"`
16-
// LastName string `validate:"required"`
17-
// Age uint8 `validate:"gte=0,lte=130"`
18-
// Email string `validate:"required,email"`
19-
// FavouriteColor string `validate:"hexcolor|rgb|rgba"`
20-
// Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
21-
// }
22-
23-
// // Address houses a users address information
24-
// type Address struct {
25-
// Street string `validate:"required"`
26-
// City string `validate:"required"`
27-
// Planet string `validate:"required"`
28-
// Phone string `validate:"required"`
29-
// }
30-
31-
// var validate *validator.Validate
32-
33-
func main() {
34-
35-
// config := validator.Config{
36-
// TagName: "validate",
37-
// ValidationFuncs: validator.BakedInValidators,
38-
// }
39-
40-
// validate = validator.New(config)
3+
import (
4+
"errors"
5+
"fmt"
6+
"reflect"
7+
8+
sql "database/sql/driver"
9+
10+
"gopkg.in/bluesuncorp/validator.v6"
11+
)
12+
13+
// User contains user information
14+
type User struct {
15+
FirstName string `validate:"required"`
16+
LastName string `validate:"required"`
17+
Age uint8 `validate:"gte=0,lte=130"`
18+
Email string `validate:"required,email"`
19+
FavouriteColor string `validate:"hexcolor|rgb|rgba"`
20+
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
21+
}
4122

42-
// validateStruct()
43-
// validateField()
23+
// Address houses a users address information
24+
type Address struct {
25+
Street string `validate:"required"`
26+
City string `validate:"required"`
27+
Planet string `validate:"required"`
28+
Phone string `validate:"required"`
4429
}
4530

46-
// func validateStruct() {
31+
var validate *validator.Validate
4732

48-
// address := &Address{
49-
// Street: "Eavesdown Docks",
50-
// Planet: "Persphone",
51-
// Phone: "none",
52-
// }
33+
func main() {
5334

54-
// user := &User{
55-
// FirstName: "Badger",
56-
// LastName: "Smith",
57-
// Age: 135,
58-
// Email: "[email protected]",
59-
// FavouriteColor: "#000",
60-
// Addresses: []*Address{address},
61-
// }
35+
config := validator.Config{
36+
TagName: "validate",
37+
ValidationFuncs: validator.BakedInValidators,
38+
}
6239

63-
// // returns nil or ValidationErrors ( map[string]*FieldError )
64-
// errs := validate.Struct(user)
40+
validate = validator.New(config)
6541

66-
// if errs != nil {
42+
validateStruct()
43+
validateField()
44+
}
6745

68-
// fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag
69-
// // Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
70-
// err := errs["User.Addresses[0].City"]
71-
// fmt.Println(err.Field) // output: City
72-
// fmt.Println(err.Tag) // output: required
73-
// fmt.Println(err.Kind) // output: string
74-
// fmt.Println(err.Type) // output: string
75-
// fmt.Println(err.Param) // output:
76-
// fmt.Println(err.Value) // output:
46+
func validateStruct() {
47+
48+
address := &Address{
49+
Street: "Eavesdown Docks",
50+
Planet: "Persphone",
51+
Phone: "none",
52+
}
53+
54+
user := &User{
55+
FirstName: "Badger",
56+
LastName: "Smith",
57+
Age: 135,
58+
59+
FavouriteColor: "#000",
60+
Addresses: []*Address{address},
61+
}
62+
63+
// returns nil or ValidationErrors ( map[string]*FieldError )
64+
errs := validate.Struct(user)
65+
66+
if errs != nil {
67+
68+
fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag
69+
// Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
70+
err := errs["User.Addresses[0].City"]
71+
fmt.Println(err.Field) // output: City
72+
fmt.Println(err.Tag) // output: required
73+
fmt.Println(err.Kind) // output: string
74+
fmt.Println(err.Type) // output: string
75+
fmt.Println(err.Param) // output:
76+
fmt.Println(err.Value) // output:
77+
78+
// from here you can create your own error messages in whatever language you wish
79+
return
80+
}
81+
82+
// save user to database
83+
}
7784

78-
// // from here you can create your own error messages in whatever language you wish
79-
// return
80-
// }
85+
func validateField() {
86+
myEmail := "joeybloggs.gmail.com"
8187

82-
// // save user to database
83-
// }
88+
errs := validate.Field(myEmail, "required,email")
8489

85-
// func validateField() {
86-
// myEmail := "joeybloggs.gmail.com"
90+
if errs != nil {
91+
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
92+
return
93+
}
8794

88-
// errs := validate.Field(myEmail, "required,email")
95+
// email ok, move on
96+
}
8997

90-
// if errs != nil {
91-
// fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
92-
// return
93-
// }
98+
var validate2 *validator.Validate
9499

95-
// // email ok, move on
96-
// }
100+
type valuer struct {
101+
Name string
102+
}
97103

98-
// var validate2 *validator.Validate
104+
func (v valuer) Value() (sql.Value, error) {
99105

100-
// type valuer struct {
101-
// Name string
102-
// }
106+
if v.Name == "errorme" {
107+
return nil, errors.New("some kind of error")
108+
}
103109

104-
// func (v valuer) Value() (sql.Value, error) {
110+
if v.Name == "blankme" {
111+
return "", nil
112+
}
105113

106-
// if v.Name == "errorme" {
107-
// return nil, errors.New("some kind of error")
108-
// }
114+
if len(v.Name) == 0 {
115+
return nil, nil
116+
}
109117

110-
// if v.Name == "blankme" {
111-
// return "", nil
112-
// }
118+
return v.Name, nil
119+
}
113120

114-
// if len(v.Name) == 0 {
115-
// return nil, nil
116-
// }
121+
// ValidateValuerType implements validator.CustomTypeFunc
122+
func ValidateValuerType(field reflect.Value) interface{} {
123+
if valuer, ok := field.Interface().(sql.Valuer); ok {
124+
val, err := valuer.Value()
125+
if err != nil {
126+
// handle the error how you want
127+
return nil
128+
}
117129

118-
// return v.Name, nil
119-
// }
130+
return val
131+
}
120132

121-
// func main2() {
133+
return nil
134+
}
122135

123-
// customTypes := map[reflect.Type]validator.CustomTypeFunc{}
124-
// customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
125-
// customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
136+
func main2() {
126137

127-
// config := validator.Config{
128-
// TagName: "validate",
129-
// ValidationFuncs: validator.BakedInValidators,
130-
// CustomTypeFuncs: customTypes,
131-
// }
138+
customTypes := map[reflect.Type]validator.CustomTypeFunc{}
139+
customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
140+
customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
132141

133-
// validate2 = validator.New(config)
142+
config := validator.Config{
143+
TagName: "validate",
144+
ValidationFuncs: validator.BakedInValidators,
145+
CustomTypeFuncs: customTypes,
146+
}
134147

135-
// validateCustomFieldType()
136-
// }
148+
validate2 = validator.New(config)
137149

138-
// func validateCustomFieldType() {
139-
// val := valuer{
140-
// Name: "blankme",
141-
// }
150+
validateCustomFieldType()
151+
}
152+
153+
func validateCustomFieldType() {
154+
val := valuer{
155+
Name: "blankme",
156+
}
142157

143-
// errs := validate2.Field(val, "required")
144-
// if errs != nil {
145-
// fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "required" tag
146-
// return
147-
// }
158+
errs := validate2.Field(val, "required")
159+
if errs != nil {
160+
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "required" tag
161+
return
162+
}
148163

149-
// // all ok
150-
// }
164+
// all ok
165+
}

0 commit comments

Comments
 (0)