summaryrefslogtreecommitdiff
path: root/spec/support/api/schema_matcher.rb
blob: 6591d56e473fafd1be926bd776278ba0fb01a28d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module SchemaPath
  def self.expand(schema, dir = '')
    Rails.root.join('spec', dir, "fixtures/api/schemas/#{schema}.json").to_s
  end
end

RSpec::Matchers.define :match_response_schema do |schema, dir: '', **options|
  match do |response|
    @errors = JSON::Validator.fully_validate(
      SchemaPath.expand(schema, dir), response.body, options)

    @errors.empty?
  end

  failure_message do |response|
    "didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
    " The validation errors were:\n#{@errors.join("\n")}"
  end
end

RSpec::Matchers.define :match_schema do |schema, dir: '', **options|
  match do |data|
    JSON::Validator.validate!(SchemaPath.expand(schema, dir), data, options)
  end
end