forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 6
/
validation_context_test.go
88 lines (82 loc) · 2.04 KB
/
validation_context_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
package jsonschema_test
import (
"bytes"
"context"
"fmt"
"reflect"
"testing"
"github.com/ory/jsonschema/v3"
)
func TestErrorsContext(t *testing.T) {
for k, tc := range []struct {
path string
doc string
expected interface{}
}{
{
path: "testdata/errors/required.json#/0",
doc: `{}`,
expected: &jsonschema.ValidationErrorContextRequired{Missing: []string{"#/bar"}},
},
{
path: "testdata/errors/required.json#/0",
doc: `{"bar":{}}`,
expected: &jsonschema.ValidationErrorContextRequired{
Missing: []string{"#/bar/foo"},
},
},
{
path: "testdata/errors/required.json#/1",
doc: `{"object":{"object":{"foo":"foo"}}}`,
expected: &jsonschema.ValidationErrorContextRequired{
Missing: []string{"#/object/object/bar"},
},
},
{
path: "testdata/errors/required.json#/1",
doc: `{"object":{"object":{"bar":"bar"}}}`,
expected: &jsonschema.ValidationErrorContextRequired{
Missing: []string{"#/object/object/foo"},
},
},
{
path: "testdata/errors/required.json#/1",
doc: `{"object":{"object":{}}}`,
expected: &jsonschema.ValidationErrorContextRequired{
Missing: []string{"#/object/object/foo", "#/object/object/bar"},
},
},
{
path: "testdata/errors/required.json#/1",
doc: `{"object":{}}`,
expected: &jsonschema.ValidationErrorContextRequired{
Missing: []string{"#/object/object"},
},
},
{
path: "testdata/errors/required.json#/1",
doc: `{}`,
expected: &jsonschema.ValidationErrorContextRequired{
Missing: []string{"#/object"},
},
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
ctx := context.Background()
var (
schema = jsonschema.MustCompile(ctx, tc.path)
err = schema.Validate(bytes.NewBufferString(tc.doc))
)
if err == nil {
t.Errorf("Expected error but got nil")
return
}
var (
actual = err.(*jsonschema.ValidationError).Context
)
if !reflect.DeepEqual(tc.expected, actual) {
t.Errorf("expected:\t%#v\n\tactual:\t%#v", tc.expected, actual)
}
})
}
}