2013-02-08 09:52:56 -05:00
|
|
|
class UsernameValidator
|
2013-07-07 07:05:18 -04:00
|
|
|
# Public: Perform the validation of a field in a given object
|
|
|
|
# it adds the errors (if any) to the object that we're giving as parameter
|
|
|
|
#
|
|
|
|
# object - Object in which we're performing the validation
|
|
|
|
# field_name - name of the field that we're validating
|
|
|
|
#
|
|
|
|
# Example: UsernameValidator.perform_validation(user, 'name')
|
|
|
|
def self.perform_validation(object, field_name)
|
|
|
|
validator = UsernameValidator.new(object.send(field_name))
|
|
|
|
unless validator.valid_format?
|
|
|
|
validator.errors.each { |e| object.errors.add(field_name.to_sym, e) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-08 09:52:56 -05:00
|
|
|
def initialize(username)
|
|
|
|
@username = username
|
2013-02-08 14:12:48 -05:00
|
|
|
@errors = []
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
2013-02-08 14:12:48 -05:00
|
|
|
attr_accessor :errors
|
2013-02-08 09:52:56 -05:00
|
|
|
attr_reader :username
|
|
|
|
|
|
|
|
def user
|
|
|
|
@user ||= User.new(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_format?
|
|
|
|
username_exist?
|
|
|
|
username_length_min?
|
|
|
|
username_length_max?
|
|
|
|
username_char_valid?
|
|
|
|
username_first_char_valid?
|
2015-09-01 22:13:44 -04:00
|
|
|
username_last_char_valid?
|
|
|
|
username_no_double_special?
|
|
|
|
username_does_not_end_with_confusing_suffix?
|
2013-02-08 14:12:48 -05:00
|
|
|
errors.empty?
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def username_exist?
|
2013-02-08 14:12:48 -05:00
|
|
|
return unless errors.empty?
|
2013-02-08 09:52:56 -05:00
|
|
|
unless username
|
2013-02-08 14:12:48 -05:00
|
|
|
self.errors << I18n.t(:'user.username.blank')
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_length_min?
|
2013-02-08 14:12:48 -05:00
|
|
|
return unless errors.empty?
|
2013-02-08 09:52:56 -05:00
|
|
|
if username.length < User.username_length.begin
|
2013-02-08 14:12:48 -05:00
|
|
|
self.errors << I18n.t(:'user.username.short', min: User.username_length.begin)
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_length_max?
|
2013-02-08 14:12:48 -05:00
|
|
|
return unless errors.empty?
|
2013-02-08 09:52:56 -05:00
|
|
|
if username.length > User.username_length.end
|
2013-02-08 14:12:48 -05:00
|
|
|
self.errors << I18n.t(:'user.username.long', max: User.username_length.end)
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_char_valid?
|
2013-02-08 14:12:48 -05:00
|
|
|
return unless errors.empty?
|
2015-09-01 22:13:44 -04:00
|
|
|
if username =~ /[^A-Za-z0-9_\.\-]/
|
2013-02-08 14:12:48 -05:00
|
|
|
self.errors << I18n.t(:'user.username.characters')
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_first_char_valid?
|
2013-02-08 14:12:48 -05:00
|
|
|
return unless errors.empty?
|
2015-09-01 22:13:44 -04:00
|
|
|
if username[0] =~ /[^A-Za-z0-9_]/
|
2013-02-08 14:12:48 -05:00
|
|
|
self.errors << I18n.t(:'user.username.must_begin_with_alphanumeric')
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|
|
|
|
end
|
2015-09-01 22:13:44 -04:00
|
|
|
|
|
|
|
def username_last_char_valid?
|
|
|
|
return unless errors.empty?
|
|
|
|
if username[-1] =~ /[^A-Za-z0-9]/
|
|
|
|
self.errors << I18n.t(:'user.username.must_end_with_alphanumeric')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_no_double_special?
|
|
|
|
return unless errors.empty?
|
|
|
|
if username =~ /[\-_\.]{2,}/
|
|
|
|
self.errors << I18n.t(:'user.username.must_not_contain_two_special_chars_in_seq')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def username_does_not_end_with_confusing_suffix?
|
|
|
|
return unless errors.empty?
|
|
|
|
if username =~ /\.(json|gif|jpeg|png|htm|js|json|xml|woff|tif|html)/i
|
|
|
|
self.errors << I18n.t(:'user.username.must_not_contain_confusing_suffix')
|
|
|
|
end
|
|
|
|
end
|
2013-02-08 09:52:56 -05:00
|
|
|
end
|