Class: RSpec::JsonApi::Matchers::MatchJsonSchema
- Inherits:
-
Object
- Object
- RSpec::JsonApi::Matchers::MatchJsonSchema
- Defined in:
- lib/rspec/json_api/matchers/match_json_schema.rb
Overview
MatchJsonSchema class is designed to match a given JSON against a predefined JSON schema.
This matcher is useful for validating JSON structures in API responses or other JSON data against a schema defined either as a Hash, an Array, or another JSON structure.
Instance Attribute Summary collapse
-
#actual ⇒ Object
readonly
The actual JSON data being tested.
-
#expected ⇒ Object
readonly
The expected JSON schema to match against.
Instance Method Summary collapse
- #description ⇒ Object
-
#does_not_match?(actual) ⇒ Boolean
A non-String actual is a mistake in the spec rather than a fact about the response, so the negated form has to fail rather than pass by default.
-
#failure_message ⇒ String
Provides a failure message for when the JSON data does not match the expected schema.
-
#failure_message_when_negated ⇒ String
Provides a failure message for when the JSON data matches the expected schema, but it was expected not to.
-
#initialize(expected) ⇒ MatchJsonSchema
constructor
Initializes the matcher with the expected JSON schema.
-
#matches?(actual) ⇒ Boolean
Matches the actual JSON data against the expected schema.
Constructor Details
#initialize(expected) ⇒ MatchJsonSchema
Initializes the matcher with the expected JSON schema.
18 19 20 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 18 def initialize(expected) @expected = expected end |
Instance Attribute Details
#actual ⇒ Object (readonly)
Returns the actual JSON data being tested.
14 15 16 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 14 def actual @actual end |
#expected ⇒ Object (readonly)
Returns the expected JSON schema to match against.
12 13 14 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 12 def expected @expected end |
Instance Method Details
#description ⇒ Object
48 49 50 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 48 def description "match JSON schema" end |
#does_not_match?(actual) ⇒ Boolean
A non-String actual is a mistake in the spec rather than a fact about the response, so the negated form has to fail rather than pass by default.
44 45 46 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 44 def does_not_match?(actual) !matches?(actual) && !@type_error end |
#failure_message ⇒ String
Provides a failure message for when the JSON data does not match the expected schema.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 54 def return if @type_error <<~MSG expected: #{expected} got: #{actual} Diff: #{diff} MSG end |
#failure_message_when_negated ⇒ String
Provides a failure message for when the JSON data matches the expected schema, but it was expected not to. This is used in negative matchers.
69 70 71 72 73 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 69 def return if @type_error "expected the JSON data not to match the provided schema, but it did." end |
#matches?(actual) ⇒ Boolean
Matches the actual JSON data against the expected schema.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 25 def matches?(actual) @diff = nil @actual = actual @type_error = !actual.is_a?(String) return false if @type_error @actual = JSON.parse(actual, symbolize_names: true) RSpec::JsonApi::SchemaMatch.match(@actual, expected) rescue JSON::ParserError @actual = actual false end |