-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_builder_test.go
65 lines (52 loc) · 1.37 KB
/
schema_builder_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
package jsonschema_test
import (
"fmt"
"reflect"
"testing"
"github.com/aacebo/jsonschema"
)
func TestSchemaBuilder(t *testing.T) {
t.Run("custom", func(t *testing.T) {
jsonschema.AddKeyword("test", jsonschema.Keyword{
Compile: func(ns *jsonschema.Namespace, ctx jsonschema.Context, config reflect.Value) []jsonschema.SchemaError {
errs := []jsonschema.SchemaError{}
if config.Kind() != reflect.String {
errs = append(errs, jsonschema.SchemaError{
Path: ctx.Path,
Keyword: "test",
Message: `must be a "string"`,
})
}
return errs
},
Validate: func(ns *jsonschema.Namespace, ctx jsonschema.Context, config, value reflect.Value) []jsonschema.SchemaError {
errs := []jsonschema.SchemaError{}
if value.Kind() != reflect.String {
return errs
}
if value.String() != "helloworld" {
errs = append(errs, jsonschema.SchemaError{
Path: ctx.Path,
Keyword: "test",
Message: fmt.Sprintf(`must be "%s"`, config.String()),
})
}
return errs
},
})
t.Run("should succeed", func(t *testing.T) {
schema := jsonschema.Builder().
String().
Build()
schema["test"] = "helloworld"
errs := jsonschema.Compile(schema)
if len(errs) > 0 {
t.Error(errs)
}
errs = jsonschema.Validate(schema, "helloworld")
if len(errs) > 0 {
t.Error(errs)
}
})
})
}