summaryrefslogtreecommitdiff
path: root/spec/support/matchers/schema_matcher.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/matchers/schema_matcher.rb')
-rw-r--r--spec/support/matchers/schema_matcher.rb58
1 files changed, 39 insertions, 19 deletions
diff --git a/spec/support/matchers/schema_matcher.rb b/spec/support/matchers/schema_matcher.rb
index f0e7a52c51e..94e4359b1dd 100644
--- a/spec/support/matchers/schema_matcher.rb
+++ b/spec/support/matchers/schema_matcher.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
module SchemaPath
+ @schema_cache = {}
+
def self.expand(schema, dir = nil)
return schema unless schema.is_a?(String)
@@ -12,38 +14,56 @@ module SchemaPath
Rails.root.join(dir.to_s, 'spec', "fixtures/api/schemas/#{schema}.json").to_s
end
+
+ def self.validator(schema_path)
+ unless @schema_cache.key?(schema_path)
+ @schema_cache[schema_path] = JSONSchemer.schema(schema_path, ref_resolver: SchemaPath.file_ref_resolver)
+ end
+
+ @schema_cache[schema_path]
+ end
+
+ def self.file_ref_resolver
+ proc do |uri|
+ file = Rails.root.join(uri.path)
+ raise StandardError, "Ref file #{uri.path} must be json" unless uri.path.ends_with?('.json')
+ raise StandardError, "File #{file.to_path} doesn't exists" unless file.exist?
+
+ Gitlab::Json.parse(File.read(file))
+ end
+ end
end
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
match do |response|
- @errors = JSON::Validator.fully_validate(
- SchemaPath.expand(schema, dir), response.body, options)
+ schema_path = Pathname.new(SchemaPath.expand(schema, dir))
+ validator = SchemaPath.validator(schema_path)
- @errors.empty?
- end
+ data = Gitlab::Json.parse(response.body)
- failure_message do |response|
- "didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
- " The validation errors were:\n#{@errors.join("\n")}"
+ validator.valid?(data)
end
end
-RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
+RSpec::Matchers.define :match_snowplow_schema do |schema, dir: nil, **options|
match do |data|
- @errors = JSON::Validator.fully_validate(
- SchemaPath.expand(schema, dir), data, options)
-
- @errors.empty?
- end
+ schema_path = Pathname.new(Rails.root.join(dir.to_s, 'spec', "fixtures/product_intelligence/#{schema}.json").to_s)
+ validator = SchemaPath.validator(schema_path)
- failure_message do |response|
- "didn't match the schema defined by #{schema_name(schema, dir)}" \
- " The validation errors were:\n#{@errors.join("\n")}"
+ validator.valid?(data.stringify_keys)
end
+end
- def schema_name(schema, dir)
- return 'provided schema' unless schema.is_a?(String)
+RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
+ match do |data|
+ schema = SchemaPath.expand(schema, dir)
+ schema = Pathname.new(schema) if schema.is_a?(String)
+ validator = SchemaPath.validator(schema)
- SchemaPath.expand(schema, dir)
+ if data.is_a?(String)
+ validator.valid?(Gitlab::Json.parse(data))
+ else
+ validator.valid?(data)
+ end
end
end