gitlab-org--gitlab-foss/app/validators/namespace_validator.rb

62 lines
1.1 KiB
Ruby
Raw Normal View History

2015-12-07 16:17:12 -05:00
# NamespaceValidator
#
# Custom validator for GitLab namespace values.
#
# Values are checked for formatting and exclusion from a list of reserved path
# names.
2015-12-07 16:17:12 -05:00
class NamespaceValidator < ActiveModel::EachValidator
RESERVED = %w[
.well-known
admin
all
assets
ci
dashboard
files
groups
help
hooks
issues
merge_requests
2016-01-08 04:19:22 -05:00
new
notes
profile
projects
public
repository
robots.txt
s
search
services
snippets
teams
u
unsubscribes
users
].freeze
def self.valid?(value)
!reserved?(value) && follow_format?(value)
end
def self.reserved?(value)
RESERVED.include?(value)
end
def self.follow_format?(value)
value =~ Gitlab::Regex.namespace_regex
end
delegate :reserved?, :follow_format?, to: :class
2015-12-07 16:17:12 -05:00
def validate_each(record, attribute, value)
unless follow_format?(value)
2015-12-07 16:17:12 -05:00
record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
end
if reserved?(value)
2015-12-07 16:17:12 -05:00
record.errors.add(attribute, "#{value} is a reserved name")
end
end
end