Class: AbstractPathValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Extended by:
Gitlab::EncodingHelper
Defined in:
app/validators/abstract_path_validator.rb

Direct Known Subclasses

NamespacePathValidator, ProjectPathValidator

Constant Summary

Constants included from Gitlab::EncodingHelper

Gitlab::EncodingHelper::BOM_UTF8, Gitlab::EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, Gitlab::EncodingHelper::ESCAPED_CHARS, Gitlab::EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::EncodingHelper

binary_io, detect_binary?, detect_encoding, detect_libgit2_binary?, encode!, encode_binary, encode_utf8, encode_utf8_no_detect, encode_utf8_with_escaping!, encode_utf8_with_replacement_character, strip_bom, unquote_path

Class Method Details

.format_error_messageObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'app/validators/abstract_path_validator.rb', line 14

def self.format_error_message
  raise NotImplementedError
end

.format_regexObject

Raises:

  • (NotImplementedError)


10
11
12
# File 'app/validators/abstract_path_validator.rb', line 10

def self.format_regex
  raise NotImplementedError
end

.path_regexObject

Raises:

  • (NotImplementedError)


6
7
8
# File 'app/validators/abstract_path_validator.rb', line 6

def self.path_regex
  raise NotImplementedError
end

.valid_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'app/validators/abstract_path_validator.rb', line 18

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

Instance Method Details

#build_full_path_to_validate_against_reserved_names?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'app/validators/abstract_path_validator.rb', line 41

def build_full_path_to_validate_against_reserved_names?
  # By default, entities using the `Routable` concern can build full paths.
  # But entities like `Organization` do not have a parent, and hence cannot build full paths,
  # and this method can be overridden to return `false` in such cases.
  true
end

#validate_each(record, attribute, value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/validators/abstract_path_validator.rb', line 23

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

  if build_full_path_to_validate_against_reserved_names?
    path_to_validate_against_reserved_names = record.build_full_path
    return unless path_to_validate_against_reserved_names
  else
    path_to_validate_against_reserved_names = value
  end

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