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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa337859deb80396749bed9a9c4d32d7079a81ab
4
- data.tar.gz: 3a8dc313f1854e2c1e41cd0bafa66855319bb06e
3
+ metadata.gz: cf14bd2b66049174e62c9b50f750692f761cb66c
4
+ data.tar.gz: 1ceafe472e8e7225bb194ad49fa5c17676800a13
5
5
  SHA512:
6
- metadata.gz: c22878991190e76a633c836dbdc45eb95cc5ce26664c13acf19579940653b4939ea67f212f81c12f2760198716b4347c71803e9135de943e952066cc1f75392f
7
- data.tar.gz: 706d1bfd640fd52d8743a90cddc3bd2727736167c6dc4deed8137b95b5651bdc880134c43a27a089e367daa76de342aa508ed3ed19c72b4f28949043ab330e57
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
 
@@ -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.6.0")
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"
@@ -11,7 +11,9 @@ module JsonMatchers
11
11
  attr_reader :options
12
12
 
13
13
  def initialize
14
- @options = {}
14
+ @options = {
15
+ record_errors: true,
16
+ }
15
17
  end
16
18
  end
17
19
  end
@@ -1,5 +1,5 @@
1
1
  require "json-schema"
2
- require "json_matchers/payload"
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
- JSON::Validator.validate!(
13
- schema_path.to_s,
14
- Payload.new(response).to_s,
15
- options,
16
- )
17
- rescue JSON::Schema::ValidationError => ex
18
- @validation_failure_message = ex.message
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
- @validation_failure_message.to_s
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
@@ -1,3 +1,3 @@
1
1
  module JsonMatchers
2
- VERSION = "0.6.3"
2
+ VERSION = "0.7.0"
3
3
  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
- create_schema("foo_schema", {
146
- "type" => "object",
147
- "properties" => {
148
- "id" => { "type" => "number" },
149
- "title" => { "type" => "string" },
150
- },
151
- })
152
-
153
- JsonMatchers.configure do |config|
154
- config.options[:strict] = true
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
- after do
164
- JsonMatchers.configure do |config|
165
- config.options.delete(:strict)
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.6.3
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: 2016-11-08 00:00:00.000000000 Z
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.6.0
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.6.0
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