summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/validatable.rb
blob: 5ced778d311965cbb6f2593e2793e5097c2637a4 (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
module Gitlab
  module Ci
    class Config
      module Entry
        module Validatable
          extend ActiveSupport::Concern

          def self.included(node)
            node.aspects.append -> do
              @validator = self.class.validator.new(self)
              @validator.validate(:new)
            end
          end

          def errors
            @validator.messages + descendants.flat_map(&:errors)
          end

          class_methods do
            def validator
              @validator ||= Class.new(Entry::Validator).tap do |validator|
                if defined?(@validations)
                  @validations.each { |rules| validator.class_eval(&rules) }
                end
              end
            end

            private

            def validations(&block)
              (@validations ||= []).append(block)
            end
          end
        end
      end
    end
  end
end