Welcome to mirror list, hosted at ThFree Co, Russian Federation.

schema_validator.rb « validators « security « parsers « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 143b930c66918c740713b9b7b4fa09dfe23a71ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.to_sym
              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")