json_matchers 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +14 -0
- data/NEWS.md +8 -0
- data/README.md +1 -1
- data/lib/json_matchers/matcher.rb +2 -1
- data/lib/json_matchers/version.rb +1 -1
- data/spec/factories.rb +1 -0
- data/spec/json_matchers/match_json_schema_spec.rb +161 -0
- data/test/json_matchers/minitest/assertions_test.rb +162 -0
- data/test/support/factory_bot.rb +1 -1
- data/test/test_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fecae5a2677672d70b145cd3414a8bdb8026322d45ce5f1ec823cb5215a2b5f
|
4
|
+
data.tar.gz: 1e0c74062d1a5070fad748bc5c07014429dc9bfae7854df6b9ccaa68b52ab4e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
-
|
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
|
data/spec/factories.rb
CHANGED
@@ -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
|
|
data/test/support/factory_bot.rb
CHANGED
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.
|
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.
|
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:
|
11
|
+
date: 2019-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json_schema
|