summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/validator.rb
blob: 55343005fe31dc0eb913fd706f1a2b273100c895 (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
module Gitlab
  module Ci
    class Config
      module Entry
        class Validator < SimpleDelegator
          include ActiveModel::Validations
          include Entry::Validators

          def initialize(entry)
            super(entry)
            @entry = entry
          end

          def messages
            errors.full_messages.map do |error|
              "#{location} #{error}".downcase
            end
          end

          def self.name
            'Validator'
          end

          private

          def location
            predecessors = ancestors.map(&:key).compact
            predecessors.append(key_name).join(':')
          end

          def key_name
            if key.blank?
              @entry.class.name.demodulize.underscore.humanize
            else
              key
            end
          end
        end
      end
    end
  end
end