Skip to content

Commit 4f29ee1

Browse files
Dean KarnDean Karn
authored andcommitted
Merge branch 'v1-development' into v1
2 parents 8ece839 + b236748 commit 4f29ee1

File tree

5 files changed

+88
-17
lines changed

5 files changed

+88
-17
lines changed

baked_in.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
11
package validator
22

33
import (
4-
"log"
4+
"fmt"
55
"reflect"
66
"strconv"
77
)
88

9+
// BakedInValidators is the map of ValidationFunc used internally
10+
// but can be used with any new Validator if desired
911
var BakedInValidators = map[string]ValidationFunc{
1012
"required": required,
1113
"len": length,
1214
"min": min,
1315
"max": max,
16+
"alpha": alpha,
17+
"alphanum": alphanum,
18+
}
19+
20+
func alphanum(field interface{}, param string) bool {
21+
22+
st := reflect.ValueOf(field)
23+
24+
switch st.Kind() {
25+
26+
case reflect.String:
27+
return alphaNumericRegex.MatchString(field.(string))
28+
default:
29+
panic(fmt.Sprintf("Bad field type %T", field))
30+
}
31+
}
32+
33+
func alpha(field interface{}, param string) bool {
34+
35+
st := reflect.ValueOf(field)
36+
37+
switch st.Kind() {
38+
39+
case reflect.String:
40+
return alphaRegex.MatchString(field.(string))
41+
default:
42+
panic(fmt.Sprintf("Bad field type %T", field))
43+
}
1444
}
1545

1646
func required(field interface{}, param string) bool {
@@ -62,8 +92,7 @@ func length(field interface{}, param string) bool {
6292
return st.Float() == p
6393

6494
default:
65-
log.Fatalf("Bad field type for Input Param %s for %s\n", param, field)
66-
return false
95+
panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field))
6796
}
6897
}
6998

@@ -103,8 +132,7 @@ func min(field interface{}, param string) bool {
103132
return st.Float() >= p
104133

105134
default:
106-
log.Fatalf("Bad field type for Input Param %s for %s\n", param, field)
107-
return false
135+
panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field))
108136
}
109137
}
110138

@@ -144,8 +172,7 @@ func max(field interface{}, param string) bool {
144172
return st.Float() <= p
145173

146174
default:
147-
log.Fatalf("Bad field type for Input Param %s for %s\n", param, field)
148-
return false
175+
panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field))
149176
}
150177
}
151178

@@ -156,7 +183,7 @@ func asInt(param string) int64 {
156183
i, err := strconv.ParseInt(param, 0, 64)
157184

158185
if err != nil {
159-
log.Fatalf("Bad Input Param %s\n", param)
186+
panic(fmt.Sprintf("Bad Input Param %s\n", param))
160187
}
161188

162189
return i
@@ -169,7 +196,7 @@ func asUint(param string) uint64 {
169196
i, err := strconv.ParseUint(param, 0, 64)
170197

171198
if err != nil {
172-
log.Fatalf("Bad Input Param %s\n", param)
199+
panic(fmt.Sprintf("Bad Input Param %s\n", param))
173200
}
174201

175202
return i
@@ -182,7 +209,7 @@ func asFloat(param string) float64 {
182209
i, err := strconv.ParseFloat(param, 64)
183210

184211
if err != nil {
185-
log.Fatalf("Bad Input Param %s\n", param)
212+
panic(fmt.Sprintf("Bad Input Param %s\n", param))
186213
}
187214

188215
return i

doc.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ Here is a list of the current built in validators:
132132
a value (Determined by the required validator) then other validation
133133
such as min or max won't run, but if a value is set validation will run.
134134
(Usage: omitempty)
135+
required
136+
This validates that the value is not the data types default value.
137+
For numbers ensures value is not zero. For strings ensures value is
138+
not "". For slices, arrays, and maps, ensures the length is not zero.
139+
(Usage: required)
135140
len
136141
For numbers, max will ensure that the value is
137142
equal to the parameter given. For strings, it checks that
@@ -147,12 +152,12 @@ Here is a list of the current built in validators:
147152
greater or equal to the parameter given. For strings, it checks that
148153
the string length is at least that number of characters. For slices,
149154
arrays, and maps, validates the number of items. (Usage: min=10)
150-
151-
required
152-
This validates that the value is not the data types default value.
153-
For numbers ensures value is not zero. For strings ensures value is
154-
not "". For slices, arrays, and maps, ensures the length is not zero.
155-
(Usage: required)
155+
alpha
156+
This validates that a strings value contains alpha characters only
157+
(Usage: alpha)
158+
alphanum
159+
This validates that a strings value contains alphanumeric characters only
160+
(Usage: alphanum)
156161
157162
Validator notes:
158163

regexes.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package validator
2+
3+
import "regexp"
4+
5+
const (
6+
alphaRegexString string = "^[a-zA-Z]+$"
7+
alphaNumericRegexString string = "^[a-zA-Z0-9]+$"
8+
)
9+
10+
var (
11+
alphaRegex = regexp.MustCompile(alphaRegexString)
12+
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
13+
)

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
279279

280280
switch valueField.Kind() {
281281

282-
case reflect.Struct, reflect.Invalid:
282+
case reflect.Struct, reflect.Interface, reflect.Invalid:
283283
panic("Invalid field passed to ValidateFieldWithTag")
284284
}
285285

validator_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,32 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
113113
c.Assert(val.ErrorTag, Equals, expectedTag)
114114
}
115115

116+
func (ms *MySuite) TestAlphaNumeric(c *C) {
117+
118+
s := "abcd123"
119+
err := validator.ValidateFieldByTag(s, "alphanum")
120+
c.Assert(err, IsNil)
121+
122+
s = "abc!23"
123+
err = validator.ValidateFieldByTag(s, "alphanum")
124+
c.Assert(err.Error(), Equals, "alphanum")
125+
126+
c.Assert(func() { validator.ValidateFieldByTag(1, "alphanum") }, PanicMatches, "Bad field type int")
127+
}
128+
129+
func (ms *MySuite) TestAlpha(c *C) {
130+
131+
s := "abcd"
132+
err := validator.ValidateFieldByTag(s, "alpha")
133+
c.Assert(err, IsNil)
134+
135+
s = "abc1"
136+
err = validator.ValidateFieldByTag(s, "alpha")
137+
c.Assert(err.Error(), Equals, "alpha")
138+
139+
c.Assert(func() { validator.ValidateFieldByTag(1, "alpha") }, PanicMatches, "Bad field type int")
140+
}
141+
116142
func (ms *MySuite) TestFlattening(c *C) {
117143

118144
tSuccess := &TestString{

0 commit comments

Comments
 (0)