blob: 29620543a0f3909be4b29a55f8dccd6c760af58f (
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
|
# 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 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
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
end
|