GitLab steht Mittwoch, den 08. Juli, zwischen 09:00 und 13:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

schema_matcher.rb 729 Bytes
Newer Older
Lin Jen-Shin's avatar
Lin Jen-Shin committed
1 2 3 4
module SchemaPath
  def self.expand(schema, dir = '')
    Rails.root.join('spec', dir, "fixtures/api/schemas/#{schema}.json").to_s
  end
5 6
end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
7
RSpec::Matchers.define :match_response_schema do |schema, dir: '', **options|
8
  match do |response|
Lin Jen-Shin's avatar
Lin Jen-Shin committed
9 10
    @errors = JSON::Validator.fully_validate(
      SchemaPath.expand(schema, dir), response.body, options)
11 12 13 14 15

    @errors.empty?
  end

  failure_message do |response|
Lin Jen-Shin's avatar
Lin Jen-Shin committed
16
    "didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
17
    " The validation errors were:\n#{@errors.join("\n")}"
18 19
  end
end
20

Lin Jen-Shin's avatar
Lin Jen-Shin committed
21
RSpec::Matchers.define :match_schema do |schema, dir: '', **options|
22
  match do |data|
Lin Jen-Shin's avatar
Lin Jen-Shin committed
23
    JSON::Validator.validate!(SchemaPath.expand(schema, dir), data, options)
24 25
  end
end