Class: SchemaMatcher::RspecMatchers::JsonSchemaMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_matcher/rspec_matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_name, options = {}) ⇒ JsonSchemaMatcher

Returns a new instance of JsonSchemaMatcher.



9
10
11
12
# File 'lib/schema_matcher/rspec_matchers.rb', line 9

def initialize(schema_name, options = {})
  @schema_name = schema_name
  @options = options
end

Instance Attribute Details

#assertionObject (readonly)

Returns the value of attribute assertion.



7
8
9
# File 'lib/schema_matcher/rspec_matchers.rb', line 7

def assertion
  @assertion
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/schema_matcher/rspec_matchers.rb', line 7

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/schema_matcher/rspec_matchers.rb', line 7

def payload
  @payload
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



7
8
9
# File 'lib/schema_matcher/rspec_matchers.rb', line 7

def schema_name
  @schema_name
end

Instance Method Details

#error_messageObject



20
21
22
# File 'lib/schema_matcher/rspec_matchers.rb', line 20

def error_message
  @assertion.errors[0]
end

#failure_messageObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/schema_matcher/rspec_matchers.rb', line 24

def failure_message
  <<~FAIL
    #{error_message}

    ---

    expected

    #{format_json(payload)}

    to match schema "#{schema_name}":

  FAIL
end

#failure_message_for_shouldObject



54
55
56
# File 'lib/schema_matcher/rspec_matchers.rb', line 54

def failure_message_for_should
  failure_message
end

#failure_message_for_should_notObject



58
59
60
# File 'lib/schema_matcher/rspec_matchers.rb', line 58

def failure_message_for_should_not
  failure_message_when_negated
end

#failure_message_when_negatedObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/schema_matcher/rspec_matchers.rb', line 39

def failure_message_when_negated
  <<~FAIL
    #{error_message}

    ---

    expected

    #{format_json(payload)}

    not to match schema "#{schema_name}":

  FAIL
end

#format_json(_json) ⇒ Object



62
63
64
# File 'lib/schema_matcher/rspec_matchers.rb', line 62

def format_json(_json)
  JSON.pretty_generate(payload)
end

#matches?(payload) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/schema_matcher/rspec_matchers.rb', line 14

def matches?(payload)
  @payload = payload
  @assertion = SchemaMatcher::Assertion.new(schema_name, payload, options)
  @assertion.valid?
end