json_matchers 0.6.3 → 0.7.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 +4 -4
- data/NEWS.md +9 -0
- data/json_matchers.gemspec +1 -1
- data/lib/json_matchers/configuration.rb +3 -1
- data/lib/json_matchers/matcher.rb +18 -9
- data/lib/json_matchers/validator.rb +38 -0
- data/lib/json_matchers/version.rb +1 -1
- data/spec/json_matchers/match_response_schema_spec.rb +33 -18
- data/spec/support/configuration.rb +20 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf14bd2b66049174e62c9b50f750692f761cb66c
|
4
|
+
data.tar.gz: 1ceafe472e8e7225bb194ad49fa5c17676800a13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58d81f70e226218c2fe359f131414cb8740963156ebc16b0616b43b5e7d2f976a3c2b465b4b4460dc0853cb69a91866697f0e06e1397c43fc478a532f0c3e8d5
|
7
|
+
data.tar.gz: 3a0d68faa322b689dc8331cf238bb5779e943d7854078b75b0627fac0e9037024627dfd6d49227a4b4e4e12e0405a87672cbe856c9e4e208c6086d53ba42a816
|
data/NEWS.md
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
master
|
2
2
|
======
|
3
3
|
|
4
|
+
0.7.0
|
5
|
+
=====
|
6
|
+
|
7
|
+
* Default `record_errors: true` [#52]
|
8
|
+
* Add support for configuring `record_errors` [#51]
|
9
|
+
|
10
|
+
[#52]: https://github.com/thoughtbot/json_matchers/pull/52
|
11
|
+
[#51]: https://github.com/thoughtbot/json_matchers/pull/51
|
12
|
+
|
4
13
|
0.6.3
|
5
14
|
=====
|
6
15
|
|
data/json_matchers.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency("json-schema", "~> 2.
|
21
|
+
spec.add_dependency("json-schema", "~> 2.7")
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.7"
|
24
24
|
spec.add_development_dependency "pry"
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require "json-schema"
|
2
|
-
require "json_matchers/
|
2
|
+
require "json_matchers/validator"
|
3
3
|
|
4
4
|
module JsonMatchers
|
5
5
|
class Matcher
|
@@ -9,28 +9,37 @@ module JsonMatchers
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def matches?(response)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
rescue JSON::Schema::ValidationError =>
|
18
|
-
|
12
|
+
validator = build_validator(response)
|
13
|
+
|
14
|
+
self.errors = validator.validate!
|
15
|
+
|
16
|
+
errors.empty?
|
17
|
+
rescue JSON::Schema::ValidationError => error
|
18
|
+
self.errors = [error.message]
|
19
19
|
false
|
20
20
|
rescue JSON::Schema::JsonParseError
|
21
21
|
raise InvalidSchemaError
|
22
22
|
end
|
23
23
|
|
24
24
|
def validation_failure_message
|
25
|
-
|
25
|
+
errors.first.to_s
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
30
|
attr_reader :schema_path, :options
|
31
|
+
attr_accessor :errors
|
31
32
|
|
32
33
|
def default_options
|
33
34
|
JsonMatchers.configuration.options || {}
|
34
35
|
end
|
36
|
+
|
37
|
+
def build_validator(response)
|
38
|
+
Validator.new(
|
39
|
+
options: options,
|
40
|
+
response: response,
|
41
|
+
schema_path: schema_path,
|
42
|
+
)
|
43
|
+
end
|
35
44
|
end
|
36
45
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "json-schema"
|
2
|
+
require "json_matchers/payload"
|
3
|
+
|
4
|
+
module JsonMatchers
|
5
|
+
class Validator
|
6
|
+
def initialize(options:, response:, schema_path:)
|
7
|
+
@options = options.dup
|
8
|
+
@payload = Payload.new(response).to_s
|
9
|
+
@schema_path = schema_path.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate!
|
13
|
+
if recording_errors?
|
14
|
+
validate_recording_errors
|
15
|
+
else
|
16
|
+
validate
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :options, :payload, :schema_path
|
23
|
+
|
24
|
+
def recording_errors?
|
25
|
+
options.fetch(:record_errors, false)
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate_recording_errors
|
29
|
+
JSON::Validator.fully_validate(schema_path, payload, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate
|
33
|
+
JSON::Validator.validate!(schema_path, payload, options)
|
34
|
+
|
35
|
+
[]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -142,27 +142,42 @@ describe JsonMatchers, "#match_response_schema" do
|
|
142
142
|
|
143
143
|
context "when options are configured globally" do
|
144
144
|
it "forwards them to the validator" do
|
145
|
-
|
146
|
-
"
|
147
|
-
|
148
|
-
"
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
145
|
+
with_options(strict: true) do
|
146
|
+
create_schema("foo_schema", {
|
147
|
+
"type" => "object",
|
148
|
+
"properties" => {
|
149
|
+
"id" => { "type" => "number" },
|
150
|
+
"title" => { "type" => "string" },
|
151
|
+
},
|
152
|
+
})
|
153
|
+
|
154
|
+
expect(response_for({ "id" => 1, "title" => "bar" })).
|
155
|
+
to match_response_schema("foo_schema")
|
156
|
+
expect(response_for({ "id" => 1 })).
|
157
|
+
not_to match_response_schema("foo_schema")
|
155
158
|
end
|
156
|
-
|
157
|
-
expect(response_for({ "id" => 1, "title" => "bar" })).
|
158
|
-
to match_response_schema("foo_schema")
|
159
|
-
expect(response_for({ "id" => 1 })).
|
160
|
-
not_to match_response_schema("foo_schema")
|
161
159
|
end
|
162
160
|
|
163
|
-
|
164
|
-
|
165
|
-
|
161
|
+
context "when configured to record errors" do
|
162
|
+
it "includes the reasons for failure in the exception's message" do
|
163
|
+
with_options(record_errors: true) do
|
164
|
+
create_schema("foo_schema", {
|
165
|
+
"type" => "object",
|
166
|
+
"properties" => {
|
167
|
+
"username" => {
|
168
|
+
"allOf": [
|
169
|
+
{ "type": "string" },
|
170
|
+
{ "minLength": 5 }
|
171
|
+
]
|
172
|
+
}
|
173
|
+
}
|
174
|
+
})
|
175
|
+
invalid_payload = response_for({ "username" => "foo" })
|
176
|
+
|
177
|
+
expect {
|
178
|
+
expect(invalid_payload).to match_response_schema("foo_schema")
|
179
|
+
}.to raise_error(/minimum/)
|
180
|
+
end
|
166
181
|
end
|
167
182
|
end
|
168
183
|
end
|
@@ -0,0 +1,20 @@
|
|
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
|
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.7.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: 2017-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: '2.7'
|
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.
|
26
|
+
version: '2.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,9 +104,11 @@ files:
|
|
104
104
|
- lib/json_matchers/matcher.rb
|
105
105
|
- lib/json_matchers/payload.rb
|
106
106
|
- lib/json_matchers/rspec.rb
|
107
|
+
- lib/json_matchers/validator.rb
|
107
108
|
- lib/json_matchers/version.rb
|
108
109
|
- spec/json_matchers/match_response_schema_spec.rb
|
109
110
|
- spec/spec_helper.rb
|
111
|
+
- spec/support/configuration.rb
|
110
112
|
- spec/support/file_helpers.rb
|
111
113
|
homepage: https://github.com/thoughtbot/json_matchers
|
112
114
|
licenses:
|
@@ -135,4 +137,5 @@ summary: Validate your Rails JSON API's JSON
|
|
135
137
|
test_files:
|
136
138
|
- spec/json_matchers/match_response_schema_spec.rb
|
137
139
|
- spec/spec_helper.rb
|
140
|
+
- spec/support/configuration.rb
|
138
141
|
- spec/support/file_helpers.rb
|