gitlab-org--gitlab-foss/spec/support/api/schema_matcher.rb

24 lines
657 B
Ruby
Raw Normal View History

def schema_path(schema)
schema_directory = "#{Dir.pwd}/spec/fixtures/api/schemas"
"#{schema_directory}/#{schema}.json"
end
2016-08-16 14:13:21 +00:00
RSpec::Matchers.define :match_response_schema do |schema, **options|
match do |response|
@errors = JSON::Validator.fully_validate(schema_path(schema), response.body, options)
@errors.empty?
end
failure_message do |response|
"didn't match the schema defined by #{schema_path(schema)}" \
" The validation errors were:\n#{@errors.join("\n")}"
end
end
RSpec::Matchers.define :match_schema do |schema, **options|
match do |data|
JSON::Validator.validate!(schema_path(schema), data, options)
end
end