This repository has been archived by the owner on Jul 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonschema_test.go
153 lines (137 loc) · 3.98 KB
/
jsonschema_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jsonschema
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
)
var notSupported = map[string]struct{}{"uniqueItems.json": {}}
func TestJSONPointerKeypath(t *testing.T) {
keypath := []string{"foo", "bar", "10", "baz"}
err := &ValidationError{keypath, ""}
str := err.JSONPointer()
expectedStr := "/foo/bar/10/baz"
if str != expectedStr {
t.Error(errors.New(fmt.Sprintf("Expected \"%s\" and got \"%s\"", expectedStr, str)))
}
}
func TestDotNotationKeypath(t *testing.T) {
keypath := []string{"foo", "bar", "10", "baz"}
err := &ValidationError{keypath, ""}
str := err.DotNotation()
expectedStr := "foo.bar.10.baz"
if str != expectedStr {
t.Error(errors.New(fmt.Sprintf("Expected \"%s\" and got \"%s\"", expectedStr, str)))
}
}
func TestDraft4(t *testing.T) {
suites := []string{
filepath.Join("JSON-Schema-Test-Suite", "tests", "draft4"),
filepath.Join("tests", "draft4"),
}
var failures, successes int
schemaCache := make(map[string]*Schema)
for _, testResources := range suites {
if _, err := os.Stat(testResources); err != nil {
t.Error("Test suite missing. Run `git submodule update --init` to download it.")
}
err := filepath.Walk(testResources, testFileRunner(t, &failures, &successes, &schemaCache))
if err != nil {
t.Error(err.Error())
}
}
t.Logf("%d failed, %d succeeded.", failures, successes)
}
func testFileRunner(t *testing.T, failures, successes *int, schemaCache *map[string]*Schema) func(string, os.FileInfo, error) error {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if _, ok := notSupported[info.Name()]; ok {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
var testFile []testCase
err = json.NewDecoder(file).Decode(&testFile)
if err != nil {
return err
}
for _, cse := range testFile {
schema, parseErr := ParseWithCache(bytes.NewReader(cse.Schema), true, schemaCache)
for _, tst := range cse.Tests {
if parseErr != nil {
t.Error(parseErrMessage(parseErr, path, cse, tst))
*failures++
continue
}
var data interface{}
decoder := json.NewDecoder(bytes.NewReader(tst.Data))
decoder.UseNumber()
decoder.Decode(&data)
errorList := schema.Validate(nil, data)
err = correctValidation(path, cse, tst, errorList)
if err != nil {
t.Error(failureMessage(err, path, cse, tst, errorList))
*failures++
continue
}
*successes++
}
}
return nil
}
}
type testCase struct {
Description string `json:"description"`
Schema json.RawMessage `json:"schema"`
Tests []testInstance `json:"tests"`
}
type testInstance struct {
Description string `json:"description"`
Data json.RawMessage `json:"data"`
Valid bool `json:"valid"`
}
func correctValidation(path string, cse testCase, tst testInstance, errorList []ValidationError) error {
validated := (len(errorList) == 0)
var failureName string
if validated && !tst.Valid {
failureName = "schema.Validate validated bad data."
} else if !validated && tst.Valid {
failureName = "schema.Validate failed to validate good data."
}
if len(failureName) > 0 {
return errors.New(failureName)
}
return nil
}
func parseErrMessage(err error, path string, cse testCase, tst testInstance) string {
return fmt.Sprintf(`error resolving $ref: %s
file: %s
test case description: %s
schema: %s
test instance description: %s
test data: %s
`, err.Error(), path, cse.Description, cse.Schema, tst.Description, tst.Data)
}
func failureMessage(err error, path string, cse testCase, tst testInstance, errorList []ValidationError) string {
return fmt.Sprintf(`%s
file: %s
test case description: %s
schema: %s
test instance description: %s
test data: %s
result of schema.Validate:
expected: %t
actual: %t
actual validation errors: %s
`, err.Error(), path, cse.Description, cse.Schema, tst.Description, tst.Data, tst.Valid, len(errorList) == 0, errorList)
}