Skip to content

Commit

Permalink
WIP - mostly passing, except for now incorrect $ref factory
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Apr 13, 2018
1 parent 59d4185 commit 2418a02
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 75 deletions.
4 changes: 2 additions & 2 deletions lib/json_matchers/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

module JsonMatchers
class Assertion
def initialize(schema_name, **options)
def initialize(schema_name)
@schema_name = schema_name
@schema_path = JsonMatchers.path_to_schema(schema_name)
@matcher = Matcher.new(schema_path, options)
@matcher = Matcher.new(schema_path)
end

def valid?(json)
Expand Down
4 changes: 2 additions & 2 deletions lib/json_matchers/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module JsonMatchers
self.schema_root = File.join("spec", "support", "api", "schemas")
end

RSpec::Matchers.define :match_json_schema do |schema_name, **options|
assertion = JsonMatchers::Assertion.new(schema_name.to_s, options)
RSpec::Matchers.define :match_json_schema do |schema_name|
assertion = JsonMatchers::Assertion.new(schema_name.to_s)

match do |json|
assertion.valid?(json)
Expand Down
90 changes: 20 additions & 70 deletions spec/json_matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@
end

it "supports $ref" do
create_schema("user", {
create(:schema, {
name: "user",
"id": "file:/user.json#",
"type": "object",
"required": ["id"],
Expand All @@ -205,7 +206,7 @@
"address": { "type": "string" },
},
})
create_schema("users/index", {
schema = create(:schema, {
"id": "file:/users/index.json#",
"type": "object",
"definitions": {
Expand All @@ -220,89 +221,38 @@
"properties": { "users": { "$ref": "#/definitions/users" } },
})

valid_response = response_for({
valid_response = build(:response, {
"users": [{ "id": 1, "name": "Me!", "address": "Here!" }],
})
invalid_response = response_for({
invalid_response = build(:response, {
"users": [{ "id": "invalid", "name": "You!", "address": "There!" }],
})

expect(valid_response).to match_response_schema("users/index")
expect(invalid_response).not_to match_response_schema("users/index")
expect(valid_response).to match_json_schema(schema)
expect(invalid_response).not_to match_json_schema(schema)
end

it "supports the 'id' keyword" do
create_schema("top-level-schema", {
create(:schema, {
name: "nested",
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/nested.json#",
"type": "object",
"properties": {
"a": { "$ref": "file:/#{JsonMatchers.schema_root}/nested.json#" },
},
"required": ["b"],
"properties": { "b": { "type": "string" } },
})
create_schema("nested-schema", {
schema = create(:schema, {
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/#{JsonMatchers.schema_root}/nested.json#",
"type": "object",
"required": ["b"],
"properties": { "b": { "type": "string" } },
"properties": {
"a": { "$ref": "file:/nested.json#" },
},
})
response_json = { a: { b: "foo" } }
invalid_response_json = { a: { b: 4 } }
response_json = build(:response, { "a": { "b": "foo" } })
invalid_response_json = build(:response, { "a": { "b": 4 } })

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
it "forwards options to the validator" do
schema = create(:schema, :object)

matching_json = build(:response, :object)
invalid_json = build(:response, { "id": 1, "title": "bar" })

expect(matching_json).to match_json_schema(schema, strict: true)
expect(invalid_json).not_to match_json_schema(schema, strict: true)
end
end

context "when options are configured globally" do
it "forwards them to the validator" do
with_options(strict: true) do
schema = create(:schema, :object)

matching_json = build(:response, :object)
invalid_json = build(:response, { "id": 1, "title": "bar" })

expect(matching_json).to match_json_schema(schema)
expect(invalid_json).not_to match_json_schema(schema)
end
end

context "when configured to record errors" do
it "includes the reasons for failure in the exception's message" do
with_options(record_errors: true) do
schema = create(:schema, {
"type": "object",
"properties": {
"username": {
"allOf": [
{ "type": "string" },
{ "minLength": 5 },
],
},
},
})

invalid_json = build(:response, { "username": "foo" })

expect {
expect(invalid_json).to match_json_schema(schema)
}.to raise_error(/minimum/)
end
end
end
expect(response_json).to match_json_schema(schema)
expect(invalid_response_json).not_to match_json_schema(schema)
end

def raise_error_containing(schema_or_body)
Expand Down
2 changes: 1 addition & 1 deletion spec/support/file_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "json_schema"
require "pathname"
require "fileutils"

module FileHelpers
def setup_fixtures(*pathnames)
JSON::Validator.clear_cache
original_schema_root = JsonMatchers.schema_root

JsonMatchers.schema_root = File.join(*pathnames)
Expand Down

0 comments on commit 2418a02

Please sign in to comment.