Skip to content

Commit 790122c

Browse files
Dean KarnDean Karn
authored andcommitted
Update simple.go
tmp blank out of file
1 parent d802346 commit 790122c

File tree

1 file changed

+121
-121
lines changed

1 file changed

+121
-121
lines changed

examples/simple.go

Lines changed: 121 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,150 @@
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-
}
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+
// }
2239

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"`
40+
// validate = validator.New(config)
41+
42+
// validateStruct()
43+
// validateField()
2944
}
3045

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

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

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

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

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

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-
}
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:
8477

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

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

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

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

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

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

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

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

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

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

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

121-
func main2() {
114+
// if len(v.Name) == 0 {
115+
// return nil, nil
116+
// }
122117

123-
customTypes := map[reflect.Type]validator.CustomTypeFunc{}
124-
customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
125-
customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
118+
// return v.Name, nil
119+
// }
126120

127-
config := validator.Config{
128-
TagName: "validate",
129-
ValidationFuncs: validator.BakedInValidators,
130-
CustomTypeFuncs: customTypes,
131-
}
121+
// func main2() {
132122

133-
validate2 = validator.New(config)
123+
// customTypes := map[reflect.Type]validator.CustomTypeFunc{}
124+
// customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
125+
// customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
134126

135-
validateCustomFieldType()
136-
}
127+
// config := validator.Config{
128+
// TagName: "validate",
129+
// ValidationFuncs: validator.BakedInValidators,
130+
// CustomTypeFuncs: customTypes,
131+
// }
137132

138-
func validateCustomFieldType() {
139-
val := valuer{
140-
Name: "blankme",
141-
}
133+
// validate2 = validator.New(config)
142134

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-
}
135+
// validateCustomFieldType()
136+
// }
148137

149-
// all ok
150-
}
138+
// func validateCustomFieldType() {
139+
// val := valuer{
140+
// Name: "blankme",
141+
// }
142+
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+
// }
148+
149+
// // all ok
150+
// }

0 commit comments

Comments
 (0)