Require display names to have a minimum unicode width of 3 columns
Fixes #4538
This commit is contained in:
parent
a1a6c577e8
commit
3360f91733
6 changed files with 30 additions and 2 deletions
|
@ -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.
|
||||
|
|
3
Gemfile
3
Gemfile
|
@ -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"
|
||||
|
|
|
@ -683,6 +683,7 @@ DEPENDENCIES
|
|||
sprockets-exporters_pack
|
||||
strong_migrations
|
||||
terser
|
||||
unicode-display_width
|
||||
validates_email_format_of (>= 1.5.1)
|
||||
vendorer
|
||||
webmock
|
||||
|
|
|
@ -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? },
|
||||
|
|
11
app/validators/width_validator.rb
Normal file
11
app/validators/width_validator.rb
Normal file
|
@ -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
|
|
@ -67,6 +67,18 @@ class UserTest < ActiveSupport::TestCase
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue