Skip to content

Commit

Permalink
Require display names to have a minimum unicode width of 3 columns
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Feb 25, 2024
1 parent a1a6c57 commit 3360f91
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -66,7 +66,7 @@ Metrics/BlockNesting:
# Offense count: 26
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 313
Max: 314

# Offense count: 59
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -136,6 +136,9 @@ gem "aws-sdk-s3"
# Used to resize user images
gem "image_processing"

# Used to validate widths
gem "unicode-display_width"

# Gems useful for development
group :development do
gem "better_errors"
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -683,6 +683,7 @@ DEPENDENCIES
sprockets-exporters_pack
strong_migrations
terser
unicode-display_width
validates_email_format_of (>= 1.5.1)
vendorer
webmock
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Expand Up @@ -98,7 +98,8 @@ class User < ApplicationRecord
:normalized_uniqueness => { :case_sensitive => false }
validates :display_name, :if => proc { |u| u.display_name_changed? },
:characters => { :url_safe => true },
:whitespace => { :leading => false, :trailing => false }
:whitespace => { :leading => false, :trailing => false },
:width => { :minimum => 3 }
validate :display_name_cannot_be_user_id_with_other_id, :if => proc { |u| u.display_name_changed? }
validates :email, :presence => true, :confirmation => true, :characters => true
validates :email, :if => proc { |u| u.email_changed? },
Expand Down
11 changes: 11 additions & 0 deletions app/validators/width_validator.rb
@@ -0,0 +1,11 @@
class WidthValidator < ActiveModel::Validations::LengthValidator
module WidthAsLength
def length
Unicode::DisplayWidth.of(to_s)
end
end

def validate_each(record, attribute, value)
super(record, attribute, value.extend(WidthAsLength))
end
end
12 changes: 12 additions & 0 deletions test/models/user_test.rb
Expand Up @@ -67,6 +67,18 @@ def test_display_name_length
assert_not_predicate user, :valid?, "should not allow nil value"
end

def test_display_name_width
user = build(:user)
user.display_name = "123"
assert_predicate user, :valid?, "should allow 3 column name name"
user.display_name = "12"
assert_not_predicate user, :valid?, "should not allow 2 column name"
user.display_name = "1\u{200B}2"
assert_not_predicate user, :valid?, "should not allow 2 column name"
user.display_name = "\u{200B}\u{200B}\u{200B}"
assert_not_predicate user, :valid?, "should not allow 0 column name"
end

def test_display_name_valid
# Due to sanitisation in the view some of these that you might not
# expect are allowed
Expand Down

0 comments on commit 3360f91

Please sign in to comment.