json_matchers 0.10.0 → 0.11.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03b79f9a5c8394f6e8ccbd26d6c066519e5d2a25ad6b623765a48f524ba38bf0
4
- data.tar.gz: 00421a6718fff3b21d1c50382ea5b9d5f855dbfe4d2522dbf50db217f2993b8f
3
+ metadata.gz: 9fecae5a2677672d70b145cd3414a8bdb8026322d45ce5f1ec823cb5215a2b5f
4
+ data.tar.gz: 1e0c74062d1a5070fad748bc5c07014429dc9bfae7854df6b9ccaa68b52ab4e4
5
5
  SHA512:
6
- metadata.gz: 9b4cc33ad05676e0ac2133c79d627ece2994d0a21878ad6269c3145081d3cf1fe0e193928e1ca312660baa418b69f5cd33661aa0f070301d50f51436b72d3c0a
7
- data.tar.gz: 830aa3570c9ae06fcd3986323d2dd0a3763c7fd91c62e09cf7029e184612ec15c5227fa706afbfe3cb8471c61c4f8dd85fa2cea68f9fd5edb704f7c7bfdbfffc
6
+ metadata.gz: cd933b24a0e345c0b7499d8eecf03edb49d0eef23a1ae1b7225bd4e9a9fa288483df4823e71668b846ed7f95a4416b9662e86a611ae311acc446acdd5ea59013
7
+ data.tar.gz: '0815f896a0015905b2945588b234e0e6e5947641bc1b55f4d9cbae6bc4141e26f6d6c9b661001900a7a5fc179cc55e1efbcc8105dd060df922c7a7f39c8178af'
data/.rubocop.yml CHANGED
@@ -17,6 +17,20 @@ Style/BracesAroundHashParameters:
17
17
  Style/SymbolArray:
18
18
  Enabled: false
19
19
 
20
+ Style/TrailingCommaInArrayLiteral:
21
+ Enabled: true
22
+ Exclude:
23
+ - 'test/**/*_test.rb'
24
+ - 'spec/**/*_spec.rb'
25
+ - 'spec/factories.rb'
26
+
27
+ Style/TrailingCommaInHashLiteral:
28
+ Enabled: true
29
+ Exclude:
30
+ - 'test/**/*_test.rb'
31
+ - 'spec/**/*_spec.rb'
32
+ - 'spec/factories.rb'
33
+
20
34
  Layout/IndentHeredoc:
21
35
  Enabled: false
22
36
 
data/NEWS.md CHANGED
@@ -1,6 +1,14 @@
1
1
  master
2
2
  ======
3
3
 
