-
-
Notifications
You must be signed in to change notification settings - Fork 12
Generalize JSON schema handling code #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a86a8fc
Generalize JSON schema handling code
per1234 cb43cc2
Add schemas path parameter to schema.Compile()
per1234 14fa848
Determine schemas path on demand
per1234 e47fcda
Move `pathURI()` to the `schema` package
per1234 fb49bcf
Move SchemasPath() to the configuration package
per1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Package schema contains code for working with JSON schema. | ||
package schema | ||
|
||
import ( | ||
"net/url" | ||
"path/filepath" | ||
"regexp" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"github.com/xeipuuv/gojsonschema" | ||
) | ||
|
||
// Compile compiles the schema files specified by the filename arguments and returns the compiled schema. | ||
func Compile(schemaFilename string, referencedSchemaFilenames []string, schemasPath *paths.Path) *gojsonschema.Schema { | ||
schemaLoader := gojsonschema.NewSchemaLoader() | ||
|
||
// Load the referenced schemas. | ||
for _, referencedSchemaFilename := range referencedSchemaFilenames { | ||
referencedSchemaPath := schemasPath.Join(referencedSchemaFilename) | ||
referencedSchemaURI := pathURI(referencedSchemaPath) | ||
err := schemaLoader.AddSchemas(gojsonschema.NewReferenceLoader(referencedSchemaURI)) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
} | ||
|
||
// Compile the schema. | ||
schemaPath := schemasPath.Join(schemaFilename) | ||
schemaURI := pathURI(schemaPath) | ||
compiledSchema, err := schemaLoader.Compile(gojsonschema.NewReferenceLoader(schemaURI)) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
return compiledSchema | ||
} | ||
|
||
// Validate validates an instance against a JSON schema and returns the gojsonschema.Result object. | ||
silvanocerza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func Validate(instanceObject interface{}, schemaObject *gojsonschema.Schema) *gojsonschema.Result { | ||
result, err := schemaObject.Validate(gojsonschema.NewGoLoader(instanceObject)) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// RequiredPropertyMissing returns whether the given required property is missing from the document. | ||
func RequiredPropertyMissing(propertyName string, validationResult *gojsonschema.Result) bool { | ||
return ValidationErrorMatch("required", "(root)", propertyName+" is required", validationResult) | ||
} | ||
|
||
// PropertyPatternMismatch returns whether the given property did not match the regular expression defined in the JSON schema. | ||
func PropertyPatternMismatch(propertyName string, validationResult *gojsonschema.Result) bool { | ||
return ValidationErrorMatch("pattern", propertyName, "", validationResult) | ||
} | ||
|
||
// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error. | ||
// See: https://github.com/xeipuuv/gojsonschema#working-with-errors | ||
func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQueryRegexp string, validationResult *gojsonschema.Result) bool { | ||
if validationResult.Valid() { | ||
// No error, so nothing to match | ||
return false | ||
} | ||
for _, validationError := range validationResult.Errors() { | ||
if typeQuery == "" || typeQuery == validationError.Type() { | ||
if fieldQuery == "" || fieldQuery == validationError.Field() { | ||
descriptionQuery := regexp.MustCompile(descriptionQueryRegexp) | ||
return descriptionQuery.MatchString(validationError.Description()) | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// pathURI returns the URI representation of the path argument. | ||
func pathURI(path *paths.Path) string { | ||
uriFriendlyPath := filepath.ToSlash(path.String()) | ||
pathURI := url.URL{ | ||
Scheme: "file", | ||
Path: uriFriendlyPath, | ||
} | ||
|
||
return pathURI.String() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.