match_json_schema.rb 584 B

123456789101112131415161718192021
  1. # frozen_string_literal: true
  2. RSpec::Matchers.define :match_json_schema do |schema|
  3. match do |input_json|
  4. schema_path = Rails.root.join('spec', 'support', 'schema', "#{schema}.json").to_s
  5. JSON::Validator.validate(schema_path, input_json, validate_schema: true)
  6. end
  7. end
  8. RSpec::Matchers.define :match_json_values do |values|
  9. match do |string|
  10. parsed_json = JSON.parse(string, symbolize_names: true)
  11. expect(parsed_json)
  12. .to include(values)
  13. end
  14. failure_message do |value|
  15. "expected that #{value} would have the same values as #{values}."
  16. end
  17. end