Skip to content

Commit 491dd6b

Browse files
authored
Merge pull request #301 from katbyte/kt/validation-make-names-consistent
Validation - Rename some validators to make names more consistant
2 parents 0cc29fd + 6fe1d94 commit 491dd6b

File tree

9 files changed

+421
-227
lines changed

9 files changed

+421
-227
lines changed

helper/validation/int.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,57 @@ import (
1010
// IntBetween returns a SchemaValidateFunc which tests if the provided value
1111
// is of type int and is between min and max (inclusive)
1212
func IntBetween(min, max int) schema.SchemaValidateFunc {
13-
return func(i interface{}, k string) (s []string, es []error) {
13+
return func(i interface{}, k string) (warnings []string, errors []error) {
1414
v, ok := i.(int)
1515
if !ok {
16-
es = append(es, fmt.Errorf("expected type of %s to be int", k))
17-
return
16+
errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
17+
return warnings, errors
1818
}
1919

2020
if v < min || v > max {
21-
es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
22-
return
21+
errors = append(errors, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
22+
return warnings, errors
2323
}
2424

25-
return
25+
return warnings, errors
2626
}
2727
}
2828

2929
// IntAtLeast returns a SchemaValidateFunc which tests if the provided value
3030
// is of type int and is at least min (inclusive)
3131
func IntAtLeast(min int) schema.SchemaValidateFunc {
32-
return func(i interface{}, k string) (s []string, es []error) {
32+
return func(i interface{}, k string) (warnings []string, errors []error) {
3333
v, ok := i.(int)
3434
if !ok {
35-
es = append(es, fmt.Errorf("expected type of %s to be int", k))
36-
return
35+
errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
36+
return warnings, errors
3737
}
3838

3939
if v < min {
40-
es = append(es, fmt.Errorf("expected %s to be at least (%d), got %d", k, min, v))
41-
return
40+
errors = append(errors, fmt.Errorf("expected %s to be at least (%d), got %d", k, min, v))
41+
return warnings, errors
4242
}
4343

44-
return
44+
return warnings, errors
4545
}
4646
}
4747

4848
// IntAtMost returns a SchemaValidateFunc which tests if the provided value
4949
// is of type int and is at most max (inclusive)
5050
func IntAtMost(max int) schema.SchemaValidateFunc {
51-
return func(i interface{}, k string) (s []string, es []error) {
51+
return func(i interface{}, k string) (warnings []string, errors []error) {
5252
v, ok := i.(int)
5353
if !ok {
54-
es = append(es, fmt.Errorf("expected type of %s to be int", k))
55-
return
54+
errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
55+
return warnings, errors
5656
}
5757

5858
if v > max {
59-
es = append(es, fmt.Errorf("expected %s to be at most (%d), got %d", k, max, v))
60-
return
59+
errors = append(errors, fmt.Errorf("expected %s to be at most (%d), got %d", k, max, v))
60+
return warnings, errors
6161
}
6262

63-
return
63+
return warnings, errors
6464
}
6565
}
6666

@@ -70,13 +70,13 @@ func IntDivisibleBy(divisor int) schema.SchemaValidateFunc {
7070
return func(i interface{}, k string) (warnings []string, errors []error) {
7171
v, ok := i.(int)
7272
if !ok {
73-
errors = append(errors, fmt.Errorf("expected type of %s to be int", k))
74-
return
73+
errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
74+
return warnings, errors
7575
}
7676

7777
if math.Mod(float64(v), float64(divisor)) != 0 {
7878
errors = append(errors, fmt.Errorf("expected %s to be divisible by %d, got: %v", k, divisor, i))
79-
return
79+
return warnings, errors
8080
}
8181

8282
return warnings, errors
@@ -86,32 +86,32 @@ func IntDivisibleBy(divisor int) schema.SchemaValidateFunc {
8686
// IntInSlice returns a SchemaValidateFunc which tests if the provided value
8787
// is of type int and matches the value of an element in the valid slice
8888
func IntInSlice(valid []int) schema.SchemaValidateFunc {
89-
return func(i interface{}, k string) (s []string, es []error) {
89+
return func(i interface{}, k string) (warnings []string, errors []error) {
9090
v, ok := i.(int)
9191
if !ok {
92-
es = append(es, fmt.Errorf("expected type of %s to be an integer", k))
93-
return
92+
errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
93+
return warnings, errors
9494
}
9595

9696
for _, validInt := range valid {
9797
if v == validInt {
98-
return
98+
return warnings, errors
9999
}
100100
}
101101

102-
es = append(es, fmt.Errorf("expected %s to be one of %v, got %d", k, valid, v))
103-
return
102+
errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %d", k, valid, v))
103+
return warnings, errors
104104
}
105105
}
106106

107107
// IntNotInSlice returns a SchemaValidateFunc which tests if the provided value
108108
// is of type int and matches the value of an element in the valid slice
109109
func IntNotInSlice(valid []int) schema.SchemaValidateFunc {
110-
return func(i interface{}, k string) (_ []string, errors []error) {
110+
return func(i interface{}, k string) (warnings []string, errors []error) {
111111
v, ok := i.(int)
112112
if !ok {
113-
errors = append(errors, fmt.Errorf("expected type of %s to be an integer", k))
114-
return
113+
errors = append(errors, fmt.Errorf("expected type of %s to be integer", k))
114+
return warnings, errors
115115
}
116116

117117
for _, validInt := range valid {
@@ -120,6 +120,6 @@ func IntNotInSlice(valid []int) schema.SchemaValidateFunc {
120120
}
121121
}
122122

123-
return
123+
return warnings, errors
124124
}
125125
}

helper/validation/int_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestValidationIntBetween(t *testing.T) {
2323
{
2424
val: "1",
2525
f: IntBetween(2, 3),
26-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"),
26+
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
2727
},
2828
})
2929
}
@@ -46,7 +46,7 @@ func TestValidationIntAtLeast(t *testing.T) {
4646
{
4747
val: "1",
4848
f: IntAtLeast(2),
49-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"),
49+
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
5050
},
5151
})
5252
}
@@ -69,7 +69,7 @@ func TestValidationIntAtMost(t *testing.T) {
6969
{
7070
val: "1",
7171
f: IntAtMost(0),
72-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"),
72+
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
7373
},
7474
})
7575
}
@@ -124,7 +124,7 @@ func TestValidationIntInSlice(t *testing.T) {
124124
{
125125
val: "InvalidValue",
126126
f: IntInSlice([]int{10, 20}),
127-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be an integer"),
127+
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
128128
},
129129
})
130130
}

helper/validation/list.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package validation
2+
3+
import "fmt"
4+
5+
// ValidateListUniqueStrings is a ValidateFunc that ensures a list has no
6+
// duplicate items in it. It's useful for when a list is needed over a set
7+
// because order matters, yet the items still need to be unique.
8+
//
9+
// Deprecated: use ListOfUniqueStrings
10+
func ValidateListUniqueStrings(i interface{}, k string) (warnings []string, errors []error) {
11+
return ListOfUniqueStrings(i, k)
12+
}
13+
14+
// ValidateListUniqueStrings is a ValidateFunc that ensures a list has no
15+
// duplicate items in it. It's useful for when a list is needed over a set
16+
// because order matters, yet the items still need to be unique.
17+
func ListOfUniqueStrings(i interface{}, k string) (warnings []string, errors []error) {
18+
v, ok := i.([]interface{})
19+
if !ok {
20+
errors = append(errors, fmt.Errorf("expected type of %q to be List", k))
21+
return warnings, errors
22+
}
23+
24+
for _, e := range v {
25+
if _, eok := e.(string); !eok {
26+
errors = append(errors, fmt.Errorf("expected %q to only contain string elements, found :%v", k, e))
27+
return warnings, errors
28+
}
29+
}
30+
31+
for n1, i1 := range v {
32+
for n2, i2 := range v {
33+
if i1.(string) == i2.(string) && n1 != n2 {
34+
errors = append(errors, fmt.Errorf("expected %q to not have duplicates: found 2 or more of %v", k, i1))
35+
return warnings, errors
36+
}
37+
}
38+
}
39+
40+
return warnings, errors
41+
}

helper/validation/list_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package validation
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestValidationListOfUniqueStrings(t *testing.T) {
8+
cases := map[string]struct {
9+
Value interface{}
10+
Error bool
11+
}{
12+
"NotList": {
13+
Value: "the list is a lie",
14+
Error: true,
15+
},
16+
"NotListOfString": {
17+
Value: []interface{}{"seven", 7},
18+
Error: true,
19+
},
20+
"NonUniqueStrings": {
21+
Value: []interface{}{"kt", "is", "kt"},
22+
Error: true,
23+
},
24+
"UniqueStrings": {
25+
Value: []interface{}{"thanks", "for", "all", "the", "fish"},
26+
Error: false,
27+
},
28+
}
29+
30+
for tn, tc := range cases {
31+
t.Run(tn, func(t *testing.T) {
32+
_, errors := ListOfUniqueStrings(tc.Value, tn)
33+
34+
if len(errors) > 0 && !tc.Error {
35+
t.Errorf("ListOfUniqueStrings(%s) produced an unexpected error", tc.Value)
36+
} else if len(errors) == 0 && tc.Error {
37+
t.Errorf("ListOfUniqueStrings(%s) did not error", tc.Value)
38+
}
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)