-
Notifications
You must be signed in to change notification settings - Fork 100
/
invalid_schemas_test.go
74 lines (66 loc) · 1.47 KB
/
invalid_schemas_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
package jsonschema_test
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"github.com/santhosh-tekuri/jsonschema/v6"
)
func TestInvalidSchemas(t *testing.T) {
f, err := os.Open("./testdata/invalid_schemas.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var tests []struct {
Description string
Remotes map[string]any
Schema any
Errors []string
}
dec := json.NewDecoder(f)
dec.UseNumber()
if err := dec.Decode(&tests); err != nil {
t.Fatal(err)
}
for _, test := range tests {
t.Log(test.Description)
url := "http://invalid-schemas.com/schema.json"
c := jsonschema.NewCompiler()
loader := jsonschema.SchemeURLLoader{
"file": jsonschema.FileLoader{},
"http": invalidRemotes(test.Remotes),
}
c.UseLoader(loader)
if err := c.AddResource(url, test.Schema); err != nil {
t.Fatalf("addResource failed: %v", err)
}
_, err := c.Compile(url)
if err == nil {
if len(test.Errors) > 0 {
t.Fatal("want compilation to fail")
}
} else {
if len(test.Errors) == 0 {
t.Log(err)
t.Fatal("want compilation to succeed")
}
got := fmt.Sprintf("%#v", err)
for _, want := range test.Errors {
if !strings.Contains(got, want) {
t.Errorf(" got %s", got)
t.Fatalf("want %s", want)
}
}
}
t.Log()
}
}
type invalidRemotes map[string]any
func (l invalidRemotes) Load(url string) (any, error) {
if v, ok := l[url]; ok {
return v, nil
}
return nil, fmt.Errorf("remote %q not found", url)
}