diff --git a/json_matchers.gemspec b/json_matchers.gemspec index 82388d3..590cf3e 100644 --- a/json_matchers.gemspec +++ b/json_matchers.gemspec @@ -18,7 +18,8 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_dependency("json-schema", "~> 2.7") + spec.add_dependency("json_schema") + spec.add_dependency("activesupport", '>= 3.0.0') spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "pry" diff --git a/lib/json_matchers/matcher.rb b/lib/json_matchers/matcher.rb index e91f0c3..b781e3d 100644 --- a/lib/json_matchers/matcher.rb +++ b/lib/json_matchers/matcher.rb @@ -1,4 +1,4 @@ -require "json-schema" +require "json_schema" require "json_matchers/validator" module JsonMatchers @@ -9,16 +9,22 @@ def initialize(schema_path, options = {}) end def matches?(response) - validator = build_validator(response) - - self.errors = validator.validate! - - errors.empty? - rescue JSON::Schema::ValidationError => error - self.errors = [error.message] - false - rescue JSON::Schema::JsonParseError - raise InvalidSchemaError + begin + add_schemata_to_document_store + schema_data = JSON.parse(File.read(@schema_path.to_s)) + response_body = JSON.parse(@response.body) + json_schema = JsonSchema.parse!(schema_data) + + json_schema.expand_references!(store: document_store) + json_schema.validate!(response_body) + rescue RuntimeError => error + self.errors = [error.message] + return false + rescue JsonSchema::SchemaError, JSON::ParserError => error + raise InvalidSchemaError.new(error) + end + + true end def validation_failure_message @@ -27,19 +33,19 @@ def validation_failure_message private - attr_reader :schema_path, :options attr_accessor :errors + attr_reader :schema_path, :options - def default_options - JsonMatchers.configuration.options || {} + def add_schemata_to_document_store + Dir.glob("#{JsonMatchers.schema_root}/**/*.json").each do |path| + schema_data = JSON.parse(File.read(path)) + extra_schema = JsonSchema.parse!(schema_data) + document_store.add_schema(extra_schema) + end end - def build_validator(response) - Validator.new( - options: options, - response: response, - schema_path: schema_path, - ) + def document_store + @document_store ||= JsonSchema::DocumentStore.new end end end diff --git a/spec/json_matchers/match_response_schema_spec.rb b/spec/json_matchers/match_response_schema_spec.rb index e68ea10..6b33e43 100644 --- a/spec/json_matchers/match_response_schema_spec.rb +++ b/spec/json_matchers/match_response_schema_spec.rb @@ -14,10 +14,10 @@ end it "fails when the body is missing a required property" do - create_schema("foo_schema", + create_schema("foo_schema", { "type": "object", "required": ["foo"], - ) + }) expect(response_for({})).not_to match_response_schema("foo_schema") end @@ -31,11 +31,13 @@ ], "properties": { "id": { "type": "number" }, + "title": {"type": "string"}, }, "additionalProperties": false, }) - expect({ "id": 1 }).to match_response_schema("foo_schema") + expect(response_for({ "id": 1, "title": "bar" })). + to match_response_schema("foo_schema") end it "fails with message when negated" do @@ -177,23 +179,60 @@ end it "supports $ref" do - create_schema("single", { + create_schema("user", { + "id": "file:/#{JsonMatchers.schema_root}/user.json#", "type": "object", - "required": ["foo"], + "required": ["id"], "properties": { - "foo": { "type": "string" }, + "id": { + "type": "integer" + } } }) - create_schema("collection", { - "type": "array", - "items": { "$ref": "single.json" }, + create_schema("users", { + "id": "file:/#{JsonMatchers.schema_root}/users.json#", + "type": "object", + "definitions": { + "users": { + "description": "A collection of users", + "example": [{ "id": "1" }], + "type": "array", + "items": { "$ref": "file:/#{JsonMatchers.schema_root}/user.json#" } + } + }, + "required": ["users"], + "properties": { "users": { "$ref": "#/definitions/users" } } }) - valid_response = response_for([{ "foo": "is a string" }]) - invalid_response = response_for([{ "foo": 0 }]) + valid_response = response_for({ "users": [{ "id": 1 }] }) + invalid_response = response_for({ "users": [{ "id": "invalid" }]}) + + expect(valid_response).to match_response_schema("users") + expect(invalid_response).not_to match_response_schema("users") + end + + it "supports the 'id' keyword" do + create_schema("top-level-schema", { + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "a": { "$ref": "file:/#{JsonMatchers.schema_root}/nested.json#" } + } + }) + create_schema("nested-schema", { + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "file:/#{JsonMatchers.schema_root}/nested.json#", + "type": "object", + "required": ["b"], + "properties": { "b": { "type": "string" } }, + }) + response_json = { a: { b: "foo" } } + invalid_response_json = { a: { b: 4 } } - expect(valid_response).to match_response_schema("collection") - expect(invalid_response).not_to match_response_schema("collection") + expect(response_for(response_json)). + to match_response_schema("top-level-schema") + expect(response_for(invalid_response_json)). + not_to match_response_schema("top-level-schema") end context "when options are passed directly to the matcher" do