forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension_test.go
49 lines (46 loc) · 1.26 KB
/
extension_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
package jsonschema_test
import (
"strings"
"testing"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func TestPowerOfExt(t *testing.T) {
t.Run("invalidSchema", func(t *testing.T) {
c := jsonschema.NewCompiler()
c.RegisterExtension("powerOf", powerOfMeta, powerOfCompiler{})
if err := c.AddResource("test.json", strings.NewReader(`{"powerOf": "hello"}`)); err != nil {
t.Fatal(err)
}
_, err := c.Compile("test.json")
if err == nil {
t.Fatal("error expected")
}
t.Log(err)
})
t.Run("validSchema", func(t *testing.T) {
c := jsonschema.NewCompiler()
c.RegisterExtension("powerOf", powerOfMeta, powerOfCompiler{})
if err := c.AddResource("test.json", strings.NewReader(`{"powerOf": 10}`)); err != nil {
t.Fatal(err)
}
sch, err := c.Compile("test.json")
if err != nil {
t.Fatal(err)
}
t.Run("validInstance", func(t *testing.T) {
if err := sch.Validate(100); err != nil {
t.Fatal(err)
}
})
t.Run("invalidInstance", func(t *testing.T) {
if err := sch.Validate(111); err == nil {
t.Fatal("validation must fail")
} else {
t.Logf("%#v", err)
if !strings.Contains(err.(*jsonschema.ValidationError).GoString(), "111 not powerOf 10") {
t.Fatal("validation error expected to contain powerOf message")
}
}
})
})
}