Class: OpenapiFirst::Validators::ResponseBody

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/validators/response_body.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ ResponseBody

Returns a new instance of ResponseBody.



13
14
15
# File 'lib/openapi_first/validators/response_body.rb', line 13

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



17
18
19
# File 'lib/openapi_first/validators/response_body.rb', line 17

def schema
  @schema
end

Class Method Details

.for(response_definition, openapi_version:) ⇒ Object



6
7
8
9
10
11
# File 'lib/openapi_first/validators/response_body.rb', line 6

def self.for(response_definition, openapi_version:)
  schema = response_definition&.content_schema
  return unless schema

  new(Schema.new(schema, write: false, openapi_version:))
end

Instance Method Details

#call(response) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/openapi_first/validators/response_body.rb', line 19

def call(response)
  begin
    parsed_body = response.body
  rescue ParseError => e
    Failure.fail!(:invalid_response_body, message: e.message)
  end
  validation = schema.validate(parsed_body)
  Failure.fail!(:invalid_response_body, errors: validation.errors) if validation.error?
end