tags: don't allow tags more than 170 chars long.

Limit tag length to 170 chars. 170 chars was chosen because it's
longer than the longest active tag on Danbooru.

Tag length is limited because in some contexts we can't deal with
excessively long tags. Tag autocorrect for example uses the levenshtein
function in Postgres, which can't handle strings more than 255 chars long.
This commit is contained in:
evazion
2020-12-17 13:41:19 -06:00
parent 1809f67b2b
commit 991896c4eb
2 changed files with 8 additions and 1 deletions

View File

@@ -1,6 +1,12 @@
class TagNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
case Tag.normalize_name(value)
value = Tag.normalize_name(value)
if value.size > 170
record.errors.add(attribute, "'#{value}' cannot be more than 255 characters long")
end
case value
when /\A_*\z/
record.errors.add(attribute, "'#{value}' cannot be blank")
when /\*/

View File

@@ -185,6 +185,7 @@ class TagTest < ActiveSupport::TestCase
should_not allow_value("café").for(:name).on(:create)
should_not allow_value("東方").for(:name).on(:create)
should_not allow_value("FAV:blah").for(:name).on(:create)
should_not allow_value("X"*171).for(:name).on(:create)
metatags = PostQueryBuilder::METATAGS + TagCategory.mapping.keys
metatags.each do |metatag|