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
-
#failure_message ⇒ String
Provides a failure message for when the JSON data does not match the expected schema.
-
#failure_message_when_negated ⇒ self
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
#failure_message ⇒ String
Provides a failure message for when the JSON data does not match the expected schema.
37 38 39 40 41 42 43 44 45 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 37 def <<~MSG expected: #{expected} got: #{actual} Diff: #{diff} MSG end |
#failure_message_when_negated ⇒ self
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.
50 51 52 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 50 def "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 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 25 def matches?(actual) @diff = nil @actual = JSON.parse(actual, symbolize_names: true) RSpec::JsonApi::SchemaMatch.match(@actual, expected) rescue JSON::ParserError @actual = actual false end |