2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Dashboard
|
|
|
|
module Validator
|
|
|
|
class Client
|
|
|
|
# @param content [Hash] Representing a raw, unprocessed
|
|
|
|
# dashboard object
|
|
|
|
# @param schema_path [String] Representing path to dashboard schema file
|
|
|
|
# @param dashboard_path[String] Representing path to dashboard content file
|
|
|
|
# @param project [Project] Project to validate dashboard against
|
|
|
|
def initialize(content, schema_path, dashboard_path: nil, project: nil)
|
|
|
|
@content = content
|
|
|
|
@schema_path = schema_path
|
|
|
|
@dashboard_path = dashboard_path
|
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
errors = validate_against_schema
|
|
|
|
errors += post_schema_validator.validate
|
|
|
|
|
|
|
|
errors.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :content, :schema_path, :project, :dashboard_path
|
|
|
|
|
|
|
|
def custom_formats
|
|
|
|
@custom_formats ||= CustomFormats.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_schema_validator
|
|
|
|
PostSchemaValidator.new(
|
2022-10-11 01:57:18 +05:30
|
|
|
project: project,
|
|
|
|
metric_ids: custom_formats.metric_ids_cache,
|
2020-10-24 23:57:45 +05:30
|
|
|
dashboard_path: dashboard_path
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def schemer
|
|
|
|
@schemer ||= ::JSONSchemer.schema(Pathname.new(schema_path), formats: custom_formats.format_handlers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_against_schema
|
|
|
|
schemer.validate(content).map do |error|
|
2020-11-24 15:15:51 +05:30
|
|
|
::Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError.new(error)
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|