json_matchers 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ FakeResponse = Struct.new(:body) do
3
3
  JSON.parse(body)
4
4
  end
5
5
 
6
- def to_json
6
+ def to_json(*)
7
7
  body
8
8
  end
9
9
  end
@@ -2,7 +2,6 @@ require "fileutils"
2
2
 
3
3
  module FileHelpers
4
4
  def setup_fixtures(*pathnames)
5
- JSON::Validator.clear_cache
6
5
  original_schema_root = JsonMatchers.schema_root
7
6
 
8
7
  JsonMatchers.schema_root = File.join(*pathnames)
@@ -21,34 +21,42 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
21
21
  end
22
22
 
23
23
  test "fails when the body contains a property with the wrong type" do
24
- schema = create(:schema, :object)
24
+ schema = create(:schema, :location)
25
25
 
26
- json = build(:response, :invalid_object)
26
+ json = build(:response, :invalid_location)
27
27
 
28
28
  refute_matches_json_schema(json, schema)
29
29
  end
30
30
 
31
31
  test "fails when the body is missing a required property" do
32
- schema = create(:schema, :object)
32
+ schema = create(:schema, :location)
33
33
 
34
34
  json = build(:response, {})
35
35
 
36
36
  refute_matches_json_schema(json, schema)
37
37
  end
38
38
 
39
+ test "can reference a schema in a directory" do
40
+ create(:schema, :location, name: "api/v1/schema")
41
+
42
+ json = build(:response, :location)
43
+
44
+ assert_matches_json_schema(json, "api/v1/schema")
45
+ end
46
+
39
47
  test "when passed a Hash, validates that the schema matches" do
40
- schema = create(:schema, :object)
48
+ schema = create(:schema, :location)
41
49
 
42
- json = build(:response, :object)
50
+ json = build(:response, :location)
43
51
  json_as_hash = json.to_h
44
52
 
45
53
  assert_matches_json_schema(json_as_hash, schema)
46
54
  end
47
55
 
48
56
  test "when passed a Hash, fails with message when negated" do
49
- schema = create(:schema, :object)
57
+ schema = create(:schema, :location)
50
58
 
51
- json = build(:response, :invalid_object)
59
+ json = build(:response, :invalid_location)
52
60
  json_as_hash = json.to_h
53
61
 
54
62
  assert_raises_error_containing(schema) do
@@ -57,27 +65,27 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
57
65
  end
58
66
 
59
67
  test "when passed a Array, validates a root-level Array in the JSON" do
60
- schema = create(:schema, :array_of, :objects)
68
+ schema = create(:schema, :array_of, :locations)
61
69
 
62
- json = build(:response, :object)
70
+ json = build(:response, :location)
63
71
  json_as_array = [json.to_h]
64
72
 
65
73
  assert_matches_json_schema(json_as_array, schema)
66
74
  end
67
75
 
68
76
  test "when passed a Array, refutes a root-level Array in the JSON" do
69
- schema = create(:schema, :array_of, :objects)
77
+ schema = create(:schema, :array_of, :locations)
70
78
 
71
- json = build(:response, :invalid_object)
79
+ json = build(:response, :invalid_location)
72
80
  json_as_array = [json.to_h]
73
81
 
74
82
  refute_matches_json_schema(json_as_array, schema)
75
83
  end
76
84
 
77
85
  test "when passed a Array, fails with message when negated" do
78
- schema = create(:schema, :array_of, :object)
86
+ schema = create(:schema, :array_of, :location)
79
87
 
80
- json = build(:response, :invalid_object)
88
+ json = build(:response, :invalid_location)
81
89
  json_as_array = [json.to_h]
82
90
 
83
91
  assert_raises_error_containing(schema) do
@@ -86,18 +94,18 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
86
94
  end
87
95
 
88
96
  test "when JSON is a string, validates that the schema matches" do
89
- schema = create(:schema, :object)
97
+ schema = create(:schema, :location)
90
98
 
91
- json = build(:response, :object)
99
+ json = build(:response, :location)
92
100
  json_as_string = json.to_json
93
101
 
94
102
  assert_matches_json_schema(json_as_string, schema)
95
103
  end
96
104
 
97
105
  test "when JSON is a string, fails with message when negated" do
98
- schema = create(:schema, :object)
106
+ schema = create(:schema, :location)
99
107
 
100
- json = build(:response, :invalid_object)
108
+ json = build(:response, :invalid_location)
101
109
  json_as_string = json.to_json
102
110
 
103
111
  assert_raises_error_containing(schema) do
@@ -106,9 +114,9 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
106
114
  end
107
115
 
108
116
  test "the failure message contains the body" do
109
- schema = create(:schema, :object)
117
+ schema = create(:schema, :location)
110
118
 
111
- json = build(:response, :invalid_object)
119
+ json = build(:response, :invalid_location)
112
120
 
113
121
  assert_raises_error_containing(json) do
114
122
  assert_matches_json_schema(json, schema)
@@ -116,9 +124,9 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
116
124
  end
117
125
 
118
126
  test "the failure message contains the schema" do
119
- schema = create(:schema, :object)
127
+ schema = create(:schema, :location)
120
128
 
121
- json = build(:response, :invalid_object)
129
+ json = build(:response, :invalid_location)
122
130
 
123
131
  assert_raises_error_containing(schema) do
124
132
  assert_matches_json_schema(json, schema)
@@ -126,9 +134,9 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
126
134
  end
127
135
 
128
136
  test "the failure message when negated, contains the body" do
129
- schema = create(:schema, :object)
137
+ schema = create(:schema, :location)
130
138
 
131
- json = build(:response, :object)
139
+ json = build(:response, :location)
132
140
 
133
141
  assert_raises_error_containing(json) do
134
142
  refute_matches_json_schema(json, schema)
