From 20595e44b91a72dc39f72090bf9aed3b7db2a1bc Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 16 Sep 2020 17:19:55 -0600 Subject: test: add spec to validate the dependency_scanning report schema --- spec/support/matchers/match_schema.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 spec/support/matchers/match_schema.rb (limited to 'spec/support/matchers') diff --git a/spec/support/matchers/match_schema.rb b/spec/support/matchers/match_schema.rb new file mode 100644 index 0000000..eef6106 --- /dev/null +++ b/spec/support/matchers/match_schema.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +RSpec::Matchers.define :match_schema do |report_type| + def schema_for(type) + relative_path = "spec/schemas/dist/#{type.gsub('_', '-')}-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 -- cgit v1.2.3