-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathschema.go
316 lines (279 loc) · 11.5 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
// This file is part of arduino-check.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-check.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
// Package schema contains code for working with JSON schema.
package schema
import (
"encoding/json"
"fmt"
"net/url"
"path"
"path/filepath"
"regexp"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/ory/jsonschema/v3"
"github.com/sirupsen/logrus"
"github.com/xeipuuv/gojsonreference"
)
// Compile compiles the schema files specified by the filename arguments and returns the compiled schema.
func Compile(schemaFilename string, referencedSchemaFilenames []string, schemasPath *paths.Path) *jsonschema.Schema {
compiler := jsonschema.NewCompiler()
// Load the referenced schemas.
for _, referencedSchemaFilename := range referencedSchemaFilenames {
if err := loadReferencedSchema(compiler, referencedSchemaFilename, schemasPath); err != nil {
panic(err)
}
}
// Compile the schema.
schemaPath := schemasPath.Join(schemaFilename)
schemaURI := pathURI(schemaPath)
compiledSchema, err := compiler.Compile(schemaURI)
if err != nil {
panic(err)
}
return compiledSchema
}
// Validate validates an instance against a JSON schema and returns nil if it was success, or the
// jsonschema.ValidationError object otherwise.
func Validate(instanceObject *properties.Map, schemaObject *jsonschema.Schema, schemasPath *paths.Path) *jsonschema.ValidationError {
// Convert the instance data from the native properties.Map type to the interface type required by the schema
// validation package.
instanceObjectMap := instanceObject.AsMap()
instanceInterface := make(map[string]interface{}, len(instanceObjectMap))
for k, v := range instanceObjectMap {
instanceInterface[k] = v
}
validationError := schemaObject.ValidateInterface(instanceInterface)
result, _ := validationError.(*jsonschema.ValidationError)
if result == nil {
logrus.Debug("Schema validation of instance document passed")
} else {
logrus.Debug("Schema validation of instance document failed:")
logValidationError(result, schemasPath)
logrus.Trace("-----------------------------------------------")
}
return result
}
// RequiredPropertyMissing returns whether the given required property is missing from the document.
func RequiredPropertyMissing(propertyName string, validationResult *jsonschema.ValidationError, schemasPath *paths.Path) bool {
return ValidationErrorMatch("#", "/required$", "", "^#/"+propertyName+"$", validationResult, schemasPath)
}
// PropertyPatternMismatch returns whether the given property did not match the regular expression defined in the JSON schema.
func PropertyPatternMismatch(propertyName string, validationResult *jsonschema.ValidationError, schemasPath *paths.Path) bool {
return ValidationErrorMatch("#/"+propertyName, "/pattern$", "", "", validationResult, schemasPath)
}
// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error.
// See: https://godoc.org/github.com/ory/jsonschema#ValidationError
func ValidationErrorMatch(
instancePointerQuery,
schemaPointerQuery,
schemaPointerValueQuery,
failureContextQuery string,
validationResult *jsonschema.ValidationError,
schemasPath *paths.Path,
) bool {
if validationResult == nil {
// No error, so nothing to match
logrus.Trace("Schema validation passed. No match is possible.")
return false
}
instancePointerRegexp := regexp.MustCompile(instancePointerQuery)
schemaPointerRegexp := regexp.MustCompile(schemaPointerQuery)
schemaPointerValueRegexp := regexp.MustCompile(schemaPointerValueQuery)
failureContextRegexp := regexp.MustCompile(failureContextQuery)
return validationErrorMatch(
instancePointerRegexp,
schemaPointerRegexp,
schemaPointerValueRegexp,
failureContextRegexp,
validationResult,
schemasPath)
}
// loadReferencedSchema adds a schema that is referenced by the parent schema to the compiler object.
func loadReferencedSchema(compiler *jsonschema.Compiler, schemaFilename string, schemasPath *paths.Path) error {
schemaPath := schemasPath.Join(schemaFilename)
schemaFile, err := schemaPath.Open()
if err != nil {
return err
}
defer schemaFile.Close()
// Get the $id value from the schema to use as the `url` argument for the `compiler.AddResource()` call.
id, err := schemaID(schemaFilename, schemasPath)
if err != nil {
return err
}
return compiler.AddResource(id, schemaFile)
}
// schemaID returns the value of the schema's $id key.
func schemaID(schemaFilename string, schemasPath *paths.Path) (string, error) {
schemaPath := schemasPath.Join(schemaFilename)
schemaInterface := unmarshalJSONFile(schemaPath)
id, ok := schemaInterface.(map[string]interface{})["$id"].(string)
if !ok {
return "", fmt.Errorf("Schema %s is missing an $id keyword", schemaPath)
}
return id, nil
}
// unmarshalJSONFile returns the data from a JSON file.
func unmarshalJSONFile(filePath *paths.Path) interface{} {
fileBuffer, err := filePath.ReadFile()
if err != nil {
panic(err)
}
var dataInterface interface{}
if err := json.Unmarshal(fileBuffer, &dataInterface); err != nil {
panic(err)
}
return dataInterface
}
// compile compiles the parent schema and returns the resulting jsonschema.Schema object.
func compile(compiler *jsonschema.Compiler, schemaFilename string, schemasPath *paths.Path) (*jsonschema.Schema, error) {
schemaPath := schemasPath.Join(schemaFilename)
schemaURI := pathURI(schemaPath)
return compiler.Compile(schemaURI)
}
// pathURI returns the URI representation of the path argument.
func pathURI(path *paths.Path) string {
absolutePath, err := path.Abs()
if err != nil {
panic(err)
}
uriFriendlyPath := filepath.ToSlash(absolutePath.String())
// In order to be valid, the path in the URI must start with `/`, but Windows paths do not.
if uriFriendlyPath[0] != '/' {
uriFriendlyPath = "/" + uriFriendlyPath
}
pathURI := url.URL{
Scheme: "file",
Path: uriFriendlyPath,
}
return pathURI.String()
}
// logValidationError logs the schema validation error data
func logValidationError(validationError *jsonschema.ValidationError, schemasPath *paths.Path) {
logrus.Trace("--------Schema validation failure cause--------")
logrus.Tracef("Error message: %s", validationError.Error())
logrus.Tracef("Instance pointer: %v", validationError.InstancePtr)
logrus.Tracef("Schema URL: %s", validationError.SchemaURL)
logrus.Tracef("Schema pointer: %s", validationError.SchemaPtr)
logrus.Tracef("Schema pointer value: %v", schemaPointerValue(validationError, schemasPath))
logrus.Tracef("Failure context: %v", validationError.Context)
logrus.Tracef("Failure context type: %T", validationError.Context)
// Recursively log all causes.
for _, validationErrorCause := range validationError.Causes {
logValidationError(validationErrorCause, schemasPath)
}
}
// schemaPointerValue returns the object identified by the given JSON pointer from the schema file.
func schemaPointerValue(validationError *jsonschema.ValidationError, schemasPath *paths.Path) interface{} {
schemaPath := schemasPath.Join(path.Base(validationError.SchemaURL))
return jsonPointerValue(validationError.SchemaPtr, schemaPath)
}
// jsonPointerValue returns the object identified by the given JSON pointer from the JSON file.
func jsonPointerValue(jsonPointer string, filePath *paths.Path) interface{} {
jsonReference, err := gojsonreference.NewJsonReference(jsonPointer)
if err != nil {
panic(err)
}
jsonInterface := unmarshalJSONFile(filePath)
jsonPointerValue, _, err := jsonReference.GetPointer().Get(jsonInterface)
if err != nil {
panic(err)
}
return jsonPointerValue
}
func validationErrorMatch(
instancePointerRegexp,
schemaPointerRegexp,
schemaPointerValueRegexp,
failureContextRegexp *regexp.Regexp,
validationError *jsonschema.ValidationError,
schemasPath *paths.Path,
) bool {
logrus.Trace("--------Checking schema validation failure match--------")
logrus.Tracef("Checking instance pointer: %s match with regexp: %s", validationError.InstancePtr, instancePointerRegexp)
if instancePointerRegexp.MatchString(validationError.InstancePtr) {
logrus.Tracef("Matched!")
logrus.Tracef("Checking schema pointer: %s match with regexp: %s", validationError.SchemaPtr, schemaPointerRegexp)
if schemaPointerRegexp.MatchString(validationError.SchemaPtr) {
logrus.Tracef("Matched!")
if validationErrorSchemaPointerValueMatch(schemaPointerValueRegexp, validationError, schemasPath) {
logrus.Tracef("Matched!")
logrus.Tracef("Checking failure context: %v match with regexp: %s", validationError.Context, failureContextRegexp)
if validationErrorContextMatch(failureContextRegexp, validationError) {
logrus.Tracef("Matched!")
return true
}
}
}
}
// Recursively check all causes for a match.
for _, validationErrorCause := range validationError.Causes {
if validationErrorMatch(
instancePointerRegexp,
schemaPointerRegexp,
schemaPointerValueRegexp,
failureContextRegexp,
validationErrorCause,
schemasPath,
) {
return true
}
}
return false
}
// validationErrorSchemaPointerValueMatch marshalls the data in the schema at the given JSON pointer and returns whether
// it matches against the given regular expression.
func validationErrorSchemaPointerValueMatch(
schemaPointerValueRegexp *regexp.Regexp,
validationError *jsonschema.ValidationError,
schemasPath *paths.Path,
) bool {
marshalledSchemaPointerValue, err := json.Marshal(schemaPointerValue(validationError, schemasPath))
logrus.Tracef("Checking schema pointer value: %s match with regexp: %s", marshalledSchemaPointerValue, schemaPointerValueRegexp)
if err != nil {
panic(err)
}
return schemaPointerValueRegexp.Match(marshalledSchemaPointerValue)
}
// validationErrorContextMatch parses the validation error context data and returns whether it matches against the given
// regular expression.
func validationErrorContextMatch(failureContextRegexp *regexp.Regexp, validationError *jsonschema.ValidationError) bool {
// This was added in the github.com/ory/jsonschema fork of github.com/santhosh-tekuri/jsonschema
// It currently only provides context about the `required` keyword.
switch contextObject := validationError.Context.(type) {
case nil:
return failureContextRegexp.MatchString("")
case *jsonschema.ValidationErrorContextRequired:
return validationErrorContextRequiredMatch(failureContextRegexp, contextObject)
default:
logrus.Errorf("Unhandled validation error context type: %T", validationError.Context)
return failureContextRegexp.MatchString("")
}
}
// validationErrorContextRequiredMatch returns whether any of the JSON pointers of missing required properties match
// against the given regular expression.
func validationErrorContextRequiredMatch(
failureContextRegexp *regexp.Regexp,
contextObject *jsonschema.ValidationErrorContextRequired,
) bool {
// See: https://godoc.org/github.com/ory/jsonschema#ValidationErrorContextRequired
for _, requiredPropertyPointer := range contextObject.Missing {
if failureContextRegexp.MatchString(requiredPropertyPointer) {
return true
}
}
return false
}