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

abstract_path_validator.rb « validators « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 45ac695c5ec5fc0f578f9f536d6bd9f411790105 (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
# frozen_string_literal: true

class AbstractPathValidator < ActiveModel::EachValidator
  extend Gitlab::EncodingHelper

  def self.path_regex
    raise NotImplementedError
  end

  def self.format_regex
    raise NotImplementedError
  end

  def self.format_error_message
    raise NotImplementedError
  end

  def self.valid_path?(path)
    encode!(path)
    "#{path}/" =~ path_regex
  end

  def validate_each(record, attribute, value)
    unless value =~ self.class.format_regex
      record.errors.add(attribute, self.class.format_error_message)
      return
    end

    full_path = record.build_full_path
    return unless full_path

    unless self.class.valid_path?(full_path)
      record.errors.add(attribute, "#{value} is a reserved name")
    end
  end
end