Files
danbooru/app/logical/user_name_validator.rb
evazion b4620f561c users: lower max username length to 25 characters.
The median username length is 8 characters. The 99% percentile is 18
characters. The 99.9% percentile is 24 characters. About 750 users have
a name more than 24 characters long.

This doesn't do anything about existing users with long usernames.

Note that this is the length in Unicode codepoints, not grapheme
clusters. Some Unicode characters and emoji may be a single glyph but
composed of multiple codepoints.
2022-03-01 21:23:21 -06:00

21 lines
861 B
Ruby

# frozen_string_literal: true
# Define a custom validator for user names.
#
# @example
# validates :name, user_name: true
#
# @see https://guides.rubyonrails.org/active_record_validations.html#custom-validators
class UserNameValidator < ActiveModel::EachValidator
def validate_each(rec, attr, value)
name = value
rec.errors.add(attr, "already exists") if User.find_by_name(name).present?
rec.errors.add(attr, "must be more than 1 character long") if name.length <= 1
rec.errors.add(attr, "must be less than 25 characters long") if name.length >= 25
rec.errors.add(attr, "cannot have whitespace or colons") if name =~ /[[:space:]]|:/
rec.errors.add(attr, "cannot begin or end with an underscore") if name =~ /\A_|_\z/
rec.errors.add(attr, "is not allowed") if name =~ Regexp.union(Danbooru.config.user_name_blacklist)
end
end