Skip to content

Commit

Permalink
all passing !
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Apr 13, 2018
1 parent 2418a02 commit c0b99ea
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
10 changes: 5 additions & 5 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@
trait :object do
json do
{
"id": "file:/#{name}.json#",
"type": "object",
"required": [
"id",
],
"required": ["id"],
"properties": {
"id": { "type": "number" },
"id": { "type": "integer" },
},
"additionalProperties": false,
}
Expand All @@ -65,8 +64,9 @@

initialize_with do
FakeSchema.new(name, {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": { "$ref": "#{items.name}.json" },
"items": { "$ref": "file:/#{items.name}.json#" },
})
end
end
Expand Down
18 changes: 0 additions & 18 deletions spec/json_matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,6 @@
end
end

it "validates against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :object)
json_as_array = [json.to_h]

expect(json_as_array).to match_json_schema(schema)
end

it "fails against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :invalid_object)
json_as_array = [json.to_h]

expect(json_as_array).not_to match_json_schema(schema)
end

it "supports $ref" do
create(:schema, {
name: "user",
Expand Down
75 changes: 59 additions & 16 deletions test/json_matchers/minitest/assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,65 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
end
end

test "asserts valid JSON against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :object)
json_as_array = [json.to_h]

assert_matches_json_schema(json_as_array, schema)
end

test "refutes valid JSON against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :invalid_object)
json_as_array = [json.to_h]

refute_matches_json_schema(json_as_array, schema)
test "supports $ref" do
create(:schema, {
name: "user",
"id": "file:/user.json#",
"type": "object",
"required": ["id"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"address": { "type": "string" },
},
})
schema = create(:schema, {
"id": "file:/users/index.json#",
"type": "object",
"definitions": {
"users": {
"description": "A collection of users",
"example": [{ "id": "1" }],
"type": "array",
"items": { "$ref": "file:/user.json#" },
},
},
"required": ["users"],
"properties": { "users": { "$ref": "#/definitions/users" } },
})

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

assert_matches_json_schema(valid_response, schema)
refute_matches_json_schema(invalid_response, schema)
end

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

assert_matches_json_schema(response_json, schema)
refute_matches_json_schema(invalid_response_json, schema)
end

def assert_raises_error_containing(schema_or_body)
Expand Down

0 comments on commit c0b99ea

Please sign in to comment.