-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema_test.go
92 lines (84 loc) · 1.97 KB
/
schema_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
package schema
import (
"testing"
)
func TestFullSuccess(t *testing.T) {
data := dataFromJSON(t, `
{
"id": 12,
"name": "Max Mustermann",
"age": 42,
"footsize": "unknown",
"admin": false,
"height": 1.91,
"address": {
"street": "Musterstrasse 12",
"zip": "12345",
"country": "Germany"
},
"tags": ["blue","red"],
"friends": ["hans", "peter", "harald", "gundel"],
"pi": [3,1,4,1,5,9,2,6,5,3,5,9]
}`)
err := Match(
Map{
"id": IsInteger,
"name": "Max Mustermann",
"age": 42,
"admin": IsBool,
"height": IsFloat,
"footsize": IsPresent,
"address": Map{
"street": IsString,
"zip": IsString,
"country": IsString,
},
"tags": Array("blue", "red"),
"friends": ArrayIncluding("hans"),
"pi": ArrayEach(IsInteger),
},
data,
)
if err != nil {
t.Fatal(err)
}
}
func TestNestingFailures(t *testing.T) {
data := dataFromJSON(t, `
{
"name": "-",
"address": {
"street": "-",
"geo": {"lat":"-", "extra": "-"}
},
"tags": ["-"]
}`)
err := Map{
"name": "Max Mustermann",
"address": Map{
"street": "Bauernhof",
"geo": Map{
"lat": "12",
},
},
"tags": Array("blue"),
}.Match(data) // explicitly call Match method to keep more elaborate errors without casting
if err == nil {
t.Fatal("expected error")
}
if err.Errors["name"] != `"-" != "Max Mustermann"` {
t.Errorf(`wrong error on "name": %s`, err.Errors["name"])
}
if err.Errors["address.street"] != `"-" != "Bauernhof"` {
t.Errorf(`wrong error on "address.street": %s`, err.Errors["address.street"])
}
if err.Errors["address.geo"] != `Found extra keys: "extra"` {
t.Errorf(`wrong error on "address.geo": %s`, err.Errors["address.geo"])
}
if err.Errors["address.geo.lat"] != `"-" != "12"` {
t.Errorf(`wrong error on "address.geo.lat": %s`, err.Errors["address.geo.lat"])
}
if err.Errors["tags[0]"] != `"-" != "blue"` {
t.Errorf(`wrong error on "tags[0]": %s`, err.Errors["tags[0]"])
}
}