-
Notifications
You must be signed in to change notification settings - Fork 54
/
schema.go
359 lines (309 loc) · 8.51 KB
/
schema.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package jsonschema
import (
"context"
"encoding/json"
"fmt"
"net/url"
"sort"
"strings"
jptr "github.com/qri-io/jsonpointer"
)
// Must turns a JSON string into a *Schema, panicing if parsing fails.
// Useful for declaring Schemas in Go code.
func Must(jsonString string) *Schema {
s := &Schema{}
if err := s.UnmarshalJSON([]byte(jsonString)); err != nil {
panic(err)
}
return s
}
type schemaType int
const (
schemaTypeObject schemaType = iota
schemaTypeFalse
schemaTypeTrue
)
// Schema is the top-level structure defining a json schema
type Schema struct {
schemaType schemaType
docPath string
hasRegistered bool
id string
extraDefinitions map[string]json.RawMessage
keywords map[string]Keyword
orderedkeywords []string
}
// NewSchema allocates a new Schema Keyword/Validator
func NewSchema() Keyword {
return &Schema{}
}
// HasKeyword is a utility function for checking if the given schema
// has an instance of the required keyword
func (s *Schema) HasKeyword(key string) bool {
_, ok := s.keywords[key]
return ok
}
// Register implements the Keyword interface for Schema
func (s *Schema) Register(uri string, registry *SchemaRegistry) {
schemaDebug("[Schema] Register")
if s.hasRegistered {
return
}
s.hasRegistered = true
registry.RegisterLocal(s)
// load default keyset if no other is present
globalRegistry, release := getGlobalKeywordRegistry()
globalRegistry.DefaultIfEmpty()
release()
address := s.id
if uri != "" && address != "" {
address, _ = SafeResolveURL(uri, address)
}
if s.docPath == "" && address != "" && address[0] != '#' {
docURI := ""
if u, err := url.Parse(address); err != nil {
docURI, _ = SafeResolveURL("https://qri.io", address)
} else {
docURI = u.String()
}
s.docPath = docURI
GetSchemaRegistry().Register(s)
uri = docURI
}
for _, keyword := range s.keywords {
keyword.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for Schema
func (s *Schema) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer.IsEmpty() {
if s.docPath != "" {
s.docPath, _ = SafeResolveURL(uri, s.docPath)
} else {
s.docPath = uri
}
return s
}
current := pointer.Head()
if s.id != "" {
if u, err := url.Parse(s.id); err == nil {
if u.IsAbs() {
uri = s.id
} else {
uri, _ = SafeResolveURL(uri, s.id)
}
}
}
keyword := s.keywords[*current]
var keywordSchema *Schema
if keyword != nil {
keywordSchema = keyword.Resolve(pointer.Tail(), uri)
}
if keywordSchema != nil {
return keywordSchema
}
found, err := pointer.Eval(s.extraDefinitions)
if err != nil {
return nil
}
if found == nil {
return nil
}
if foundSchema, ok := found.(*Schema); ok {
return foundSchema
}
return nil
}
// JSONProp implements the JSONPather for Schema
func (s Schema) JSONProp(name string) interface{} {
if keyword, ok := s.keywords[name]; ok {
return keyword
}
return s.extraDefinitions[name]
}
// JSONChildren implements the JSONContainer interface for Schema
func (s Schema) JSONChildren() map[string]JSONPather {
ch := map[string]JSONPather{}
if s.keywords != nil {
for key, val := range s.keywords {
if jp, ok := val.(JSONPather); ok {
ch[key] = jp
}
}
}
return ch
}
// _schema is an internal struct for encoding & decoding purposes
type _schema struct {
ID string `json:"$id,omitempty"`
}
// UnmarshalJSON implements the json.Unmarshaler interface for Schema
func (s *Schema) UnmarshalJSON(data []byte) error {
var b bool
if err := json.Unmarshal(data, &b); err == nil {
if b {
// boolean true Always passes validation, as if the empty schema {}
*s = Schema{schemaType: schemaTypeTrue}
return nil
}
// boolean false Always fails validation, as if the schema { "not":{} }
*s = Schema{schemaType: schemaTypeFalse}
return nil
}
keywordRegistry := copyGlobalKeywordRegistry()
keywordRegistry.DefaultIfEmpty()
_s := _schema{}
if err := json.Unmarshal(data, &_s); err != nil {
return err
}
sch := &Schema{
id: _s.ID,
keywords: map[string]Keyword{},
}
valprops := map[string]json.RawMessage{}
if err := json.Unmarshal(data, &valprops); err != nil {
return err
}
for prop, rawmsg := range valprops {
var keyword Keyword
if keywordRegistry.IsRegisteredKeyword(prop) {
keyword = keywordRegistry.GetKeyword(prop)
} else if keywordRegistry.IsNotSupportedKeyword(prop) {
schemaDebug(fmt.Sprintf("[Schema] WARN: '%s' is not supported and will be ignored\n", prop))
continue
} else {
if sch.extraDefinitions == nil {
sch.extraDefinitions = map[string]json.RawMessage{}
}
sch.extraDefinitions[prop] = rawmsg
continue
}
if _, ok := keyword.(*Void); !ok {
if err := json.Unmarshal(rawmsg, keyword); err != nil {
return fmt.Errorf("error unmarshaling %s from json: %s", prop, err.Error())
}
}
sch.keywords[prop] = keyword
}
// ensures proper and stable keyword validation order
keyOrders := make([]_keyOrder, len(sch.keywords))
i := 0
for k := range sch.keywords {
keyOrders[i] = _keyOrder{
Key: k,
Order: keywordRegistry.GetKeywordOrder(k),
}
i++
}
sort.SliceStable(keyOrders, func(i, j int) bool {
if keyOrders[i].Order == keyOrders[j].Order {
return keywordRegistry.GetKeywordInsertOrder(keyOrders[i].Key) < keywordRegistry.GetKeywordInsertOrder(keyOrders[j].Key)
}
return keyOrders[i].Order < keyOrders[j].Order
})
orderedKeys := make([]string, len(sch.keywords))
i = 0
for _, keyOrder := range keyOrders {
orderedKeys[i] = keyOrder.Key
i++
}
sch.orderedkeywords = orderedKeys
*s = Schema(*sch)
return nil
}
// _keyOrder is an internal struct assigning evaluation order of keywords
type _keyOrder struct {
Key string
Order int
}
// Validate initiates a fresh validation state and triggers the evaluation
func (s *Schema) Validate(ctx context.Context, data interface{}) *ValidationState {
currentState := NewValidationState(s)
s.ValidateKeyword(ctx, currentState, data)
return currentState
}
// ValidateKeyword uses the schema to check an instance, collecting validation
// errors in a slice
func (s *Schema) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Schema] Validating")
if s == nil {
currentState.AddError(data, fmt.Sprintf("schema is nil"))
return
}
if s.schemaType == schemaTypeTrue {
return
}
if s.schemaType == schemaTypeFalse {
currentState.AddError(data, fmt.Sprintf("schema is always false"))
return
}
s.Register("", currentState.LocalRegistry)
currentState.LocalRegistry.RegisterLocal(s)
currentState.Local = s
refKeyword := s.keywords["$ref"]
if refKeyword == nil {
if currentState.BaseURI == "" {
currentState.BaseURI = s.docPath
} else if s.docPath != "" {
if u, err := url.Parse(s.docPath); err == nil {
if u.IsAbs() {
currentState.BaseURI = s.docPath
} else {
currentState.BaseURI, _ = SafeResolveURL(currentState.BaseURI, s.docPath)
}
}
}
}
if currentState.BaseURI != "" && strings.HasSuffix(currentState.BaseURI, "#") {
currentState.BaseURI = strings.TrimRight(currentState.BaseURI, "#")
}
// TODO(arqu): only on versions bellow draft2019_09
// if refKeyword != nil {
// refKeyword.ValidateKeyword(currentState, errs)
// return
// }
s.validateSchemakeywords(ctx, currentState, data)
}
// validateSchemakeywords triggers validation of sub schemas and keywords
func (s *Schema) validateSchemakeywords(ctx context.Context, currentState *ValidationState, data interface{}) {
if s.keywords != nil {
for _, keyword := range s.orderedkeywords {
s.keywords[keyword].ValidateKeyword(ctx, currentState, data)
}
}
}
// ValidateBytes performs schema validation against a slice of json
// byte data
func (s *Schema) ValidateBytes(ctx context.Context, data []byte) ([]KeyError, error) {
var doc interface{}
if err := json.Unmarshal(data, &doc); err != nil {
return nil, fmt.Errorf("error parsing JSON bytes: %w", err)
}
vs := s.Validate(ctx, doc)
return *vs.Errs, nil
}
// TopLevelType returns a string representing the schema's top-level type.
func (s *Schema) TopLevelType() string {
if t, ok := s.keywords["type"].(*Type); ok {
return t.String()
}
return "unknown"
}
// MarshalJSON implements the json.Marshaler interface for Schema
func (s Schema) MarshalJSON() ([]byte, error) {
switch s.schemaType {
case schemaTypeFalse:
return []byte("false"), nil
case schemaTypeTrue:
return []byte("true"), nil
default:
obj := map[string]interface{}{}
for k, v := range s.keywords {
obj[k] = v
}
for k, v := range s.extraDefinitions {
obj[k] = v
}
return json.Marshal(obj)
}
}