2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
RSpec::Matchers.define :match_json_schema do |schema_name|
|
|
|
|
match do |response|
|
2018-01-21 22:21:00 +00:00
|
|
|
schema_path = Rails.root.join('doc', 'api', 'schemas', "#{schema_name}.json").to_s
|
2016-09-21 17:22:40 +00:00
|
|
|
|
|
|
|
# schema customizations
|
2018-01-21 22:21:00 +00:00
|
|
|
schema = JSON.parse(File.read(schema_path))
|
2016-09-21 17:22:40 +00:00
|
|
|
schema = update_file_refs(schema)
|
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
JSON::Validator.validate!(schema, data, validate_schema: true)
|
2016-02-09 03:20:27 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-21 17:22:40 +00:00
|
|
|
|
|
|
|
def get_json_example(resource)
|
2018-01-21 22:21:00 +00:00
|
|
|
filepath = Rails.root.join('doc', 'api', 'examples', "#{resource}.json")
|
2016-09-21 17:22:40 +00:00
|
|
|
OpenStruct.new(body: File.read(filepath))
|
|
|
|
end
|
|
|
|
|
|
|
|
# add full paths to file references
|
|
|
|
def update_file_refs(schema)
|
|
|
|
schema.each_pair do |key, value|
|
|
|
|
schema[key] = if value.is_a? Hash
|
|
|
|
update_file_refs(value)
|
|
|
|
elsif key == '$ref'
|
2018-01-21 22:21:00 +00:00
|
|
|
Rails.root.join('doc', 'api', 'schemas', value).to_s
|
2016-09-21 17:22:40 +00:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
schema
|
|
|
|
end
|