-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
86 lines (67 loc) · 1.39 KB
/
validator_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package validate
import (
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
//
// Test helper types
//
type testStruct struct {
Foo string `json:"foo"`
f func() error
}
func (s *testStruct) Validate() error {
if s.f == nil {
return nil
}
return s.f()
}
type testNestedStruct struct {
OtherField *testStruct `yaml:"other_field"`
}
type MyStruct struct {
Name string
Kind string
}
//
// Tests
//
func TestNew(t *testing.T) {
got := New()
assert.IsType(t, &Validator{}, got)
}
func TestValidator_FieldNameFunc(t *testing.T) {
v := New()
v.FieldNameFunc(func(sf reflect.StructField) string {
return "<" + strings.ToUpper(sf.Name) + ">"
})
err := v.Validate(&testNestedStruct{
OtherField: &testStruct{f: func() error {
return &Error{Field: "Foo", Msg: "oops"}
}},
})
got := Errors(err)
assert.ElementsMatch(t, []error{
&Error{Field: "<OTHERFIELD>.<FOO>", Msg: "oops"},
}, got)
}
func TestValidator_FieldJoinFunc(t *testing.T) {
v := New()
v.FieldJoinFunc(func(path []string, field string) string {
if field != "" {
path = append(path, field)
}
return "[" + strings.Join(path, "][") + "]"
})
err := v.Validate(&testNestedStruct{
OtherField: &testStruct{f: func() error {
return &Error{Field: "Foo", Msg: "oops"}
}},
})
got := Errors(err)
assert.ElementsMatch(t, []error{
&Error{Field: "[other_field][foo]", Msg: "oops"},
}, got)
}