class ActiveModelSerializers::Test::Schema::AssertSchema

def action

def action
  request.filtered_parameters[:action]
end

def add_schema_to_document_store

def add_schema_to_document_store
  Dir.glob("#{schema_directory}/**/*.json").each do |path|
    schema_data = load_json_file(path)
    extra_schema = JsonSchema.parse!(schema_data)
    document_store.add_schema(extra_schema)
  end
end

def call

def call
  json_schema.expand_references!(store: document_store)
  status, errors = json_schema.validate(response_body)
  @message = [message, errors.map(&:to_s).to_sentence].compact.join(': ')
  status
end

def controller_path

def controller_path
  request.filtered_parameters[:controller]
end

def initialize(schema_path, request, response, message, payload = nil)

Interface may change.
def initialize(schema_path, request, response, message, payload = nil)
  require_json_schema!
  @request = request
  @response = response
  @payload = payload
  @schema_path = schema_path || schema_path_default
  @message = message
  @document_store = JsonSchema::DocumentStore.new
  add_schema_to_document_store
end

def json_schema

def json_schema
  @json_schema ||= JsonSchema.parse!(schema_data)
end

def load_json(json)

def load_json(json)
  JSON.parse(json)
rescue JSON::ParserError => ex
  raise InvalidSchemaError, ex.message
end

def load_json_file(path)

def load_json_file(path)
  load_json(File.read(path))
rescue Errno::ENOENT
  raise MissingSchema, "No Schema file at #{schema_full_path}"
end

def request_params

def request_params
  request.env['action_dispatch.request.request_parameters']
end

def require_json_schema!

def require_json_schema!
  require 'json_schema'
rescue LoadError
  raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
end

def response_body

def response_body
  load_json(response.body)
end

def schema_data

def schema_data
  load_json_file(schema_full_path)
end

def schema_directory

def schema_directory
  ActiveModelSerializers.config.schema_path
end

def schema_full_path

def schema_full_path
  "#{schema_directory}/#{schema_path}"
end

def schema_path_default

def schema_path_default
  "#{controller_path}/#{action}.json"
end