-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
json_schema
instead of json-schema
The problem: * `json_matchers` cannot easily be used concurrently with Heroku's JSON API tools, i.e. `prmd` and `committee`, because `json_matchers` makes different assumptions about the structure of the user's schemata. An example of an incompatibility can be found in #25: `json_matchers` breaks when the `id` property is present within a schema, but the Heroku tools require the presence of the `id` property ([reference](https://github.com/interagent/prmd/blob/master/docs/schemata.md#meta-data)). This is happening because the libraries used to dereference JSON pointers behave differently. `json-schema`, the library we're currently using, appears to conform less strictly to the JSON Schema specification than the library the Heroku tools use, `json_schema`. The solution: * One solution to this problem is to update `json_matchers` to use the same approach to validating schemata as the Heroku tools. This will require the following changes: 1. Use `json_schema` instead of `json-schema` to validate schemata 2. Update documentation to instruct readers to follow Heroku's guidelines for structuring schemata: https://github.com/interagent/prmd/blob/master/docs/schemata.md * In this commit I've replaced `json-schema` with `json_schema` and updated the schemata fixtures in the specs. Per [this json_schema issue](brandur/json_schema#22), in order to dereference JSON pointers referencing schemata in other files we need to access the gem's DocumentStore API directly. This is done in `Matcher#build_and_populate_document_store`.
- Loading branch information
1 parent
48e7620
commit dfba814
Showing
14 changed files
with
218 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,21 @@ | ||
module JsonMatchers | ||
class Parser | ||
def initialize(schema_path) | ||
@schema_path = schema_path | ||
end | ||
|
||
def parse | ||
JsonSchema.parse!(schema_data) | ||
rescue JSON::ParserError, JsonSchema::SchemaError => error | ||
raise InvalidSchemaError.new(error) | ||
end | ||
|
||
private | ||
|
||
attr_reader :schema_path | ||
|
||
def schema_data | ||
JSON.parse(schema_path.read) | ||
end | ||
end | ||
end |
This file contains 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 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 |
---|---|---|
@@ -1,18 +1,34 @@ | ||
require "json-schema" | ||
require "json_matchers/parser" | ||
|
||
module JsonMatchers | ||
class Validator | ||
def initialize(payload:, schema_path:) | ||
@payload = payload | ||
@schema_path = schema_path.to_s | ||
def initialize(document_store:, schema_path:) | ||
@document_store = document_store | ||
@schema_path = schema_path | ||
end | ||
|
||
def validate! | ||
JSON::Validator.fully_validate(schema_path, payload, record_errors: true) | ||
def validate(payload) | ||
json_schema.validate!(payload.as_json) | ||
|
||
[] | ||
rescue JsonSchema::Error => error | ||
[error.message] | ||
end | ||
|
||
private | ||
|
||
attr_reader :payload, :schema_path | ||
attr_reader :document_store, :schema_path | ||
|
||
def json_schema | ||
@json_schema ||= build_json_schema_with_expanded_references | ||
end | ||
|
||
def build_json_schema_with_expanded_references | ||
json_schema = Parser.new(schema_path).parse | ||
|
||
json_schema.expand_references!(store: document_store) | ||
|
||
json_schema | ||
end | ||
end | ||
end |
Oops, something went wrong.