BigW Consortium Gitlab

namespace_validator.rb 1.07 KB
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 43 44 45 46 47 48 49 50 51
  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

52
  def validate_each(record, attribute, value)
53
    unless follow_format?(value)
54 55 56
      record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
    end

57
    if reserved?(value)
58 59 60 61
      record.errors.add(attribute, "#{value} is a reserved name")
    end
  end
end