summaryrefslogtreecommitdiff
path: root/app/validators/namespace_validator.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-12-08 12:50:25 +0100
committerDouwe Maan <douwe@gitlab.com>2015-12-08 12:50:25 +0100
commit3c80a543153f8f686ec99d2d04b181b2d2c8a665 (patch)
tree679ca3921b00bd9e0904b4ad1be4e01d0a753ee9 /app/validators/namespace_validator.rb
parent1a942111653e9376d6275c5bd31f23cb27f58ab5 (diff)
parentf5430e48b42227f1c1874ca27c6907f0f704be28 (diff)
downloadgitlab-ce-ui/misc.tar.gz
Merge branch 'master' into ui/miscui/misc
Diffstat (limited to 'app/validators/namespace_validator.rb')
-rw-r--r--app/validators/namespace_validator.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/app/validators/namespace_validator.rb b/app/validators/namespace_validator.rb
new file mode 100644
index 00000000000..10e35ce665a
--- /dev/null
+++ b/app/validators/namespace_validator.rb
@@ -0,0 +1,50 @@
+# NamespaceValidator
+#
+# Custom validator for GitLab namespace values.
+#
+# Values are checked for formatting and exclusion from a list of reserved path
+# names.
+class NamespaceValidator < ActiveModel::EachValidator
+ RESERVED = %w(
+ admin
+ all
+ assets
+ ci
+ dashboard
+ files
+ groups
+ help
+ hooks
+ issues
+ merge_requests
+ notes
+ profile
+ projects
+ public
+ repository
+ s
+ search
+ services
+ snippets
+ teams
+ u
+ unsubscribes
+ users
+ ).freeze
+
+ def validate_each(record, attribute, value)
+ unless value =~ Gitlab::Regex.namespace_regex
+ record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
+ end
+
+ if reserved?(value)
+ record.errors.add(attribute, "#{value} is a reserved name")
+ end
+ end
+
+ private
+
+ def reserved?(value)
+ RESERVED.include?(value)
+ end
+end