@@ -136,9 +144,9 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
136
144
  end
137
145
 
138
146
  test "the failure message when negated, contains the schema" do
139
- schema = create(:schema, :object)
147
+ schema = create(:schema, :location)
140
148
 
141
- json = build(:response, :object)
149
+ json = build(:response, :location)
142
150
 
143
151
  assert_raises_error_containing(schema) do
144
152
  refute_matches_json_schema(json, schema)
@@ -146,23 +154,41 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
146
154
  end
147
155
 
148
156
  test "asserts valid JSON against a schema that uses $ref" do
149
- schema = create(:schema, :referencing_objects)
157
+ schema = create(:schema, :referencing_locations)
150
158
 
151
- json = build(:response, :object)
159
+ json = build(:response, :location)
152
160
  json_as_array = [json.to_h]
153
161
 
154
162
  assert_matches_json_schema(json_as_array, schema)
155
163
  end
156
164
 
157
165
  test "refutes valid JSON against a schema that uses $ref" do
158
- schema = create(:schema, :referencing_objects)
166
+ schema = create(:schema, :referencing_locations)
159
167
 
160
- json = build(:response, :invalid_object)
168
+ json = build(:response, :invalid_location)
161
169
  json_as_array = [json.to_h]
162
170
 
163
171
  refute_matches_json_schema(json_as_array, schema)
164
172
  end
165
173
 
174
+ test "validates against a schema referencing with 'definitions'" do
175
+ schema = create(:schema, :referencing_definitions)
176
+
177
+ json = build(:response, :location)
178
+ json_as_hash = { "locations" => [json] }
179
+
180
+ assert_matches_json_schema(json_as_hash, schema)
181
+ end
182
+
183
+ test "fails against a schema referencing with 'definitions'" do
184
+ schema = create(:schema, :referencing_definitions)
185
+
186
+ json = build(:response, :invalid_location)
187
+ json_as_hash = { "locations" => [json] }
188
+
189
+ refute_matches_json_schema(json_as_hash, schema)
190
+ end
191
+
166
192
  def assert_raises_error_containing(schema_or_body)
167
193
  raised_error = assert_raises(Minitest::Assertion) do
168
194
  yield
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-13 00:00:00.000000000 Z
11
+ date: 2018-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: json-schema
14
+ name: json_schema
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.7'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2.7'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -145,19 +145,17 @@ files:
145
145
  - json_matchers.gemspec
146
146
  - lib/json_matchers.rb
147
147
  - lib/json_matchers/assertion.rb
148
- - lib/json_matchers/configuration.rb
149
148
  - lib/json_matchers/errors.rb
150
149
  - lib/json_matchers/matcher.rb
151
150
  - lib/json_matchers/minitest/assertions.rb
151
+ - lib/json_matchers/parser.rb
152
152
  - lib/json_matchers/payload.rb
153
153
  - lib/json_matchers/rspec.rb
154
154
  - lib/json_matchers/validator.rb
155
155
  - lib/json_matchers/version.rb
156
156
  - spec/factories.rb
157
- - spec/json_matchers/configuration_spec.rb
158
157
  - spec/json_matchers/match_json_schema_spec.rb
159
158
  - spec/spec_helper.rb
160
- - spec/support/configuration.rb
161
159
  - spec/support/factory_bot.rb
162
160
  - spec/support/fake_response.rb
163
161
  - spec/support/fake_schema.rb
@@ -186,16 +184,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
184
  version: '0'
187
185
  requirements: []
188
186
  rubyforge_project:
189
- rubygems_version: 2.7.3
187
+ rubygems_version: 2.7.7
190
188
  signing_key:
191
189
  specification_version: 4
192
190
  summary: Validate your Rails JSON API's JSON
193
191
  test_files:
194
192
  - spec/factories.rb
195
- - spec/json_matchers/configuration_spec.rb
196
193
  - spec/json_matchers/match_json_schema_spec.rb
197
194
  - spec/spec_helper.rb
198
- - spec/support/configuration.rb
199
195
  - spec/support/factory_bot.rb
200
196
  - spec/support/fake_response.rb
201
197
  - spec/support/fake_schema.rb
@@ -1,27 +0,0 @@
1
- module JsonMatchers
2
- def self.configuration
3
- @configuration ||= Configuration.new
4
- end
5
-
6
- def self.configure
7
- warn <<-WARN
8
- DEPRECATION: `JsonMatchers.configure`
9
- After `json_matchers@0.9.x`, JsonMatchers.configure will be removed.
10
-
11
- See https://github.com/thoughtbot/json_matchers/pull/31 for more information.
12
-
13
- WARN
14
-
15
- yield(configuration)
16
- end
17
-
18
- class Configuration
19
- def initialize
20
- @options = {}
21
- end
22
-
23
- def options
24
- @options.merge!(record_errors: true)
25
- end
26
- end
27
- end
@@ -1,9 +0,0 @@
1
- describe JsonMatchers, ".configure" do
2
- it "ignores :record_errors configuration" do
3
- with_options(record_errors: false) do
4
- configured_options = JsonMatchers.configuration.options
5
-
6
- expect(configured_options).to include(record_errors: true)
7
- end
8
- end
9
- end
@@ -1,20 +0,0 @@
1
- module ConfigurationHelpers
2
- def with_options(options)
3
- original_options = JsonMatchers.configuration.options.dup
4
-
5
- JsonMatchers.configure do |config|
6
- config.options.merge!(options)
7
- end
8
-
9
- yield
10
-
11
- JsonMatchers.configure do |config|
12
- config.options.clear
13
- config.options.merge!(original_options)
14
- end
15
- end
16
- end
17
-
18
- RSpec.configure do |config|
19
- config.include ConfigurationHelpers
20
- end