summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/validation_helpers.rb
blob: 72f648975dcf1c3ee2f6249ef31499ad535309ae (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
module Gitlab
  module Ci
    class Config
      module Node
        module ValidationHelpers
          private

          def validate_duration(value)
            value.is_a?(String) && ChronicDuration.parse(value)
          rescue ChronicDuration::DurationParseError
            false
          end

          def validate_array_of_strings(values)
            values.is_a?(Array) && values.all? { |value| validate_string(value) }
          end

          def validate_array_of_strings_or_regexps(values)
            values.is_a?(Array) && values.all? { |value| validate_string_or_regexp(value) }
          end

          def validate_variables(variables)
            variables.is_a?(Hash) &&
              variables.all? { |key, value| validate_string(key) && validate_string(value) }
          end

          def validate_string(value)
            value.is_a?(String) || value.is_a?(Symbol)
          end

          def validate_string_or_regexp(value)
            return true if value.is_a?(Symbol)
            return false unless value.is_a?(String)

            if value.first == '/' && value.last == '/'
              Regexp.new(value[1...-1])
            else
              true
            end
          rescue RegexpError
            false
          end

          def validate_environment(value)
            value.is_a?(String) && value =~ Gitlab::Regex.environment_name_regex
          end

          def validate_boolean(value)
            value.in?([true, false])
          end
        end
      end
    end
  end
end