|
1 | 1 | package main |
2 | 2 |
|
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 | +} |
41 | 22 |
|
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"` |
44 | 29 | } |
45 | 30 |
|
46 | | -// func validateStruct() { |
| 31 | +var validate *validator.Validate |
47 | 32 |
|
48 | | -// address := &Address{ |
49 | | -// Street: "Eavesdown Docks", |
50 | | -// Planet: "Persphone", |
51 | | -// Phone: "none", |
52 | | -// } |
| 33 | +func main() { |
53 | 34 |
|
54 | | -// user := &User{ |
55 | | -// FirstName: "Badger", |
56 | | -// LastName: "Smith", |
57 | | -// Age: 135, |
58 | | - |
59 | | -// FavouriteColor: "#000", |
60 | | -// Addresses: []*Address{address}, |
61 | | -// } |
| 35 | + config := validator.Config{ |
| 36 | + TagName: "validate", |
| 37 | + ValidationFuncs: validator.BakedInValidators, |
| 38 | + } |
62 | 39 |
|
63 | | -// // returns nil or ValidationErrors ( map[string]*FieldError ) |
64 | | -// errs := validate.Struct(user) |
| 40 | + validate = validator.New(config) |
65 | 41 |
|
66 | | -// if errs != nil { |
| 42 | + validateStruct() |
| 43 | + validateField() |
| 44 | +} |
67 | 45 |
|
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 | +} |
77 | 84 |
|
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" |
81 | 87 |
|
82 | | -// // save user to database |
83 | | -// } |
| 88 | + errs := validate.Field(myEmail, "required,email") |
84 | 89 |
|
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 | + } |
87 | 94 |
|
88 | | -// errs := validate.Field(myEmail, "required,email") |
| 95 | + // email ok, move on |
| 96 | +} |
89 | 97 |
|
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 |
94 | 99 |
|
95 | | -// // email ok, move on |
96 | | -// } |
| 100 | +type valuer struct { |
| 101 | + Name string |
| 102 | +} |
97 | 103 |
|
98 | | -// var validate2 *validator.Validate |
| 104 | +func (v valuer) Value() (sql.Value, error) { |
99 | 105 |
|
100 | | -// type valuer struct { |
101 | | -// Name string |
102 | | -// } |
| 106 | + if v.Name == "errorme" { |
| 107 | + return nil, errors.New("some kind of error") |
| 108 | + } |
103 | 109 |
|
104 | | -// func (v valuer) Value() (sql.Value, error) { |
| 110 | + if v.Name == "blankme" { |
| 111 | + return "", nil |
| 112 | + } |
105 | 113 |
|
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 | + } |
109 | 117 |
|
110 | | -// if v.Name == "blankme" { |
111 | | -// return "", nil |
112 | | -// } |
| 118 | + return v.Name, nil |
| 119 | +} |
113 | 120 |
|
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 | + } |
117 | 129 |
|
118 | | -// return v.Name, nil |
119 | | -// } |
| 130 | + return val |
| 131 | + } |
120 | 132 |
|
121 | | -// func main2() { |
| 133 | + return nil |
| 134 | +} |
122 | 135 |
|
123 | | -// customTypes := map[reflect.Type]validator.CustomTypeFunc{} |
124 | | -// customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType |
125 | | -// customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType |
| 136 | +func main2() { |
126 | 137 |
|
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 |
132 | 141 |
|
133 | | -// validate2 = validator.New(config) |
| 142 | + config := validator.Config{ |
| 143 | + TagName: "validate", |
| 144 | + ValidationFuncs: validator.BakedInValidators, |
| 145 | + CustomTypeFuncs: customTypes, |
| 146 | + } |
134 | 147 |
|
135 | | -// validateCustomFieldType() |
136 | | -// } |
| 148 | + validate2 = validator.New(config) |
137 | 149 |
|
138 | | -// func validateCustomFieldType() { |
139 | | -// val := valuer{ |
140 | | -// Name: "blankme", |
141 | | -// } |
| 150 | + validateCustomFieldType() |
| 151 | +} |
| 152 | + |
| 153 | +func validateCustomFieldType() { |
| 154 | + val := valuer{ |
| 155 | + Name: "blankme", |
| 156 | + } |
142 | 157 |
|
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 | + } |
148 | 163 |
|
149 | | -// // all ok |
150 | | -// } |
| 164 | + // all ok |
| 165 | +} |
0 commit comments