class Swaggable::CheckBodySchema

Attributes

endpoint[R]
request[R]

Public Class Methods

call(*args) click to toggle source
# File lib/swaggable/check_body_schema.rb, line 10
def self.call(*args)
  new(*args).send :errors
end
new(args) click to toggle source
# File lib/swaggable/check_body_schema.rb, line 5
def initialize args
  @endpoint = args.fetch(:endpoint)
  @request = args.fetch(:request)
end

Private Instance Methods

body() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 68
def body
  request.parsed_body
end
body_definition() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 60
def body_definition
  endpoint.body
end
errors() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 16
def errors
  Errors::ValidationsCollection.new.tap do |errors|
    errors_for_required_parameters_in_body.each {|e| errors << e }
  end
end
errors_for_required_parameters_in_body() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 24
def errors_for_required_parameters_in_body
  if body_definition == nil
    []
  elsif !body_definition.required?
    []
  elsif !request.body
    [Errors::Validation.new("Missing body")]
  elsif body_definition.schema.empty?
    []
  else
    missing_attribute_errors + unexpected_attribute_errors
  end
end
missing_attribute_errors() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 38
def missing_attribute_errors
  [].tap do |errors|
    schema.attributes.select(&:required?).each do |attr|
      unless body.has_key? attr.name
        errors << Errors::Validation.new("Missing body parameter #{attr.inspect}")
      end
    end
  end
end
schema() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 64
def schema
  body_definition.schema
end
unexpected_attribute_errors() click to toggle source
# File lib/swaggable/check_body_schema.rb, line 48
def unexpected_attribute_errors
  [].tap do |errors|
    body.each do |key, value|
      unless schema.attributes[key]
        errors << Errors::Validation.new("Unexpected body parameter #{key.inspect}")
      end
    end
  end
end