forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
193 lines (165 loc) · 4.11 KB
/
example_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package jsonschema_test
import (
"encoding/hex"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func Example() {
sch, err := jsonschema.Compile("testdata/person_schema.json")
if err != nil {
log.Fatalf("%#v", err)
}
data, err := os.ReadFile("testdata/person.json")
if err != nil {
log.Fatal(err)
}
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
log.Fatal(err)
}
if err = sch.Validate(v); err != nil {
log.Fatalf("%#v", err)
}
// Output:
}
// Example_fromString shows how to load schema from string.
func Example_fromString() {
schema := `{"type": "object"}`
instance := `{"foo": "bar"}`
sch, err := jsonschema.CompileString("schema.json", schema)
if err != nil {
log.Fatalf("%#v", err)
}
var v interface{}
if err := json.Unmarshal([]byte(instance), &v); err != nil {
log.Fatal(err)
}
if err = sch.Validate(v); err != nil {
log.Fatalf("%#v", err)
}
// Output:
}
// Example_fromStrings shows how to load schema from more than one string.
func Example_fromStrings() {
c := jsonschema.NewCompiler()
if err := c.AddResource("main.json", strings.NewReader(`{"$ref":"obj.json"}`)); err != nil {
log.Fatal(err)
}
if err := c.AddResource("obj.json", strings.NewReader(`{"type":"object"}`)); err != nil {
log.Fatal(err)
}
sch, err := c.Compile("main.json")
if err != nil {
log.Fatalf("%#v", err)
}
var v interface{}
if err := json.Unmarshal([]byte("{}"), &v); err != nil {
log.Fatal(err)
}
if err = sch.Validate(v); err != nil {
log.Fatalf("%#v", err)
}
// Output:
}
// Example_userDefinedFormat shows how to define 'odd-number' format.
func Example_userDefinedFormat() {
c := jsonschema.NewCompiler()
c.AssertFormat = true
c.Formats["odd-number"] = func(v interface{}) bool {
switch v := v.(type) {
case json.Number, float32, float64, int, int8, int32, int64, uint, uint8, uint32, uint64:
n, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64)
return n%2 != 0
default:
return true
}
}
schema := `{
"type": "integer",
"format": "odd-number"
}`
instance := 5
if err := c.AddResource("schema.json", strings.NewReader(schema)); err != nil {
log.Fatalf("%v", err)
}
sch, err := c.Compile("schema.json")
if err != nil {
log.Fatalf("%#v", err)
}
if err = sch.Validate(instance); err != nil {
log.Fatalf("%#v", err)
}
// Output:
}
// Example_userDefinedContent shows how to define
// "hex" contentEncoding and "application/xml" contentMediaType
func Example_userDefinedContent() {
c := jsonschema.NewCompiler()
c.AssertContent = true
c.Decoders["hex"] = hex.DecodeString
c.MediaTypes["application/xml"] = func(b []byte) error {
return xml.Unmarshal(b, new(interface{}))
}
schema := `{
"type": "object",
"properties": {
"xml" : {
"type": "string",
"contentEncoding": "hex",
"contentMediaType": "application/xml"
}
}
}`
instance := `{"xml": "3c726f6f742f3e"}`
if err := c.AddResource("schema.json", strings.NewReader(schema)); err != nil {
log.Fatalf("%v", err)
}
sch, err := c.Compile("schema.json")
if err != nil {
log.Fatalf("%#v", err)
}
var v interface{}
if err := json.Unmarshal([]byte(instance), &v); err != nil {
log.Fatal(err)
}
if err = sch.Validate(v); err != nil {
log.Fatalf("%#v", err)
}
// Output:
}
// Example_userDefinedLoader shows how to define custom schema loader.
//
// we are implementing a "map" protocol which servers schemas from
// go map variable.
func Example_userDefinedLoader() {
var schemas = map[string]string{
"main.json": `{"$ref":"obj.json"}`,
"obj.json": `{"type":"object"}`,
}
jsonschema.Loaders["map"] = func(url string) (io.ReadCloser, error) {
schema, ok := schemas[strings.TrimPrefix(url, "map:///")]
if !ok {
return nil, fmt.Errorf("%q not found", url)
}
return io.NopCloser(strings.NewReader(schema)), nil
}
sch, err := jsonschema.Compile("map:///main.json")
if err != nil {
log.Fatalf("%+v", err)
}
var v interface{}
if err := json.Unmarshal([]byte("{}"), &v); err != nil {
log.Fatal(err)
}
if err = sch.Validate(v); err != nil {
log.Fatalf("%#v", err)
}
// Output:
}