Merge pull request #2882 from evazion/fix-tag-name-validation

Enforce stricter rules for tag names
This commit is contained in:
Albert Yi
2017-02-10 12:02:55 -08:00
committed by GitHub
5 changed files with 172 additions and 21 deletions

View File

@@ -0,0 +1,30 @@
class TagNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
case Tag.normalize_name(value)
when /\A_*\z/
record.errors[attribute] << "'#{value}' cannot be blank"
when /\*/
record.errors[attribute] << "'#{value}' cannot contain asterisks ('*')"
when /,/
record.errors[attribute] << "'#{value}' cannot contain commas (',')"
when /\A~/
record.errors[attribute] << "'#{value}' cannot begin with a tilde ('~')"
when /\A-/
record.errors[attribute] << "'#{value}' cannot begin with a dash ('-')"
when /\A_/
record.errors[attribute] << "'#{value}' cannot begin with an underscore"
when /_\z/
record.errors[attribute] << "'#{value}' cannot end with an underscore"
when /__/
record.errors[attribute] << "'#{value}' cannot contain consecutive underscores"
when /[^[[:graph:]]]/
record.errors[attribute] << "'#{value}' cannot contain non-printable characters"
when /[^[[:ascii:]]]/
record.errors[attribute] << "'#{value}' must consist of only ASCII characters"
when /\A(#{Tag::METATAGS}|#{Tag::SUBQUERY_METATAGS}):(.+)\z/i
record.errors[attribute] << "'#{value}' cannot begin with '#{$1}:'"
when /\A(#{Tag.categories.regexp}):(.+)\z/i
record.errors[attribute] << "'#{value}' cannot begin with '#{$1}:'"
end
end
end