Class: RSpec::JsonApi::Matchers::MatchJsonSchema

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ MatchJsonSchema

Initializes the matcher with the expected JSON schema.

Parameters:

  • expected (Object)

    The expected JSON schema as a Hash, Array, or other JSON-compatible structure.



18
19
20
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 18

def initialize(expected)
  @expected = expected
end

Instance Attribute Details

#actualObject (readonly)

Returns the actual JSON data being tested.

Returns:

  • (Object)

    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

#expectedObject (readonly)

Returns the expected JSON schema to match against.

Returns:

  • (Object)

    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_messageString

Provides a failure message for when the JSON data does not match the expected schema.

Returns:

  • (String)

    A descriptive message detailing the mismatch between expected and actual JSON.



42
43
44
45
46
47
48
49
50
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 42

def failure_message
  <<~MSG
    expected: #{expected}
         got: #{actual}

    Diff:
    #{@diff}
  MSG
end

#failure_message_when_negatedself

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.

Returns:

  • (self)

    Returns itself, but typically this method should be implemented to return a descriptive message



55
56
57
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 55

def failure_message_when_negated
  "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.

Parameters:

  • actual (String)

    The JSON string to test against the expected schema.

Returns:

  • (Boolean)

    true if the actual JSON matches the expected schema, false otherwise.



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