4
+ 0.11.0
5
+ ======
6
+
7
+ * Expand references of DocuemntStore schemas ([#93], [3b66b4d])
8
+
9
+ [#93]: https://github.com/thoughtbot/json_matchers/pull/93
10
+ [3b66b4d]: https://github.com/thoughtbot/json_matchers/commit/3b66b4ddf369ec09b50cfe39c614266b34c7f3fe
11
+
4
12
  0.10.0
5
13
  ======
6
14
 
data/README.md CHANGED
@@ -50,7 +50,7 @@ require "json_matchers/minitest/assertions"
50
50
 
51
51
  JsonMatchers.schema_root = "test/support/api/schemas"
52
52
 
53
- Minitest::Test.send(:include, JsonMatchers::Minitest::Assertions)
53
+ Minitest::Test.include(JsonMatchers::Minitest::Assertions)
54
54
  ```
55
55
 
56
56
  ### Declare
@@ -34,7 +34,8 @@ module JsonMatchers
34
34
  Dir.glob("#{JsonMatchers.schema_root}/**/*.json").
35
35
  map { |path| Pathname.new(path) }.
36
36
  map { |schema_path| Parser.new(schema_path).parse }.
37
- each { |schema| document_store.add_schema(schema) }
37
+ map { |schema| document_store.add_schema(schema) }.
38
+ each { |schema| schema.expand_references!(store: document_store) }
38
39
 
39
40
  document_store
40
41
  end
@@ -1,3 +1,3 @@
1
1
  module JsonMatchers
2
- VERSION = "0.10.0".freeze
2
+ VERSION = "0.11.0".freeze
3
3
  end
data/spec/factories.rb CHANGED
@@ -68,6 +68,7 @@ FactoryBot.define do
68
68
 
69
69
  initialize_with do
70
70
  FakeSchema.new(name, {
71
+ "id": "file:/#{name}.json#",
71
72
  "$schema": "https://json-schema.org/draft-04/schema#",
72
73
  "type": "array",
73
74
  "items": { "$ref": "file:/#{items.name}.json#" },
@@ -59,6 +59,147 @@ describe JsonMatchers, "#match_json_schema" do
59
59
  expect(json).to match_json_schema("api/v1/schema")
60
60
  end
61
61
 
62
+ it "supports invalidating the referenced schema when using local references" do
63
+ create(:schema, name: "post", json: {
64
+ "$schema": "https://json-schema.org/draft-04/schema#",
65
+ "id": "file:/post.json#",
66
+ "definitions": {
67
+ "attributes": {
68
+ "type": "object",
69
+ "required": [
70
+ "id",
71
+ "name",
72
+ ],
73
+ "properties": {
74
+ "id": { "type": "string" },
75
+ "name": { "type": "string" }
76
+ }
77
+ }
78
+ },
79
+ "type": "object",
80
+ "required": [
81
+ "id",
82
+ "type",
83
+ "attributes"
84
+ ],
85
+ "properties": {
86
+ "id": { "type": "string" },
87
+ "type": { "type": "string" },
88
+ "attributes": {
89
+ "$ref": "#/definitions/attributes",
90
+ }
91
+ }
92
+ })
93
+ posts_index = create(:schema, name: "posts/index", json: {
94
+ "$schema": "https://json-schema.org/draft-04/schema#",
95
+ "id": "file:/posts/index.json#",
96
+ "type": "object",
97
+ "required": [
98
+ "data"
99
+ ],
100
+ "definitions": {
101
+ "posts": {
102
+ "type": "array",
103
+ "items": {
104
+ "$ref": "file:/post.json#"
105
+ }
106
+ }
107
+ },
108
+ "properties": {
109
+ "data": {
110
+ "$ref": "#/definitions/posts"
111
+ }
112
+ }
113
+ })
114
+
115
+ json = build(:response, {
116
+ "data": [{
117
+ "id": "1",
118
+ "type": "Post",
119
+ "attributes": {
120
+ "id": 1,
121
+ "name": "The Post's Name"
122
+ }
123
+ }]
124
+ })
125
+
126
+ expect(json).not_to match_json_schema(posts_index)
127
+ end
128
+
129
+ it "can reference a schema relatively" do
130
+ create(:schema, name: "post", json: {
131
+ "$schema": "https://json-schema.org/draft-04/schema#",
132
+ "id": "file:/post.json#",
133
+ "type": "object",
134
+ "required": [
135
+ "id",
136
+ "type",
137
+ "attributes"
138
+ ],
139
+ "properties": {
140
+ "id": { "type": "string" },
141
+ "type": { "type": "string" },
142
+ "attributes": {
143
+ "type": "object",
144
+ "required": [
145
+ "id",
146
+ "name"
147
+ ],
148
+ "properties": {
149
+ "id": { "type": "string" },
150
+ "name": { "type": "string" },
151
+ "user": {
152
+ "type": "object",
153
+ "required": [
154
+ "id"
155
+ ],
156
+ "properties": {
157
+ "id": { "type": "string" }
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+ })
164
+ posts_index = create(:schema, name: "posts/index", json: {
165
+ "$schema": "https://json-schema.org/draft-04/schema#",
166
+ "id": "file:/posts/index.json#",
167
+ "type": "object",
168
+ "required": [
169
+ "data"
170
+ ],
171
+ "definitions": {
172
+ "posts": {
173
+ "type": "array",
174
+ "items": {
175
+ "$ref": "file:/post.json#"
176
+ }
177
+ }
178
+ },
179
+ "properties": {
180
+ "data": {
181
+ "$ref": "#/definitions/posts"
182
+ }
183
+ }
184
+ })
185
+
186
+ json = build(:response, {
187
+ "data": [{
188
+ "id": "1",
189
+ "type": "Post",
190
+ "attributes": {
191
+ "id": "1",
192
+ "name": "The Post's Name",
193
+ "user": {
194
+ "id": "1"
195
+ }
196
+ }
197
+ }]
198
+ })
199
+
200
+ expect(json).to match_json_schema(posts_index)
201
+ end
202
+
62
203
  context "when passed a Hash" do
63
204
  it "validates that the schema matches" do
64
205
  schema = create(:schema, :location)
@@ -202,6 +343,26 @@ describe JsonMatchers, "#match_json_schema" do
202
343
  expect(json_as_array).not_to match_json_schema(schema)
203
344
  end
204
345
 
346
+ it "validates against a schema that uses nested $refs" do
347
+ items = create(:schema, :referencing_locations)
348
+ schema = create(:schema, :referencing_locations, items: items)
349
+
350
+ json = build(:response, :location)
351
+ json_as_array = [[json.to_h]]
352
+
353
+ expect(json_as_array).to match_json_schema(schema)
354
+ end
355
+
356
+ it "fails against a schema that uses nested $refs" do
357
+ items = create(:schema, :referencing_locations)
358
+ schema = create(:schema, :referencing_locations, items: items)
359
+
360
+ json = build(:response, :invalid_location)
361
+ json_as_array = [[json.to_h]]
362
+
363
+ expect(json_as_array).not_to match_json_schema(schema)
364
+ end
365
+
205
366
  it "validates against a schema referencing with 'definitions'" do
206
367
  schema = create(:schema, :referencing_definitions)
207
368
 
@@ -44,6 +44,147 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
44
44
  assert_matches_json_schema(json, "api/v1/schema")
45
45
  end
46
46
 
47
+ test "supports invalidating the referenced schema when using local references" do
48
+ create(:schema, name: "post", json: {
49
+ "$schema": "https://json-schema.org/draft-04/schema#",
50
+ "id": "file:/post.json#",
51
+ "definitions": {
52
+ "attributes": {
53
+ "type": "object",
54
+ "required": [
55
+ "id",
56
+ "name",
57
+ ],
58
+ "properties": {
59
+ "id": { "type": "string" },
60
+ "name": { "type": "string" }
61
+ }
62
+ }
63
+ },
64
+ "type": "object",
65
+ "required": [
66
+ "id",
67
+ "type",
68
+ "attributes"
69
+ ],
70
+ "properties": {
71
+ "id": { "type": "string" },
72
+ "type": { "type": "string" },
73
+ "attributes": {
74
+ "$ref": "#/definitions/attributes",
75
+ }
76
+ }
77
+ })
78
+ posts_index = create(:schema, name: "posts/index", json: {
79
+ "$schema": "https://json-schema.org/draft-04/schema#",
80
+ "id": "file:/posts/index.json#",
81
+ "type": "object",
82
+ "required": [
83
+ "data"
84
+ ],
85
+ "definitions": {
86
+ "posts": {
87
+ "type": "array",
88
+ "items": {
89
+ "$ref": "file:/post.json#"
90
+ }
91
+ }
92
+ },
93
+ "properties": {
94
+ "data": {
95
+ "$ref": "#/definitions/posts"
96
+ }
97
+ }
98
+ })
99
+
100
+ json = build(:response, {
101
+ "data": [{
102
+ "id": "1",
103
+ "type": "Post",
104
+ "attributes": {
105
+ "id": 1,
106
+ "name": "The Post's Name"
107
+ }
108
+ }]
109
+ })
110
+
111
+ refute_matches_json_schema(json, posts_index)
112
+ end
113
+
114
+ test "can reference a schema relatively" do
115
+ create(:schema, name: "post", json: {
116
+ "$schema": "https://json-schema.org/draft-04/schema#",
117
+ "id": "file:/post.json#",
118
+ "type": "object",
119
+ "required": [
120
+ "id",
121
+ "type",
122
+ "attributes"
123
+ ],
124
+ "properties": {
125
+ "id": { "type": "string" },
126
+ "type": { "type": "string" },
127
+ "attributes": {
128
+ "type": "object",
129
+ "required": [
130
+ "id",
131
+ "name"
132
+ ],
133
+ "properties": {
134
+ "id": { "type": "string" },
135
+ "name": { "type": "string" },
136
+ "user": {
137
+ "type": "object",
138
+ "required": [
139
+ "id"
140
+ ],
141
+ "properties": {
142
+ "id": { "type": "string" }
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ })
149
+ posts_index = create(:schema, name: "posts/index", json: {
150
+ "$schema": "https://json-schema.org/draft-04/schema#",
151
+ "id": "file:/posts/index.json#",
152
+ "type": "object",
153
+ "required": [
154
+ "data"
155
+ ],
156
+ "definitions": {
157
+ "posts": {
158
+ "type": "array",
159
+ "items": {
160
+ "$ref": "file:/post.json#"
161
+ }
162
+ }
163
+ },
164
+ "properties": {
165
+ "data": {
166
+ "$ref": "#/definitions/posts"
167
+ }
168
+ }
169
+ })
170
+
171
+ json = build(:response, {
172
+ "data": [{
173
+ "id": "1",
174
+ "type": "Post",
175
+ "attributes": {
176
+ "id": "1",
177
+ "name": "The Post's Name",
178
+ "user": {
179
+ "id": "1"
180
+ }
181
+ }
182
+ }]
183
+ })
184
+
185
+ assert_matches_json_schema(json, posts_index)
186
+ end
187
+
47
188
  test "when passed a Hash, validates that the schema matches" do
48
189
  schema = create(:schema, :location)
49
190
 
@@ -171,6 +312,27 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
171
312
  refute_matches_json_schema(json_as_array, schema)
172
313
  end
173
314
 
315
+ test "validates against a schema that uses nested $refs" do
316
+ items = create(:schema, :referencing_locations)
317
+ schema = create(:schema, :referencing_locations, items: items)
318
+
319
+ json = build(:response, :location)
320
+ json_as_array = [[json.to_h]]
321
+
322
+ assert_matches_json_schema(json_as_array, schema)
323
+ end
324
+
325
+ test "fails against a schema that uses nested $refs" do
326
+ items = create(:schema, :referencing_locations)
327
+ schema = create(:schema, :referencing_locations, items: items)
328
+
329
+ json = build(:response, :invalid_location)
330
+ json_as_array = [[json.to_h]]
331
+
332
+ refute_matches_json_schema(json_as_array, schema)
333
+ end
334
+
335
+
174
336
  test "validates against a schema referencing with 'definitions'" do
175
337
  schema = create(:schema, :referencing_definitions)
176
338
 
@@ -2,4 +2,4 @@ require "factory_bot"
2
2
 
3
3
  FactoryBot.find_definitions
4
4
 
5
- Minitest::Test.send(:include, FactoryBot::Syntax::Methods)
5
+ Minitest::Test.include(FactoryBot::Syntax::Methods)
data/test/test_helper.rb CHANGED
@@ -3,6 +3,6 @@ require "json_matchers/minitest/assertions"
3
3
 
4
4
  JsonMatchers.schema_root = "/test/support/api/schemas"
5
5
 
6
- Minitest::Test.send(:include, JsonMatchers::Minitest::Assertions)
6
+ Minitest::Test.include(JsonMatchers::Minitest::Assertions)
7
7
 
8
8
  Dir["./test/support/**/*.rb"].each { |file| require file }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.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-08-02 00:00:00.000000000 Z
11
+ date: 2019-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_schema