-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator_test.go
135 lines (124 loc) · 3.09 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
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
package openapi_test
import (
"context"
"encoding/json"
"errors"
"io"
"io/fs"
"strings"
"testing"
"github.com/chanced/openapi"
"github.com/chanced/uri"
"github.com/santhosh-tekuri/jsonschema/v5"
"gopkg.in/yaml.v3"
)
func TestValidation(t *testing.T) {
ctx := context.Background()
c, err := openapi.SetupCompiler(jsonschema.NewCompiler()) // adding schema files
if err != nil {
t.Fatal(err)
}
v, err := openapi.NewValidator(c)
if err != nil {
t.Fatal(err)
}
// you can Load either JSON or YAML
err = fs.WalkDir(testdata, "testdata/documents/validation/pass", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
t.Run(strings.TrimPrefix(p, "testdata/documents/validation/"), func(t *testing.T) {
f, err := testdata.Open(p)
if err != nil {
t.Fatal(err)
}
fn := func(_ context.Context, uri uri.URI, kind openapi.Kind) (openapi.Kind, []byte, error) {
d, err := io.ReadAll(f)
if err != nil {
return 0, nil, err
}
return openapi.KindDocument, d, nil
}
doc, err := openapi.Load(ctx, p, v, fn)
if err != nil {
t.Errorf("failed to load document: %v", err)
}
err = v.ValidateDocument(doc)
if err != nil {
t.Errorf("failed to validate document: %v", err)
}
})
return nil
})
if err != nil {
t.Errorf("expected document to be valid, received: %v", err)
}
// you can Load either JSON or YAML
err = fs.WalkDir(testdata, "testdata/documents/validation/fail", func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
t.Run(strings.TrimPrefix(p, "testdata/documents/validation/"), func(t *testing.T) {
f, err := testdata.Open(p)
if err != nil {
t.Fatal(err)
}
fn := func(_ context.Context, uri uri.URI, kind openapi.Kind) (openapi.Kind, []byte, error) {
d, err := io.ReadAll(f)
if err != nil {
return 0, nil, err
}
return openapi.KindDocument, d, nil
}
_, err = openapi.Load(ctx, p, v, fn)
if err == nil {
t.Errorf("expected document to be invalid, received: %v", err)
}
var ve *openapi.ValidationError
if !errors.As(err, &ve) {
t.Errorf("expected document to be invalid, received: %v", err)
}
})
return nil
})
if err != nil {
t.Errorf("expected document to be valid, received: %v", err)
}
}
// issue #19
// https://github.com/chanced/openapi/issues/19
func TestValidator_Schema(t *testing.T) {
// ctx := context.Background()
c, err := openapi.SetupCompiler(jsonschema.NewCompiler()) // adding schema files
if err != nil {
t.Fatal(err)
}
v, err := openapi.NewValidator(c)
if err != nil {
t.Fatal(err)
}
f, err := testdata.Open("testdata/schemas/string-map.yaml")
if err != nil {
t.Fatal(err)
}
d, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
var s openapi.Schema
err = yaml.Unmarshal(d, &s)
if err != nil {
t.Fatal(err)
}
d, err = json.Marshal(s)
if err != nil {
t.Fatal(err)
}
v.Validate(d, *uri.MustParse("testdata/schemas/string-map.yaml"), openapi.KindSchema, openapi.Version3_1, openapi.JSONSchemaDialect202012)
}