From 9f0bb94b93c651cac247f381419c11ef0500ea23 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 13 Apr 2018 12:50:40 -0400 Subject: [PATCH] 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]: https://github.com/thoughtbot/json_matchers/pull/31 [gem]: https://github.com/brandur/json_schema#programmatic --- NEWS.md | 2 + README.md | 46 +----------------- lib/json_matchers.rb | 1 - lib/json_matchers/assertion.rb | 6 +-- lib/json_matchers/configuration.rb | 27 ----------- lib/json_matchers/matcher.rb | 10 +--- lib/json_matchers/rspec.rb | 14 +----- lib/json_matchers/validator.rb | 25 ++-------- spec/json_matchers/configuration_spec.rb | 9 ---- spec/json_matchers/match_json_schema_spec.rb | 50 -------------------- spec/support/configuration.rb | 20 -------- 11 files changed, 13 insertions(+), 197 deletions(-) delete mode 100644 lib/json_matchers/configuration.rb delete mode 100644 spec/json_matchers/configuration_spec.rb delete mode 100644 spec/support/configuration.rb diff --git a/NEWS.md b/NEWS.md index 1ff8933..122e2ad 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ master ====== +* *Breaking Change* - remove support for configuring validation options. + 0.9.0 ===== diff --git a/README.md b/README.md index 27e14d2..4d7b322 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -203,9 +162,8 @@ To learn more about `$ref`, check out [Understanding JSON Schema Structuring](ht ## Upgrading from `0.9.x` -After `json_matchers@0.9.x`, 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 diff --git a/lib/json_matchers.rb b/lib/json_matchers.rb index e9e12c7..e4dea53 100644 --- a/lib/json_matchers.rb +++ b/lib/json_matchers.rb @@ -1,5 +1,4 @@ require "json_matchers/version" -require "json_matchers/configuration" require "json_matchers/matcher" require "json_matchers/errors" diff --git a/lib/json_matchers/assertion.rb b/lib/json_matchers/assertion.rb index 1bb84aa..93537d7 100644 --- a/lib/json_matchers/assertion.rb +++ b/lib/json_matchers/assertion.rb @@ -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) diff --git a/lib/json_matchers/configuration.rb b/lib/json_matchers/configuration.rb deleted file mode 100644 index b760424..0000000 --- a/lib/json_matchers/configuration.rb +++ /dev/null @@ -1,27 +0,0 @@ -module JsonMatchers - def self.configuration - @configuration ||= Configuration.new - end - - def self.configure - warn <<-WARN -DEPRECATION: `JsonMatchers.configure` - After `json_matchers@0.9.x`, JsonMatchers.configure will be removed. - - See https://github.com/thoughtbot/json_matchers/pull/31 for more information. - -WARN - - yield(configuration) - end - - class Configuration - def initialize - @options = {} - end - - def options - @options.merge!(record_errors: true) - end - end -end diff --git a/lib/json_matchers/matcher.rb b/lib/json_matchers/matcher.rb index b2792a4..a5c50e0 100644 --- a/lib/json_matchers/matcher.rb +++ b/lib/json_matchers/matcher.rb @@ -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) @@ -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, ) diff --git a/lib/json_matchers/rspec.rb b/lib/json_matchers/rspec.rb index 61de184..00ce152 100644 --- a/lib/json_matchers/rspec.rb +++ b/lib/json_matchers/rspec.rb @@ -5,19 +5,7 @@ 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 `json_matchers@0.9.x`, 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 - +RSpec::Matchers.define :match_json_schema do |schema_name| assertion = JsonMatchers::Assertion.new(schema_name.to_s, options) match do |json| diff --git a/lib/json_matchers/validator.rb b/lib/json_matchers/validator.rb index 5704afc..6baec9f 100644 --- a/lib/json_matchers/validator.rb +++ b/lib/json_matchers/validator.rb @@ -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 diff --git a/spec/json_matchers/configuration_spec.rb b/spec/json_matchers/configuration_spec.rb deleted file mode 100644 index 342287f..0000000 --- a/spec/json_matchers/configuration_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -describe JsonMatchers, ".configure" do - it "ignores :record_errors configuration" do - with_options(record_errors: false) do - configured_options = JsonMatchers.configuration.options - - expect(configured_options).to include(record_errors: true) - end - end -end diff --git a/spec/json_matchers/match_json_schema_spec.rb b/spec/json_matchers/match_json_schema_spec.rb index ebac209..3e00953 100644 --- a/spec/json_matchers/match_json_schema_spec.rb +++ b/spec/json_matchers/match_json_schema_spec.rb @@ -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 diff --git a/spec/support/configuration.rb b/spec/support/configuration.rb deleted file mode 100644 index b36d17f..0000000 --- a/spec/support/configuration.rb +++ /dev/null @@ -1,20 +0,0 @@ -module ConfigurationHelpers - def with_options(options) - original_options = JsonMatchers.configuration.options.dup - - JsonMatchers.configure do |config| - config.options.merge!(options) - end - - yield - - JsonMatchers.configure do |config| - config.options.clear - config.options.merge!(original_options) - end - end -end - -RSpec.configure do |config| - config.include ConfigurationHelpers -end