Skip to content

Commit

Permalink
Remove support for configuring validation options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
seanpdoyle committed Apr 13, 2018
1 parent f840139 commit 48e7620
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 198 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
master
======

* *Breaking Change* - remove support for configuring validation options.

0.9.0
=====

Expand Down
46 changes: 2 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,47 +116,6 @@ def test_GET_posts_returns_Posts
end
```

### DEPRECATED: Passing options to the validator

The matcher accepts options, which it passes to the validator:

`spec/requests/posts_spec.rb`

```ruby
describe "GET /posts" do
it "returns Posts" do
get posts_path, format: :json

expect(response.status).to eq 200
expect(response).to match_json_schema("posts", strict: true)
end
end
```

A list of available options can be found [here][options].

[options]: https://github.com/ruby-json-schema/json-schema/blob/2.2.4/lib/json-schema/validator.rb#L160-L162

### DEPRECATED: Global matcher options

To configure the default options passed to *all* matchers, call
`JsonMatchers.configure`.

`spec/support/json_matchers.rb`:

```rb
JsonMatchers.configure do |config|
config.options[:strict] = true
end
```

A list of available options can be found [here][options].

### DEPRECATED: Default matcher options

* `record_errors: true` - *NOTE* `json_matchers` will always set
`record_errors: true`. This cannot be overridden.

### Embedding other Schemas

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

## Upgrading from `0.9.x`

After `[email protected]`, calls to `match_json_schema` and
`match_response_schema` no longer accept options, and `JsonMatchers.configure`
will been removed.
Calls to `match_json_schema` and `match_response_schema` no longer accept
options, and `JsonMatchers.configure` has been removed.

## Contributing

Expand Down
1 change: 0 additions & 1 deletion lib/json_matchers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "json_matchers/version"
require "json_matchers/configuration"
require "json_matchers/matcher"
require "json_matchers/errors"

Expand Down
6 changes: 3 additions & 3 deletions lib/json_matchers/assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

module JsonMatchers
class Assertion
def initialize(schema_name, **options)
@schema_name = schema_name
def initialize(schema_name)
@schema_name = schema_name.to_s
@schema_path = JsonMatchers.path_to_schema(schema_name)
@matcher = Matcher.new(schema_path, options)
@matcher = Matcher.new(schema_path)
end

def valid?(json)
Expand Down
27 changes: 0 additions & 27 deletions lib/json_matchers/configuration.rb

This file was deleted.

10 changes: 2 additions & 8 deletions lib/json_matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

module JsonMatchers
class Matcher
def initialize(schema_path, options = {})
def initialize(schema_path)
@schema_path = schema_path
@options = default_options.merge(options)
end

def matches?(payload)
Expand All @@ -27,16 +26,11 @@ def validation_failure_message

private

attr_reader :schema_path, :options
attr_reader :schema_path
attr_accessor :errors

def default_options
JsonMatchers.configuration.options || {}
end

def build_validator(payload)
Validator.new(
options: options,
payload: payload,
schema_path: schema_path,
)
Expand Down
16 changes: 2 additions & 14 deletions lib/json_matchers/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,8 @@ module JsonMatchers
self.schema_root = File.join("spec", "support", "api", "schemas")
end

RSpec::Matchers.define :match_json_schema do |schema_name, **options|
if options.present?
warn <<-WARN
DEPRECATION:
After `[email protected]`, calls to `match_json_schema` and
`match_response_schema` will no longer accept options.
See https://github.com/thoughtbot/json_matchers/pull/31 for more information.
WARN
end

assertion = JsonMatchers::Assertion.new(schema_name.to_s, options)
RSpec::Matchers.define :match_json_schema do |schema_name|
assertion = JsonMatchers::Assertion.new(schema_name.to_s)

match do |json|
assertion.valid?(json)
Expand Down
25 changes: 3 additions & 22 deletions lib/json_matchers/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,17 @@

module JsonMatchers
class Validator
def initialize(options:, payload:, schema_path:)
@options = options.dup
def initialize(payload:, schema_path:)
@payload = payload
@schema_path = schema_path.to_s
end

def validate!
if recording_errors?
validate_recording_errors
else
validate
end
JSON::Validator.fully_validate(schema_path, payload, record_errors: true)
end

private

attr_reader :options, :payload, :schema_path

def recording_errors?
options.fetch(:record_errors, false)
end

def validate_recording_errors
JSON::Validator.fully_validate(schema_path, payload, options)
end

def validate
JSON::Validator.validate!(schema_path, payload, options)

[]
end
attr_reader :payload, :schema_path
end
end
9 changes: 0 additions & 9 deletions spec/json_matchers/configuration_spec.rb

This file was deleted.

50 changes: 0 additions & 50 deletions spec/json_matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,56 +194,6 @@
expect(json_as_array).not_to match_json_schema(schema)
end

context "when options are passed directly to the matcher" do
it "forwards options to the validator" do
schema = create(:schema, :object)

matching_json = build(:response, :object)
invalid_json = build(:response, { "id": 1, "title": "bar" })

expect(matching_json).to match_json_schema(schema, strict: true)
expect(invalid_json).not_to match_json_schema(schema, strict: true)
end
end

context "when options are configured globally" do
it "forwards them to the validator" do
with_options(strict: true) do
schema = create(:schema, :object)

matching_json = build(:response, :object)
invalid_json = build(:response, { "id": 1, "title": "bar" })

expect(matching_json).to match_json_schema(schema)
expect(invalid_json).not_to match_json_schema(schema)
end
end

context "when configured to record errors" do
it "includes the reasons for failure in the exception's message" do
with_options(record_errors: true) do
schema = create(:schema, {
"type": "object",
"properties": {
"username": {
"allOf": [
{ "type": "string" },
{ "minLength": 5 },
],
},
},
})

invalid_json = build(:response, { "username": "foo" })

expect {
expect(invalid_json).to match_json_schema(schema)
}.to raise_error(/minimum/)
end
end
end
end

def raise_error_containing(schema_or_body)
raise_error do |error|
sanitized_message = error.message.squish
Expand Down
20 changes: 0 additions & 20 deletions spec/support/configuration.rb

This file was deleted.

0 comments on commit 48e7620

Please sign in to comment.