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

#descriptionObject



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.

Parameters:

  • actual (String)

    The JSON string to test against the expected schema.

Returns:

  • (Boolean)

    true if the actual JSON does not match the expected schema.



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_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.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 54

def failure_message
  return type_error_message if @type_error

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

    Diff:
    #{diff}
  MSG
end

#failure_message_when_negatedString

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:

  • (String)

    A descriptive message indicating the JSON was expected not to match the schema.



69
70
71
72
73
# File 'lib/rspec/json_api/matchers/match_json_schema.rb', line 69

def failure_message_when_negated
  return type_error_message 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.

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)
  @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