Class: RuboCop::Cop::Iotventure::SchemaDefinitionPerResponse

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/iotventure/schema_definition_per_response.rb

Overview

This cop checks that there is exactly one top-level schema definition per response declaration.

Examples:


# bad

response 200, 'response description' {}

# bad

response 200, 'response description' do
  context 'context' do
    schema '$ref' => '#/components/schemas/object'
  end
end

# bad

response 200, 'response description' do
    schema '$ref' => '#/components/schemas/object1'
    schema '$ref' => '#/components/schemas/object2'
end

# bad

schema '$ref' => '#/components/schemas/object'
response 200, 'response description' {}

# bad

response 204, 'response description' do
  schema '$ref' => '#/components/schemas/object'
end

# good

response 200, 'response description' do
  schema '$ref' => '#/components/schemas/object'
end

# good

response 204, 'response description' do
end

Constant Summary collapse

MISSING_MSG =
'Schema definition is missing for response declaration at %<current>s.'
DUPLICATED_MSG =
'Schema definition is defined both at %<first>s and %<second>s '\
'for response declaration at %<current>s.'
MISPLACED_MSG =
'Schema definition for %<schema_name>s is defined at %<current>s, '\
'but should be defined immediately after response declaration at %<other>s.'
MISPLACED_WITHOUT_RESPONSE_DEFINITION_MSG =
'Schema definition for %<schema_name>s is '\
'outside of response declaration.'
NO_CONTENT_SCHEMA_MSG =
'Schema definition for %<schema_name>s is defined at %<current>s, '\
'but 204 response should not have schema.'

Instance Method Summary collapse

Instance Method Details

#no_content_response(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/rubocop/cop/iotventure/schema_definition_per_response.rb', line 92

def_node_matcher :no_content_response, <<~PATTERN
  (block
    (send nil? :response
      (:int 204)
      (:str _)
      ...
    )
    ...
  )
PATTERN

#on_block(node) ⇒ Object Also known as: on_begin



138
139
140
141
142
143
144
# File 'lib/rubocop/cop/iotventure/schema_definition_per_response.rb', line 138

def on_block(node)
  return check_schema_definition_count(node) if response_block(node)

  return if begin_inside_response_block?(node)

  check_for_misplaced_schema(node)
end

#response_block?(node) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rubocop/cop/iotventure/schema_definition_per_response.rb', line 82

def_node_matcher :response_block, <<~PATTERN
  (block
    (send nil? :response
      (int _)
      ...)
    ...
  )
PATTERN

#schema_definition?(node) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/rubocop/cop/iotventure/schema_definition_per_response.rb', line 104

def_node_matcher :schema_definition, <<~PATTERN
  (send nil? :schema
    (hash
      ...
    )
  )
PATTERN

#schema_definition_by_child?(node) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rubocop/cop/iotventure/schema_definition_per_response.rb', line 125

def_node_matcher :schema_definition_by_child, <<~PATTERN
  ({block | begin}
    <
      $(send nil? :schema
        (hash
          ...
        )
      )
      ...
    >
  )
PATTERN

#schema_name(node) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/rubocop/cop/iotventure/schema_definition_per_response.rb', line 113

def_node_matcher :schema_name, <<~PATTERN
  (send nil? :schema
    (hash
      (pair
        (str "$ref")
        (str $_)
      )
    )
  )
PATTERN