summaryrefslogtreecommitdiff
path: root/spec/support/matchers/match_schema.rb
blob: 5abac67bd1c7c5677beae2ece338d2e647235eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

RSpec::Matchers.define :match_schema do |report_type|
  def schema_for(type)
    relative_path = "spec/schemas/dist/#{type.tr('_', '-')}-report-format.json"
    json = JSON.parse(Pathname.pwd.join(relative_path).read)
    json.delete('$schema')
    json
  end

  match do |actual|
    !actual.nil? && (@errors = JSON::Validator.fully_validate(schema_for(report_type.to_s), actual.to_h)).empty?
  end

  failure_message do |response|
    "didn't match the schema for #{report_type}" \
    " The validation errors were:\n#{@errors.join("\n")}"
  end
end