-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_test.go
137 lines (120 loc) · 2.13 KB
/
object_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package valval
import (
"errors"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestObjectValidator(t *testing.T) {
type t1 struct {
A string
B int
C bool
D *string
E *int
F *bool
}
i1 := NewIntValidator(func(v int64) error {
if v >= 0 && v <= 100 {
return nil
}
return errors.New("invalid value")
})
s1 := NewStringValidator(func(v string) error {
if v == "abc" {
return nil
}
return errors.New("invalid value")
})
Convey("Object validator", t, func() {
v1 := Object(M{
"A": String(s1),
"B": Number(i1),
"C": Bool(),
"D": String(s1),
"E": Number(i1),
"F": Bool(),
})
st1 := t1{
A: "abc",
B: 1,
C: false,
}
So(v1.Validate(st1), ShouldBeNil)
st2 := t1{
A: "abc",
B: 1,
C: false,
D: &st1.A,
E: &st1.B,
F: &st1.C,
}
So(v1.Validate(st2), ShouldBeNil)
st3 := t1{
A: "abcd",
B: 1,
C: false,
D: &st1.A,
E: &st1.B,
F: &st1.C,
}
Convey("object", func() {
So(v1.Validate(1), ShouldNotBeNil)
So(v1.Validate(nil), ShouldBeNil)
So(v1.Validate("aa"), ShouldNotBeNil)
So(v1.Validate(true), ShouldNotBeNil)
So(v1.Validate(st3), ShouldNotBeNil)
})
Convey("map", func() {
m1 := map[string]interface{}{
"A": "abc",
"B": 1,
"C": false,
}
So(v1.Validate(m1), ShouldBeNil)
m2 := map[string]interface{}{
"A": "abc",
"B": 1,
"C": false,
"D": &st1.A,
"E": &st1.B,
"F": &st1.C,
}
So(v1.Validate(m2), ShouldBeNil)
v2 := v1.Self(func(content map[string]interface{}) error {
if content["D"] == nil && content["E"] == nil {
return errors.New("D and E needed")
}
return nil
})
So(v1, ShouldNotEqual, v2)
st4 := t1{
A: "abc",
B: 1,
C: false,
D: nil,
E: &st1.B,
F: &st1.C,
}
So(v2.Validate(st4), ShouldBeNil)
st5 := t1{
A: "abc",
B: 1,
C: false,
D: nil,
E: nil,
F: &st1.C,
}
So(v2.Validate(st5), ShouldNotBeNil)
})
Convey("Pointer nil", func() {
type t2 struct {
P *t1
}
v := Object(M{
"P": Object(M{}),
})
o := t1{}
So(v.Validate(o), ShouldBeNil)
})
})
}