diff options
Diffstat (limited to 'lib/gitlab/ci/parsers/security/validators/schema_validator.rb')
-rw-r--r-- | lib/gitlab/ci/parsers/security/validators/schema_validator.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/gitlab/ci/parsers/security/validators/schema_validator.rb b/lib/gitlab/ci/parsers/security/validators/schema_validator.rb new file mode 100644 index 00000000000..3d92886cba8 --- /dev/null +++ b/lib/gitlab/ci/parsers/security/validators/schema_validator.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Parsers + module Security + module Validators + class SchemaValidator + class Schema + def root_path + File.join(__dir__, 'schemas') + end + + def initialize(report_type) + @report_type = report_type + end + + delegate :validate, to: :schemer + + private + + attr_reader :report_type + + def schemer + JSONSchemer.schema(pathname) + end + + def pathname + Pathname.new(schema_path) + end + + def schema_path + File.join(root_path, file_name) + end + + def file_name + "#{report_type}.json" + end + end + + def initialize(report_type, report_data) + @report_type = report_type + @report_data = report_data + end + + def valid? + errors.empty? + end + + def errors + @errors ||= schema.validate(report_data).map { |error| JSONSchemer::Errors.pretty(error) } + end + + private + + attr_reader :report_type, :report_data + + def schema + Schema.new(report_type) + end + end + end + end + end + end +end + +Gitlab::Ci::Parsers::Security::Validators::SchemaValidator::Schema.prepend_mod_with("Gitlab::Ci::Parsers::Security::Validators::SchemaValidator::Schema") |