Skip to content

Commit a228105

Browse files
committed
Remove support for configuring validation options
In preparation for [#31][#31], this commit removes support for global and matcher-specific options, like `strict: true`. The [`json_schema` gem][gem] does not accept similar configuration options, so we'll remove support entirely. [#31]: #31 [gem]: https://github.com/brandur/json_schema#programmatic
1 parent f840139 commit a228105

File tree

11 files changed

+13
-199
lines changed

11 files changed

+13
-199
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
master
22
======
33

4+
* *Breaking Change* - remove support for configuring validation options.
5+
46
0.9.0
57
=====
68

README.md

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -116,47 +116,6 @@ def test_GET_posts_returns_Posts
116116
end
117117
```
118118

119-
### DEPRECATED: Passing options to the validator
120-
121-
The matcher accepts options, which it passes to the validator:
122-
123-
`spec/requests/posts_spec.rb`
124-
125-
```ruby
126-
describe "GET /posts" do
127-
it "returns Posts" do
128-
get posts_path, format: :json
129-
130-
expect(response.status).to eq 200
131-
expect(response).to match_json_schema("posts", strict: true)
132-
end
133-
end
134-
```
135-
136-
A list of available options can be found [here][options].
137-
138-
[options]: https://github.com/ruby-json-schema/json-schema/blob/2.2.4/lib/json-schema/validator.rb#L160-L162
139-
140-
### DEPRECATED: Global matcher options
141-
142-
To configure the default options passed to *all* matchers, call
143-
`JsonMatchers.configure`.
144-
145-
`spec/support/json_matchers.rb`:
146-
147-
```rb
148-
JsonMatchers.configure do |config|
149-
config.options[:strict] = true
150-
end
151-
```
152-
153-
A list of available options can be found [here][options].
154-
155-
### DEPRECATED: Default matcher options
156-
157-
* `record_errors: true` - *NOTE* `json_matchers` will always set
158-
`record_errors: true`. This cannot be overridden.
159-
160119
### Embedding other Schemas
161120

162121
To DRY up your schema definitions, use JSON schema's `$ref`.
@@ -203,9 +162,8 @@ To learn more about `$ref`, check out [Understanding JSON Schema Structuring](ht
203162

204163
## Upgrading from `0.9.x`
205164

206-
After `json_matchers@0.9.x`, calls to `match_json_schema` and
207-
`match_response_schema` no longer accept options, and `JsonMatchers.configure`
208-
will been removed.
165+
Calls to `match_json_schema` and `match_response_schema` no longer accept
166+
options, and `JsonMatchers.configure` has been removed.
209167

210168
## Contributing
211169

lib/json_matchers.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "json_matchers/version"
2-
require "json_matchers/configuration"
32
require "json_matchers/matcher"
43
require "json_matchers/errors"
54

lib/json_matchers/assertion.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
module JsonMatchers
77
class Assertion
8-
def initialize(schema_name, **options)
9-
@schema_name = schema_name
8+
def initialize(schema_name)
9+
@schema_name = schema_name.to_s
1010
@schema_path = JsonMatchers.path_to_schema(schema_name)
11-
@matcher = Matcher.new(schema_path, options)
11+
@matcher = Matcher.new(schema_path)
1212
end
1313

1414
def valid?(json)

lib/json_matchers/configuration.rb

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/json_matchers/matcher.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
module JsonMatchers
55
class Matcher
6-
def initialize(schema_path, options = {})
6+
def initialize(schema_path)
77
@schema_path = schema_path
8-
@options = default_options.merge(options)
98
end
109

1110
def matches?(payload)
@@ -27,16 +26,11 @@ def validation_failure_message
2726

2827
private
2928

30-
attr_reader :schema_path, :options
29+
attr_reader :schema_path
3130
attr_accessor :errors
3231

33-
def default_options
34-
JsonMatchers.configuration.options || {}
35-
end
36-
3732
def build_validator(payload)
3833
Validator.new(
39-
options: options,
4034
payload: payload,
4135
schema_path: schema_path,
4236
)

lib/json_matchers/rspec.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,7 @@ module JsonMatchers
55
self.schema_root = File.join("spec", "support", "api", "schemas")
66
end
77

8-
RSpec::Matchers.define :match_json_schema do |schema_name, **options|
9-
if options.present?
10-
warn <<-WARN
11-
DEPRECATION:
12-
13-
After `json_matchers@0.9.x`, calls to `match_json_schema` and
14-
`match_response_schema` will no longer accept options.
15-
16-
See https://github.com/thoughtbot/json_matchers/pull/31 for more information.
17-
18-
WARN
19-
end
20-
21-
assertion = JsonMatchers::Assertion.new(schema_name.to_s, options)
22-
8+
RSpec::Matchers.define :match_json_schema do |schema_name|
239
match do |json|
2410
assertion.valid?(json)
2511
end

lib/json_matchers/validator.rb

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,17 @@
22

33
module JsonMatchers
44
class Validator
5-
def initialize(options:, payload:, schema_path:)
6-
@options = options.dup
5+
def initialize(payload:, schema_path:)
76
@payload = payload
87
@schema_path = schema_path.to_s
98
end
109

1110
def validate!
12-
if recording_errors?
13-
validate_recording_errors
14-
else
15-
validate
16-
end
11+
JSON::Validator.fully_validate(schema_path, payload, record_errors: true)
1712
end
1813

1914
private
2015

21-
attr_reader :options, :payload, :schema_path
22-
23-
def recording_errors?
24-
options.fetch(:record_errors, false)
25-
end
26-
27-
def validate_recording_errors
28-
JSON::Validator.fully_validate(schema_path, payload, options)
29-
end
30-
31-
def validate
32-
JSON::Validator.validate!(schema_path, payload, options)
33-
34-
[]
35-
end
16+
attr_reader :payload, :schema_path
3617
end
3718
end

spec/json_matchers/configuration_spec.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

spec/json_matchers/match_json_schema_spec.rb

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -194,56 +194,6 @@
194194
expect(json_as_array).not_to match_json_schema(schema)
195195
end
196196

197-
context "when options are passed directly to the matcher" do
198-
it "forwards options to the validator" do
199-
schema = create(:schema, :object)
200-
201-
matching_json = build(:response, :object)
202-
invalid_json = build(:response, { "id": 1, "title": "bar" })
203-
204-
expect(matching_json).to match_json_schema(schema, strict: true)
205-
expect(invalid_json).not_to match_json_schema(schema, strict: true)
206-
end
207-
end
208-
209-
context "when options are configured globally" do
210-
it "forwards them to the validator" do
211-
with_options(strict: true) do
212-
schema = create(:schema, :object)
213-
214-
matching_json = build(:response, :object)
215-
invalid_json = build(:response, { "id": 1, "title": "bar" })
216-
217-
expect(matching_json).to match_json_schema(schema)
218-
expect(invalid_json).not_to match_json_schema(schema)
219-
end
220-
end
221-
222-
context "when configured to record errors" do
223-
it "includes the reasons for failure in the exception's message" do
224-
with_options(record_errors: true) do
225-
schema = create(:schema, {
226-
"type": "object",
227-
"properties": {
228-
"username": {
229-
"allOf": [
230-
{ "type": "string" },
231-
{ "minLength": 5 },
232-
],
233-
},
234-
},
235-
})
236-
237-
invalid_json = build(:response, { "username": "foo" })
238-
239-
expect {
240-
expect(invalid_json).to match_json_schema(schema)
241-
}.to raise_error(/minimum/)
242-
end
243-
end
244-
end
245-
end
246-
247197
def raise_error_containing(schema_or_body)
248198
raise_error do |error|
249199
sanitized_message = error.message.squish

0 commit comments

Comments
 (0)