-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_reflectvalue_test.go
58 lines (52 loc) · 1.53 KB
/
validator_reflectvalue_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package structvalidator
import (
"reflect"
"testing"
)
// In the struct-db-postgres, struct-validator is used to validate map of values against reflect.Value which is actually
// a pointer of a pointer to struct.
// TODO: This should be revisited at some point.
type Wrapper struct {
DoesntMatter string
UseMeToValidate []*Test1
}
func TestWithInvalidValuesOnReflectValue(t *testing.T) {
o := &Wrapper{}
v := reflect.ValueOf(o)
i := reflect.Indirect(v)
s := i.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
k := f.Type.Kind()
// Only field which are slices of pointers to struct instances
if k != reflect.Slice || f.Type.Elem().Kind() != reflect.Ptr || f.Type.Elem().Elem().Kind() != reflect.Struct {
continue
}
expectedBool := false
expectedFailedFields := map[string]int{
"FirstName": FailLenMax,
"LastName": FailLenMin,
"Age": FailValMin,
"PostCode": FailRegexp,
"Email": FailEmail,
"BelowZero": FailValMax,
"DiscountPrice": FailValMax,
"Country": FailRegexp,
}
opts := &ValidationOptions{
OverwriteFieldValues: map[string]interface{}{
"FirstName": "123456789012345678901234567890",
"LastName": "b",
"Age": 15,
"Price": 0,
"PostCode": "AA123",
"Email": "invalidEmail",
"BelowZero": 8,
"DiscountPrice": 9999,
"Country": "Tokelau",
"County": "",
},
}
compare(reflect.New(f.Type.Elem()), expectedBool, expectedFailedFields, opts, t)
}
}