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.
42 43 44 45 46 47 48 49 50 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 42 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.
55 56 57 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 55 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 34 35 36 37 38 |
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 25 def matches?(actual) @actual = JSON.parse(actual, symbolize_names: true) @diff = Diffy::Diff.new(expected, @actual, context: 5) return false unless @actual.instance_of?(expected.class) if expected.instance_of?(Array) RSpec::JsonApi::CompareArray.compare(@actual, expected) else return false unless @actual.deep_keys.deep_sort == expected.deep_keys.deep_sort RSpec::JsonApi::CompareHash.compare(@actual, expected) end end |