BigW Consortium Gitlab

namespace_validator.rb 907 Bytes
Newer Older
1 2 3 4
# NamespaceValidator
#
# Custom validator for GitLab namespace values.
#
5 6
# Values are checked for formatting and exclusion from a list of reserved path
# names.
7
class NamespaceValidator < ActiveModel::EachValidator
8 9
  RESERVED = %w[
    .well-known
10 11 12 13 14 15 16 17 18 19 20
    admin
    all
    assets
    ci
    dashboard
    files
    groups
    help
    hooks
    issues
    merge_requests
Robert Schilling committed
21
    new
22 23 24 25 26
    notes
    profile
    projects
    public
    repository
27
    robots.txt
28 29 30 31 32 33 34 35
    s
    search
    services
    snippets
    teams
    u
    unsubscribes
    users
36
  ].freeze
37

38 39 40 41 42
  def validate_each(record, attribute, value)
    unless value =~ Gitlab::Regex.namespace_regex
      record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
    end

43
    if reserved?(value)
44 45 46 47 48 49
      record.errors.add(attribute, "#{value} is a reserved name")
    end
  end

  private

50 51
  def reserved?(value)
    RESERVED.include?(value)
52 53
  end
end